Skip to content

Commit

Permalink
Merge pull request #51 from remarkablemark/feat/subscription-status-set
Browse files Browse the repository at this point in the history
feat: add subscription status set
  • Loading branch information
remarkablemark authored Aug 21, 2022
2 parents b16566f + 566afb0 commit 7d2fdc6
Show file tree
Hide file tree
Showing 9 changed files with 121 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@ This library currently supports a subset of the [Braze API endpoints](https://ww

### Subscription groups

- [ ] /subscription/status/set
- [x] /subscription/status/set
- [ ] /v2/subscription/status/set
- [ ] /subscription/status/get
- [ ] /subscription/user/status

Expand Down
8 changes: 8 additions & 0 deletions src/Braze.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type {
MessagesScheduleUpdateObject,
MessagesSendObject,
SendsIdCreateObject,
SubscriptionStatusSetObject,
TransactionalV1CampaignsSendObject,
UsersAliasObject,
UsersDeleteObject,
Expand Down Expand Up @@ -183,6 +184,13 @@ it('calls sends.id.create()', async () => {
expect(mockedRequest).toBeCalledTimes(1)
})

it('calls subscription.status.set()', async () => {
mockedRequest.mockResolvedValueOnce(response)
expect(await braze.subscription.status.set(body as SubscriptionStatusSetObject)).toBe(response)
expect(mockedRequest).toBeCalledWith(`${apiUrl}/subscription/status/set`, body, options)
expect(mockedRequest).toBeCalledTimes(1)
})

it('calls transactional.v1.campaigns.send()', async () => {
mockedRequest.mockResolvedValueOnce(response)
const campaignId = 'YOUR_CAMPAIGN_ID_HERE'
Expand Down
8 changes: 8 additions & 0 deletions src/Braze.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as campaigns from './campaigns'
import * as canvas from './canvas'
import * as messages from './messages'
import * as sends from './sends'
import * as subscription from './subscription'
import * as transactional from './transactional'
import * as users from './users'

Expand Down Expand Up @@ -94,6 +95,13 @@ export class Braze {
},
}

subscription = {
status: {
set: (body: subscription.status.SubscriptionStatusSetObject) =>
subscription.status.set(this.apiUrl, this.apiKey, body),
},
}

transactional = {
v1: {
campaigns: {
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export * from './common/types'
export * from './messages/schedule/types'
export * from './messages/types'
export * from './sends/id/types'
export * from './subscription/status/types'
export * from './transactional/v1/campaigns/types'
export * from './users/alias/types'
export * from './users/export/types'
Expand Down
1 change: 1 addition & 0 deletions src/subscription/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * as status from './status'
2 changes: 2 additions & 0 deletions src/subscription/status/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './set'
export * from './types'
52 changes: 52 additions & 0 deletions src/subscription/status/set.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { post } from '../../common/request'
import { set } from '.'
import type { SubscriptionStatusSetObject } from './types'

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

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

describe('/subscription/status/set', () => {
const apiUrl = 'https://rest.iad-01.braze.com'
const apiKey = 'apiKey'
const data = {}

it('calls SMS request with url and body', async () => {
mockedPost.mockResolvedValueOnce(data)
const body: SubscriptionStatusSetObject = {
subscription_group_id: 'subscription_group_identifier',
subscription_state: 'unsubscribed',
external_id: ['external_identifier'],
email: ['example1@email.com', 'example2@email.com'],
}
expect(await set(apiUrl, apiKey, body)).toBe(data)
expect(mockedPost).toBeCalledWith(`${apiUrl}/subscription/status/set`, body, {
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${apiKey}`,
},
})
expect(mockedPost).toBeCalledTimes(1)
})

it('calls email request with url and body', async () => {
mockedPost.mockResolvedValueOnce(data)
const body: SubscriptionStatusSetObject = {
subscription_group_id: 'subscription_group_identifier',
subscription_state: 'subscribed',
external_id: ['external_identifier'],
phone: ['+12223334444', '+11112223333'],
}
expect(await set(apiUrl, apiKey, body)).toBe(data)
expect(mockedPost).toBeCalledWith(`${apiUrl}/subscription/status/set`, body, {
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${apiKey}`,
},
})
expect(mockedPost).toBeCalledTimes(1)
})
})
27 changes: 27 additions & 0 deletions src/subscription/status/set.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { post } from '../../common/request'
import type { SubscriptionStatusSetObject } from './types'

/**
* Update user’s subscription group status.
*
* Use these endpoints to batch update the subscription state of up to 50 users on the Braze dashboard.
*
* {@link https://www.braze.com/docs/api/endpoints/subscription_groups/post_update_user_subscription_group_status/}
*
* @param apiUrl - Braze REST endpoint.
* @param apiKey - Braze API key.
* @param body - Request parameters.
* @returns - Braze response.
*/
export function set(apiUrl: string, apiKey: string, body: SubscriptionStatusSetObject) {
const options = {
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${apiKey}`,
},
}

return post(`${apiUrl}/subscription/status/set`, body, options) as Promise<{
message: 'success' | string
}>
}
20 changes: 20 additions & 0 deletions src/subscription/status/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Request body for update user’s subscription group status.
*
* {@link https://www.braze.com/docs/api/endpoints/subscription_groups/post_update_user_subscription_group_status/#request-body}
*/
export type SubscriptionStatusSetObject = SubscriptionStatusWithPhone | SubscriptionStatusWithEmail

interface SubscriptionStatus {
subscription_group_id: string
subscription_state: 'unsubscribed' | 'subscribed'
external_id: string[]
}

interface SubscriptionStatusWithPhone extends SubscriptionStatus {
phone: string[]
}

interface SubscriptionStatusWithEmail extends SubscriptionStatus {
email: string[]
}

0 comments on commit 7d2fdc6

Please sign in to comment.