Skip to content

The open alternative to Salesforce, designed for AI.

[messaging] fix participant handles with trailing spaces (#4457)

Weiko991bb09622f0
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 00000000000..59e95240f84
--- /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: '[email protected] ' },
+ ],
+ html: '',
+ text: '',
+ };
+
+ const result = service.formatAddressObjectAsParticipants(
+ addressObject,
+ 'from',
+ );
+
+ expect(result).toEqual([
+ {
+ role: 'from',
+ handle: '[email protected]',
+ displayName: 'John Doe',
+ },
+ {
+ role: 'from',
+ handle: '[email protected]',
+ 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 f9186479f02..267d0b749c2 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<any, any>[],
): Promise<{ messages: GmailMessage[]; errors: any[] }> {