Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,19 +226,21 @@ export class APIClient {
* @param recipients Recipient selector. See above.
* @returns The parsed JSON response body.
*/
triggerBroadcast(broadcastId: string | number, data: RequestData, recipients: Recipients) {
triggerBroadcast(broadcastId: string | number, data: RequestData = {}, recipients: Recipients = {}) {
let payload = {};
let customRecipientField = (
Object.keys(BROADCASTS_ALLOWED_RECIPIENT_FIELDS) as BroadcastsAllowedRecipientFieldsKeys[]
).find((field) => recipients[field]);

if (customRecipientField) {
payload = Object.assign({ data }, filterRecipientsDataForField(recipients, customRecipientField));
} else {
} else if (Object.keys(recipients).length > 0) {
payload = {
data,
recipients,
};
} else {
payload = { data };
}

return this.request.post(`${this.apiRoot}/campaigns/${encodeURIComponent(broadcastId)}/triggers`, payload);
Expand Down
30 changes: 30 additions & 0 deletions test/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,36 @@ test('#sendEmail: error', async (t) => {
t.truthy((t.context.client.request.post as SinonStub).calledWith(`${RegionUS.apiUrl}/send/email`, req.message));
});

test('#triggerBroadcast works with no data or recipients', (t) => {
sinon.stub(t.context.client.request, 'post');
t.context.client.triggerBroadcast(1);
t.truthy(
(t.context.client.request.post as SinonStub).calledWith(`${RegionUS.apiUrl}/campaigns/1/triggers`, {
data: {},
}),
);
});

test('#triggerBroadcast works with data only', (t) => {
sinon.stub(t.context.client.request, 'post');
t.context.client.triggerBroadcast(1, { type: 'data' });
t.truthy(
(t.context.client.request.post as SinonStub).calledWith(`${RegionUS.apiUrl}/campaigns/1/triggers`, {
data: { type: 'data' },
}),
);
});

test('#triggerBroadcast works with empty recipients', (t) => {
sinon.stub(t.context.client.request, 'post');
t.context.client.triggerBroadcast(1, { type: 'data' }, {});
t.truthy(
(t.context.client.request.post as SinonStub).calledWith(`${RegionUS.apiUrl}/campaigns/1/triggers`, {
data: { type: 'data' },
}),
);
});

test('#triggerBroadcast works', (t) => {
sinon.stub(t.context.client.request, 'post');
t.context.client.triggerBroadcast(1, { type: 'data' }, { type: 'recipients' });
Expand Down