Changes
Co-authored-by: renee-png <262607627+renee-png@users.noreply.github.com>
This commit is contained in:
co-authored by
renee-png
parent
d87d4c5749
commit
61f8e4eebd
@@ -16,6 +16,7 @@ import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@
|
||||
import { setArchived } from "@/lib/archive";
|
||||
import { NewCaseDialog } from "@/components/cases/new-case-dialog";
|
||||
import { BulkConvertToCollectionsDialog } from "@/components/cases/bulk-convert-to-collections-dialog";
|
||||
import { StatusFlagBadges } from "@/components/collections/status-flag-badges";
|
||||
import { formatDistanceToNow } from "date-fns";
|
||||
import { toast } from "sonner";
|
||||
|
||||
@@ -61,7 +62,7 @@ function CasesList() {
|
||||
// Aggregate latest activity from related child tables.
|
||||
const [
|
||||
tasksRes, timeRes, expensesRes, docsRes, callRes,
|
||||
commentsRes, invoicesRes, eventsRes,
|
||||
commentsRes, invoicesRes, eventsRes, collectionsRes,
|
||||
] = await Promise.all([
|
||||
supabase.from("tasks").select("case_id, updated_at").not("case_id", "is", null).limit(20000),
|
||||
supabase.from("time_entries").select("case_id, updated_at").limit(20000),
|
||||
@@ -71,6 +72,7 @@ function CasesList() {
|
||||
supabase.from("comments").select("case_id, created_at").not("case_id", "is", null).limit(20000),
|
||||
supabase.from("invoices").select("case_id, updated_at").not("case_id", "is", null).limit(20000),
|
||||
supabase.from("events").select("case_id, updated_at").not("case_id", "is", null).limit(20000),
|
||||
supabase.from("collections").select("case_id, in_bankruptcy, on_payment_plan").limit(20000),
|
||||
]);
|
||||
|
||||
const latest: Record<string, string> = {};
|
||||
@@ -91,11 +93,23 @@ function CasesList() {
|
||||
ingest(invoicesRes.data, "updated_at");
|
||||
ingest(eventsRes.data, "updated_at");
|
||||
|
||||
// Roll up BK / PP flags: case has the flag if ANY of its collections do.
|
||||
const flagMap: Record<string, { bk: boolean; pp: boolean }> = {};
|
||||
for (const r of collectionsRes.data ?? []) {
|
||||
const cid = (r as any).case_id as string | null;
|
||||
if (!cid) continue;
|
||||
const cur = flagMap[cid] ?? { bk: false, pp: false };
|
||||
if ((r as any).in_bankruptcy) cur.bk = true;
|
||||
if ((r as any).on_payment_plan) cur.pp = true;
|
||||
flagMap[cid] = cur;
|
||||
}
|
||||
|
||||
// Fallback to the case's own updated_at if no child activity found.
|
||||
const enriched = baseRows.map((c: any) => {
|
||||
const childTs = latest[c.id];
|
||||
const lastActivity = childTs && (!c.updated_at || childTs > c.updated_at) ? childTs : c.updated_at;
|
||||
return { ...c, last_activity: lastActivity };
|
||||
const flags = flagMap[c.id];
|
||||
return { ...c, last_activity: lastActivity, in_bankruptcy: !!flags?.bk, on_payment_plan: !!flags?.pp };
|
||||
});
|
||||
|
||||
setCases(enriched);
|
||||
|
||||
Reference in New Issue
Block a user