This commit is contained in:
seb
2026-10-28 19:32:19 +01:00
parent 3c51de06c2
commit 550586f46e
6 changed files with 113 additions and 137 deletions

View File

@@ -211,6 +211,7 @@ const initialUserSettings = readInitialUserSettings();
const initialState = {
// 1. Company profile
companies: [],
current: null,
status: 'idle',
error: null,
@@ -385,18 +386,33 @@ async function fetchCarriersList() {
const actions = {
// #region 1. Company profile & orchestration
// MARK: calls company.getById from ../api/index.js
// MARK: calls company.list from ../api/index.js
async load() {
const companyId = authAutomat.state.user?.selectedCompanyId;
if (!companyId) return null;
const hasAuth = Boolean(
authAutomat.state.hasAuth ??
(authAutomat.state.status === 'authed' && authAutomat.state.user),
);
if (!hasAuth) return null;
if (inflightCompany) return inflightCompany;
companyAutomat.setState({ status: 'loading', error: null });
inflightCompany = (async () => {
try {
const data = await companyApi.getById(companyId);
const normalized = normalizeCompany(data.company);
companyAutomat.setState({ current: normalized, status: 'succeeded', error: null });
return normalized;
const data = await companyApi.list();
const companies = Array.isArray(data?.companies)
? data.companies.map((item) => normalizeCompany(item)).filter(Boolean)
: [];
const companyId = authAutomat.state.user?.selectedCompanyId;
const current =
companyId != null
? (companies.find(
(item) => item.id === companyId || String(item.id) === String(companyId),
) ?? null)
: null;
if (companyId != null && !current) {
throw new Error('Unternehmen nicht gefunden.');
}
companyAutomat.setState({ companies, current, status: 'succeeded', error: null });
return current;
} catch (err) {
companyAutomat.setState({ status: 'failed', error: err?.message ?? String(err) });
throw err;
@@ -407,16 +423,55 @@ const actions = {
return inflightCompany;
},
// MARK: calls company.update and company.getById from ../api/index.js
// MARK: calls company.list from ../api/index.js
async reload() {
const hasAuth = Boolean(
authAutomat.state.hasAuth ??
(authAutomat.state.status === 'authed' && authAutomat.state.user),
);
if (!hasAuth) return null;
try {
const data = await companyApi.list();
const companies = Array.isArray(data?.companies)
? data.companies.map((item) => normalizeCompany(item)).filter(Boolean)
: [];
const companyId = authAutomat.state.user?.selectedCompanyId;
const current =
companyId != null
? (companies.find(
(item) => item.id === companyId || String(item.id) === String(companyId),
) ?? companyAutomat.state.current)
: null;
companyAutomat.setState({ companies, current, status: 'succeeded', error: null });
return current;
} catch (err) {
companyAutomat.setState({ error: err?.message ?? String(err) });
throw err;
}
},
// MARK: calls company.update and company.list from ../api/index.js
async update(payload) {
const companyId = authAutomat.state.user?.selectedCompanyId;
if (!companyId) throw new Error('Kein Unternehmen ausgewählt.');
companyAutomat.setState({ status: 'loading', error: null });
try {
await companyApi.update(companyId, payload);
const data = await companyApi.getById(companyId);
const normalized = normalizeCompany(data.company, payload);
companyAutomat.setState({ current: normalized, status: 'succeeded', error: null });
const data = await companyApi.list();
const companies = Array.isArray(data?.companies)
? data.companies
.map((item) =>
item.id === companyId || String(item.id) === String(companyId)
? normalizeCompany(item, payload)
: normalizeCompany(item),
)
.filter(Boolean)
: [];
const normalized =
companies.find((item) => item.id === companyId || String(item.id) === String(companyId)) ??
null;
if (!normalized) throw new Error('Unternehmen nicht gefunden.');
companyAutomat.setState({ companies, current: normalized, status: 'succeeded', error: null });
return normalized;
} catch (err) {
companyAutomat.setState({ status: 'failed', error: err?.message ?? String(err) });
@@ -1391,17 +1446,24 @@ companyAutomat = new Automat(initialState, actions, { name: 'company' });
hydrateUserSettingsFromIdb(companyAutomat);
// #region authAutomat synchronization
let lastCompanyKey;
let lastAuthKey;
function syncWithAuth(auth) {
const hasAuth = Boolean(
auth?.hasAuth ?? (auth?.status === 'authed' && auth?.user),
);
const hasCompany = Boolean(
auth?.hasCompany ?? (auth?.status === 'authed' && auth?.user?.selectedCompanyId != null),
auth?.hasCompany ?? (hasAuth && auth?.user?.selectedCompanyId != null),
);
const companyId = hasCompany ? String(auth.user.selectedCompanyId) : null;
if (companyId === lastCompanyKey) return;
lastCompanyKey = companyId;
if (companyId == null) {
const authKey = hasAuth ? `authed:${companyId ?? ''}` : 'anon';
if (authKey === lastAuthKey) return;
lastAuthKey = authKey;
if (!hasAuth) {
companyAutomat.actions.reset();
} else if (companyId == null) {
companyAutomat.actions.reset();
void companyAutomat.actions.load().catch(() => {});
} else {
companyAutomat.actions.reset();
void companyAutomat.actions.loadAll();
@@ -1414,6 +1476,7 @@ syncWithAuth(authAutomat.state);
// #region selectors & exported action helpers
// Company profile selectors
export const selectCompanies = () => companyAutomat.state.companies;
export const selectCurrentCompany = () => companyAutomat.state.current;
export const selectCompanyId = () => companyAutomat.state.current?.id ?? null;
export const selectCompanyStatus = () => companyAutomat.state.status;