Skip to content

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

add postmark edge function

Jean-Baptiste Kaiser1e804b6f74de
diff --git a/supabase/config.toml b/supabase/config.toml
index ca00d512..4d79acbf 100644
--- a/supabase/config.toml
+++ b/supabase/config.toml
@@ -151,4 +151,7 @@ s3_access_key = "env(S3_ACCESS_KEY)"
s3_secret_key = "env(S3_SECRET_KEY)"
[functions.mailgun]
+verify_jwt = false
+
+[functions.postmark]
verify_jwt = false
\ No newline at end of file
diff --git a/supabase/functions/postmark/index.ts b/supabase/functions/postmark/index.ts
new file mode 100644
index 00000000..6f19812f
--- /dev/null
+++ b/supabase/functions/postmark/index.ts
@@ -0,0 +1,279 @@
+// Follow this setup guide to integrate the Deno language server with your editor:
+// https://deno.land/manual/getting_started/setup_your_environment
+// This enables autocomplete, go to definition, etc.
+
+// Setup type definitions for built-in Supabase Runtime APIs
+import 'jsr:@supabase/functions-js/edge-runtime.d.ts';
+import { supabaseAdmin } from '../_shared/supabaseAdmin.ts';
+
+Deno.serve(async req => {
+ // Only allow POST requests
+ if (req.method !== 'POST') {
+ return new Response(null, { status: 405 });
+ }
+
+ const { ToFull, FromFull, Subject, StrippedTextReply } = await req.json();
+
+ if (!ToFull || !ToFull.length)
+ return new Response('Missing parameter: ToFull', { status: 403 });
+ if (!FromFull)
+ return new Response('Missing parameter: FromFull', { status: 403 });
+ if (!Subject)
+ return new Response('Missing parameter: Subject', { status: 403 });
+ if (!StrippedTextReply)
+ return new Response('Missing parameter: StrippedTextReply', {
+ status: 403,
+ });
+
+ const noteContent = getNoteContent(Subject, StrippedTextReply);
+
+ const { Email: salesEmail } = FromFull;
+ if (!salesEmail)
+ return new Response(
+ `Could not extract sales email from FromFull: ${FromFull}`,
+ { status: 403 }
+ );
+ 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 }
+ );
+
+ const { firstName, lastName, email, domain } =
+ extractMailContactData(ToFull);
+ if (!email)
+ return new Response(`Could not extract email from ToFull: ${ToFull}`, {
+ status: 403,
+ });
+
+ 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
+ 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');
+});
+
+/**
+ * Extracts the first name, last name, email, and domain from a mail contact.
+ *
+ * Example:
+ * "ToFull": [
+ * {
+ * "Email": "[email protected]",
+ * "Name": "Firstname Lastname"
+ * }
+ * ]
+ *
+ * Return Value:
+ * {
+ * firstName: "Firstname",
+ * lastName: "Lastname",
+ * email: "[email protected]",
+ * domain: "marmelab.com.com"
+ * }
+ *
+ */
+const extractMailContactData = (
+ ToFull: {
+ Email: string;
+ Name: string;
+ }[]
+) => {
+ // We only support one recipient for now
+ const contact = ToFull[0];
+
+ const domain = contact.Email.split('@')[1];
+ const fullName = contact.Name;
+ let firstName = '';
+ let lastName = fullName;
+ if (fullName && fullName.includes(' ')) {
+ const parts = fullName.split(' ');
+ firstName = parts[0];
+ lastName = parts.slice(1).join(' ');
+ }
+ return { firstName, lastName, email: contact.Email, domain };
+};
+
+const getNoteContent = (subject: string, strippedText: string) => `${subject}
+
+${strippedText}`;
+
+/* To invoke locally:
+ 1. Run `make start`
+ 2. Make sure to have a Sales with email "[email protected]" (create it if needed)
+ 3. OPTIONAL: Create a Contact with email "[email protected]"
+ 4. In another terminal, run `make start-supabase-functions`
+ 5. In another terminal, make an HTTP request:
+ curl -i --location --request POST 'http://127.0.0.1:54321/functions/v1/postmark' \
+ --header 'Content-Type: application/json' \
+ --data '{
+ "FromName": "Postmarkapp Support",
+ "From": "[email protected]",
+ "FromFull": {
+ "Email": "[email protected]",
+ "Name": "Postmarkapp Support",
+ "MailboxHash": ""
+ },
+ "To": "\"Firstname Lastname\" <[email protected]>",
+ "ToFull": [
+ {
+ "Email": "[email protected]",
+ "Name": "Firstname Lastname",
+ "MailboxHash": "SampleHash"
+ }
+ ],
+ "Cc": "\"First Cc\" <[email protected]>, [email protected]",
+ "CcFull": [
+ {
+ "Email": "[email protected]",
+ "Name": "First Cc",
+ "MailboxHash": ""
+ },
+ {
+ "Email": "[email protected]",
+ "Name": "",
+ "MailboxHash": ""
+ }
+ ],
+ "Bcc": "\"First Bcc\" <[email protected]>, [email protected]",
+ "BccFull": [
+ {
+ "Email": "[email protected]",
+ "Name": "First Bcc",
+ "MailboxHash": ""
+ },
+ {
+ "Email": "[email protected]",
+ "Name": "",
+ "MailboxHash": ""
+ }
+ ],
+ "OriginalRecipient": "[email protected]",
+ "Subject": "Test subject",
+ "MessageID": "73e6d360-66eb-11e1-8e72-a8904824019b",
+ "ReplyTo": "[email protected]",
+ "MailboxHash": "SampleHash",
+ "Date": "Fri, 1 Aug 2014 16:45:32 -04:00",
+ "TextBody": "This is a test text body.",
+ "HtmlBody": "<html><body><p>This is a test html body.</p></body></html>",
+ "StrippedTextReply": "This is the reply text",
+ "Tag": "TestTag",
+ "Headers": [
+ {
+ "Name": "X-Header-Test",
+ "Value": ""
+ },
+ {
+ "Name": "X-Spam-Status",
+ "Value": "No"
+ },
+ {
+ "Name": "X-Spam-Score",
+ "Value": "-0.1"
+ },
+ {
+ "Name": "X-Spam-Tests",
+ "Value": "DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,SPF_PASS"
+ }
+ ],
+ "Attachments": [
+ {
+ "Name": "test.txt",
+ "Content": "VGhpcyBpcyBhdHRhY2htZW50IGNvbnRlbnRzLCBiYXNlLTY0IGVuY29kZWQu",
+ "ContentType": "text/plain",
+ "ContentLength": 45
+ }
+ ]
+ }'
+*/