Skip to content

Commit

Permalink
DEVEXP-519: E2E SMS/Webhooks
Browse files Browse the repository at this point in the history
  • Loading branch information
asein-sinch committed Aug 22, 2024
1 parent dc3e7cd commit d276a51
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
6 changes: 6 additions & 0 deletions examples/webhooks/src/services/sms-event.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ export class SmsEventService {
console.log(`:: INCOMING EVENT :: ${event.type}`);
if (event.type === 'delivery_report_sms' || event.type === 'delivery_report_mms') {
return this.handleDeliveryReportEvent(event as Sms.DeliveryReport);
} else if (event.type === 'recipient_delivery_report_sms' || event.type === 'recipient_delivery_report_mms') {
return this.handleRecipientDeliveryReportEvent(event as Sms.RecipientDeliveryReport);
} else if (event.type === 'mo_text') {
return this.handleSmsEvent(event as Sms.MOText);
} else if (event.type === 'mo_binary') {
Expand All @@ -21,6 +23,10 @@ export class SmsEventService {
console.log(`The batch ${event.batch_id} has the following statuses:\n${event.statuses.map((status) => ' - \'' + status.status + '\' for the recipients: ' + status.recipients?.join(', ')).join('\n')} `);
}

private handleRecipientDeliveryReportEvent(event: Sms.RecipientDeliveryReport): void {
console.log(`The batch ${event.batch_id} has the status "${event.status}" (code: ${event.code}) for the recipient ${event.recipient}`);
}

private handleSmsEvent(event: Sms.MOText): void {
console.log(`A SMS sent by ${event.from} has been received by ${event.to} at ${event.received_at}. The content is:\n"${event.body}"`);
}
Expand Down
84 changes: 84 additions & 0 deletions packages/sms/tests/rest/v1/callbacks/webhooks-events.steps.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { Given, Then, When } from '@cucumber/cucumber';
import { SmsCallbackWebhooks, SmsCallback, Sms } from '../../../../src';
import assert from 'assert';

let smsCallbackWebhook: SmsCallbackWebhooks;
let rawEvent: any;
let event: SmsCallback;

const processEvent = async (response: Response) => {
rawEvent = await response.text();
event = smsCallbackWebhook.parseEvent(JSON.parse(rawEvent));
};

Given('the SMS Webhooks handler is available', () => {
smsCallbackWebhook = new SmsCallbackWebhooks();
});

When('I send a request to trigger an "incoming SMS" event', async () => {
const response = await fetch('http://localhost:3017/webhooks/sms/incoming-sms');
await processEvent(response);
});

Then('the SMS event describes an "incoming SMS" event', () => {
const incomingSmsEvent = event as Sms.MOText;
assert.equal(incomingSmsEvent.id, '01W4FFL35P4NC4K35SMSBATCH8');
assert.equal(incomingSmsEvent.from, '12015555555');
assert.equal(incomingSmsEvent.to, '12017777777');
assert.equal(incomingSmsEvent.body, 'Hello John! 👋');
assert.equal(incomingSmsEvent.type, 'mo_text');
assert.equal(incomingSmsEvent.operator_id, '311071');
assert.deepEqual(incomingSmsEvent.received_at, new Date('2024-06-06T07:52:37.386Z'));
});

When('I send a request to trigger an "SMS delivery report" event', async () => {
const response = await fetch('http://localhost:3017/webhooks/sms/delivery-report-sms');
await processEvent(response);
});

Then('the SMS event describes an "SMS delivery report" event', () => {
const deliveryReportEvent = event as Sms.DeliveryReport;
assert.equal(deliveryReportEvent.batch_id, '01W4FFL35P4NC4K35SMSBATCH8');
assert.equal(deliveryReportEvent.client_reference, 'client-ref');
assert.ok(deliveryReportEvent.statuses);
const status = deliveryReportEvent.statuses[0];
assert.equal(status.code, 0);
assert.equal(status.count, 2);
const deliveryStatus: Sms.DeliveryReportStatusEnum = 'Delivered';
assert.equal(status.status, deliveryStatus);
assert.ok(status.recipients);
assert.equal(status.recipients[0], '12017777777');
assert.equal(status.recipients[1], '33612345678');
assert.equal(deliveryReportEvent.type, 'delivery_report_sms');
});

// eslint-disable-next-line max-len
When('I send a request to trigger an "SMS recipient delivery report" event with the status {string}', async (status: string) => {
const response = await fetch(`http://localhost:3017/webhooks/sms/recipient-delivery-report-sms-${status.toLowerCase()}`);
await processEvent(response);
});

Then('the SMS event describes an SMS recipient delivery report event with the status "Delivered"', () => {
const recipientDeliveryReportEvent = event as Sms.RecipientDeliveryReport;
assert.equal(recipientDeliveryReportEvent.batch_id, '01W4FFL35P4NC4K35SMSBATCH9');
assert.equal(recipientDeliveryReportEvent.recipient, '12017777777');
assert.equal(recipientDeliveryReportEvent.code, 0);
const deliveryStatus: Sms.DeliveryReportStatusEnum = 'Delivered';
assert.equal(recipientDeliveryReportEvent.status, deliveryStatus);
assert.equal(recipientDeliveryReportEvent.type, 'recipient_delivery_report_sms');
assert.equal(recipientDeliveryReportEvent.client_reference, 'client-ref');
assert.deepEqual(recipientDeliveryReportEvent.at, new Date('2024-06-06T08:17:19.210Z'));
assert.deepEqual(recipientDeliveryReportEvent.operator_status_at, new Date('2024-06-06T08:17:00Z'));
});

Then('the SMS event describes an SMS recipient delivery report event with the status "Aborted"', () => {
const recipientDeliveryReportEvent = event as Sms.RecipientDeliveryReport;
assert.equal(recipientDeliveryReportEvent.batch_id, '01W4FFL35P4NC4K35SMSBATCH9');
assert.equal(recipientDeliveryReportEvent.recipient, '12010000000');
assert.equal(recipientDeliveryReportEvent.code, 412);
const deliveryStatus: Sms.DeliveryReportStatusEnum = 'Aborted';
assert.equal(recipientDeliveryReportEvent.status, deliveryStatus);
assert.equal(recipientDeliveryReportEvent.type, 'recipient_delivery_report_sms');
assert.equal(recipientDeliveryReportEvent.client_reference, 'client-ref');
assert.deepEqual(recipientDeliveryReportEvent.at, new Date('2024-06-06T08:17:15.603Z'));
});

0 comments on commit d276a51

Please sign in to comment.