Skip to content

A full-featured CRM built with React, shadcn/ui, and Supabase.

do not create company if it match amail provider

ThieryMichel8e87da3877a0
diff --git a/supabase/functions/postmark/addNoteToContact.ts b/supabase/functions/postmark/addNoteToContact.ts
index 5d7ccd1c..7627b340 100644
--- a/supabase/functions/postmark/addNoteToContact.ts
+++ b/supabase/functions/postmark/addNoteToContact.ts
@@ -1,5 +1,98 @@
import { supabaseAdmin } from "../_shared/supabaseAdmin.ts";
import type { Attachment } from "./extractAndUploadAttachments.ts";
+import { MAIL_PROVIDERS } from "./mailProvider.const.ts";
+
+export const getOrCreateCompanyFromDomain = async (
+ domain: string,
+ salesId: number,
+) => {
+ // Check if the company already exists
+ const { data: existingCompany, error: fetchCompanyError } =
+ await supabaseAdmin
+ .from("companies")
+ .select("*")
+ .eq("name", domain)
+ .maybeSingle();
+ if (fetchCompanyError) {
+ throw new Error(
+ `Could not fetch companies from database, name: ${domain}, error: ${fetchCompanyError.message}`,
+ );
+ }
+
+ if (existingCompany) {
+ return existingCompany;
+ }
+
+ if (MAIL_PROVIDERS.includes(domain)) {
+ // We don't want to create companies for generic mail providers, as they are not really companies and it would pollute the database with useless entries.
+ return null;
+ }
+
+ const { data: newCompanies, error: createCompanyError } = await supabaseAdmin
+ .from("companies")
+ .insert({ name: domain, sales_id: salesId })
+ .select();
+ if (createCompanyError) {
+ throw new Error(
+ `Could not create company in database, name: ${domain}, error: ${createCompanyError.message}`,
+ );
+ }
+ return newCompanies[0];
+};
+
+export const getOrCreateContactFromEmailInfo = async ({
+ email,
+ firstName,
+ lastName,
+ salesId,
+ domain,
+}: {
+ email: string;
+ firstName: string;
+ lastName: string;
+ salesId: number;
+ domain: string;
+}) => {
+ // Check if the contact already exists
+ const { data: existingContact, error: fetchContactError } =
+ await supabaseAdmin
+ .from("contacts")
+ .select("*")
+ .contains("email_jsonb", JSON.stringify([{ email }]))
+ .maybeSingle();
+ if (fetchContactError) {
+ throw new Error(
+ `Could not fetch contact from database, email: ${email}, error: ${fetchContactError.message}`,
+ );
+ }
+
+ if (existingContact) {
+ return existingContact;
+ }
+
+ const company = await getOrCreateCompanyFromDomain(domain, salesId);
+
+ // Create the contact
+ const { data: newContacts, error: createContactError } = await supabaseAdmin
+ .from("contacts")
+ .insert({
+ first_name: firstName,
+ last_name: lastName,
+ email_jsonb: [{ email, type: "Work" }],
+ company_id: company ? company.id : null,
+ sales_id: salesId,
+ first_seen: new Date(),
+ last_seen: new Date(),
+ tags: [],
+ })
+ .select();
+ if (createContactError || !newContacts[0]) {
+ throw new Error(
+ `Could not create contact in database, email: ${email}, error: ${createContactError.message}`,
+ );
+ }
+ return newContacts[0];
+};
export const addNoteToContact = async ({
salesEmail,
@@ -40,82 +133,13 @@ export const addNoteToContact = async ({
);
}
- // Check if the contact already exists
- const { data: existingContact, error: fetchContactError } =
- await supabaseAdmin
- .from("contacts")
- .select("*")
- .contains("email_jsonb", JSON.stringify([{ email }]))
- .maybeSingle();
- if (fetchContactError) {
- return new Response(
- `Could not fetch contact from database, email: ${email}`,
- { status: 500 },
- );
- }
-
- // deno-lint-ignore no-explicit-any
- let contact: any = undefined;
- if (existingContact) {
- contact = existingContact;
- } else {
- // If the contact does not exist, we need to create it, along with the company if needed
-
- // Check if the company already exists
- const { data: existingCompany, error: fetchCompanyError } =
- await supabaseAdmin
- .from("companies")
- .select("*")
- .eq("name", domain)
- .maybeSingle();
- if (fetchCompanyError) {
- return new Response(
- `Could not fetch companies from database, name: ${domain}`,
- { status: 500 },
- );
- }
-
- // deno-lint-ignore no-explicit-any
- let company: any = undefined;
- if (existingCompany) {
- company = existingCompany;
- } else {
- const { data: newCompanies, error: createCompanyError } =
- await supabaseAdmin
- .from("companies")
- .insert({ name: domain, sales_id: sales.id })
- .select();
- if (createCompanyError) {
- return new Response(
- `Could not create company in database, name: ${domain}`,
- { status: 500 },
- );
- }
- company = newCompanies[0];
- }
-
- // Create the contact
- const { data: newContacts, error: createContactError } = await supabaseAdmin
- .from("contacts")
- .insert({
- first_name: firstName,
- last_name: lastName,
- email_jsonb: [{ email, type: "Work" }],
- company_id: company.id,
- sales_id: sales.id,
- first_seen: new Date(),
- last_seen: new Date(),
- tags: [],
- })
- .select();
- if (createContactError || !newContacts[0]) {
- return new Response(
- `Could not create contact in database, email: ${email}`,
- { status: 500 },
- );
- }
- contact = newContacts[0];
- }
+ const contact = await getOrCreateContactFromEmailInfo({
+ email,
+ firstName,
+ lastName,
+ salesId: sales.id,
+ domain,
+ });
// Add note to contact
const { error: createNoteError } = await supabaseAdmin
diff --git a/supabase/functions/postmark/mailProvider.const.ts b/supabase/functions/postmark/mailProvider.const.ts
new file mode 100644
index 00000000..fe57286c
--- /dev/null
+++ b/supabase/functions/postmark/mailProvider.const.ts
@@ -0,0 +1,104 @@
+export const MAIL_PROVIDERS = [
+ "gmail.com",
+ "yahoo.com",
+ "hotmail.com",
+ "aol.com",
+ "hotmail.co.uk",
+ "hotmail.fr",
+ "msn.com",
+ "yahoo.fr",
+ "wanadoo.fr",
+ "orange.fr",
+ "comcast.net",
+ "yahoo.co.uk",
+ "yahoo.com.br",
+ "yahoo.co.in",
+ "live.com",
+ "rediffmail.com",
+ "free.fr",
+ "gmx.de",
+ "web.de",
+ "yandex.ru",
+ "ymail.com",
+ "libero.it",
+ "outlook.com",
+ "uol.com.br",
+ "bol.com.br",
+ "mail.ru",
+ "cox.net",
+ "hotmail.it",
+ "sbcglobal.net",
+ "sfr.fr",
+ "live.fr",
+ "verizon.net",
+ "live.co.uk",
+ "googlemail.com",
+ "yahoo.es",
+ "ig.com.br",
+ "live.nl",
+ "bigpond.com",
+ "terra.com.br",
+ "yahoo.it",
+ "neuf.fr",
+ "yahoo.de",
+ "alice.it",
+ "rocketmail.com",
+ "att.net",
+ "laposte.net",
+ "facebook.com",
+ "bellsouth.net",
+ "yahoo.in",
+ "hotmail.es",
+ "charter.net",
+ "yahoo.ca",
+ "yahoo.com.au",
+ "rambler.ru",
+ "hotmail.de",
+ "tiscali.it",
+ "shaw.ca",
+ "yahoo.co.jp",
+ "sky.com",
+ "earthlink.net",
+ "optonline.net",
+ "freenet.de",
+ "t-online.de",
+ "aliceadsl.fr",
+ "virgilio.it",
+ "home.nl",
+ "qq.com",
+ "telenet.be",
+ "me.com",
+ "yahoo.com.ar",
+ "tiscali.co.uk",
+ "yahoo.com.mx",
+ "voila.fr",
+ "gmx.net",
+ "mail.com",
+ "planet.nl",
+ "tin.it",
+ "live.it",
+ "ntlworld.com",
+ "arcor.de",
+ "yahoo.co.id",
+ "frontiernet.net",
+ "hetnet.nl",
+ "live.com.au",
+ "yahoo.com.sg",
+ "zonnet.nl",
+ "club-internet.fr",
+ "juno.com",
+ "optusnet.com.au",
+ "blueyonder.co.uk",
+ "bluewin.ch",
+ "skynet.be",
+ "sympatico.ca",
+ "windstream.net",
+ "mac.com",
+ "centurytel.net",
+ "chello.nl",
+ "live.ca",
+ "aim.com",
+ "bigpond.net.au",
+ "online.de",
+ "apple.com",
+];