A full-featured CRM built with React, shadcn/ui, and Supabase.
further improve readability of the function
Jean-Baptiste Kaiser07c2c6600bd4
diff --git a/supabase/functions/postmark/addNoteToContact.ts b/supabase/functions/postmark/addNoteToContact.tsnew file mode 100644index 00000000..8929e910--- /dev/null+++ b/supabase/functions/postmark/addNoteToContact.ts@@ -0,0 +1,123 @@+import { supabaseAdmin } from '../_shared/supabaseAdmin.ts';++export const addNoteToContact = async ({+ salesEmail,+ email,+ domain,+ firstName,+ lastName,+ noteContent,+}: {+ salesEmail: string;+ email: string;+ domain: string;+ firstName: string;+ lastName: string;+ noteContent: string;+}) => {+ const { data: sales, error: fetchSalesError } = await supabaseAdmin+ .from('sales')+ .select('*')+ .eq('email', salesEmail)+ .maybeSingle();+ if (fetchSalesError)+ return new Response(+ `Could not fetch sales from database, email: ${salesEmail}`,+ { status: 500 }+ );+ if (!sales)+ return new Response(+ `Unable to find sales in database, email: ${salesEmail}`,+ { status: 403 }+ );++ // Check if the contact already exists+ const { data: existingContact, error: fetchContactError } =+ await supabaseAdmin+ .from('contacts')+ .select('*')+ .eq('email', 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,+ 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];+ }++ // Add note to contact+ const { error: createNoteError } = await supabaseAdmin+ .from('contactNotes')+ .insert({+ contact_id: contact.id,+ text: noteContent,+ sales_id: sales.id,+ });+ if (createNoteError)+ return new Response(+ `Could not add note to contact ${email}, sales ${salesEmail}`,+ { status: 500 }+ );++ return new Response('OK');+};diff --git a/supabase/functions/postmark/getNoteContent.ts b/supabase/functions/postmark/getNoteContent.tsnew file mode 100644index 00000000..5a99b700--- /dev/null+++ b/supabase/functions/postmark/getNoteContent.ts@@ -0,0 +1,6 @@+export const getNoteContent = (+ subject: string,+ strippedText: string+) => `${subject}++${strippedText}`;diff --git a/supabase/functions/postmark/index.ts b/supabase/functions/postmark/index.tsindex 75206cd6..489e2148 100644--- a/supabase/functions/postmark/index.ts+++ b/supabase/functions/postmark/index.ts@@ -4,9 +4,10 @@// Setup type definitions for built-in Supabase Runtime APIsimport 'jsr:@supabase/functions-js/edge-runtime.d.ts';-import { supabaseAdmin } from '../_shared/supabaseAdmin.ts';import { getExpectedAuthorization } from './getExpectedAuthorization.ts';import { extractMailContactData } from './extractMailContactData.ts';+import { addNoteToContact } from './addNoteToContact.ts';+import { getNoteContent } from './getNoteContent.ts';const webhookUser = Deno.env.get('POSTMARK_WEBHOOK_USER');const webhookPassword = Deno.env.get('POSTMARK_WEBHOOK_PASSWORD');@@ -105,132 +106,6 @@ const checkBody = (json: any) => {});};-const getNoteContent = (subject: string, strippedText: string) => `${subject}--${strippedText}`;--const addNoteToContact = async ({- salesEmail,- email,- domain,- firstName,- lastName,- noteContent,-}: {- salesEmail: string;- email: string;- domain: string;- firstName: string;- lastName: string;- noteContent: string;-}) => {- const { data: sales, error: fetchSalesError } = await supabaseAdmin- .from('sales')- .select('*')- .eq('email', salesEmail)- .maybeSingle();- if (fetchSalesError)- return new Response(- `Could not fetch sales from database, email: ${salesEmail}`,- { status: 500 }- );- if (!sales)- return new Response(- `Unable to find sales in database, email: ${salesEmail}`,- { status: 403 }- );-- // Check if the contact already exists- const { data: existingContact, error: fetchContactError } =- await supabaseAdmin- .from('contacts')- .select('*')- .eq('email', 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,- 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];- }-- // Add note to contact- const { error: createNoteError } = await supabaseAdmin- .from('contactNotes')- .insert({- contact_id: contact.id,- text: noteContent,- sales_id: sales.id,- });- if (createNoteError)- return new Response(- `Could not add note to contact ${email}, sales ${salesEmail}`,- { status: 500 }- );-- return new Response('OK');-};-/* To invoke locally:1. Run `make start`2. Make sure to have a Sales with email "[email protected]" (create it if needed)