@flowselections/e-mail-installatie-techniek 1.0.8 → 1.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +68 -0
- package/dist-lib/components/WerkbonPostvakInstelling.d.ts +2 -0
- package/dist-lib/components/WerkbonPostvakInstelling.d.ts.map +1 -0
- package/dist-lib/components/WerkbonPostvakInstelling.js +76 -0
- package/dist-lib/components/mail/CompetentiePicker.d.ts +9 -0
- package/dist-lib/components/mail/CompetentiePicker.d.ts.map +1 -0
- package/dist-lib/components/mail/CompetentiePicker.js +49 -0
- package/dist-lib/components/mail/MailPane.d.ts.map +1 -1
- package/dist-lib/components/mail/MailPane.js +13 -9
- package/dist-lib/components/mail/WerkbonProposalDialog.d.ts.map +1 -1
- package/dist-lib/components/mail/WerkbonProposalDialog.js +32 -4
- package/dist-lib/components/mail/WerkbonProposalForm.d.ts +3 -1
- package/dist-lib/components/mail/WerkbonProposalForm.d.ts.map +1 -1
- package/dist-lib/components/mail/WerkbonProposalForm.js +47 -21
- package/dist-lib/hooks/useAutoAnalyse.d.ts.map +1 -1
- package/dist-lib/hooks/useAutoAnalyse.js +36 -25
- package/dist-lib/index.d.ts +1 -0
- package/dist-lib/index.d.ts.map +1 -1
- package/dist-lib/index.js +4 -0
- package/dist-lib/integrations/supabase/previewAuthStorage.d.ts.map +1 -1
- package/dist-lib/integrations/supabase/previewAuthStorage.js +2 -1
- package/dist-lib/integrations/supabase/types.d.ts +14525 -392
- package/dist-lib/integrations/supabase/types.d.ts.map +1 -1
- package/dist-lib/integrations/supabase/types.js +1 -0
- package/dist-lib/lib/aantal-paginate.test.d.ts +2 -0
- package/dist-lib/lib/aantal-paginate.test.d.ts.map +1 -0
- package/dist-lib/lib/aantal-paginate.test.js +39 -0
- package/dist-lib/lib/aantal.d.ts +2 -0
- package/dist-lib/lib/aantal.d.ts.map +1 -0
- package/dist-lib/lib/aantal.js +10 -0
- package/dist-lib/lib/competenties.d.ts +12 -0
- package/dist-lib/lib/competenties.d.ts.map +1 -0
- package/dist-lib/lib/competenties.functions.d.ts +13 -0
- package/dist-lib/lib/competenties.functions.d.ts.map +1 -0
- package/dist-lib/lib/competenties.functions.js +22 -0
- package/dist-lib/lib/competenties.js +24 -0
- package/dist-lib/lib/competenties.test.d.ts +2 -0
- package/dist-lib/lib/competenties.test.d.ts.map +1 -0
- package/dist-lib/lib/competenties.test.js +15 -0
- package/dist-lib/lib/klant-match.d.ts +38 -0
- package/dist-lib/lib/klant-match.d.ts.map +1 -0
- package/dist-lib/lib/klant-match.js +88 -0
- package/dist-lib/lib/klant-match.test.d.ts +2 -0
- package/dist-lib/lib/klant-match.test.d.ts.map +1 -0
- package/dist-lib/lib/klant-match.test.js +76 -0
- package/dist-lib/lib/klanten.functions.d.ts.map +1 -1
- package/dist-lib/lib/klanten.functions.js +44 -51
- package/dist-lib/lib/paginate.d.ts +10 -0
- package/dist-lib/lib/paginate.d.ts.map +1 -0
- package/dist-lib/lib/paginate.js +18 -0
- package/dist-lib/lib/rekenregels-gelijk.test.d.ts +2 -0
- package/dist-lib/lib/rekenregels-gelijk.test.d.ts.map +1 -0
- package/dist-lib/lib/rekenregels-gelijk.test.js +63 -0
- package/dist-lib/lib/rekenregels.d.ts +44 -0
- package/dist-lib/lib/rekenregels.d.ts.map +1 -0
- package/dist-lib/lib/rekenregels.js +159 -0
- package/dist-lib/lib/rekenregels.test.d.ts +2 -0
- package/dist-lib/lib/rekenregels.test.d.ts.map +1 -0
- package/dist-lib/lib/rekenregels.test.js +74 -0
- package/dist-lib/lib/werkbon-approve-rows.d.ts +64 -0
- package/dist-lib/lib/werkbon-approve-rows.d.ts.map +1 -0
- package/dist-lib/lib/werkbon-approve-rows.js +56 -0
- package/dist-lib/lib/werkbon-approve-rows.test.d.ts +2 -0
- package/dist-lib/lib/werkbon-approve-rows.test.d.ts.map +1 -0
- package/dist-lib/lib/werkbon-approve-rows.test.js +103 -0
- package/dist-lib/lib/werkbon-postvak-settings.d.ts +15 -0
- package/dist-lib/lib/werkbon-postvak-settings.d.ts.map +1 -0
- package/dist-lib/lib/werkbon-postvak-settings.functions.d.ts +14 -0
- package/dist-lib/lib/werkbon-postvak-settings.functions.d.ts.map +1 -0
- package/dist-lib/lib/werkbon-postvak-settings.functions.js +24 -0
- package/dist-lib/lib/werkbon-postvak-settings.js +59 -0
- package/dist-lib/lib/werkbon-proposals.functions.d.ts +33 -0
- package/dist-lib/lib/werkbon-proposals.functions.d.ts.map +1 -1
- package/dist-lib/lib/werkbon-proposals.functions.js +100 -81
- package/package.json +131 -7
- package/supabase/werkbon-postvak-installatie.sql +758 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"aantal-paginate.test.d.ts","sourceRoot":"","sources":["../../src/lib/aantal-paginate.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { parseAantal } from "./aantal";
|
|
3
|
+
import { fetchAllPages } from "./paginate";
|
|
4
|
+
import { DEFAULT_POSTVAK_SETTINGS, normalizePostvakSettings } from "./werkbon-postvak-settings";
|
|
5
|
+
describe("parseAantal", () => {
|
|
6
|
+
it("komma en punt", () => {
|
|
7
|
+
expect(parseAantal("1,5")).toBe(1.5);
|
|
8
|
+
expect(parseAantal("1.5")).toBe(1.5);
|
|
9
|
+
expect(parseAantal("3")).toBe(3);
|
|
10
|
+
});
|
|
11
|
+
it("leeg → null, ongeldig → NaN", () => {
|
|
12
|
+
expect(parseAantal("")).toBeNull();
|
|
13
|
+
expect(Number.isNaN(parseAantal("1,2,3"))).toBe(true);
|
|
14
|
+
});
|
|
15
|
+
it("gelijk aan oud voor hele getallen", () => {
|
|
16
|
+
for (const t of ["1", "2", "10"])
|
|
17
|
+
expect(parseAantal(t)).toBe(Number(t));
|
|
18
|
+
});
|
|
19
|
+
});
|
|
20
|
+
describe("fetchAllPages", () => {
|
|
21
|
+
it("haalt meer dan 1000 rijen op", async () => {
|
|
22
|
+
const rows = Array.from({ length: 2500 }, (_, i) => i);
|
|
23
|
+
const r = await fetchAllPages(async (from, to) => ({ data: rows.slice(from, to + 1), error: null }), "t");
|
|
24
|
+
expect(r.data?.length).toBe(2500);
|
|
25
|
+
});
|
|
26
|
+
it("geeft een fout door", async () => {
|
|
27
|
+
const r = await fetchAllPages(async () => ({ data: null, error: { message: "x" } }), "t");
|
|
28
|
+
expect(r.error?.message).toBe("x");
|
|
29
|
+
});
|
|
30
|
+
});
|
|
31
|
+
describe("nieuwe instellingen", () => {
|
|
32
|
+
it("vallen terug op de standaard", () => {
|
|
33
|
+
const s = normalizePostvakSettings({ timezone: "Geen/Zone", analyse_claim_ttl_seconds: -1 });
|
|
34
|
+
expect(s.timezone).toBe("Europe/Amsterdam");
|
|
35
|
+
expect(s.analyse_claim_ttl_seconds).toBe(300);
|
|
36
|
+
expect(s.ai_local_date).toBe(false);
|
|
37
|
+
expect(DEFAULT_POSTVAK_SETTINGS.ai_local_date).toBe(false);
|
|
38
|
+
});
|
|
39
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"aantal.d.ts","sourceRoot":"","sources":["../../src/lib/aantal.ts"],"names":[],"mappings":"AAEA,wBAAgB,WAAW,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAKzD"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
// Aantal uit invoer: komma of punt als decimaalteken. "" → null (leeg),
|
|
2
|
+
// ongeldig → NaN zodat het scherm een melding kan tonen.
|
|
3
|
+
export function parseAantal(invoer) {
|
|
4
|
+
const s = String(invoer ?? "").trim();
|
|
5
|
+
if (s === "")
|
|
6
|
+
return null;
|
|
7
|
+
if (!/^\d+([.,]\d+)?$/.test(s))
|
|
8
|
+
return Number.NaN;
|
|
9
|
+
return Number(s.replace(",", "."));
|
|
10
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export interface CompetentieOptie {
|
|
2
|
+
id: string;
|
|
3
|
+
code: string;
|
|
4
|
+
naam: string;
|
|
5
|
+
categorie: string;
|
|
6
|
+
volgorde: number;
|
|
7
|
+
actief: boolean;
|
|
8
|
+
}
|
|
9
|
+
/** Ontdubbelt, houdt alleen bekende actieve ids over en geeft altijd een array terug. */
|
|
10
|
+
export declare function normaliseerCompetentieIds(ids: unknown, lijst?: CompetentieOptie[] | null): string[];
|
|
11
|
+
export declare function sorteerCompetenties(lijst: unknown): CompetentieOptie[];
|
|
12
|
+
//# sourceMappingURL=competenties.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"competenties.d.ts","sourceRoot":"","sources":["../../src/lib/competenties.ts"],"names":[],"mappings":"AAEA,MAAM,WAAW,gBAAgB;IAC/B,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,SAAS,EAAE,MAAM,CAAC;IAClB,QAAQ,EAAE,MAAM,CAAC;IACjB,MAAM,EAAE,OAAO,CAAC;CACjB;AAED,yFAAyF;AACzF,wBAAgB,yBAAyB,CAAC,GAAG,EAAE,OAAO,EAAE,KAAK,CAAC,EAAE,gBAAgB,EAAE,GAAG,IAAI,GAAG,MAAM,EAAE,CAWnG;AAED,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,OAAO,GAAG,gBAAgB,EAAE,CAMtE"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { type CompetentieOptie } from "./competenties";
|
|
2
|
+
export type { CompetentieOptie } from "./competenties";
|
|
3
|
+
/** Actieve competenties; available=false als de tabel van Urenregistratie ontbreekt. */
|
|
4
|
+
export declare const getCompetenties: import("@tanstack/start-client-core").OptionalFetcher<readonly [import("@tanstack/start-client-core").FunctionMiddlewareAfterServer<{}, unknown, undefined, {
|
|
5
|
+
supabase: import("../integrations/supabase/auth-middleware").AuthedSupabaseClient;
|
|
6
|
+
userId: string;
|
|
7
|
+
claims: import("@supabase/auth-js").UserMetadata;
|
|
8
|
+
}, undefined, undefined, undefined>], undefined, Promise<{
|
|
9
|
+
available: boolean;
|
|
10
|
+
items: CompetentieOptie[];
|
|
11
|
+
error: string | null;
|
|
12
|
+
}>>;
|
|
13
|
+
//# sourceMappingURL=competenties.functions.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"competenties.functions.d.ts","sourceRoot":"","sources":["../../src/lib/competenties.functions.ts"],"names":[],"mappings":"AAEA,OAAO,EAAuB,KAAK,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAE5E,YAAY,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAEvD,wFAAwF;AACxF,eAAO,MAAM,eAAe;;;;;eAEyB,OAAO;WAAS,gBAAgB,EAAE;WAAS,MAAM,GAAG,IAAI;GAezG,CAAC"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { createServerFn } from "@tanstack/react-start";
|
|
2
|
+
import { requireSupabaseAuth } from "../integrations/supabase/auth-middleware";
|
|
3
|
+
import { sorteerCompetenties } from "./competenties";
|
|
4
|
+
/** Actieve competenties; available=false als de tabel van Urenregistratie ontbreekt. */
|
|
5
|
+
export const getCompetenties = createServerFn({ method: "POST" })
|
|
6
|
+
.middleware([requireSupabaseAuth])
|
|
7
|
+
.handler(async ({ context }) => {
|
|
8
|
+
const sb = context.supabase;
|
|
9
|
+
const { data, error } = await sb
|
|
10
|
+
.from("uren_competenties")
|
|
11
|
+
.select("id, code, naam, categorie, volgorde, actief")
|
|
12
|
+
.eq("actief", true)
|
|
13
|
+
.order("categorie")
|
|
14
|
+
.order("volgorde")
|
|
15
|
+
.range(0, 999);
|
|
16
|
+
if (error) {
|
|
17
|
+
console.error("uren_competenties read failed", error);
|
|
18
|
+
const missing = error.code === "42P01" || error.code === "PGRST205";
|
|
19
|
+
return { available: false, items: [], error: missing ? null : error.message };
|
|
20
|
+
}
|
|
21
|
+
return { available: true, items: sorteerCompetenties(data), error: null };
|
|
22
|
+
});
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// Competenties uit de module Urenregistratie (tabel uren_competenties, alleen lezen).
|
|
2
|
+
/** Ontdubbelt, houdt alleen bekende actieve ids over en geeft altijd een array terug. */
|
|
3
|
+
export function normaliseerCompetentieIds(ids, lijst) {
|
|
4
|
+
const invoer = Array.isArray(ids) ? ids : [];
|
|
5
|
+
const toegestaan = Array.isArray(lijst) ? new Set(lijst.filter((c) => c.actief).map((c) => c.id)) : null;
|
|
6
|
+
const uit = [];
|
|
7
|
+
for (const v of invoer) {
|
|
8
|
+
if (typeof v !== "string" || v.trim() === "")
|
|
9
|
+
continue;
|
|
10
|
+
const id = v.trim();
|
|
11
|
+
if (toegestaan && !toegestaan.has(id))
|
|
12
|
+
continue;
|
|
13
|
+
if (!uit.includes(id))
|
|
14
|
+
uit.push(id);
|
|
15
|
+
}
|
|
16
|
+
return uit;
|
|
17
|
+
}
|
|
18
|
+
export function sorteerCompetenties(lijst) {
|
|
19
|
+
const safe = Array.isArray(lijst) ? lijst : [];
|
|
20
|
+
return safe
|
|
21
|
+
.filter((c) => c && c.actief)
|
|
22
|
+
.slice()
|
|
23
|
+
.sort((a, b) => a.categorie.localeCompare(b.categorie, "nl") || a.volgorde - b.volgorde || a.naam.localeCompare(b.naam, "nl"));
|
|
24
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"competenties.test.d.ts","sourceRoot":"","sources":["../../src/lib/competenties.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { normaliseerCompetentieIds, sorteerCompetenties } from "./competenties";
|
|
3
|
+
const c = (id, actief = true, categorie = "A", volgorde = 1) => ({ id, code: id, naam: id, categorie, volgorde, actief });
|
|
4
|
+
describe("normaliseerCompetentieIds", () => {
|
|
5
|
+
it("lege of foute invoer geeft []", () => {
|
|
6
|
+
expect(normaliseerCompetentieIds(undefined)).toEqual([]);
|
|
7
|
+
expect(normaliseerCompetentieIds(null)).toEqual([]);
|
|
8
|
+
expect(normaliseerCompetentieIds("x")).toEqual([]);
|
|
9
|
+
});
|
|
10
|
+
it("ontdubbelt en behoudt volgorde", () => expect(normaliseerCompetentieIds(["a", "b", "a"])).toEqual(["a", "b"]));
|
|
11
|
+
it("haalt onbekende en inactieve ids weg", () => expect(normaliseerCompetentieIds(["a", "b", "z"], [c("a"), c("b", false)])).toEqual(["a"]));
|
|
12
|
+
});
|
|
13
|
+
describe("sorteerCompetenties", () => {
|
|
14
|
+
it("sorteert op categorie en volgorde, alleen actief", () => expect(sorteerCompetenties([c("x", true, "B", 1), c("y", true, "A", 2), c("z", true, "A", 1), c("q", false)]).map((v) => v.id)).toEqual(["z", "y", "x"]));
|
|
15
|
+
});
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
export interface MatchCustomerRow {
|
|
2
|
+
id: string;
|
|
3
|
+
name: string | null;
|
|
4
|
+
general_email: string | null;
|
|
5
|
+
}
|
|
6
|
+
export interface MatchContactRow {
|
|
7
|
+
customer_id: string;
|
|
8
|
+
name: string | null;
|
|
9
|
+
email: string | null;
|
|
10
|
+
}
|
|
11
|
+
export declare function fullName(value: unknown): string;
|
|
12
|
+
export declare function uniqueMatch(ids: string[]): string | null;
|
|
13
|
+
/** Namen die als "zekere" naam tellen: minstens twee woorden. */
|
|
14
|
+
export declare function matchNames(candidates: unknown[]): string[];
|
|
15
|
+
/**
|
|
16
|
+
* Alleen een zekere match: exact e-mailadres (precies één klant) of
|
|
17
|
+
* exacte voor- + achternaam (precies één klant).
|
|
18
|
+
*/
|
|
19
|
+
export declare function matchKlant(customers: MatchCustomerRow[], contacts: MatchContactRow[], emails: string[], names: string[]): string | null;
|
|
20
|
+
export interface CandidateRow {
|
|
21
|
+
src: "customer" | "contact";
|
|
22
|
+
customer_id: string;
|
|
23
|
+
name: string | null;
|
|
24
|
+
email: string | null;
|
|
25
|
+
sim: number;
|
|
26
|
+
}
|
|
27
|
+
/** Splitst kandidaten uit de database terug naar klant- en contactrijen. */
|
|
28
|
+
export declare function splitCandidates(rows: CandidateRow[]): {
|
|
29
|
+
customers: MatchCustomerRow[];
|
|
30
|
+
contacts: MatchContactRow[];
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* Ruime herkenning (alleen als de schakelaar aan staat): precies één klant
|
|
34
|
+
* met een naamgelijkenis boven de grens, of precies één klant op hetzelfde
|
|
35
|
+
* (niet-gratis) e-maildomein.
|
|
36
|
+
*/
|
|
37
|
+
export declare function fuzzyMatch(rows: CandidateRow[], threshold: number, domains: string[]): string | null;
|
|
38
|
+
//# sourceMappingURL=klant-match.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"klant-match.d.ts","sourceRoot":"","sources":["../../src/lib/klant-match.ts"],"names":[],"mappings":"AAIA,MAAM,WAAW,gBAAgB;IAC/B,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;IACpB,aAAa,EAAE,MAAM,GAAG,IAAI,CAAC;CAC9B;AAED,MAAM,WAAW,eAAe;IAC9B,WAAW,EAAE,MAAM,CAAC;IACpB,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;IACpB,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;CACtB;AAGD,wBAAgB,QAAQ,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,CAS/C;AAED,wBAAgB,WAAW,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,MAAM,GAAG,IAAI,CAGxD;AAED,iEAAiE;AACjE,wBAAgB,UAAU,CAAC,UAAU,EAAE,OAAO,EAAE,GAAG,MAAM,EAAE,CAE1D;AAED;;;GAGG;AACH,wBAAgB,UAAU,CACxB,SAAS,EAAE,gBAAgB,EAAE,EAC7B,QAAQ,EAAE,eAAe,EAAE,EAC3B,MAAM,EAAE,MAAM,EAAE,EAChB,KAAK,EAAE,MAAM,EAAE,GACd,MAAM,GAAG,IAAI,CAyBf;AAED,MAAM,WAAW,YAAY;IAC3B,GAAG,EAAE,UAAU,GAAG,SAAS,CAAC;IAC5B,WAAW,EAAE,MAAM,CAAC;IACpB,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;IACpB,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;IACrB,GAAG,EAAE,MAAM,CAAC;CACb;AAED,4EAA4E;AAC5E,wBAAgB,eAAe,CAAC,IAAI,EAAE,YAAY,EAAE,GAAG;IACrD,SAAS,EAAE,gBAAgB,EAAE,CAAC;IAC9B,QAAQ,EAAE,eAAe,EAAE,CAAC;CAC7B,CAUA;AAED;;;;GAIG;AACH,wBAAgB,UAAU,CAAC,IAAI,EAAE,YAAY,EAAE,EAAE,SAAS,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,GAAG,MAAM,GAAG,IAAI,CAYpG"}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
// Pure regels voor het herkennen van een bestaande klant bij een mail.
|
|
2
|
+
// Gedeeld door de huidige aanpak (alles ophalen) en de database-aanpak
|
|
3
|
+
// (alleen kandidaten ophalen), zodat beide dezelfde klant vinden.
|
|
4
|
+
// Volledige naam: kleine letters, "Achternaam, Voornaam" omgedraaid.
|
|
5
|
+
export function fullName(value) {
|
|
6
|
+
let text = String(value ?? "").trim();
|
|
7
|
+
const comma = text.split(",");
|
|
8
|
+
if (comma.length === 2)
|
|
9
|
+
text = `${comma[1]} ${comma[0]}`;
|
|
10
|
+
return text
|
|
11
|
+
.toLowerCase()
|
|
12
|
+
.replace(/[^\p{L}\p{N}' -]+/gu, " ")
|
|
13
|
+
.replace(/\s+/g, " ")
|
|
14
|
+
.trim();
|
|
15
|
+
}
|
|
16
|
+
export function uniqueMatch(ids) {
|
|
17
|
+
const unique = Array.from(new Set(ids.filter(Boolean)));
|
|
18
|
+
return unique.length === 1 ? String(unique[0]) : null;
|
|
19
|
+
}
|
|
20
|
+
/** Namen die als "zekere" naam tellen: minstens twee woorden. */
|
|
21
|
+
export function matchNames(candidates) {
|
|
22
|
+
return Array.from(new Set(candidates.map(fullName).filter((n) => n.split(" ").length >= 2)));
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Alleen een zekere match: exact e-mailadres (precies één klant) of
|
|
26
|
+
* exacte voor- + achternaam (precies één klant).
|
|
27
|
+
*/
|
|
28
|
+
export function matchKlant(customers, contacts, emails, names) {
|
|
29
|
+
const cs = Array.isArray(customers) ? customers : [];
|
|
30
|
+
const ps = Array.isArray(contacts) ? contacts : [];
|
|
31
|
+
if (emails.length > 0) {
|
|
32
|
+
const ids = [
|
|
33
|
+
...cs
|
|
34
|
+
.filter((c) => emails.includes(String(c.general_email ?? "").trim().toLowerCase()))
|
|
35
|
+
.map((c) => String(c.id)),
|
|
36
|
+
...ps
|
|
37
|
+
.filter((c) => emails.includes(String(c.email ?? "").trim().toLowerCase()))
|
|
38
|
+
.map((c) => String(c.customer_id)),
|
|
39
|
+
];
|
|
40
|
+
const match = uniqueMatch(ids);
|
|
41
|
+
if (match)
|
|
42
|
+
return match;
|
|
43
|
+
if (new Set(ids).size > 1)
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
if (names.length > 0) {
|
|
47
|
+
const ids = [
|
|
48
|
+
...cs.filter((c) => names.includes(fullName(c.name))).map((c) => String(c.id)),
|
|
49
|
+
...ps.filter((c) => names.includes(fullName(c.name))).map((c) => String(c.customer_id)),
|
|
50
|
+
];
|
|
51
|
+
const match = uniqueMatch(ids);
|
|
52
|
+
if (match)
|
|
53
|
+
return match;
|
|
54
|
+
}
|
|
55
|
+
return null;
|
|
56
|
+
}
|
|
57
|
+
/** Splitst kandidaten uit de database terug naar klant- en contactrijen. */
|
|
58
|
+
export function splitCandidates(rows) {
|
|
59
|
+
const list = Array.isArray(rows) ? rows : [];
|
|
60
|
+
return {
|
|
61
|
+
customers: list
|
|
62
|
+
.filter((r) => r.src === "customer")
|
|
63
|
+
.map((r) => ({ id: String(r.customer_id), name: r.name, general_email: r.email })),
|
|
64
|
+
contacts: list
|
|
65
|
+
.filter((r) => r.src === "contact")
|
|
66
|
+
.map((r) => ({ customer_id: String(r.customer_id), name: r.name, email: r.email })),
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Ruime herkenning (alleen als de schakelaar aan staat): precies één klant
|
|
71
|
+
* met een naamgelijkenis boven de grens, of precies één klant op hetzelfde
|
|
72
|
+
* (niet-gratis) e-maildomein.
|
|
73
|
+
*/
|
|
74
|
+
export function fuzzyMatch(rows, threshold, domains) {
|
|
75
|
+
const list = Array.isArray(rows) ? rows : [];
|
|
76
|
+
const byName = uniqueMatch(list.filter((r) => Number(r.sim) >= threshold).map((r) => String(r.customer_id)));
|
|
77
|
+
if (byName)
|
|
78
|
+
return byName;
|
|
79
|
+
if (domains.length === 0)
|
|
80
|
+
return null;
|
|
81
|
+
const byDomain = list
|
|
82
|
+
.filter((r) => {
|
|
83
|
+
const e = String(r.email ?? "").trim().toLowerCase();
|
|
84
|
+
return domains.some((d) => e.endsWith(`@${d}`));
|
|
85
|
+
})
|
|
86
|
+
.map((r) => String(r.customer_id));
|
|
87
|
+
return uniqueMatch(byDomain);
|
|
88
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"klant-match.test.d.ts","sourceRoot":"","sources":["../../src/lib/klant-match.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { matchKlant, matchNames, fullName, splitCandidates, fuzzyMatch, } from "./klant-match";
|
|
3
|
+
import { normalizePostvakSettings, DEFAULT_POSTVAK_SETTINGS } from "./werkbon-postvak-settings";
|
|
4
|
+
const customers = [
|
|
5
|
+
{ id: "c1", name: "Jansen, Piet", general_email: "info@jansen.nl" },
|
|
6
|
+
{ id: "c2", name: "Bakker Installaties", general_email: " Inkoop@Bakker.nl " },
|
|
7
|
+
{ id: "c3", name: "Kees de Vries", general_email: null },
|
|
8
|
+
{ id: "c4", name: "Piet Jansen", general_email: "piet@other.nl" },
|
|
9
|
+
];
|
|
10
|
+
const contacts = [
|
|
11
|
+
{ customer_id: "c2", name: "Anna Smit", email: "anna@bakker.nl" },
|
|
12
|
+
{ customer_id: "c3", name: "Kees de Vries", email: "kees@vries.nl" },
|
|
13
|
+
];
|
|
14
|
+
// Simuleert wat de database-RPC teruggeeft: rijen met hetzelfde e-mailadres
|
|
15
|
+
// of (via pg_trgm) een gelijkende naam. Een exacte naam is altijd gelijkend.
|
|
16
|
+
function dbCandidates(emails, names) {
|
|
17
|
+
const rows = [];
|
|
18
|
+
for (const c of customers) {
|
|
19
|
+
const e = String(c.general_email ?? "").trim().toLowerCase();
|
|
20
|
+
if (emails.includes(e) || names.includes(fullName(c.name)))
|
|
21
|
+
rows.push({ src: "customer", customer_id: c.id, name: c.name, email: c.general_email, sim: 1 });
|
|
22
|
+
}
|
|
23
|
+
for (const p of contacts) {
|
|
24
|
+
const e = String(p.email ?? "").trim().toLowerCase();
|
|
25
|
+
if (emails.includes(e) || names.includes(fullName(p.name)))
|
|
26
|
+
rows.push({ src: "contact", customer_id: p.customer_id, name: p.name, email: p.email, sim: 1 });
|
|
27
|
+
}
|
|
28
|
+
return rows;
|
|
29
|
+
}
|
|
30
|
+
const cases = [
|
|
31
|
+
{ emails: ["inkoop@bakker.nl"], names: [], expected: "c2" },
|
|
32
|
+
{ emails: ["anna@bakker.nl"], names: [], expected: "c2" },
|
|
33
|
+
{ emails: ["onbekend@x.nl"], names: ["Kees de Vries"], expected: "c3" },
|
|
34
|
+
{ emails: [], names: ["Piet Jansen"], expected: null }, // c1 én c4 → geen zekere match
|
|
35
|
+
{ emails: ["info@jansen.nl", "piet@other.nl"], names: [], expected: null },
|
|
36
|
+
{ emails: [], names: ["Kees"], expected: null }, // één woord telt niet
|
|
37
|
+
{ emails: [], names: [], expected: null },
|
|
38
|
+
];
|
|
39
|
+
describe("klantherkenning: database-kandidaten = alles ophalen", () => {
|
|
40
|
+
for (const c of cases) {
|
|
41
|
+
it(`${c.emails.join(",") || "-"} / ${c.names.join(",") || "-"} → ${c.expected}`, () => {
|
|
42
|
+
const names = matchNames(c.names);
|
|
43
|
+
const old = matchKlant(customers, contacts, c.emails, names);
|
|
44
|
+
const split = splitCandidates(dbCandidates(c.emails, names));
|
|
45
|
+
const viaDb = matchKlant(split.customers, split.contacts, c.emails, names);
|
|
46
|
+
expect(old).toBe(c.expected);
|
|
47
|
+
expect(viaDb).toBe(old);
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
describe("ruime herkenning", () => {
|
|
52
|
+
const rows = [
|
|
53
|
+
{ src: "customer", customer_id: "c1", name: "Jansen BV", email: "info@jansen.nl", sim: 0.7 },
|
|
54
|
+
{ src: "customer", customer_id: "c2", name: "Bakker", email: "x@bakker.nl", sim: 0.2 },
|
|
55
|
+
];
|
|
56
|
+
it("één klant boven de grens", () => {
|
|
57
|
+
expect(fuzzyMatch(rows, 0.6, [])).toBe("c1");
|
|
58
|
+
});
|
|
59
|
+
it("niemand boven de grens, wel domein", () => {
|
|
60
|
+
expect(fuzzyMatch(rows, 0.9, ["bakker.nl"])).toBe("c2");
|
|
61
|
+
});
|
|
62
|
+
it("twee klanten boven de grens → geen match", () => {
|
|
63
|
+
expect(fuzzyMatch(rows, 0.1, [])).toBeNull();
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
describe("instellingen", () => {
|
|
67
|
+
it("ontbrekende rij = huidig gedrag", () => {
|
|
68
|
+
expect(normalizePostvakSettings(null)).toEqual(DEFAULT_POSTVAK_SETTINGS);
|
|
69
|
+
expect(DEFAULT_POSTVAK_SETTINGS.customer_match_in_db).toBe(false);
|
|
70
|
+
expect(DEFAULT_POSTVAK_SETTINGS.optimistic_lock_enabled).toBe(false);
|
|
71
|
+
expect(DEFAULT_POSTVAK_SETTINGS.confirmation_require_valid_email).toBe(false);
|
|
72
|
+
});
|
|
73
|
+
it("ongeldige grens valt terug op 0,6", () => {
|
|
74
|
+
expect(normalizePostvakSettings({ customer_match_similarity: 7 }).customer_match_similarity).toBe(0.6);
|
|
75
|
+
});
|
|
76
|
+
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"klanten.functions.d.ts","sourceRoot":"","sources":["../../src/lib/klanten.functions.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"klanten.functions.d.ts","sourceRoot":"","sources":["../../src/lib/klanten.functions.ts"],"names":[],"mappings":"AA2BA,MAAM,MAAM,YAAY,GAAG,UAAU,GAAG,SAAS,CAAC;AAElD,MAAM,WAAW,iBAAiB;IAChC,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACrB,KAAK,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACtB,KAAK,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;CACvB;AAED,MAAM,WAAW,iBAAiB;IAChC,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACvB,IAAI,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACrB,QAAQ,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACzB,YAAY,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;CAC9B;AAED,MAAM,WAAW,UAAU;IACzB,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,YAAY,CAAC;IACtB,aAAa,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAC9B,aAAa,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAC9B,cAAc,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAC/B,gBAAgB,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACjC,YAAY,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAC7B,QAAQ,CAAC,EAAE,iBAAiB,EAAE,CAAC;IAC/B,SAAS,CAAC,EAAE,iBAAiB,EAAE,CAAC;CACjC;AAED,MAAM,WAAW,YAAY;IAC3B,EAAE,EAAE,MAAM,CAAC;IACX,KAAK,EAAE,MAAM,CAAC;IACd,eAAe,EAAE,MAAM,GAAG,IAAI,CAAC;CAChC;AAWD,eAAO,MAAM,WAAW;;;;6CAEC,UAAU,uCAwD/B,CAAC;AAwDL,MAAM,WAAW,eAAe;IAC9B,QAAQ,EAAE,MAAM,CAAC;IACjB,UAAU,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAC3B,SAAS,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAC1B,WAAW,CAAC,EAAE,MAAM,EAAE,GAAG,IAAI,CAAC;CAC/B;AA4BD,wBAAsB,kBAAkB,CACtC,KAAK,EAAE,GAAG,EACV,IAAI,EAAE,eAAe,GACpB,OAAO,CAAC;IAAE,WAAW,EAAE,MAAM,GAAG,IAAI,CAAA;CAAE,CAAC,CA0EzC;AAED,eAAO,MAAM,iBAAiB;;;;6CAEL,eAAe;iBAIY,MAAM,GAAG,IAAI;GAG7D,CAAC;AAML,MAAM,WAAW,WAAW;IAC1B,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAAC;IACpC,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC;IACtB,KAAK,EAAE,MAAM,EAAE,CAAC;CACjB;AAED,wBAAsB,eAAe,CAAC,KAAK,EAAE,GAAG,EAAE,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC,WAAW,CAAC,CAmB1F;AAED,eAAO,MAAM,cAAc;;;;6CAEF;IAAE,WAAW,EAAE,MAAM,CAAA;CAAE;iBAAR,MAAM;wBAO1C,CAAC"}
|
|
@@ -2,7 +2,10 @@
|
|
|
2
2
|
// werkbonvoorstel: klant herkennen op e-mailadres en een nieuwe klant aanmaken
|
|
3
3
|
// die daarna ook in de klantenmodule zichtbaar is.
|
|
4
4
|
import { createServerFn } from "@tanstack/react-start";
|
|
5
|
+
import { fetchAllPages } from "./paginate";
|
|
5
6
|
import { requireSupabaseAuth } from "../integrations/supabase/auth-middleware";
|
|
7
|
+
import { matchKlant, matchNames, splitCandidates, fuzzyMatch, } from "./klant-match";
|
|
8
|
+
import { readPostvakSettings } from "./werkbon-postvak-settings";
|
|
6
9
|
async function adminClient() {
|
|
7
10
|
const { supabaseAdmin } = await import("../integrations/supabase/client.server");
|
|
8
11
|
return supabaseAdmin;
|
|
@@ -126,10 +129,6 @@ function normalizeName(value) {
|
|
|
126
129
|
.replace(/[^a-z0-9]+/g, " ")
|
|
127
130
|
.trim();
|
|
128
131
|
}
|
|
129
|
-
function uniqueMatch(ids) {
|
|
130
|
-
const unique = Array.from(new Set(ids.filter(Boolean)));
|
|
131
|
-
return unique.length === 1 ? String(unique[0]) : null;
|
|
132
|
-
}
|
|
133
132
|
function senderOf(value) {
|
|
134
133
|
const first = Array.isArray(value) ? value[0] : value;
|
|
135
134
|
if (!first)
|
|
@@ -154,18 +153,6 @@ function senderOf(value) {
|
|
|
154
153
|
}
|
|
155
154
|
return { name: "", email: "" };
|
|
156
155
|
}
|
|
157
|
-
// Volledige naam: kleine letters, "Achternaam, Voornaam" omgedraaid.
|
|
158
|
-
function fullName(value) {
|
|
159
|
-
let text = String(value ?? "").trim();
|
|
160
|
-
const comma = text.split(",");
|
|
161
|
-
if (comma.length === 2)
|
|
162
|
-
text = `${comma[1]} ${comma[0]}`;
|
|
163
|
-
return text
|
|
164
|
-
.toLowerCase()
|
|
165
|
-
.replace(/[^\p{L}\p{N}' -]+/gu, " ")
|
|
166
|
-
.replace(/\s+/g, " ")
|
|
167
|
-
.trim();
|
|
168
|
-
}
|
|
169
156
|
// Gedeelde herkenning: ook gebruikt door de mailanalyse.
|
|
170
157
|
// Alleen een zekere match: exact e-mailadres of exacte voor- + achternaam.
|
|
171
158
|
export async function findKlantIdForMail(admin, data) {
|
|
@@ -182,42 +169,48 @@ export async function findKlantIdForMail(admin, data) {
|
|
|
182
169
|
.filter((v) => typeof v === "string")
|
|
183
170
|
.map((v) => v.trim().toLowerCase()),
|
|
184
171
|
].filter((e) => e.includes("@"))));
|
|
185
|
-
const
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
...
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
172
|
+
const names = matchNames([sender.name, data.hint_name]);
|
|
173
|
+
const settings = await readPostvakSettings(admin);
|
|
174
|
+
if (settings.customer_match_in_db) {
|
|
175
|
+
// Alleen kandidaten uit de database (bestaande indexen op itk_customers).
|
|
176
|
+
const domains = settings.customer_match_fuzzy
|
|
177
|
+
? Array.from(new Set(emails.map((e) => domainOf(e)).filter((d) => !!d)))
|
|
178
|
+
: [];
|
|
179
|
+
const { data: rows, error } = await admin.rpc("werkbon_match_customer_candidates", {
|
|
180
|
+
p_emails: emails,
|
|
181
|
+
p_names: [...names, ...[sender.name, data.hint_name].filter((n) => !!n && !!n.trim())],
|
|
182
|
+
p_domains: domains,
|
|
183
|
+
p_limit: 200,
|
|
184
|
+
});
|
|
185
|
+
if (error) {
|
|
186
|
+
console.error("werkbon_match_customer_candidates failed", error);
|
|
187
|
+
throw new Error(`Klant herkennen is mislukt: ${error.message}`);
|
|
188
|
+
}
|
|
189
|
+
const candidates = asArray(rows);
|
|
190
|
+
const { customers, contacts } = splitCandidates(candidates);
|
|
191
|
+
const exact = matchKlant(customers, contacts, emails, names);
|
|
192
|
+
if (exact)
|
|
193
|
+
return { customer_id: exact };
|
|
194
|
+
if (settings.customer_match_fuzzy) {
|
|
195
|
+
return { customer_id: fuzzyMatch(candidates, settings.customer_match_similarity, domains) };
|
|
196
|
+
}
|
|
197
|
+
return { customer_id: null };
|
|
206
198
|
}
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
199
|
+
const [{ data: customerRows, error: cErr }, { data: contactRows, error: pErr }] = await Promise.all([
|
|
200
|
+
fetchAllPages((from, to) => admin.from("itk_customers").select("id, name, general_email").order("id").range(from, to), "itk_customers"),
|
|
201
|
+
fetchAllPages((from, to) => admin
|
|
202
|
+
.from("itk_contact_persons")
|
|
203
|
+
.select("id, customer_id, name, email")
|
|
204
|
+
.order("id")
|
|
205
|
+
.range(from, to), "itk_contact_persons"),
|
|
206
|
+
]);
|
|
207
|
+
if (cErr)
|
|
208
|
+
console.error("itk_customers read failed", cErr);
|
|
209
|
+
if (pErr)
|
|
210
|
+
console.error("itk_contact_persons read failed", pErr);
|
|
211
|
+
return {
|
|
212
|
+
customer_id: matchKlant(asArray(customerRows), asArray(contactRows), emails, names),
|
|
213
|
+
};
|
|
221
214
|
}
|
|
222
215
|
export const matchKlantForMail = createServerFn({ method: "POST" })
|
|
223
216
|
.middleware([requireSupabaseAuth])
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export declare const PAGE_SIZE = 1000;
|
|
2
|
+
type Page = {
|
|
3
|
+
data: unknown[] | null;
|
|
4
|
+
error: {
|
|
5
|
+
message: string;
|
|
6
|
+
} | null;
|
|
7
|
+
};
|
|
8
|
+
export declare function fetchAllPages(query: (from: number, to: number) => PromiseLike<Page>, label: string, pageSize?: number): Promise<Page>;
|
|
9
|
+
export {};
|
|
10
|
+
//# sourceMappingURL=paginate.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"paginate.d.ts","sourceRoot":"","sources":["../../src/lib/paginate.ts"],"names":[],"mappings":"AAGA,eAAO,MAAM,SAAS,OAAO,CAAC;AAE9B,KAAK,IAAI,GAAG;IAAE,IAAI,EAAE,OAAO,EAAE,GAAG,IAAI,CAAC;IAAC,KAAK,EAAE;QAAE,OAAO,EAAE,MAAM,CAAA;KAAE,GAAG,IAAI,CAAA;CAAE,CAAC;AAE1E,wBAAsB,aAAa,CACjC,KAAK,EAAE,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,KAAK,WAAW,CAAC,IAAI,CAAC,EACtD,KAAK,EAAE,MAAM,EACb,QAAQ,SAAY,GACnB,OAAO,CAAC,IAAI,CAAC,CAaf"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// Haalt alle rijen op in pagina's, zodat Supabase niet stil afkapt op zijn
|
|
2
|
+
// maximum per verzoek. Geeft hetzelfde { data, error } terug als één query.
|
|
3
|
+
export const PAGE_SIZE = 1000;
|
|
4
|
+
export async function fetchAllPages(query, label, pageSize = PAGE_SIZE) {
|
|
5
|
+
const all = [];
|
|
6
|
+
for (let from = 0;; from += pageSize) {
|
|
7
|
+
const { data, error } = await query(from, from + pageSize - 1);
|
|
8
|
+
if (error) {
|
|
9
|
+
console.error(`${label} read failed`, error);
|
|
10
|
+
return { data: all.length ? all : null, error };
|
|
11
|
+
}
|
|
12
|
+
const rows = Array.isArray(data) ? data : [];
|
|
13
|
+
all.push(...rows);
|
|
14
|
+
if (rows.length < pageSize)
|
|
15
|
+
break;
|
|
16
|
+
}
|
|
17
|
+
return { data: all, error: null };
|
|
18
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rekenregels-gelijk.test.d.ts","sourceRoot":"","sources":["../../src/lib/rekenregels-gelijk.test.ts"],"names":[],"mappings":""}
|