Files
dashfront/src/automats/articleDetailAutomat.js
2026-10-04 22:47:26 +02:00

75 lines
2.4 KiB
JavaScript

/**
* @file articleDetailAutomat.js
* @description SKU-keyed automats for article detail tabs: article history
* (`articleHistoryAutomat`) and assigned suppliers (`articleSupplierAutomat`).
*
* Upstream Triggers:
* - `sseAutomat`: Calls `articleHistoryAutomat.reload(sku)` and
* `articleSupplierAutomat.reload(sku)` via `invalidateArticle(sku)` when an
* `article-<sku>` SSE event is received.
* - UI / Router: Triggers keyed `.load(sku)` or `.key(sku)` when viewing
* history or supplier details of a specific article.
*/
import { KeyedAutomat } from 'react-automat';
import { article as articleApi } from '../api';
import { requireArray, requireObject } from '../utils/apiShape';
/**
* @typedef {Object} ArticleHistoryEntry
* @property {string|number} id History entry id.
* @property {string} date Timestamp (ISO).
* @property {string} text Human-readable change description.
*/
/**
* @typedef {Object} ArticleSupplierRow
* @property {string|number} id Supplier link id.
* @property {string|number} entityId Supplier id.
* @property {string|null} sku Supplier SKU.
* @property {string|null} name Supplier name.
* @property {number|null} pu Pack unit.
* @property {number|null} stock Supplier stock.
* @property {number|null} netPrice Net purchase price.
* @property {number|null} taxRate Tax rate in percent.
*/
/**
* Per-SKU article change history (`GET /api/article/{sku}/history`).
*
* The backend returns the full list; pagination happens client-side in
* `ArticleHistoryTab`. Keyed by SKU so several tabs can watch the same article.
*
* @type {KeyedAutomat<Array<ArticleHistoryEntry>>}
*/
export const articleHistoryAutomat = new KeyedAutomat(
null,
{},
{
name: 'articleHistory',
loader: async (sku) => {
const res = await articleApi.getBySkuHistory(String(sku));
requireObject(res, 'GET /api/article/:sku/history');
return requireArray(res.history, 'history');
},
},
);
/**
* Suppliers assigned to an article (`GET /api/article/{sku}/supplier`).
*
* @type {KeyedAutomat<Array<ArticleSupplierRow>>}
*/
export const articleSupplierAutomat = new KeyedAutomat(
null,
{},
{
name: 'articleSuppliers',
loader: async (sku) => {
const res = await articleApi.getSuppliers(String(sku));
requireObject(res, 'GET /api/article/:sku/supplier');
return requireArray(res.suppliers, 'suppliers');
},
},
);