Files
demat-facturation/server/exportDestinationResolver.ts

43 lines
1.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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 lexistant.
*/
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",
};
}