diff --git a/src/email.ts b/src/email.ts index 15e2238..7186c94 100644 --- a/src/email.ts +++ b/src/email.ts @@ -41,7 +41,7 @@ export function getRecipients( cc: email?.cc || [], bcc: email?.bcc || [], from: email?.from || [], - to: [], + to: email?.to || [], }; } else { const { diff --git a/test/email.test.ts b/test/email.test.ts index c5861dc..f44e13b 100644 --- a/test/email.test.ts +++ b/test/email.test.ts @@ -9,15 +9,17 @@ const createIncomingEmail = ({ from = [], cc = [], bcc = [], + to = [], }: { from?: string[]; cc?: string[]; bcc?: string[]; + to?: string[]; }) => { return { message_type: MessageType.INCOMING, content_attributes: { - email: { from, cc, bcc }, + email: { from, cc, bcc, to }, }, } as IncomingEmailMessage; }; @@ -157,6 +159,52 @@ describe('getRecipients', () => { }); }); + describe('To Recipients', () => { + test('should add extra emails in the "to" field to the "cc" array', () => { + const lastEmail = createIncomingEmail({ + from: ['sender@example.com'], + to: [inboxEmail, 'colleague@example.com'], + }); + const result = getRecipients( + lastEmail, + conversationContact, + inboxEmail, + forwardToEmail + ); + expect(result.cc).toContain('colleague@example.com'); + expect(result.cc).not.toContain(inboxEmail); + }); + + test('should keep "to" recipients alongside existing "cc" recipients', () => { + const lastEmail = createIncomingEmail({ + from: ['sender@example.com'], + to: [inboxEmail, 'colleague@example.com'], + cc: ['cc1@example.com'], + }); + const result = getRecipients( + lastEmail, + conversationContact, + inboxEmail, + forwardToEmail + ); + expect(result.cc).toEqual( + expect.arrayContaining(['cc1@example.com', 'colleague@example.com']) + ); + }); + + test('should not error when to is null', () => { + // @ts-ignore + const lastEmail = createIncomingEmail({ from: ['a@b.com'], to: null }); + const result = getRecipients( + lastEmail, + conversationContact, + inboxEmail, + forwardToEmail + ); + expect(result.to).toEqual(['a@b.com']); + }); + }); + describe('BCC Recipients', () => { test('should add emails in the "bcc" field to the "bcc" array', () => { const bccEmails = ['bcc1@example.com', 'bcc2@example.com'];