A full-featured CRM built with React, shadcn/ui, and Supabase.
fix(search): keep the task backfill in the past, and stop dropping id 0
erwanMarmelab2897aa9c1724
diff --git a/src/components/atomic-crm/providers/commons/search.ts b/src/components/atomic-crm/providers/commons/search.tsindex bb2a4654..6033b388 100644--- a/src/components/atomic-crm/providers/commons/search.ts+++ b/src/components/atomic-crm/providers/commons/search.ts@@ -36,7 +36,9 @@ const getLabels = async <T extends { id: Identifier }>(ids: (Identifier | undefined | null)[],getLabel: (record: T) => string,): Promise<Map<Identifier, string>> => {- const uniqueIds = [...new Set(ids.filter(Boolean) as Identifier[])];+ const uniqueIds = [+ ...new Set(ids.filter((id) => id != null) as Identifier[]),+ ];if (uniqueIds.length === 0) {return new Map();}@@ -139,9 +141,10 @@ export async function getSearchResults(resource: "deals" as const,record_id: deal.id,title: deal.name,- subtitle: deal.company_id- ? (companyLabels.get(deal.company_id) ?? null)- : null,+ subtitle:+ deal.company_id != null+ ? (companyLabels.get(deal.company_id) ?? null)+ : null,contact_id: null,deal_id: deal.id,date: deal.created_at ?? null,diff --git a/src/components/atomic-crm/search/GlobalSearchDialog.test.tsx b/src/components/atomic-crm/search/GlobalSearchDialog.test.tsxindex bca8f132..65c0c656 100644--- a/src/components/atomic-crm/search/GlobalSearchDialog.test.tsx+++ b/src/components/atomic-crm/search/GlobalSearchDialog.test.tsx@@ -77,8 +77,8 @@ const renderDialog = (open = true) =>describe("GlobalSearchDialog", () => {// The dialog reads useIsMobile, and the layouts expose different resources,// so the viewport is part of the arrangement in every test below.- beforeEach(() => {- page.viewport(1600, 900);+ beforeEach(async () => {+ await page.viewport(1600, 900);});it("puts nothing in the document while closed", async () => {@@ -171,6 +171,62 @@ describe("GlobalSearchDialog", () => {.toBeVisible();});+ it("keeps subtitles for records on id 0, which FakeRest generators use", async () => {+ const screen = await render(+ <StoryWrapper+ data={{+ companies: [{ id: 0, name: "Zero Corp", sector: "Food" }],+ contacts: [+ buildContact({+ company_id: 0,+ company_name: "Zero Corp",+ id: 0,+ }),+ ],+ contact_notes: [+ {+ contact_id: 0,+ date: "2025-02-01T10:00:00.000Z",+ id: 0,+ sales_id: 0,+ status: "warm",+ text: "Note on the zeroth contact about sourdough",+ },+ ],+ deals: [+ {+ company_id: 0,+ created_at: "2025-01-05T10:00:00.000Z",+ id: 0,+ name: "Deal zero for sourdough",+ stage: "opportunity",+ },+ ],+ }}+ >+ <GlobalSearchDialog open onOpenChange={() => {}} />+ </StoryWrapper>,+ );++ await screen.getByPlaceholder(PLACEHOLDER).fill("sourdough");++ await expect+ .element(screen.getByText("Note on the zeroth contact about sourdough"))+ .toBeVisible();+ await expect+ .element(+ screen+ .getByRole("group", { name: "Contact notes" })+ .getByText("Ada Lovelace"),+ )+ .toBeVisible();+ await expect+ .element(+ screen.getByRole("group", { name: "Deals" }).getByText("Zero Corp"),+ )+ .toBeVisible();+ });+it("shows the deal's company as its subtitle", async () => {const screen = await renderDialog();@@ -209,8 +265,8 @@ describe("GlobalSearchDialog", () => {});describe("GlobalSearchDialog on mobile", () => {- beforeEach(() => {- page.viewport(375, 667);+ beforeEach(async () => {+ await page.viewport(375, 667);});it("omits deals, which have no page on the mobile layout", async () => {diff --git a/src/components/atomic-crm/search/getSearchResultUrl.test.ts b/src/components/atomic-crm/search/getSearchResultUrl.test.tsindex 686a05d7..aefb8245 100644--- a/src/components/atomic-crm/search/getSearchResultUrl.test.ts+++ b/src/components/atomic-crm/search/getSearchResultUrl.test.ts@@ -95,6 +95,33 @@ describe("getSearchResultUrl", () => {expect(getSearchResultUrl(result, true)).toBeNull();});+ it("links records whose parent id is 0", () => {+ // FakeRest generators assign array-index ids, so 0 is a real record.+ expect(+ getSearchResultUrl(+ { resource: "tasks", record_id: 0, contact_id: 0, deal_id: null },+ false,+ ),+ ).toBe("/contacts/0/show");+ expect(+ getSearchResultUrl(+ {+ resource: "contact_notes",+ record_id: 0,+ contact_id: 0,+ deal_id: null,+ },+ true,+ ),+ ).toBe("/contacts/0/notes/0");+ expect(+ getSearchResultUrl(+ { resource: "deal_notes", record_id: 0, contact_id: null, deal_id: 0 },+ false,+ ),+ ).toBe("/deals/0/show");+ });+it("returns null when the parent record of a note or task is missing", () => {expect(getSearchResultUrl(diff --git a/src/components/atomic-crm/search/getSearchResultUrl.ts b/src/components/atomic-crm/search/getSearchResultUrl.tsindex 1f50585f..30076d81 100644--- a/src/components/atomic-crm/search/getSearchResultUrl.ts+++ b/src/components/atomic-crm/search/getSearchResultUrl.ts@@ -25,16 +25,18 @@ export const getSearchResultUrl = (return isMobile ? null : `/deals/${result.record_id}/show`;case "tasks":// Tasks have no page of their own: link to the contact they belong to.- return result.contact_id ? `/contacts/${result.contact_id}/show` : null;+ return result.contact_id != null+ ? `/contacts/${result.contact_id}/show`+ : null;case "contact_notes":- if (!result.contact_id) {+ if (result.contact_id == null) {return null;}return isMobile? `/contacts/${result.contact_id}/notes/${result.record_id}`: `/contacts/${result.contact_id}/show`;case "deal_notes":- return isMobile || !result.deal_id+ return isMobile || result.deal_id == null? null: `/deals/${result.deal_id}/show`;default:diff --git a/supabase/migrations/20260910140459_global_search.sql b/supabase/migrations/20260910140459_global_search.sqlindex e099ff09..e0e9e016 100644--- a/supabase/migrations/20260910140459_global_search.sql+++ b/supabase/migrations/20260910140459_global_search.sql@@ -1,6 +1,22 @@alter table "public"."tasks" add column "created_at" timestamp with time zone not null default now();-update "public"."tasks" set "created_at" = coalesce("done_date", "due_date", "created_at");+-- Existing rows have no creation time to recover, so fall back to the best+-- available proxy: when the task was completed, a due date that has already+-- passed, or the contact's first_seen. `least(..., now())` keeps the result in+-- the past -- search_index sorts on this column, and a future date would put+-- every open task above every note, contact and deal. A bare `now()` would be+-- no better: it would tie every open task at the top of that same sort.+update "public"."tasks" t+set "created_at" = least(+ coalesce(+ t."done_date",+ case when t."due_date" < now() then t."due_date" end,+ c."first_seen"+ ),+ now()+)+from "public"."contacts" c+where c."id" = t."contact_id";create or replace view "public"."search_index" with (security_invoker = on) as SELECT ('company.'::text || c.id) AS id,'companies'::text AS resource,