[messaging] Fix messaging formatAddress tests (#4482)

* [messaging] Fix messaging formatAddress tests

* rebase

* remove unused test
This commit is contained in:
Weiko 2024-03-15 14:58:02 +01:00 committed by GitHub
parent 94487f6737
commit 7555e7aad5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 81 additions and 96 deletions

View File

@ -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([]);
});
});
});

View File

@ -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[] }> {

View File

@ -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([]);
});
});

View File

@ -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();
};