From f71d5562ef18a1893d126a01ed7d3e461bd701fe Mon Sep 17 00:00:00 2001 From: "gpt-engineer-app[bot]" <159125892+gpt-engineer-app[bot]@users.noreply.github.com> Date: Mon, 20 Apr 2026 00:12:23 +0000 Subject: [PATCH] Changes Co-authored-by: renee-png <262607627+renee-png@users.noreply.github.com> --- .../bulk-convert-to-collections-dialog.tsx | 51 +++++++++++++++---- 1 file changed, 41 insertions(+), 10 deletions(-) diff --git a/src/components/cases/bulk-convert-to-collections-dialog.tsx b/src/components/cases/bulk-convert-to-collections-dialog.tsx index 13f4b5b..796a08c 100644 --- a/src/components/cases/bulk-convert-to-collections-dialog.tsx +++ b/src/components/cases/bulk-convert-to-collections-dialog.tsx @@ -249,16 +249,47 @@ export function BulkConvertToCollectionsDialog({ open, onOpenChange, cases, onCr } const rows: any[] = []; - for (const r of selectedRows) { - const homeownerId = contactToHomeowner.get(r.contact_id); - if (!homeownerId) continue; - if (existingPairs.has(`${r.case_id}::${homeownerId}`)) continue; - rows.push({ - case_id: r.case_id, - homeowner_id: homeownerId, - status: "none", - created_by: user?.id, - }); + // Cases that already have ANY collection — skip in combine mode to avoid duplicates. + const casesWithExisting = new Set(); + for (const e of (existing ?? []) as any[]) casesWithExisting.add(e.case_id); + + if (combine) { + // Group selected contacts by case, pick first as primary, attach others by name. + const byCase = new Map(); + for (const r of selectedRows) { + if (!byCase.has(r.case_id)) byCase.set(r.case_id, [] as any); + byCase.get(r.case_id)!.push(r); + } + for (const [caseId, group] of byCase) { + if (casesWithExisting.has(caseId)) continue; + const primary = group[0]; + const primaryHoId = contactToHomeowner.get(primary.contact_id); + if (!primaryHoId) continue; + const others = group.slice(1).map((r) => r.name); + const name = + others.length > 0 + ? `${primary.name} + ${others.length} other${others.length === 1 ? "" : "s"}` + : null; + rows.push({ + case_id: caseId, + homeowner_id: primaryHoId, + status: "none", + name, + created_by: user?.id, + }); + } + } else { + for (const r of selectedRows) { + const homeownerId = contactToHomeowner.get(r.contact_id); + if (!homeownerId) continue; + if (existingPairs.has(`${r.case_id}::${homeownerId}`)) continue; + rows.push({ + case_id: r.case_id, + homeowner_id: homeownerId, + status: "none", + created_by: user?.id, + }); + } } if (includeEmpty) {