Changes
Co-authored-by: renee-png <262607627+renee-png@users.noreply.github.com>
This commit is contained in:
co-authored by
renee-png
parent
e997476b31
commit
1629c5c135
@@ -134,6 +134,50 @@ export function ClientFormDialog({
|
||||
toast.error("Could not save client", { description: error.message });
|
||||
return;
|
||||
}
|
||||
|
||||
// Auto-create contacts for board members that aren't already in contacts
|
||||
try {
|
||||
const members = (values.board_members || []).filter((m) => m.name?.trim());
|
||||
if (members.length > 0) {
|
||||
const emails = members.map((m) => m.email?.trim().toLowerCase()).filter(Boolean) as string[];
|
||||
const names = members.map((m) => m.name.trim());
|
||||
const { data: existing } = await supabase
|
||||
.from("contacts")
|
||||
.select("name,email")
|
||||
.or(
|
||||
[
|
||||
emails.length ? `email.in.(${emails.join(",")})` : "",
|
||||
`name.in.(${names.map((n) => `"${n.replace(/"/g, '\\"')}"`).join(",")})`,
|
||||
]
|
||||
.filter(Boolean)
|
||||
.join(","),
|
||||
);
|
||||
const existingEmails = new Set((existing ?? []).map((c) => c.email?.toLowerCase()).filter(Boolean));
|
||||
const existingNames = new Set((existing ?? []).map((c) => c.name?.toLowerCase()));
|
||||
const toInsert = members
|
||||
.filter((m) => {
|
||||
const e = m.email?.trim().toLowerCase();
|
||||
if (e && existingEmails.has(e)) return false;
|
||||
if (existingNames.has(m.name.trim().toLowerCase())) return false;
|
||||
return true;
|
||||
})
|
||||
.map((m) => ({
|
||||
name: m.name.trim(),
|
||||
title: m.role?.trim() || null,
|
||||
email: m.email?.trim() || null,
|
||||
phone: m.phone?.trim() || null,
|
||||
contact_type: "board_member",
|
||||
created_by: user?.id ?? null,
|
||||
}));
|
||||
if (toInsert.length > 0) {
|
||||
await supabase.from("contacts").insert(toInsert);
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
// Non-fatal
|
||||
console.warn("Board member contact sync failed", e);
|
||||
}
|
||||
|
||||
toast.success(client ? "Client updated" : "Client created");
|
||||
onOpenChange(false);
|
||||
onSaved?.();
|
||||
|
||||
Reference in New Issue
Block a user