Co-authored-by: renee-png <262607627+renee-png@users.noreply.github.com>
This commit is contained in:
gpt-engineer-app[bot]
2026-04-18 04:02:09 +00:00
co-authored by renee-png
parent 4b02eaea9c
commit 3babc82cba
+41 -2
View File
@@ -685,10 +685,49 @@ function ImportPage() {
if (cfg.postInsert) cfg.postInsert(inserted_rows, ctx);
// Mirror imported "client" contacts into the clients table so they appear on the Clients page.
let mirrored = 0;
if (cfg.key === "contacts" && contactGroup === "client" && inserted_rows.length > 0) {
const clientRows = inserted_rows
.filter((c) => c.external_id || c.name)
.map((c) => ({
external_id: c.external_id ?? null,
name: c.name || c.company || "Unnamed client",
client_type: "hoa" as const,
primary_contact_email: c.email ?? null,
primary_contact_phone: c.phone ?? null,
primary_contact_name: c.name ?? null,
address_line1: c.address_line1 ?? null,
address_line2: c.address_line2 ?? null,
city: c.city ?? null,
state: c.state ?? null,
postal_code: c.postal_code ?? null,
notes: c.notes ?? null,
created_by: user.id,
}));
const withExt = clientRows.filter((r) => r.external_id);
const withoutExt = clientRows.filter((r) => !r.external_id);
if (withExt.length > 0) {
const { data, error } = await supabase
.from("clients")
.upsert(withExt, { onConflict: "external_id", ignoreDuplicates: false })
.select("id");
if (error) errors.push(`Client mirror (upsert): ${error.message}`);
else mirrored += data?.length ?? 0;
}
if (withoutExt.length > 0) {
const { data, error } = await supabase.from("clients").insert(withoutExt).select("id");
if (error) errors.push(`Client mirror (insert): ${error.message}`);
else mirrored += data?.length ?? 0;
}
}
const summary = { total: rawRows.length, inserted, skipped, errors, skipReasons };
setResults((r) => ({ ...r, [cfg.key]: summary }));
if (errors.length === 0) toast.success(`${cfg.label}: ${inserted} imported`);
else toast.error(`${cfg.label}: ${errors.length} batch error(s)`);
if (errors.length === 0) {
const extra = mirrored > 0 ? ` (+${mirrored} mirrored to Clients)` : "";
toast.success(`${cfg.label}: ${inserted} imported${extra}`);
} else toast.error(`${cfg.label}: ${errors.length} batch error(s)`);
};
return (