mirror of
https://github.com/lingble/twenty.git
synced 2025-11-20 16:04:57 +00:00
[messaging] Fix messaging formatAddress tests (#4482)
* [messaging] Fix messaging formatAddress tests * rebase * remove unused test
This commit is contained in:
@@ -1,53 +0,0 @@
|
|||||||
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([]);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
@@ -1,16 +1,14 @@
|
|||||||
import { Injectable, Logger } from '@nestjs/common';
|
import { Injectable, Logger } from '@nestjs/common';
|
||||||
|
|
||||||
import { AxiosResponse } from 'axios';
|
import { AxiosResponse } from 'axios';
|
||||||
import { simpleParser, AddressObject } from 'mailparser';
|
import { simpleParser } from 'mailparser';
|
||||||
import planer from 'planer';
|
import planer from 'planer';
|
||||||
|
|
||||||
import {
|
import { GmailMessage } from 'src/business/modules/message/types/gmail-message';
|
||||||
GmailMessage,
|
|
||||||
Participant,
|
|
||||||
} from 'src/business/modules/message/types/gmail-message';
|
|
||||||
import { MessageQuery } from 'src/business/modules/message/types/message-or-thread-query';
|
import { MessageQuery } from 'src/business/modules/message/types/message-or-thread-query';
|
||||||
import { GmailMessageParsedResponse } from 'src/business/modules/message/types/gmail-message-parsed-response';
|
import { GmailMessageParsedResponse } from 'src/business/modules/message/types/gmail-message-parsed-response';
|
||||||
import { FetchByBatchesService } from 'src/business/modules/message/services/fetch-by-batch.service';
|
import { FetchByBatchesService } from 'src/business/modules/message/services/fetch-by-batch.service';
|
||||||
|
import { formatAddressObjectAsParticipants } from 'src/business/modules/message/services/utils/format-address-object-as-participants.util';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class FetchMessagesByBatchesService {
|
export class FetchMessagesByBatchesService {
|
||||||
@@ -90,10 +88,10 @@ export class FetchMessagesByBatchesService {
|
|||||||
if (!from) throw new Error('From value is missing');
|
if (!from) throw new Error('From value is missing');
|
||||||
|
|
||||||
const participants = [
|
const participants = [
|
||||||
...this.formatAddressObjectAsParticipants(from, 'from'),
|
...formatAddressObjectAsParticipants(from, 'from'),
|
||||||
...this.formatAddressObjectAsParticipants(to, 'to'),
|
...formatAddressObjectAsParticipants(to, 'to'),
|
||||||
...this.formatAddressObjectAsParticipants(cc, 'cc'),
|
...formatAddressObjectAsParticipants(cc, 'cc'),
|
||||||
...this.formatAddressObjectAsParticipants(bcc, 'bcc'),
|
...formatAddressObjectAsParticipants(bcc, 'bcc'),
|
||||||
];
|
];
|
||||||
|
|
||||||
let textWithoutReplyQuotations = text;
|
let textWithoutReplyQuotations = text;
|
||||||
@@ -141,40 +139,6 @@ export class FetchMessagesByBatchesService {
|
|||||||
return { messages: filteredMessages, errors };
|
return { messages: filteredMessages, errors };
|
||||||
}
|
}
|
||||||
|
|
||||||
formatAddressObjectAsArray(
|
|
||||||
addressObject: AddressObject | AddressObject[],
|
|
||||||
): AddressObject[] {
|
|
||||||
return Array.isArray(addressObject) ? addressObject : [addressObject];
|
|
||||||
}
|
|
||||||
|
|
||||||
formatAddressObjectAsParticipants(
|
|
||||||
addressObject: AddressObject | AddressObject[] | undefined,
|
|
||||||
role: 'from' | 'to' | 'cc' | 'bcc',
|
|
||||||
): Participant[] {
|
|
||||||
if (!addressObject) return [];
|
|
||||||
const addressObjects = this.formatAddressObjectAsArray(addressObject);
|
|
||||||
|
|
||||||
const participants = addressObjects.map((addressObject) => {
|
|
||||||
const emailAdresses = addressObject.value;
|
|
||||||
|
|
||||||
return emailAdresses.map((emailAddress) => {
|
|
||||||
const { name, address } = emailAddress;
|
|
||||||
|
|
||||||
return {
|
|
||||||
role,
|
|
||||||
handle: address ? this.removeSpacesAndLowerCase(address) : '',
|
|
||||||
displayName: name || '',
|
|
||||||
};
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
return participants.flat();
|
|
||||||
}
|
|
||||||
|
|
||||||
private removeSpacesAndLowerCase(email: string): string {
|
|
||||||
return email.replace(/\s/g, '').toLowerCase();
|
|
||||||
}
|
|
||||||
|
|
||||||
async formatBatchResponsesAsGmailMessages(
|
async formatBatchResponsesAsGmailMessages(
|
||||||
batchResponses: AxiosResponse<any, any>[],
|
batchResponses: AxiosResponse<any, any>[],
|
||||||
): Promise<{ messages: GmailMessage[]; errors: any[] }> {
|
): Promise<{ messages: GmailMessage[]; errors: any[] }> {
|
||||||
|
|||||||
@@ -0,0 +1,37 @@
|
|||||||
|
import { formatAddressObjectAsParticipants } from 'src/business/modules/message/services/utils/format-address-object-as-participants.util';
|
||||||
|
|
||||||
|
describe('formatAddressObjectAsParticipants', () => {
|
||||||
|
it('should format address object as participants', () => {
|
||||||
|
const addressObject = {
|
||||||
|
value: [
|
||||||
|
{ name: 'John Doe', address: 'john.doe @example.com' },
|
||||||
|
{ name: 'Jane Smith', address: 'jane.smith@example.com ' },
|
||||||
|
],
|
||||||
|
html: '',
|
||||||
|
text: '',
|
||||||
|
};
|
||||||
|
|
||||||
|
const result = 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 = formatAddressObjectAsParticipants(addressObject, 'to');
|
||||||
|
|
||||||
|
expect(result).toEqual([]);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
import { AddressObject } from 'mailparser';
|
||||||
|
|
||||||
|
import { Participant } from 'src/business/modules/message/types/gmail-message';
|
||||||
|
|
||||||
|
const formatAddressObjectAsArray = (
|
||||||
|
addressObject: AddressObject | AddressObject[],
|
||||||
|
): AddressObject[] => {
|
||||||
|
return Array.isArray(addressObject) ? addressObject : [addressObject];
|
||||||
|
};
|
||||||
|
|
||||||
|
const removeSpacesAndLowerCase = (email: string): string => {
|
||||||
|
return email.replace(/\s/g, '').toLowerCase();
|
||||||
|
};
|
||||||
|
|
||||||
|
export const formatAddressObjectAsParticipants = (
|
||||||
|
addressObject: AddressObject | AddressObject[] | undefined,
|
||||||
|
role: 'from' | 'to' | 'cc' | 'bcc',
|
||||||
|
): Participant[] => {
|
||||||
|
if (!addressObject) return [];
|
||||||
|
const addressObjects = formatAddressObjectAsArray(addressObject);
|
||||||
|
|
||||||
|
const participants = addressObjects.map((addressObject) => {
|
||||||
|
const emailAdresses = addressObject.value;
|
||||||
|
|
||||||
|
return emailAdresses.map((emailAddress) => {
|
||||||
|
const { name, address } = emailAddress;
|
||||||
|
|
||||||
|
return {
|
||||||
|
role,
|
||||||
|
handle: address ? removeSpacesAndLowerCase(address) : '',
|
||||||
|
displayName: name || '',
|
||||||
|
};
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
return participants.flat();
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user