diff --git a/src/components/cases/convert-to-collections-dialog.tsx b/src/components/cases/convert-to-collections-dialog.tsx index 666eabf..5744a15 100644 --- a/src/components/cases/convert-to-collections-dialog.tsx +++ b/src/components/cases/convert-to-collections-dialog.tsx @@ -56,12 +56,16 @@ export function ConvertToCollectionsDialog({ const [selected, setSelected] = useState>(new Set()); const [search, setSearch] = useState(""); const [status, setStatus] = useState("none"); + const [includeEmpty, setIncludeEmpty] = useState(false); + const [emptyName, setEmptyName] = useState(""); useEffect(() => { if (!open) return; setSelected(new Set()); setSearch(""); setStatus("none"); + setIncludeEmpty(false); + setEmptyName(""); (async () => { setLoading(true); const [{ data: hos }, { data: cols }] = await Promise.all([ @@ -77,7 +81,9 @@ export function ConvertToCollectionsDialog({ .eq("case_id", caseId), ]); setHomeowners(hos ?? []); - setExistingIds(new Set((cols ?? []).map((c) => c.homeowner_id))); + setExistingIds( + new Set((cols ?? []).map((c) => c.homeowner_id).filter((id): id is string => !!id)), + ); setLoading(false); })(); }, [open, caseId, clientId]); @@ -123,17 +129,26 @@ export function ConvertToCollectionsDialog({ }; const submit = async () => { - if (selected.size === 0) { - toast.error("Pick at least one homeowner"); + if (selected.size === 0 && !includeEmpty) { + toast.error("Pick at least one homeowner or enable an empty collection"); return; } setSubmitting(true); - const rows = Array.from(selected).map((homeowner_id) => ({ + const rows: any[] = Array.from(selected).map((homeowner_id) => ({ case_id: caseId, homeowner_id, status, created_by: user?.id, })); + if (includeEmpty) { + rows.push({ + case_id: caseId, + homeowner_id: null, + status, + name: emptyName.trim() || null, + created_by: user?.id, + }); + } const { error } = await supabase.from("collections").insert(rows); setSubmitting(false); if (error) { @@ -252,15 +267,36 @@ export function ConvertToCollectionsDialog({ )} + +
+ + {includeEmpty && ( + setEmptyName(e.target.value)} + /> + )} +
- diff --git a/src/integrations/supabase/types.ts b/src/integrations/supabase/types.ts index 1f19e6e..7d2b0f3 100644 --- a/src/integrations/supabase/types.ts +++ b/src/integrations/supabase/types.ts @@ -742,7 +742,7 @@ export type Database = { created_at: string created_by: string | null current_stage: string | null - homeowner_id: string + homeowner_id: string | null id: string interest_rate_override: number | null name: string | null @@ -757,7 +757,7 @@ export type Database = { created_at?: string created_by?: string | null current_stage?: string | null - homeowner_id: string + homeowner_id?: string | null id?: string interest_rate_override?: number | null name?: string | null @@ -772,7 +772,7 @@ export type Database = { created_at?: string created_by?: string | null current_stage?: string | null - homeowner_id?: string + homeowner_id?: string | null id?: string interest_rate_override?: number | null name?: string | null diff --git a/src/routes/collections.index.tsx b/src/routes/collections.index.tsx index d522b41..bceedce 100644 --- a/src/routes/collections.index.tsx +++ b/src/routes/collections.index.tsx @@ -56,7 +56,7 @@ function CollectionsIndexPage() { supabase .from("collections") .select( - "id, status, current_stage, opened_at, homeowner:homeowners(id, first_name, last_name, unit_number, opening_balance), case:cases(id, case_number, title, client:clients(id, name))", + "id, status, current_stage, opened_at, name, homeowner:homeowners(id, first_name, last_name, unit_number, opening_balance), case:cases(id, case_number, title, client:clients(id, name))", ) .order("created_at", { ascending: false }), supabase @@ -234,10 +234,18 @@ function CollectionsIndexPage() { params={{ collectionId: r.id }} className="hover:underline" > - {r.homeowner?.last_name}, {r.homeowner?.first_name} - {r.homeowner?.unit_number && ( - - · Unit {r.homeowner.unit_number} + {r.homeowner ? ( + <> + {r.homeowner.last_name}, {r.homeowner.first_name} + {r.homeowner.unit_number && ( + + · Unit {r.homeowner.unit_number} + + )} + + ) : ( + + {(r as any).name || "Unassigned"} )} diff --git a/supabase/migrations/20260418073324_19f5b5c4-1eba-4b5a-8422-6e0323ca8437.sql b/supabase/migrations/20260418073324_19f5b5c4-1eba-4b5a-8422-6e0323ca8437.sql new file mode 100644 index 0000000..0666283 --- /dev/null +++ b/supabase/migrations/20260418073324_19f5b5c4-1eba-4b5a-8422-6e0323ca8437.sql @@ -0,0 +1 @@ +ALTER TABLE public.collections ALTER COLUMN homeowner_id DROP NOT NULL; \ No newline at end of file