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
27 changes: 15 additions & 12 deletions lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,19 +226,22 @@ export class APIClient {
* @param recipients Recipient selector. See above.
* @returns The parsed JSON response body.
*/
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));
triggerBroadcast(broadcastId: string | number, data: RequestData = {}, recipients: Recipients = {}) {
let payload: Record<string, unknown>;
const hasRecipients = Object.keys(recipients).length > 0;

if (hasRecipients) {
const customRecipientField = (
Object.keys(BROADCASTS_ALLOWED_RECIPIENT_FIELDS) as BroadcastsAllowedRecipientFieldsKeys[]
).find((field) => recipients[field]);

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

return this.request.post(`${this.apiRoot}/campaigns/${encodeURIComponent(broadcastId)}/triggers`, payload);
Expand Down
50 changes: 50 additions & 0 deletions test/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,56 @@ test('#triggerBroadcast discards extraneous fields', (t) => {
);
});

test('#triggerBroadcast works with no data and no 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 (no 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 with empty recipients object', (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 undefined data and undefined recipients', (t) => {
sinon.stub(t.context.client.request, 'post');
t.context.client.triggerBroadcast(1, undefined, undefined);
t.truthy(
(t.context.client.request.post as SinonStub).calledWith(`${RegionUS.apiUrl}/campaigns/1/triggers`, {
data: {},
}),
);
});

test('#triggerBroadcast works with empty data and empty 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('#listExports: success', (t) => {
sinon.stub(t.context.client.request, 'get');
t.context.client.listExports();
Expand Down