ui-nyla/src/utils/mandateBillingFormMerge.ts
2026-04-21 00:50:42 +02:00

147 lines
4.5 KiB
TypeScript

/**
* Merges mandate core fields with billing settings for SysAdmin mandate forms.
* Billing is stored separately (BillingSettings); attributes API only exposes Mandate model fields.
*/
import type { AttributeDefinition } from '../components/FormGenerator/FormGeneratorForm';
import type { BillingSettings, BillingSettingsUpdate } from '../api/billingApi';
export const mandateBillingFieldNames = [
'warningThresholdPercent',
'notifyOnWarning',
'notifyEmails',
] as const;
export type MandateBillingFieldName = (typeof mandateBillingFieldNames)[number];
/** FormGenerator attribute definitions for mandate billing (appended after /api/attributes/Mandate). */
export function getMandateBillingFormAttributes(): AttributeDefinition[] {
return [
{
name: 'warningThresholdPercent',
type: 'float',
label: 'Warnschwelle (%)',
description: 'Benachrichtigung, wenn das Guthaben unter diesem Prozentsatz fällt.',
required: false,
default: 10,
editable: true,
order: 102,
},
{
name: 'notifyOnWarning',
type: 'checkbox',
label: 'Bei Warnung benachrichtigen',
required: false,
default: true,
editable: true,
order: 102,
},
{
name: 'notifyEmails',
type: 'textarea',
label: 'Benachrichtigungs-E-Mails',
description: 'Eine Adresse pro Zeile oder durch Komma/Semikolon getrennt.',
required: false,
default: '',
editable: true,
order: 103,
minRows: 2,
maxRows: 6,
},
];
}
function _parseNotifyEmailsInput(val: unknown): string[] {
if (Array.isArray(val)) {
return val.map(String).map(s => s.trim()).filter(Boolean);
}
if (typeof val !== 'string') {
return [];
}
return val
.split(/[\n,;]+/)
.map(s => s.trim())
.filter(Boolean);
}
export function mergeBillingIntoMandateFormData(
mandate: Record<string, unknown>,
settings: BillingSettings | null
): Record<string, unknown> {
if (!settings) {
return {
...mandate,
warningThresholdPercent: 10,
notifyOnWarning: true,
notifyEmails: '',
};
}
return {
...mandate,
warningThresholdPercent: Number(settings.warningThresholdPercent ?? 10),
notifyOnWarning: settings.notifyOnWarning ?? true,
notifyEmails: (settings.notifyEmails || []).join('\n'),
};
}
/** Mandate fields that the AdminMandates form is allowed to update. */
const _MANDATE_INVOICE_FIELDS = [
'invoiceCompanyName',
'invoiceContactName',
'invoiceEmail',
'invoiceLine1',
'invoiceLine2',
'invoicePostalCode',
'invoiceCity',
'invoiceState',
'invoiceCountry',
'invoiceVatNumber',
] as const;
/**
* Split form submit payload into mandate PUT body and billing POST body.
*
* Only fields that the user can actually edit are forwarded. Audit-only /
* read-only fields (id, deletedAt, isSystem, ...) are intentionally dropped.
* The structured ``invoice*`` address fields are round-tripped here so the
* address entered in the form is persisted on Mandate; empty strings are
* normalized to ``null`` so the backend stores nothing instead of "".
*/
export function splitMandateAndBillingFromForm(
formData: Record<string, unknown>
): { mandatePayload: Record<string, unknown>; billingUpdate: BillingSettingsUpdate } {
const mandatePayload: Record<string, unknown> = {};
if ('name' in formData) mandatePayload.name = formData.name;
if ('label' in formData) mandatePayload.label = formData.label;
if ('enabled' in formData) mandatePayload.enabled = formData.enabled;
for (const fieldName of _MANDATE_INVOICE_FIELDS) {
if (!(fieldName in formData)) continue;
const raw = formData[fieldName];
if (raw === null || raw === undefined) {
mandatePayload[fieldName] = null;
} else if (typeof raw === 'string') {
const trimmed = raw.trim();
mandatePayload[fieldName] = trimmed.length === 0 ? null : trimmed;
} else {
mandatePayload[fieldName] = raw;
}
}
const billingUpdate: BillingSettingsUpdate = {};
if (
'warningThresholdPercent' in formData &&
formData.warningThresholdPercent !== undefined &&
formData.warningThresholdPercent !== ''
) {
const n = Number(formData.warningThresholdPercent);
if (!Number.isNaN(n)) billingUpdate.warningThresholdPercent = n;
}
if ('notifyOnWarning' in formData) {
billingUpdate.notifyOnWarning = Boolean(formData.notifyOnWarning);
}
if ('notifyEmails' in formData) {
billingUpdate.notifyEmails = _parseNotifyEmailsInput(formData.notifyEmails);
}
return { mandatePayload, billingUpdate };
}