diff --git a/src/routes/settings.import.tsx b/src/routes/settings.import.tsx index e48ff0a..c658aad 100644 --- a/src/routes/settings.import.tsx +++ b/src/routes/settings.import.tsx @@ -516,22 +516,50 @@ function ImportPage() { caseByNumber: new Map(), }), [user?.id]); - // Pre-load lookup caches from existing rows so subsequent imports can join + // Pre-load lookup caches from existing rows so subsequent imports can join. + // Always reload (and paginate past Supabase's 1000-row default limit) so caches + // pick up rows added since the page mounted (e.g., from a prior CSV import). + const PAGE = 1000; + const fetchAllPaginated = async ( + table: "clients" | "cases" | "contacts" | "homeowners" | "invoices", + columns: string, + ): Promise => { + const out: T[] = []; + for (let from = 0; ; from += PAGE) { + const { data, error } = await supabase + .from(table) + .select(columns) + .range(from, from + PAGE - 1); + if (error) { console.warn(`[import] paginate ${table} failed`, error); break; } + const batch = (data ?? []) as T[]; + out.push(...batch); + if (batch.length < PAGE) break; + } + return out; + }; + const ensureLookupsLoaded = async () => { - if (ctx.clientByExt.size === 0) { - const { data } = await supabase.from("clients").select("id, name, external_id"); - for (const r of data ?? []) { - if (r.external_id) ctx.clientByExt.set(r.external_id, r.id); - if (r.name) ctx.clientByName.set(r.name.toLowerCase(), r.id); - } + ctx.clientByExt.clear(); + ctx.clientByName.clear(); + const clients = await fetchAllPaginated<{ id: string; name: string | null; external_id: string | null }>( + "clients", + "id, name, external_id", + ); + for (const r of clients) { + if (r.external_id) ctx.clientByExt.set(r.external_id, r.id); + if (r.name) ctx.clientByName.set(r.name.toLowerCase(), r.id); } - if (ctx.caseByExt.size === 0) { - const { data } = await supabase.from("cases").select("id, case_number, external_id"); - for (const r of data ?? []) { - if (r.external_id) ctx.caseByExt.set(r.external_id, r.id); - if (r.case_number) ctx.caseByNumber.set(r.case_number, r.id); - } + ctx.caseByExt.clear(); + ctx.caseByNumber.clear(); + const cases = await fetchAllPaginated<{ id: string; case_number: string | null; external_id: string | null }>( + "cases", + "id, case_number, external_id", + ); + 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); } + console.info(`[import] lookups loaded: clients=${ctx.clientByExt.size}/${ctx.clientByName.size} cases=${ctx.caseByExt.size}/${ctx.caseByNumber.size}`); }; const handleFile = async (cfg: ImporterConfig, file: File) => {