diff --git a/src/routes/settings.import.tsx b/src/routes/settings.import.tsx index 45cac22..627bf6d 100644 --- a/src/routes/settings.import.tsx +++ b/src/routes/settings.import.tsx @@ -56,6 +56,8 @@ interface ImportCtx { clientByName: Map; caseByNumber: Map; caseByTitle: Map; + // Composite key "client_name||case_title" (both lowercased/trimmed) → case uuid + caseByClientAndTitle: Map; userByName: Map; userByEmail: Map; // Set of case ids considered "active" (not closed, not archived) @@ -663,7 +665,8 @@ const IMPORTERS: ImporterConfig[] = [ { key: "status_updates", label: "Status updates → Status", - description: "Case status reports.", + description: + "Case status reports. Match cases by title + client name (recommended), or by case number / external id.", table: "status_updates", conflict: "external_id", required: ["title", "body", "case_id"], @@ -673,14 +676,18 @@ const IMPORTERS: ImporterConfig[] = [ body: "body", content: "body", notes: "body", description: "body", caseid: "_caseext", matterid: "_caseext", casenumber: "_casenum", casename: "_casename", casetitle: "_casename", matter: "_casename", mattername: "_casename", + clientname: "_clientname", client: "_clientname", hoa: "_clientname", association: "_clientname", }, transform: (r, ctx) => { + const titleKey = r._casename ? String(r._casename).toLowerCase().trim() : ""; + const clientKey = r._clientname ? String(r._clientname).toLowerCase().trim() : ""; r.case_id = (r._caseext && ctx.caseByExt.get(String(r._caseext))) || + (titleKey && clientKey && ctx.caseByClientAndTitle.get(`${clientKey}||${titleKey}`)) || (r._casenum && ctx.caseByNumber.get(String(r._casenum))) || - (r._casename && ctx.caseByTitle.get(String(r._casename).toLowerCase().trim())) || + (titleKey && ctx.caseByTitle.get(titleKey)) || null; - delete r._caseext; delete r._casenum; delete r._casename; + delete r._caseext; delete r._casenum; delete r._casename; delete r._clientname; if (!r.case_id || !r.body) return null; return r; }, @@ -722,6 +729,7 @@ function ImportPage() { clientByName: new Map(), caseByNumber: new Map(), caseByTitle: new Map(), + caseByClientAndTitle: new Map(), userByName: new Map(), userByEmail: new Map(), activeCaseIds: new Set(), @@ -798,16 +806,27 @@ function ImportPage() { ctx.caseByExt.clear(); ctx.caseByNumber.clear(); ctx.caseByTitle.clear(); + ctx.caseByClientAndTitle.clear(); ctx.activeCaseIds.clear(); - const cases = await fetchAllPaginated<{ id: string; case_number: string | null; title: string | null; external_id: string | null; status: string | null; archived_at: string | null }>( + const cases = await fetchAllPaginated<{ id: string; case_number: string | null; title: string | null; external_id: string | null; status: string | null; archived_at: string | null; client_id: string | null }>( "cases", - "id, case_number, title, external_id, status, archived_at", + "id, case_number, title, external_id, status, archived_at, client_id", ); + // Build id → client_name lookup from already-loaded clients + const clientNameById = new Map(); + for (const [name, id] of ctx.clientByName.entries()) clientNameById.set(id, name); const CLOSED_STATUSES = new Set(["closed", "closed_won", "closed_lost"]); for (const r of cases) { if (r.external_id) ctx.caseByExt.set(r.external_id, r.id); if (r.case_number) ctx.caseByNumber.set(r.case_number, r.id); - if (r.title) ctx.caseByTitle.set(r.title.toLowerCase().trim(), r.id); + if (r.title) { + const titleKey = r.title.toLowerCase().trim(); + ctx.caseByTitle.set(titleKey, r.id); + if (r.client_id) { + const cname = clientNameById.get(r.client_id); + if (cname) ctx.caseByClientAndTitle.set(`${cname}||${titleKey}`, r.id); + } + } if (!r.archived_at && !CLOSED_STATUSES.has(String(r.status ?? "").toLowerCase())) { ctx.activeCaseIds.add(r.id); } @@ -819,7 +838,7 @@ function ImportPage() { if (p.full_name) ctx.userByName.set(p.full_name.toLowerCase().trim(), p.id); if (p.email) ctx.userByEmail.set(p.email.toLowerCase().trim(), p.id); } - console.info(`[import] lookups loaded: clients=${ctx.clientByExt.size}/${ctx.clientByName.size} cases=${ctx.caseByExt.size}/${ctx.caseByNumber.size}/${ctx.caseByTitle.size} users=${ctx.userByName.size}`); + console.info(`[import] lookups loaded: clients=${ctx.clientByExt.size}/${ctx.clientByName.size} cases=${ctx.caseByExt.size}/${ctx.caseByNumber.size}/${ctx.caseByTitle.size}/${ctx.caseByClientAndTitle.size} users=${ctx.userByName.size}`); }; const handleFile = async (cfg: ImporterConfig, source: File | string) => {