Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions src/tests/supabase/functions/postmark.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ describe('extractMailContactData', () => {
lastName: 'Lastname',
email: '[email protected]',
domain: 'marmelab.com',
companyName: 'Marmelab',
},
]);
});
Expand All @@ -43,12 +44,14 @@ describe('extractMailContactData', () => {
lastName: 'Lastname',
email: '[email protected]',
domain: 'marmelab.com',
companyName: 'Marmelab',
},
{
firstName: 'John',
lastName: 'Doe',
email: '[email protected]',
domain: 'marmelab.com',
companyName: 'Marmelab',
},
]);
});
Expand All @@ -66,6 +69,7 @@ describe('extractMailContactData', () => {
lastName: 'Name',
email: '[email protected]',
domain: 'marmelab.com',
companyName: 'Marmelab',
},
]);
});
Expand All @@ -83,6 +87,7 @@ describe('extractMailContactData', () => {
lastName: 'Word Name',
email: '[email protected]',
domain: 'marmelab.com',
companyName: 'Marmelab',
},
]);
});
Expand All @@ -101,6 +106,7 @@ describe('extractMailContactData', () => {
lastName: 'Doe',
email: '"john@doe"@marmelab.com',
domain: 'marmelab.com',
companyName: 'Marmelab',
},
]);
});
Expand All @@ -118,6 +124,7 @@ describe('extractMailContactData', () => {
lastName: 'doe',
email: '[email protected]',
domain: 'marmelab.com',
companyName: 'Marmelab',
},
]);
});
Expand All @@ -135,6 +142,7 @@ describe('extractMailContactData', () => {
lastName: 'john',
email: '[email protected]',
domain: 'marmelab.com',
companyName: 'Marmelab',
},
]);
});
Expand All @@ -152,6 +160,7 @@ describe('extractMailContactData', () => {
lastName: 'doe multi',
email: '[email protected]',
domain: 'marmelab.com',
companyName: 'Marmelab',
},
]);
});
Expand All @@ -170,6 +179,45 @@ describe('extractMailContactData', () => {
lastName: 'doe"',
email: '"john@doe"@marmelab.com',
domain: 'marmelab.com',
companyName: 'Marmelab',
},
]);
});

it('should support subdomains', () => {
// Because it is allowed by https://www.rfc-editor.org/rfc/rfc5322
const result = extractMailContactData([
{
Email: '[email protected]',
Name: 'John DOE',
},
]);
expect(result).toEqual([
{
firstName: 'John',
lastName: 'DOE',
email: '[email protected]',
domain: 'sub.marmelab.com',
companyName: 'Marmelab',
},
]);
});

it('should support domains with hyphens and underscores', () => {
// Because it is allowed by https://www.rfc-editor.org/rfc/rfc5322
const result = extractMailContactData([
{
Email: '[email protected]_lab.com',
Name: 'John DOE',
},
]);
expect(result).toEqual([
{
firstName: 'John',
lastName: 'DOE',
email: '[email protected]_lab.com',
domain: 'sub.mar-me_lab.com',
companyName: 'Mar Me Lab',
},
]);
});
Expand Down
10 changes: 8 additions & 2 deletions supabase/functions/postmark/addNoteToContact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ export const addNoteToContact = async ({
firstName,
lastName,
noteContent,
companyName,
}: {
salesEmail: string;
email: string;
domain: string;
firstName: string;
lastName: string;
noteContent: string;
companyName: string;
}) => {
const { data: sales, error: fetchSalesError } = await supabaseAdmin
.from('sales')
Expand Down Expand Up @@ -60,7 +62,7 @@ export const addNoteToContact = async ({
await supabaseAdmin
.from('companies')
.select('*')
.eq('name', domain)
.eq('name', companyName)
.maybeSingle();
if (fetchCompanyError)
return new Response(
Expand All @@ -76,7 +78,11 @@ export const addNoteToContact = async ({
const { data: newCompanies, error: createCompanyError } =
await supabaseAdmin
.from('companies')
.insert({ name: domain, sales_id: sales.id })
.insert({
name: companyName,
website: `https://${domain}`,
sales_id: sales.id,
})
.select();
if (createCompanyError)
return new Response(
Expand Down
22 changes: 21 additions & 1 deletion supabase/functions/postmark/extractMailContactData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,26 @@ export const extractMailContactData = (
firstName = parts[0];
lastName = parts.slice(1).join(' ');
}
return { firstName, lastName, email: contact.Email, domain };
return {
firstName,
lastName,
email: contact.Email,
domain,
companyName: extractCompanyName(domain),
};
});
};

export function extractCompanyName(domain: string) {
const name = domain.split('.').at(-2)!;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This won't work for second level domains, e.g. .co.uk. IMO the best thing to do is rely on a third-party lib to strip those from the domain...


const lowerCaseName = name
.replace('-', ' ')
.replace('_', ' ')
.toLowerCase();

return lowerCaseName
.split(' ')
.map(word => word.charAt(0).toUpperCase() + word.slice(1))
.join(' ');
}
9 changes: 8 additions & 1 deletion supabase/functions/postmark/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,13 @@ Deno.serve(async req => {

const contacts = extractMailContactData(ToFull);

for (const { firstName, lastName, email, domain } of contacts) {
for (const {
firstName,
lastName,
email,
domain,
companyName,
} of contacts) {
if (!email) {
// Return a 403 to let Postmark know that it's no use to retry this request
// https://postmarkapp.com/developer/webhooks/inbound-webhook#errors-and-retries
Expand All @@ -67,6 +73,7 @@ Deno.serve(async req => {
firstName,
lastName,
noteContent,
companyName,
});
}

Expand Down