Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(mail): Support recipient with empty Name #52

Merged
merged 2 commits into from
Aug 9, 2024
Merged
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
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

77 changes: 77 additions & 0 deletions src/tests/supabase/functions/postmark.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,81 @@ describe('extractMailContactData', () => {
domain: 'marmelab.com',
});
});

it('should support multiple @ in email', () => {
// Because it is allowed by https://www.rfc-editor.org/rfc/rfc5322
const result = extractMailContactData([
{
Email: '"john@doe"@marmelab.com',
Name: 'John Doe',
},
]);
expect(result).toEqual({
firstName: 'John',
lastName: 'Doe',
email: '"john@doe"@marmelab.com',
domain: 'marmelab.com',
});
});

it('should use first part of email when Name is empty', () => {
const result = extractMailContactData([
{
Email: 'john.doe@marmelab.com',
Name: '',
},
]);
expect(result).toEqual({
firstName: 'john',
lastName: 'doe',
email: 'john.doe@marmelab.com',
domain: 'marmelab.com',
});
});

it('should use first part of email when Name is empty and support single word', () => {
const result = extractMailContactData([
{
Email: 'john@marmelab.com',
Name: '',
},
]);
expect(result).toEqual({
firstName: '',
lastName: 'john',
email: 'john@marmelab.com',
domain: 'marmelab.com',
});
});

it('should use first part of email when Name is empty and support multiple words', () => {
const result = extractMailContactData([
{
Email: 'john.doe.multi@marmelab.com',
Name: '',
},
]);
expect(result).toEqual({
firstName: 'john',
lastName: 'doe multi',
email: 'john.doe.multi@marmelab.com',
domain: 'marmelab.com',
});
});

it('should support empty Name and multiple @ in email', () => {
// Because it is allowed by https://www.rfc-editor.org/rfc/rfc5322
const result = extractMailContactData([
{
Email: '"john@doe"@marmelab.com',
Name: '',
},
]);
expect(result).toEqual({
firstName: '"john',
lastName: 'doe"',
email: '"john@doe"@marmelab.com',
domain: 'marmelab.com',
});
});
});
4 changes: 3 additions & 1 deletion supabase/functions/postmark/extractMailContactData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ export const extractMailContactData = (
const contact = ToFull[0];

const domain = contact.Email.split('@').at(-1);
const fullName = contact.Name;
const fullName =
contact.Name ||
contact.Email.split('@').slice(0, -1).join(' ').split('.').join(' ');
let firstName = '';
let lastName = fullName;
if (fullName && fullName.includes(' ')) {
Expand Down
Loading