Checkpoint: Correctifs validés en sandbox : retour contextuel depuis le détail vers Factures BAP avec filtres et tri conservés, et nouvelle action Abonnement Oui/Non/Ne pas modifier dans les automatismes. Ajout de tests de non-régression.
This commit is contained in:
66
server/automationEngine.test.ts
Normal file
66
server/automationEngine.test.ts
Normal file
@@ -0,0 +1,66 @@
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import type { Invoice } from "../drizzle/schema";
|
||||
import { getAutomationRulesByUser } from "./db";
|
||||
import { applyAutomationRules } from "./automationEngine";
|
||||
|
||||
vi.mock("./db", () => ({
|
||||
getAutomationRulesByUser: vi.fn(),
|
||||
}));
|
||||
|
||||
const mockedGetAutomationRules = vi.mocked(getAutomationRulesByUser);
|
||||
|
||||
const invoice = {
|
||||
id: 1,
|
||||
supplierName: "Microsoft Ireland Operations Ltd",
|
||||
isSubscription: 0,
|
||||
} as Invoice;
|
||||
|
||||
describe("applyAutomationRules", () => {
|
||||
beforeEach(() => {
|
||||
mockedGetAutomationRules.mockReset();
|
||||
});
|
||||
|
||||
it("applique Abonnement = Oui et trace le champ automatisé", async () => {
|
||||
mockedGetAutomationRules.mockResolvedValue([
|
||||
{
|
||||
id: 1,
|
||||
userId: 1,
|
||||
name: "Microsoft est un abonnement",
|
||||
isActive: 1,
|
||||
priority: 1,
|
||||
conditions: JSON.stringify([{ field: "supplierName", operator: "contains", value: "microsoft" }]),
|
||||
conditionsLogic: "AND",
|
||||
actions: JSON.stringify({ isSubscription: 1 }),
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
},
|
||||
] as never);
|
||||
|
||||
await expect(applyAutomationRules(1, invoice)).resolves.toMatchObject({
|
||||
isSubscription: 1,
|
||||
autoFilledFields: JSON.stringify(["isSubscription"]),
|
||||
});
|
||||
});
|
||||
|
||||
it("autorise Abonnement = Non sans le confondre avec une absence d’action", async () => {
|
||||
mockedGetAutomationRules.mockResolvedValue([
|
||||
{
|
||||
id: 2,
|
||||
userId: 1,
|
||||
name: "Microsoft n’est pas un abonnement",
|
||||
isActive: 1,
|
||||
priority: 1,
|
||||
conditions: JSON.stringify([{ field: "supplierName", operator: "contains", value: "microsoft" }]),
|
||||
conditionsLogic: "AND",
|
||||
actions: JSON.stringify({ isSubscription: 0 }),
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
},
|
||||
] as never);
|
||||
|
||||
await expect(applyAutomationRules(1, invoice)).resolves.toMatchObject({
|
||||
isSubscription: 0,
|
||||
autoFilledFields: JSON.stringify(["isSubscription"]),
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -11,6 +11,7 @@ interface Actions {
|
||||
typeAchat?: string;
|
||||
serviceConcerne?: string;
|
||||
ventilationComptable?: string;
|
||||
isSubscription?: 0 | 1;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -111,6 +112,11 @@ export async function applyAutomationRules(
|
||||
updates.ventilationComptable = actions.ventilationComptable;
|
||||
autoFilledFieldsList.push("ventilationComptable");
|
||||
}
|
||||
// 0 est une valeur métier valide : ne jamais la tester par vérité.
|
||||
if (actions.isSubscription !== undefined && updates.isSubscription === undefined) {
|
||||
updates.isSubscription = actions.isSubscription;
|
||||
autoFilledFieldsList.push("isSubscription");
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`[AutomationEngine] Error processing rule ${rule.id}:`, error);
|
||||
|
||||
52
server/bapNavigation.test.ts
Normal file
52
server/bapNavigation.test.ts
Normal file
@@ -0,0 +1,52 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import {
|
||||
buildBapDetailLocation,
|
||||
getBapReturnLocation,
|
||||
parseBapFilters,
|
||||
} from "../client/src/lib/bapNavigation";
|
||||
|
||||
describe("navigation Factures BAP", () => {
|
||||
it("transmet tous les filtres et le tri à la page de détail", () => {
|
||||
const location = buildBapDetailLocation(42, {
|
||||
searchQuery: "microsoft europe",
|
||||
statusFilter: "bap_pending",
|
||||
selectedYear: "2026",
|
||||
selectedMonth: "05",
|
||||
sortField: "invoiceDate",
|
||||
sortDir: "asc",
|
||||
});
|
||||
|
||||
expect(location).toBe(
|
||||
"/invoices/42?returnTo=bap&q=microsoft+europe&status=bap_pending&year=2026&month=05&sort=invoiceDate&dir=asc"
|
||||
);
|
||||
});
|
||||
|
||||
it("restaure la liste BAP et les filtres de façon sûre", () => {
|
||||
const detailSearch =
|
||||
"?returnTo=bap&q=microsoft+europe&status=bap_pending&year=2026&month=05&sort=invoiceDate&dir=asc";
|
||||
|
||||
expect(getBapReturnLocation(detailSearch)).toBe(
|
||||
"/invoices-bap?q=microsoft+europe&status=bap_pending&year=2026&month=05&sort=invoiceDate&dir=asc"
|
||||
);
|
||||
expect(parseBapFilters(detailSearch, "2026")).toEqual({
|
||||
searchQuery: "microsoft europe",
|
||||
statusFilter: "bap_pending",
|
||||
selectedYear: "2026",
|
||||
selectedMonth: "05",
|
||||
sortField: "invoiceDate",
|
||||
sortDir: "asc",
|
||||
});
|
||||
});
|
||||
|
||||
it("retombe sur les valeurs BAP sûres si les paramètres sont absents ou invalides", () => {
|
||||
expect(parseBapFilters("?returnTo=bap&status=invalid&year=x&sort=nope&dir=down", "2026")).toEqual({
|
||||
searchQuery: "",
|
||||
statusFilter: "all",
|
||||
selectedYear: "2026",
|
||||
selectedMonth: "all",
|
||||
sortField: "createdAt",
|
||||
sortDir: "desc",
|
||||
});
|
||||
expect(getBapReturnLocation("?returnTo=invoices")).toBe("/invoices");
|
||||
});
|
||||
});
|
||||
@@ -11,6 +11,7 @@ interface Actions {
|
||||
typeAchat?: string;
|
||||
serviceConcerne?: string;
|
||||
ventilationComptable?: string;
|
||||
isSubscription?: 0 | 1;
|
||||
}
|
||||
import { getSessionCookieOptions } from "./_core/cookies";
|
||||
import { systemRouter } from "./_core/systemRouter";
|
||||
@@ -2028,7 +2029,7 @@ export const appRouter = router({
|
||||
|
||||
for (const invoice of bapInvoices) {
|
||||
const updates = await applyAutomationRules(ctx.user.id, invoice);
|
||||
if (updates.typeAchat || updates.serviceConcerne || updates.ventilationComptable || updates.autoFilledFields) {
|
||||
if (updates.typeAchat || updates.serviceConcerne || updates.ventilationComptable || updates.isSubscription !== undefined || updates.autoFilledFields) {
|
||||
await updateInvoice(invoice.id, updates);
|
||||
}
|
||||
}
|
||||
@@ -2059,7 +2060,7 @@ export const appRouter = router({
|
||||
|
||||
for (const invoice of bapInvoices) {
|
||||
const updates = await applyAutomationRules(ctx.user.id, invoice);
|
||||
if (updates.typeAchat || updates.serviceConcerne || updates.ventilationComptable || updates.autoFilledFields) {
|
||||
if (updates.typeAchat || updates.serviceConcerne || updates.ventilationComptable || updates.isSubscription !== undefined || updates.autoFilledFields) {
|
||||
await updateInvoice(invoice.id, updates);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user