33 lines
1.1 KiB
TypeScript
33 lines
1.1 KiB
TypeScript
/** Valeurs nécessaires pour déterminer une destination d’export. */
|
||
export type ExportableInvoice = {
|
||
recipientName?: string | null;
|
||
ventilationComptable?: string | null;
|
||
};
|
||
|
||
export type ExportRuleCandidate = {
|
||
isActive: number;
|
||
priority: number;
|
||
conditionField: "recipientName" | "ventilationComptable";
|
||
conditionValue: string;
|
||
};
|
||
|
||
/**
|
||
* Sélectionne la première règle active par priorité. Les comparaisons sont
|
||
* volontairement insensibles à la casse et aux espaces superflus.
|
||
*/
|
||
export function findMatchingExportRule<T extends ExportRuleCandidate>(
|
||
rules: T[],
|
||
invoice: ExportableInvoice,
|
||
): T | undefined {
|
||
const normalized = (value: string | null | undefined) => value?.trim().toLocaleLowerCase("fr-FR") || "";
|
||
const ordered = [...rules].sort((a, b) => a.priority - b.priority || a.conditionValue.localeCompare(b.conditionValue, "fr-FR"));
|
||
|
||
return ordered.find((rule) => {
|
||
if (rule.isActive !== 1) return false;
|
||
const invoiceValue = rule.conditionField === "recipientName"
|
||
? invoice.recipientName
|
||
: invoice.ventilationComptable;
|
||
return normalized(invoiceValue) === normalized(rule.conditionValue);
|
||
});
|
||
}
|