A full-featured CRM built with React, shadcn/ui, and Supabase.
copilot code review
Jean-Baptiste Kaiser8aa3b5c223fd
diff --git a/supabase/functions/postmark/addNoteToContact.ts b/supabase/functions/postmark/addNoteToContact.tsindex edad8649..a4abfb52 100644--- a/supabase/functions/postmark/addNoteToContact.ts+++ b/supabase/functions/postmark/addNoteToContact.ts@@ -23,7 +23,7 @@ export const getOrCreateCompanyFromDomain = async ({await supabaseAdmin.from("companies").select("*")- .or(`website.eq.${website},name.eq.${companyName}`)+ .or(`website.eq.${website},name.eq.${companyName},name.eq.${domain}`).maybeSingle();if (fetchCompanyError) {throw new Error(diff --git a/supabase/functions/postmark/extractMailContactData.test.ts b/supabase/functions/postmark/extractMailContactData.test.tsindex 17e14c63..07d467b2 100644--- a/supabase/functions/postmark/extractMailContactData.test.ts+++ b/supabase/functions/postmark/extractMailContactData.test.ts@@ -183,6 +183,24 @@ describe("extractMailContactData", () => {expect(extractCompanyName("Example_Company.com")).toBe("Example Company");});+ it("extracts company name with multiple hyphens", () => {+ expect(extractCompanyName("my-super-cool-company.com")).toBe(+ "My Super Cool Company",+ );+ });++ it("extracts company name with multiple underscores", () => {+ expect(extractCompanyName("my_super_cool_company.com")).toBe(+ "My Super Cool Company",+ );+ });++ it("extracts company name with mixed separators", () => {+ expect(extractCompanyName("my-super_cool-company.com")).toBe(+ "My Super Cool Company",+ );+ });+it("handles invalid or unknown TLD domains gracefully", () => {expect(extractCompanyName("invalid.domain")).toBe("Invalid");});diff --git a/supabase/functions/postmark/extractMailContactData.ts b/supabase/functions/postmark/extractMailContactData.tsindex 950c3675..941ac89f 100644--- a/supabase/functions/postmark/extractMailContactData.ts+++ b/supabase/functions/postmark/extractMailContactData.ts@@ -12,8 +12,7 @@ export function extractCompanyName(domain: string) {}const humanizedName = name- .replace("-", " ")- .replace("_", " ")+ .replace(/[-_]+/g, " ").split(" ").map((word) => word.charAt(0).toUpperCase() + word.slice(1)).join(" ");diff --git a/supabase/functions/postmark/index.ts b/supabase/functions/postmark/index.tsindex aefd8ba4..50eeee13 100644--- a/supabase/functions/postmark/index.ts+++ b/supabase/functions/postmark/index.ts@@ -58,11 +58,11 @@ Deno.serve(async (req) => {const firstToEmail = (ToFull[0]?.Email || "").toLowerCase();- // If we have an INBOUND_EMAIL and the email is sent to the inbound email address, and the sender is a known sales email,+ // If we have an INBOUND_EMAIL and the email is sent only to the inbound email address, and the sender is a known sales email,// then we can try to extract the real recipient email from the body of the emailif (INBOUND_EMAIL &&- firstToEmail &&+ ToFull.length === 1 &&firstToEmail === INBOUND_EMAIL &&salesEmails.includes(salesEmail)) {diff --git a/supabase/migrations/20260320120000_lowercase_email_website.sql b/supabase/migrations/20260320120000_lowercase_email_website.sqlindex d6ae1425..dd7abdab 100644--- a/supabase/migrations/20260320120000_lowercase_email_website.sql+++ b/supabase/migrations/20260320120000_lowercase_email_website.sql@@ -6,23 +6,36 @@ SET search_path TO 'public'AS $$BEGINIF NEW.email_jsonb IS NOT NULL THEN- NEW.email_jsonb = (+ NEW.email_jsonb = COALESCE((SELECT jsonb_agg(jsonb_set(elem, '{email}', to_jsonb(LOWER(elem->>'email'))))FROM jsonb_array_elements(NEW.email_jsonb) AS elem- );+ ), '[]'::jsonb);END IF;RETURN NEW;END;$$;--- Trigger for contacts table to lowercase emails+-- Drop and recreate triggers to ensure proper ordering+-- Lowercase triggers must run before the avatar/favicon triggers++-- Drop existing triggers+DROP TRIGGER IF EXISTS lowercase_contact_emails ON contacts;+DROP TRIGGER IF EXISTS contact_saved ON contacts;++-- Create lowercase trigger first (so it runs before contact_saved)CREATE TRIGGER lowercase_contact_emailsBEFORE INSERT OR UPDATE ON contactsFOR EACH ROWEXECUTE FUNCTION lowercase_email_jsonb();+-- Recreate contact_saved trigger (will run after lowercase_contact_emails)+CREATE TRIGGER contact_saved+ BEFORE INSERT OR UPDATE ON contacts+ FOR EACH ROW+ EXECUTE FUNCTION handle_contact_saved();+-- Function to lowercase website URLCREATE OR REPLACE FUNCTION lowercase_website()RETURNS TRIGGER@@ -37,12 +50,22 @@ BEGINEND;$$;--- Trigger for companies table to lowercase website+-- Drop existing triggers+DROP TRIGGER IF EXISTS lowercase_company_website ON companies;+DROP TRIGGER IF EXISTS company_saved ON companies;++-- Create lowercase trigger first (so it runs before company_saved)CREATE TRIGGER lowercase_company_websiteBEFORE INSERT OR UPDATE ON companiesFOR EACH ROWEXECUTE FUNCTION lowercase_website();+-- Recreate company_saved trigger (will run after lowercase_company_website)+CREATE TRIGGER company_saved+ BEFORE INSERT OR UPDATE ON companies+ FOR EACH ROW+ EXECUTE FUNCTION handle_company_saved();+-- Function to lowercase email addressCREATE OR REPLACE FUNCTION lowercase_email()RETURNS TRIGGER@@ -57,6 +80,9 @@ BEGINEND;$$;+-- Drop existing triggers+DROP TRIGGER IF EXISTS lowercase_sales_email ON sales;+-- Trigger for sales table to lowercase emailCREATE TRIGGER lowercase_sales_emailBEFORE INSERT OR UPDATE ON sales@@ -65,12 +91,12 @@ CREATE TRIGGER lowercase_sales_email-- Update existing contacts to lowercase email addressesUPDATE contacts-SET email_jsonb = (+SET email_jsonb = COALESCE((SELECT jsonb_agg(jsonb_set(elem, '{email}', to_jsonb(LOWER(elem->>'email'))))FROM jsonb_array_elements(email_jsonb) AS elem-)+), '[]'::jsonb)WHERE email_jsonb IS NOT NULL;-- Update existing companies to lowercase website URLsdiff --git a/supabase/schemas/02_functions.sql b/supabase/schemas/02_functions.sqlindex 0afb9523..0b520ad9 100644--- a/supabase/schemas/02_functions.sql+++ b/supabase/schemas/02_functions.sql@@ -443,12 +443,12 @@ CREATE OR REPLACE FUNCTION "public"."lowercase_email_jsonb"() RETURNS "trigger"AS $$BEGINIF NEW.email_jsonb IS NOT NULL THEN- NEW.email_jsonb = (+ NEW.email_jsonb = COALESCE((SELECT jsonb_agg(jsonb_set(elem, '{email}', to_jsonb(LOWER(elem->>'email'))))FROM jsonb_array_elements(NEW.email_jsonb) AS elem- );+ ), '[]'::jsonb);END IF;RETURN NEW;END;diff --git a/supabase/schemas/04_triggers.sql b/supabase/schemas/04_triggers.sqlindex c4731a12..dcceeb89 100644--- a/supabase/schemas/04_triggers.sql+++ b/supabase/schemas/04_triggers.sql@@ -28,26 +28,26 @@ create or replace trigger set_task_sales_id_triggerbefore insert on public.tasksfor each row execute function public.set_sales_id_default();--- Auto-fetch company logo from website favicon on save-create or replace trigger company_saved- before insert or update on public.companies- for each row execute function public.handle_company_saved();---- Auto-fetch contact avatar from email on save-create or replace trigger contact_saved- before insert or update on public.contacts- for each row execute function public.handle_contact_saved();---- Lowercase company website before insert or update+-- Lowercase company website before insert or update (must run before company_saved)create or replace trigger lowercase_company_websitebefore insert or update on public.companiesfor each row execute function public.lowercase_website();--- Lowercase contact emails before insert or update+-- Auto-fetch company logo from website favicon on save (runs after lowercase_company_website)+create or replace trigger company_saved+ before insert or update on public.companies+ for each row execute function public.handle_company_saved();++-- Lowercase contact emails before insert or update (must run before contact_saved)create or replace trigger lowercase_contact_emailsbefore insert or update on public.contactsfor each row execute function public.lowercase_email_jsonb();+-- Auto-fetch contact avatar from email on save (runs after lowercase_contact_emails)+create or replace trigger contact_saved+ before insert or update on public.contacts+ for each row execute function public.handle_contact_saved();+-- Lowercase sales email before insert or updatecreate or replace trigger lowercase_sales_emailbefore insert or update on public.sales