u
This commit is contained in:
@@ -39,6 +39,7 @@ import {
|
||||
import { requireArray, requireDefined, requireObject } from '../utils/apiShape';
|
||||
import { normalizeCompany } from '../utils/companyForm';
|
||||
import authAutomat from './authAutomat';
|
||||
import sseAutomat from './sseAutomat';
|
||||
|
||||
// #region userSettings persistence helpers
|
||||
const USER_SETTINGS_STORAGE_KEY = 'dash.userSettings';
|
||||
@@ -704,15 +705,21 @@ const actions = {
|
||||
},
|
||||
|
||||
async reloadSuppliers() {
|
||||
try {
|
||||
const suppliers = await fetchSuppliersList();
|
||||
companyAutomat.setState({ suppliers, suppliersStatus: 'ready', suppliersError: null });
|
||||
void actions.loadAllSupplierArticles().catch(() => {});
|
||||
return suppliers;
|
||||
} catch (error) {
|
||||
companyAutomat.setState({ suppliersError: error });
|
||||
throw error;
|
||||
}
|
||||
if (inflightSuppliers) return inflightSuppliers;
|
||||
inflightSuppliers = (async () => {
|
||||
try {
|
||||
const suppliers = await fetchSuppliersList();
|
||||
companyAutomat.setState({ suppliers, suppliersStatus: 'ready', suppliersError: null });
|
||||
void actions.loadAllSupplierArticles().catch(() => {});
|
||||
return suppliers;
|
||||
} catch (error) {
|
||||
companyAutomat.setState({ suppliersError: error });
|
||||
throw error;
|
||||
} finally {
|
||||
inflightSuppliers = null;
|
||||
}
|
||||
})();
|
||||
return inflightSuppliers;
|
||||
},
|
||||
|
||||
async loadArticlesForSupplier(supplierId) {
|
||||
@@ -756,6 +763,44 @@ const actions = {
|
||||
}
|
||||
},
|
||||
|
||||
syncArticleSuppliers(sku, suppliers) {
|
||||
const artSku = String(sku).trim().toLowerCase();
|
||||
const assignedSupplierIds = new Set(
|
||||
(suppliers || []).map((s) => String(s.entityId ?? s.supplierId ?? s.id))
|
||||
);
|
||||
const prevMap = companyAutomat.state.articlesBySupplier || {};
|
||||
let changed = false;
|
||||
const nextMap = { ...prevMap };
|
||||
|
||||
for (const sId of assignedSupplierIds) {
|
||||
const currentList = nextMap[sId] ? [...nextMap[sId]] : [];
|
||||
const hasSku = currentList.some(
|
||||
(item) => String(item.article?.sku ?? item.sku ?? '').toLowerCase() === artSku
|
||||
);
|
||||
if (!hasSku) {
|
||||
currentList.push({ sku, article: { sku } });
|
||||
nextMap[sId] = currentList;
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
|
||||
for (const [sId, list] of Object.entries(nextMap)) {
|
||||
if (!assignedSupplierIds.has(sId) && Array.isArray(list)) {
|
||||
const filtered = list.filter(
|
||||
(item) => String(item.article?.sku ?? item.sku ?? '').toLowerCase() !== artSku
|
||||
);
|
||||
if (filtered.length !== list.length) {
|
||||
nextMap[sId] = filtered;
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (changed) {
|
||||
companyAutomat.setState({ articlesBySupplier: nextMap });
|
||||
}
|
||||
},
|
||||
|
||||
async createSupplier(body) {
|
||||
const data = await supplierApi.create(body);
|
||||
requireObject(data, 'POST /api/supplier');
|
||||
@@ -1474,6 +1519,42 @@ authAutomat.subscribe(syncWithAuth);
|
||||
syncWithAuth(authAutomat.state);
|
||||
// #endregion
|
||||
|
||||
// #region sseAutomat synchronization
|
||||
sseAutomat.actions.on('supplier', () => {
|
||||
void companyAutomat.actions.reloadSuppliers().catch(() => {});
|
||||
});
|
||||
sseAutomat.actions.on('warehouse', () => {
|
||||
void companyAutomat.actions.reloadWarehouses().catch(() => {});
|
||||
});
|
||||
sseAutomat.actions.on('brand', () => {
|
||||
void companyAutomat.actions.loadBrands(true).catch(() => {});
|
||||
});
|
||||
sseAutomat.actions.on('unit', () => {
|
||||
void companyAutomat.actions.loadUnits().catch(() => {});
|
||||
});
|
||||
sseAutomat.actions.on('carrier', () => {
|
||||
void companyAutomat.actions.reloadCarriers().catch(() => {});
|
||||
});
|
||||
sseAutomat.actions.on('posterminal', () => {
|
||||
void companyAutomat.actions.loadTerminals().catch(() => {});
|
||||
});
|
||||
sseAutomat.actions.on('customergroup', () => {
|
||||
void companyAutomat.actions.reloadCustomergroups().catch(() => {});
|
||||
});
|
||||
sseAutomat.actions.on('propertyname', () => {
|
||||
void companyAutomat.actions.loadProperties().catch(() => {});
|
||||
});
|
||||
sseAutomat.actions.on('attributename', () => {
|
||||
void companyAutomat.actions.loadAttributes().catch(() => {});
|
||||
});
|
||||
sseAutomat.actions.on('account', () => {
|
||||
void companyAutomat.actions.reloadAccounts().catch(() => {});
|
||||
});
|
||||
sseAutomat.actions.on('company', () => {
|
||||
void companyAutomat.actions.reload().catch(() => {});
|
||||
});
|
||||
// #endregion
|
||||
|
||||
// #region selectors & exported action helpers
|
||||
// Company profile selectors
|
||||
export const selectCompanies = () => companyAutomat.state.companies;
|
||||
|
||||
Reference in New Issue
Block a user