Changes
Co-authored-by: renee-png <262607627+renee-png@users.noreply.github.com>
This commit is contained in:
co-authored by
renee-png
parent
07662da2e5
commit
f71d5562ef
@@ -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<string>();
|
||||
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<string, typeof selectedRows>();
|
||||
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) {
|
||||
|
||||
Reference in New Issue
Block a user