Skip to content

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.ts
index 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.ts
index 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.ts
index 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.ts
index 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 email
if (
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.sql
index 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 $$
BEGIN
IF 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_emails
BEFORE INSERT OR UPDATE ON contacts
FOR EACH ROW
EXECUTE 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 URL
CREATE OR REPLACE FUNCTION lowercase_website()
RETURNS TRIGGER
@@ -37,12 +50,22 @@ BEGIN
END;
$$;
--- 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_website
BEFORE INSERT OR UPDATE ON companies
FOR EACH ROW
EXECUTE 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 address
CREATE OR REPLACE FUNCTION lowercase_email()
RETURNS TRIGGER
@@ -57,6 +80,9 @@ BEGIN
END;
$$;
+-- Drop existing triggers
+DROP TRIGGER IF EXISTS lowercase_sales_email ON sales;
+
-- Trigger for sales table to lowercase email
CREATE TRIGGER lowercase_sales_email
BEFORE INSERT OR UPDATE ON sales
@@ -65,12 +91,12 @@ CREATE TRIGGER lowercase_sales_email
-- Update existing contacts to lowercase email addresses
UPDATE 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 URLs
diff --git a/supabase/schemas/02_functions.sql b/supabase/schemas/02_functions.sql
index 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 $$
BEGIN
IF 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.sql
index 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_trigger
before insert on public.tasks
for 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_website
before insert or update on public.companies
for 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_emails
before insert or update on public.contacts
for 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 update
create or replace trigger lowercase_sales_email
before insert or update on public.sales