From 991bb09622f0182ab81d8ebb6213dc4ad093012e Mon Sep 17 00:00:00 2001 From: Weiko Date: Thu, 14 Mar 2024 13:46:38 +0100 Subject: [PATCH] [messaging] fix participant handles with trailing spaces (#4457) --- .../fetch-messages-by-batches.service.spec.ts | 53 +++++++++++++++++++ .../fetch-messages-by-batches.service.ts | 6 ++- 2 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 packages/twenty-server/src/workspace/messaging/services/fetch-messages-by-batches.service.spec.ts diff --git a/packages/twenty-server/src/workspace/messaging/services/fetch-messages-by-batches.service.spec.ts b/packages/twenty-server/src/workspace/messaging/services/fetch-messages-by-batches.service.spec.ts new file mode 100644 index 000000000..59e95240f --- /dev/null +++ b/packages/twenty-server/src/workspace/messaging/services/fetch-messages-by-batches.service.spec.ts @@ -0,0 +1,53 @@ +import { AddressObject } from 'mailparser'; + +import { FetchMessagesByBatchesService } from './fetch-messages-by-batches.service'; + +describe('FetchMessagesByBatchesService', () => { + let service: FetchMessagesByBatchesService; + + beforeEach(() => { + service = new FetchMessagesByBatchesService(); + }); + + describe('formatAddressObjectAsParticipants', () => { + it('should format address object as participants', () => { + const addressObject: AddressObject = { + value: [ + { name: 'John Doe', address: 'john.doe @example.com' }, + { name: 'Jane Smith', address: 'jane.smith@example.com ' }, + ], + html: '', + text: '', + }; + + const result = service.formatAddressObjectAsParticipants( + addressObject, + 'from', + ); + + expect(result).toEqual([ + { + role: 'from', + handle: 'john.doe@example.com', + displayName: 'John Doe', + }, + { + role: 'from', + handle: 'jane.smith@example.com', + displayName: 'Jane Smith', + }, + ]); + }); + + it('should return an empty array if address object is undefined', () => { + const addressObject = undefined; + + const result = service.formatAddressObjectAsParticipants( + addressObject, + 'to', + ); + + expect(result).toEqual([]); + }); + }); +}); diff --git a/packages/twenty-server/src/workspace/messaging/services/fetch-messages-by-batches.service.ts b/packages/twenty-server/src/workspace/messaging/services/fetch-messages-by-batches.service.ts index f9186479f..267d0b749 100644 --- a/packages/twenty-server/src/workspace/messaging/services/fetch-messages-by-batches.service.ts +++ b/packages/twenty-server/src/workspace/messaging/services/fetch-messages-by-batches.service.ts @@ -162,7 +162,7 @@ export class FetchMessagesByBatchesService { return { role, - handle: address?.toLowerCase() || '', + handle: address ? this.removeSpacesAndLowerCase(address) : '', displayName: name || '', }; }); @@ -171,6 +171,10 @@ export class FetchMessagesByBatchesService { return participants.flat(); } + private removeSpacesAndLowerCase(email: string): string { + return email.replace(/\s/g, '').toLowerCase(); + } + async formatBatchResponsesAsGmailMessages( batchResponses: AxiosResponse[], ): Promise<{ messages: GmailMessage[]; errors: any[] }> {