43 lines
1.5 KiB
TypeScript
43 lines
1.5 KiB
TypeScript
import type { ImportSettings, Invoice } from "../drizzle/schema";
|
||
import { findMatchingExportRule } from "@shared/exportAutomation";
|
||
import { getExportAutomationRulesByUser } from "./db";
|
||
|
||
export type ResolvedExportDestination = {
|
||
exportMode: "browser" | "folder" | "both";
|
||
destinationPath: string | null;
|
||
destinationType: "local" | "teams" | "sharepoint";
|
||
source: "rule" | "default";
|
||
ruleName?: string;
|
||
};
|
||
|
||
/**
|
||
* Résout une destination pour une validation BAP. Une règle active ciblant le
|
||
* destinataire ou la ventilation est prioritaire sur les anciens paramètres
|
||
* globaux, qui restent le comportement de repli pour préserver l’existant.
|
||
*/
|
||
export async function resolveBapExportDestination(
|
||
userId: number,
|
||
invoice: Pick<Invoice, "recipientName" | "ventilationComptable">,
|
||
settings: ImportSettings | null,
|
||
): Promise<ResolvedExportDestination> {
|
||
const rules = await getExportAutomationRulesByUser(userId);
|
||
const matchingRule = findMatchingExportRule(rules, invoice);
|
||
|
||
if (matchingRule) {
|
||
return {
|
||
exportMode: matchingRule.openInBrowser === 1 ? "both" : "folder",
|
||
destinationPath: matchingRule.destinationPath,
|
||
destinationType: matchingRule.destinationType,
|
||
source: "rule",
|
||
ruleName: matchingRule.name,
|
||
};
|
||
}
|
||
|
||
return {
|
||
exportMode: settings?.bapExportMode || "browser",
|
||
destinationPath: settings?.exportFolder || null,
|
||
destinationType: settings?.exportFolderType || "local",
|
||
source: "default",
|
||
};
|
||
}
|