Skip to content

Commit

Permalink
[Braze Cloud] Adds sync mode to new createAlias V2 action (#2385)
Browse files Browse the repository at this point in the history
* Braze syncmode for createAlias action

* Fixes build; generates types

* Splits off V2 action into it's own directory

* Updates snapshot for v1 action unit tests

* Removes unneeded git diffs

* Updates unit test with better test names and a check for the expected error message when testing if syncMode != add

* Updates generated snapshot with new unit test names
  • Loading branch information
nick-Ag authored Sep 24, 2024
1 parent 3d28a5b commit 891332d
Show file tree
Hide file tree
Showing 5 changed files with 191 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Testing snapshot for Braze's createAlias2 destination V2 action: snapshot with all possible fields 1`] = `
Object {
"user_aliases": Array [
Object {
"alias_label": "gYiVmU)MW(1BFgh^@[",
"alias_name": "gYiVmU)MW(1BFgh^@[",
"external_id": "gYiVmU)MW(1BFgh^@[",
},
],
}
`;

exports[`Testing snapshot for Braze's createAlias2 destination V2 action: snapshot with only required fields 1`] = `
Object {
"user_aliases": Array [
Object {
"alias_label": "test_label",
"alias_name": "test_name",
},
],
}
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import { createTestEvent, createTestIntegration } from '@segment/actions-core'
import { generateTestData } from '../../../../lib/test-data'
import destination from '../../index'
import nock from 'nock'

const testDestination = createTestIntegration(destination)
const actionSlug = 'createAlias2'
const destinationSlug = 'Braze'
const seedName = `${destinationSlug}#${actionSlug}`

describe(`Testing snapshot for ${destinationSlug}'s ${actionSlug} destination V2 action:`, () => {
it('fails if sync mode is not add', async () => {
const action = destination.actions[actionSlug]
const [eventData, settingsData] = generateTestData(seedName, destination, action, false)

const event = createTestEvent({
properties: eventData
})

await expect(
testDestination.testAction(actionSlug, {
event: event,
mapping: { ...event.properties, __segment_internal_sync_mode: 'upsert' },
settings: settingsData,
auth: undefined
})
).rejects.toThrowError('syncMode must be "add"')
})

it('snapshot with only required fields', async () => {
const action = destination.actions[actionSlug]
const [eventData, settingsData] = generateTestData(seedName, destination, action, true)

nock(/.*/).persist().get(/.*/).reply(200)
nock(/.*/).persist().post(/.*/).reply(200)
nock(/.*/).persist().put(/.*/).reply(200)

const event = createTestEvent({
properties: eventData
})

const responses = await testDestination.testAction(actionSlug, {
event: event,
mapping: { alias_name: 'test_name', alias_label: 'test_label', __segment_internal_sync_mode: 'add' },
settings: settingsData,
auth: undefined
})

const request = responses[0].request
const rawBody = await request.text()

try {
const json = JSON.parse(rawBody)
expect(json).toMatchSnapshot()
return
} catch (err) {
expect(rawBody).toMatchSnapshot()
}

expect(request.headers).toMatchSnapshot()
})

it('snapshot with all possible fields', async () => {
const action = destination.actions[actionSlug]
const [eventData, settingsData] = generateTestData(seedName, destination, action, false)

nock(/.*/).persist().get(/.*/).reply(200)
nock(/.*/).persist().post(/.*/).reply(200)
nock(/.*/).persist().put(/.*/).reply(200)

const event = createTestEvent({
properties: eventData
})

const responses = await testDestination.testAction(actionSlug, {
event: event,
mapping: { ...event.properties, __segment_internal_sync_mode: 'add' },
settings: settingsData,
auth: undefined
})

const request = responses[0].request
const rawBody = await request.text()

try {
const json = JSON.parse(rawBody)
expect(json).toMatchSnapshot()
return
} catch (err) {
expect(rawBody).toMatchSnapshot()
}
})
})

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import type { ActionDefinition } from '@segment/actions-core'
import { IntegrationError } from '@segment/actions-core'
import type { Settings } from '../generated-types'
import type { Payload } from './generated-types'

const action: ActionDefinition<Settings, Payload> = {
title: 'Create Alias V2',
description: 'Create new user aliases for existing identified users, or to create new unidentified users.',
defaultSubscription: 'event = "Create Alias"',
fields: {
external_id: {
label: 'External ID',
description: 'The external ID of the user to create an alias for.',
type: 'string',
allowNull: true
},
alias_name: {
label: 'Alias Name',
description: 'The alias identifier',
type: 'string',
required: true
},
alias_label: {
label: 'Alias Label',
description: 'A label indicating the type of alias',
type: 'string',
required: true
}
},
syncMode: {
description: 'Define how the records from your destination will be synced.',
label: 'How to add Aliases',
default: 'add',
choices: [{ label: 'Create Alias', value: 'add' }]
},
perform: (request, { settings, payload, syncMode }) => {
if (syncMode === 'add') {
return request(`${settings.endpoint}/users/alias/new`, {
method: 'post',
json: {
user_aliases: [
{
external_id: payload.external_id ?? undefined,
alias_name: payload.alias_name,
alias_label: payload.alias_label
}
]
}
})
}

throw new IntegrationError('syncMode must be "add"', 'Invalid syncMode', 400)
}
}

export default action
2 changes: 2 additions & 0 deletions packages/destination-actions/src/destinations/braze/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { DestinationDefinition } from '@segment/actions-core'
import type { Settings } from './generated-types'
import { DEFAULT_REQUEST_TIMEOUT, defaultValues } from '@segment/actions-core'
import createAlias from './createAlias'
import createAlias2 from './createAlias2'
import identifyUser from './identifyUser'
import trackEvent from './trackEvent'
import trackPurchase from './trackPurchase'
Expand Down Expand Up @@ -72,6 +73,7 @@ const destination: DestinationDefinition<Settings> = {
trackEvent,
trackPurchase,
createAlias,
createAlias2,
identifyUser
},
presets: [
Expand Down

0 comments on commit 891332d

Please sign in to comment.