This commit is contained in:
seb
2026-07-31 17:51:57 +02:00
parent 797ea59bed
commit a6a9777f31
33 changed files with 1432 additions and 422 deletions

20
src/utils/apiShape.js Normal file
View File

@@ -0,0 +1,20 @@
export function requireObject(value, label) {
if (value == null || typeof value !== 'object' || Array.isArray(value)) {
throw new Error(`${label}: Objekt erwartet`);
}
return value;
}
export function requireArray(value, label) {
if (!Array.isArray(value)) {
throw new Error(`${label}: Array erwartet`);
}
return value;
}
export function requireDefined(value, label) {
if (value == null) {
throw new Error(`${label}: fehlt`);
}
return value;
}

View File

@@ -1,5 +1,6 @@
export const ISO639_OPTIONS = ['de', 'en'];
/** Default für leeres Anlege-Formular (Browser-Sprache), nicht für API-Parsing. */
export function defaultIso639() {
return navigator.language?.toLowerCase().startsWith('de') ? 'de' : 'en';
}
@@ -10,21 +11,14 @@ export function parseTaxRate(value) {
return Number.isFinite(parsed) && parsed >= 0 ? parsed : null;
}
/** Until backend returns iso639 on company objects. */
export function companyLanguageToIso639(company) {
if (company?.iso639) return company.iso639;
if (company?.languageId === 2) return 'en';
return 'de';
}
export function normalizeCompany(raw, overrides = {}) {
if (!raw) return null;
return {
id: raw.id,
name: overrides.name ?? raw.name ?? '',
iso639: overrides.iso639 ?? companyLanguageToIso639(raw),
taxrate: overrides.taxrate ?? raw.taxrate ?? null,
reducedtaxrate: overrides.reducedtaxrate ?? raw.reducedtaxrate ?? null,
rootCategoryId: overrides.rootCategoryId ?? raw.rootCategoryId ?? null,
name: overrides.name !== undefined ? overrides.name : raw.name,
iso639: overrides.iso639 !== undefined ? overrides.iso639 : raw.iso639,
taxrate: overrides.taxrate !== undefined ? overrides.taxrate : raw.taxrate,
reducedtaxrate: overrides.reducedtaxrate !== undefined ? overrides.reducedtaxrate : raw.reducedtaxrate,
rootCategoryId: overrides.rootCategoryId !== undefined ? overrides.rootCategoryId : raw.rootCategoryId,
};
}