From 82c4616215bf840d5030c759f7a8b6f527583f59 Mon Sep 17 00:00:00 2001 From: Antoine SEIN <142824551+asein-sinch@users.noreply.github.com> Date: Thu, 14 Nov 2024 22:57:03 +0100 Subject: [PATCH] Implement new purge queues strategy --- .../src/mailgun/emails/purgeDomainQueues.ts | 3 +- .../rest/v1/emails/emails-api.jest.fixture.ts | 3 +- .../mailgun/src/rest/v1/emails/emails-api.ts | 32 ++++++++++++++++--- .../tests/rest/v1/emails/emails-api.test.ts | 9 +++--- .../tests/rest/v1/emails/emails.steps.ts | 2 +- .../sdk-client/src/domain/domain-helper.ts | 4 ++- .../sdk-client/src/domain/domain-interface.ts | 11 +++++++ 7 files changed, 52 insertions(+), 12 deletions(-) diff --git a/examples/simple-examples/src/mailgun/emails/purgeDomainQueues.ts b/examples/simple-examples/src/mailgun/emails/purgeDomainQueues.ts index 524ea4b..130cb70 100644 --- a/examples/simple-examples/src/mailgun/emails/purgeDomainQueues.ts +++ b/examples/simple-examples/src/mailgun/emails/purgeDomainQueues.ts @@ -1,4 +1,5 @@ import { getMailgunDomainFromConfig, initMailgunService, printFullResponse } from '../../config'; +import { MailgunStorageRegion } from '@sinch/sdk-client'; (async () => { console.log('*********************'); @@ -10,7 +11,7 @@ import { getMailgunDomainFromConfig, initMailgunService, printFullResponse } fro const mailgunService = initMailgunService(); let response; try { - response = await mailgunService.emails.purgeDomainQueues(domainName); + response = await mailgunService.emails.purgeDomainQueues(domainName, MailgunStorageRegion.US); } catch (error) { console.error('Error when trying to purge the domain queues'); throw error; diff --git a/packages/mailgun/src/rest/v1/emails/emails-api.jest.fixture.ts b/packages/mailgun/src/rest/v1/emails/emails-api.jest.fixture.ts index 309f793..ab2ec6e 100644 --- a/packages/mailgun/src/rest/v1/emails/emails-api.jest.fixture.ts +++ b/packages/mailgun/src/rest/v1/emails/emails-api.jest.fixture.ts @@ -7,6 +7,7 @@ import { SendEmailRequest, SendMimeEmailRequest, } from '../../../models'; +import { MailgunStorageRegion } from '@sinch/sdk-client'; export class EmailsApiFixture implements Partial> { /** @@ -24,7 +25,7 @@ export class EmailsApiFixture implements Partial> { /** * Fixture associated to function purgeDomainQueues */ - public purgeDomainQueues: jest.Mock, [string]> = jest.fn(); + public purgeDomainQueues: jest.Mock, [string, MailgunStorageRegion]> = jest.fn(); /** * Fixture associated to function getSendingQueuesStatus */ diff --git a/packages/mailgun/src/rest/v1/emails/emails-api.ts b/packages/mailgun/src/rest/v1/emails/emails-api.ts index 647478d..5bb5434 100644 --- a/packages/mailgun/src/rest/v1/emails/emails-api.ts +++ b/packages/mailgun/src/rest/v1/emails/emails-api.ts @@ -16,7 +16,13 @@ import { transformSendingQueuesStatusResponseIntoClientResponse, transformSendMimeEmailRequestIntoApiRequestBody, } from '../../../models'; -import { RequestBody, SinchClientParameters, MAILGUN_STORAGE_HOSTNAMES } from '@sinch/sdk-client'; +import { + RequestBody, + SinchClientParameters, + MailgunStorageRegion, + MAILGUN_STORAGE_HOSTNAMES_US, + MAILGUN_STORAGE_HOSTNAMES_EUROPE, +} from '@sinch/sdk-client'; import { MailgunDomainApi } from '../mailgun-domain-api'; export class EmailsApi extends MailgunDomainApi { @@ -29,7 +35,7 @@ export class EmailsApi extends MailgunDomainApi { */ constructor(sinchClientParameters: SinchClientParameters) { super(sinchClientParameters, 'EmailsApi'); - this.storageHostnames = MAILGUN_STORAGE_HOSTNAMES; + this.storageHostnames = []; } /** @@ -123,9 +129,27 @@ export class EmailsApi extends MailgunDomainApi { * * The storage hosts are `storage-us-east4.api.mailgun.net`, `storage-us-west1.api.mailgun.net`, and `storage-europe-west1.api.mailgun.net`. * @param { string } domainName - The name of the domain you want to delete envelope from + * @param { MailgunStorageRegion } storageRegion - The region where the domain is defined (us or europe) */ - public async purgeDomainQueues(domainName: string): Promise { - const requests = this.storageHostnames.map((hostname) => + public async purgeDomainQueues(domainName: string, storageRegion: MailgunStorageRegion): Promise { + let storagesToPurge: string[]; + if (storageRegion === MailgunStorageRegion.US) { + storagesToPurge = [ + ...MAILGUN_STORAGE_HOSTNAMES_US, + ...this.storageHostnames, + ]; + } else if (storageRegion === MailgunStorageRegion.EUROPE) { + storagesToPurge = [ + ...MAILGUN_STORAGE_HOSTNAMES_EUROPE, + ...this.storageHostnames, + ]; + } else { + console.warn(`Trying to purge the queues for the domain '${domainName}' on an unsupported region: '${storageRegion}'`); + storagesToPurge = [ + ...this.storageHostnames, + ]; + } + const requests = storagesToPurge.map((hostname) => this.purgeStorageQueue(hostname, domainName) .then((response) => { return { hostname, response }; diff --git a/packages/mailgun/tests/rest/v1/emails/emails-api.test.ts b/packages/mailgun/tests/rest/v1/emails/emails-api.test.ts index 818f021..6fb4a31 100644 --- a/packages/mailgun/tests/rest/v1/emails/emails-api.test.ts +++ b/packages/mailgun/tests/rest/v1/emails/emails-api.test.ts @@ -1,4 +1,4 @@ -import { MailgunCredentials } from '@sinch/sdk-client'; +import { MailgunCredentials, MailgunStorageRegion } from '@sinch/sdk-client'; import { EmailsApi, EmailsApiFixture } from '../../../../src'; import { sendEmailRequestWithHtml, @@ -56,7 +56,7 @@ describe('EmailsApi', () => { }); }); - describe('getEmail', () => { + describe('getStoredEmail', () => { it('should make a GET request to retrieve an email', async () => { // Given const domainName: string = 'domainName'; @@ -77,15 +77,16 @@ describe('EmailsApi', () => { it('should make a DELETE request to purge the domain queues', async () => { // Given const domainName: string = 'domainName'; + const storageRegion = MailgunStorageRegion.US; // When fixture.purgeDomainQueues.mockResolvedValue(genericResponse); emailsApi.purgeDomainQueues = fixture.purgeDomainQueues; - const response = await emailsApi.purgeDomainQueues(domainName); + const response = await emailsApi.purgeDomainQueues(domainName, storageRegion); // Then expect(response).toEqual(genericResponse); - expect(fixture.purgeDomainQueues).toHaveBeenCalledWith(domainName); + expect(fixture.purgeDomainQueues).toHaveBeenCalledWith(domainName, storageRegion); }); }); diff --git a/packages/mailgun/tests/rest/v1/emails/emails.steps.ts b/packages/mailgun/tests/rest/v1/emails/emails.steps.ts index 5082e89..29d815e 100644 --- a/packages/mailgun/tests/rest/v1/emails/emails.steps.ts +++ b/packages/mailgun/tests/rest/v1/emails/emails.steps.ts @@ -108,7 +108,7 @@ Then('the response contains the sending queues status', () => { When('I send a request to purge the domain queues', async () => { emailsApi.setStorageHostnames(['http://localhost:3021']); - purgeDomainQueuesResponse = await emailsApi.purgeDomainQueues(domainName); + purgeDomainQueuesResponse = await emailsApi.purgeDomainQueues(domainName, 'anyRegion'); }); Then('the response indicates the purge has been done', () => { diff --git a/packages/sdk-client/src/domain/domain-helper.ts b/packages/sdk-client/src/domain/domain-helper.ts index b2622ea..c65dbf0 100644 --- a/packages/sdk-client/src/domain/domain-helper.ts +++ b/packages/sdk-client/src/domain/domain-helper.ts @@ -6,9 +6,11 @@ export const CONVERSATION_TEMPLATES_HOSTNAME = `https://${REGION_PATTERN}templat export const ELASTIC_SIP_TRUNKING_HOSTNAME = 'https://elastic-trunking.api.sinch.com'; export const FAX_HOSTNAME = `https://${REGION_PATTERN}fax.api.sinch.com`; export const MAILGUN_HOSTNAME = `https://api.${REGION_PATTERN}mailgun.net`; -export const MAILGUN_STORAGE_HOSTNAMES = [ +export const MAILGUN_STORAGE_HOSTNAMES_US = [ 'https://storage-us-east4.api.mailgun.net', 'https://storage-us-west1.api.mailgun.net', +]; +export const MAILGUN_STORAGE_HOSTNAMES_EUROPE = [ 'https://storage-europe-west1.api.mailgun.net', ]; export const NUMBERS_HOSTNAME = 'https://numbers.api.sinch.com'; diff --git a/packages/sdk-client/src/domain/domain-interface.ts b/packages/sdk-client/src/domain/domain-interface.ts index 5be3e24..e0d714a 100644 --- a/packages/sdk-client/src/domain/domain-interface.ts +++ b/packages/sdk-client/src/domain/domain-interface.ts @@ -181,3 +181,14 @@ export type MailgunRegion = SupportedMailgunRegion | string; export const MailgunRegion = { ...SupportedMailgunRegion, }; + +export enum SupportedMailgunStorageRegion { + US, + EUROPE +} + +export type MailgunStorageRegion = SupportedMailgunStorageRegion | string; + +export const MailgunStorageRegion = { + ...SupportedMailgunStorageRegion, +};