Skip to content

Commit

Permalink
Merge pull request #43 from remarkablemark/feat/messages-schedule-create
Browse files Browse the repository at this point in the history
feat: add messages schedule create
  • Loading branch information
remarkablemark authored Aug 20, 2022
2 parents ad2aefa + d3c4fd8 commit 55dd233
Show file tree
Hide file tree
Showing 10 changed files with 160 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ This library currently supports a subset of the [Braze API endpoints](https://ww
- [x] /canvas/trigger/schedule/create
- [x] /canvas/trigger/schedule/delete
- [x] /canvas/trigger/schedule/update
- [ ] /messages/schedule/create
- [x] /messages/schedule/create
- [ ] /messages/schedule/delete
- [ ] /messages/schedule/update
- [ ] /messages/scheduled_broadcasts
Expand Down
8 changes: 8 additions & 0 deletions src/Braze.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type {
CanvasTriggerScheduleDeleteObject,
CanvasTriggerScheduleUpdateObject,
CanvasTriggerSendObject,
MessagesScheduleCreateObject,
MessagesSendObject,
SendsIdCreateObject,
TransactionalV1CampaignsSendObject,
Expand Down Expand Up @@ -134,6 +135,13 @@ it('calls canvas.trigger.send()', async () => {
expect(mockedRequest).toBeCalledTimes(1)
})

it('calls messages.schedule.create()', async () => {
mockedRequest.mockResolvedValueOnce(response)
expect(await braze.messages.schedule.create(body as MessagesScheduleCreateObject)).toBe(response)
expect(mockedRequest).toBeCalledWith(`${apiUrl}/messages/schedule/create`, body, options)
expect(mockedRequest).toBeCalledTimes(1)
})

it('calls messages.send()', async () => {
mockedRequest.mockResolvedValueOnce(response)
expect(await braze.messages.send(body as MessagesSendObject)).toBe(response)
Expand Down
5 changes: 5 additions & 0 deletions src/Braze.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ export class Braze {
}

messages = {
schedule: {
create: (body: messages.schedule.MessagesScheduleCreateObject) =>
messages.schedule.create(this.apiUrl, this.apiKey, body),
},

send: (body: messages.MessagesSendObject) => messages.send(this.apiUrl, this.apiKey, body),
}

Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export * from './campaigns/trigger/types'
export * from './canvas/trigger/schedule/types'
export * from './canvas/trigger/types'
export * from './common/types'
export * from './messages/schedule/types'
export * from './messages/types'
export * from './sends/id/types'
export * from './transactional/v1/campaigns/types'
Expand Down
1 change: 1 addition & 0 deletions src/messages/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * as schedule from './schedule'
export * from './send'
export * from './types'
98 changes: 98 additions & 0 deletions src/messages/schedule/create.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import { post } from '../../common/request'
import { create } from '.'
import type { MessagesScheduleCreateObject } from './types'

jest.mock('../../common/request')
const mockedPost = jest.mocked(post)

beforeEach(() => {
jest.clearAllMocks()
})

describe('/messages/schedule/create', () => {
const apiUrl = 'https://rest.iad-01.braze.com'
const apiKey = 'apiKey'

const body: MessagesScheduleCreateObject = {
broadcast: false,
external_user_ids: ['external_user_identifiers'],
user_aliases: [
{
alias_name: 'example_name',
alias_label: 'example_label',
},
],
segment_id: 'segment_identifiers',
audience: {
AND: [
{
custom_attribute: {
custom_attribute_name: 'eye_color',
comparison: 'equals',
value: 'blue',
},
},
{
custom_attribute: {
custom_attribute_name: 'favorite_foods',
comparison: 'includes_value',
value: 'pizza',
},
},
{
OR: [
{
custom_attribute: {
custom_attribute_name: 'last_purchase_time',
comparison: 'less_than_x_days_ago',
value: 2,
},
},
{
push_subscription_status: {
comparison: 'is',
value: 'opted_in',
},
},
],
},
{
email_subscription_status: {
comparison: 'is_not',
value: 'subscribed',
},
},
{
last_used_app: {
comparison: 'after',
value: '2019-07-22T13:17:55+0000',
},
},
],
},
campaign_id: 'campaign_identifier',
send_id: 'send_identifier',
override_messaging_limits: false,
recipient_subscription_state: 'subscribed',
schedule: {
time: '',
in_local_time: true,
at_optimal_time: true,
},
messages: {},
}

const data = {}

it('calls request with url and body', async () => {
mockedPost.mockResolvedValueOnce(data)
expect(await create(apiUrl, apiKey, body)).toBe(data)
expect(mockedPost).toBeCalledWith(`${apiUrl}/messages/schedule/create`, body, {
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${apiKey}`,
},
})
expect(mockedPost).toBeCalledTimes(1)
})
})
29 changes: 29 additions & 0 deletions src/messages/schedule/create.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { post } from '../../common/request'
import type { MessagesScheduleCreateObject } from './types'

/**
* Create scheduled messages.
*
* Use this endpoint to send messages directly from the API.
*
* {@link https://www.braze.com/docs/api/endpoints/messaging/schedule_messages/post_schedule_messages/}
*
* @param apiUrl - Braze REST endpoint.
* @param apiKey - Braze API key.
* @param body - Request parameters.
* @returns - Braze response.
*/
export function create(apiUrl: string, apiKey: string, body: MessagesScheduleCreateObject) {
const options = {
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${apiKey}`,
},
}

return post(`${apiUrl}/messages/schedule/create`, body, options) as Promise<{
dispatch_id: string
schedule_id: string
message: 'success' | string
}>
}
2 changes: 2 additions & 0 deletions src/messages/schedule/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './create'
export * from './types'
13 changes: 13 additions & 0 deletions src/messages/schedule/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import type { ScheduleObject } from '../../common/types'
import type { MessagesSendObject } from '../types'

/**
* Request body for create scheduled messages.
*
* {@link https://www.braze.com/docs/api/endpoints/messaging/schedule_messages/post_schedule_messages/#request-body}
*/
export interface MessagesScheduleCreateObject
extends Omit<MessagesSendObject, 'override_frequency_capping'> {
override_messaging_limits?: boolean
schedule: ScheduleObject
}
7 changes: 2 additions & 5 deletions src/messages/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ConnectedAudienceObject } from '../common/types'
import type { ConnectedAudienceObject, UserAlias } from '../common/types'

/**
* Request body for sending messages immediately via API only.
Expand All @@ -8,10 +8,7 @@ import type { ConnectedAudienceObject } from '../common/types'
export interface MessagesSendObject {
broadcast?: boolean
external_user_ids?: string[]
user_aliases?: {
alias_name: string
alias_label: string
}[]
user_aliases?: UserAlias[]
segment_id?: string
audience?: ConnectedAudienceObject
campaign_id?: string
Expand Down

0 comments on commit 55dd233

Please sign in to comment.