mirror of
https://github.com/twentyhq/twenty.git
synced 2024-12-25 21:13:01 +03:00
[messaging] Fix messaging formatAddress tests (#4482)
* [messaging] Fix messaging formatAddress tests * rebase * remove unused test
This commit is contained in:
parent
94487f6737
commit
7555e7aad5
@ -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 { AxiosResponse } from 'axios';
|
||||
import { simpleParser, AddressObject } from 'mailparser';
|
||||
import { simpleParser } from 'mailparser';
|
||||
import planer from 'planer';
|
||||
|
||||
import {
|
||||
GmailMessage,
|
||||
Participant,
|
||||
} from 'src/business/modules/message/types/gmail-message';
|
||||
import { GmailMessage } from 'src/business/modules/message/types/gmail-message';
|
||||
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 { 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()
|
||||
export class FetchMessagesByBatchesService {
|
||||
@ -90,10 +88,10 @@ export class FetchMessagesByBatchesService {
|
||||
if (!from) throw new Error('From value is missing');
|
||||
|
||||
const participants = [
|
||||
...this.formatAddressObjectAsParticipants(from, 'from'),
|
||||
...this.formatAddressObjectAsParticipants(to, 'to'),
|
||||
...this.formatAddressObjectAsParticipants(cc, 'cc'),
|
||||
...this.formatAddressObjectAsParticipants(bcc, 'bcc'),
|
||||
...formatAddressObjectAsParticipants(from, 'from'),
|
||||
...formatAddressObjectAsParticipants(to, 'to'),
|
||||
...formatAddressObjectAsParticipants(cc, 'cc'),
|
||||
...formatAddressObjectAsParticipants(bcc, 'bcc'),
|
||||
];
|
||||
|
||||
let textWithoutReplyQuotations = text;
|
||||
@ -141,40 +139,6 @@ export class FetchMessagesByBatchesService {
|
||||
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(
|
||||
batchResponses: AxiosResponse<any, 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();
|
||||
};
|
Loading…
Reference in New Issue
Block a user