Skip to content

Commit

Permalink
Implement new purge queues strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
asein-sinch committed Nov 14, 2024
1 parent 415148c commit 82c4616
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { getMailgunDomainFromConfig, initMailgunService, printFullResponse } from '../../config';
import { MailgunStorageRegion } from '@sinch/sdk-client';

(async () => {
console.log('*********************');
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
SendEmailRequest,
SendMimeEmailRequest,
} from '../../../models';
import { MailgunStorageRegion } from '@sinch/sdk-client';

export class EmailsApiFixture implements Partial<Readonly<EmailsApi>> {
/**
Expand All @@ -24,7 +25,7 @@ export class EmailsApiFixture implements Partial<Readonly<EmailsApi>> {
/**
* Fixture associated to function purgeDomainQueues
*/
public purgeDomainQueues: jest.Mock<Promise<GenericResponse>, [string]> = jest.fn();
public purgeDomainQueues: jest.Mock<Promise<GenericResponse>, [string, MailgunStorageRegion]> = jest.fn();
/**
* Fixture associated to function getSendingQueuesStatus
*/
Expand Down
32 changes: 28 additions & 4 deletions packages/mailgun/src/rest/v1/emails/emails-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -29,7 +35,7 @@ export class EmailsApi extends MailgunDomainApi {
*/
constructor(sinchClientParameters: SinchClientParameters) {
super(sinchClientParameters, 'EmailsApi');
this.storageHostnames = MAILGUN_STORAGE_HOSTNAMES;
this.storageHostnames = [];
}

/**
Expand Down Expand Up @@ -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<GenericResponse> {
const requests = this.storageHostnames.map((hostname) =>
public async purgeDomainQueues(domainName: string, storageRegion: MailgunStorageRegion): Promise<GenericResponse> {
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 };
Expand Down
9 changes: 5 additions & 4 deletions packages/mailgun/tests/rest/v1/emails/emails-api.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MailgunCredentials } from '@sinch/sdk-client';
import { MailgunCredentials, MailgunStorageRegion } from '@sinch/sdk-client';
import { EmailsApi, EmailsApiFixture } from '../../../../src';
import {
sendEmailRequestWithHtml,
Expand Down Expand Up @@ -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';
Expand All @@ -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);
});
});

Expand Down
2 changes: 1 addition & 1 deletion packages/mailgun/tests/rest/v1/emails/emails.steps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
4 changes: 3 additions & 1 deletion packages/sdk-client/src/domain/domain-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
11 changes: 11 additions & 0 deletions packages/sdk-client/src/domain/domain-interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

0 comments on commit 82c4616

Please sign in to comment.