-
Notifications
You must be signed in to change notification settings - Fork 243
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Braze Cloud] Adds sync mode to new identifyUser V2 action (#2386)
* Braze syncmode for identifyUser action * Fixes a build error * Generates types * Splits off V2 action into it's own directory * Manyally updates updateUserProfile snapshot test to hopefully pass? * Removes unneeded git diffs * Updates unit test with better test names and a check for the expected error message when testing if syncMode != add or upsert --------- Co-authored-by: Joe Ayoub <[email protected]>
- Loading branch information
1 parent
c38b360
commit 2172e30
Showing
5 changed files
with
191 additions
and
0 deletions.
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
...ctions/src/destinations/braze/identifyUser2/__tests__/__snapshots__/snapshot.test.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`Testing snapshot for Braze's identifyUser2 destination V2 action: snapshot with all fields 1`] = ` | ||
Object { | ||
"aliases_to_identify": Array [ | ||
Object { | ||
"external_id": "O$J@g9^nDz", | ||
"user_alias": Object { | ||
"alias_label": "O$J@g9^nDz", | ||
"alias_name": "O$J@g9^nDz", | ||
}, | ||
}, | ||
], | ||
"merge_behavior": "none", | ||
} | ||
`; |
78 changes: 78 additions & 0 deletions
78
packages/destination-actions/src/destinations/braze/identifyUser2/__tests__/snapshot.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
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 = 'identifyUser2' | ||
const destinationSlug = 'Braze' | ||
const seedName = `${destinationSlug}#${actionSlug}` | ||
|
||
describe(`Testing snapshot for ${destinationSlug}'s ${actionSlug} destination V2 action:`, () => { | ||
it('fails if sync mode is delete', 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: 'delete' }, | ||
settings: settingsData, | ||
auth: undefined | ||
}) | ||
).rejects.toThrowError('syncMode must be "add" or "upsert"') | ||
}) | ||
|
||
it('fails if sync mode is update', 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: 'update' }, | ||
settings: settingsData, | ||
auth: undefined | ||
}) | ||
).rejects.toThrowError('syncMode must be "add" or "upsert"') | ||
}) | ||
|
||
it('snapshot with all 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() | ||
} | ||
}) | ||
}) |
19 changes: 19 additions & 0 deletions
19
packages/destination-actions/src/destinations/braze/identifyUser2/generated-types.ts
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
76 changes: 76 additions & 0 deletions
76
packages/destination-actions/src/destinations/braze/identifyUser2/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
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: 'Identify User V2', | ||
description: | ||
'Identifies an unidentified (alias-only) user. Use alongside the Create Alias action, or with user aliases you have already defined.', | ||
fields: { | ||
external_id: { | ||
label: 'External ID', | ||
description: 'The external ID of the user to identify.', | ||
type: 'string', | ||
required: true | ||
}, | ||
user_alias: { | ||
label: 'User Alias Object', | ||
description: | ||
'A user alias object. See [the docs](https://www.braze.com/docs/api/objects_filters/user_alias_object/).', | ||
type: 'object', | ||
required: true, | ||
properties: { | ||
alias_name: { | ||
label: 'Alias Name', | ||
type: 'string', | ||
required: true | ||
}, | ||
alias_label: { | ||
label: 'Alias Label', | ||
type: 'string', | ||
required: true | ||
} | ||
} | ||
}, | ||
merge_behavior: { | ||
label: 'Merge Behavior', | ||
description: | ||
'Sets the endpoint to merge some fields found exclusively on the anonymous user to the identified user. See [the docs](https://www.braze.com/docs/api/endpoints/user_data/post_user_identify/#request-parameters).', | ||
type: 'string', | ||
choices: [ | ||
{ value: 'none', label: 'None' }, | ||
{ value: 'merge', label: 'Merge' } | ||
] | ||
} | ||
}, | ||
syncMode: { | ||
description: 'Define how the records from your destination will be synced.', | ||
label: 'How to identify Users', | ||
default: 'add', | ||
choices: [ | ||
{ label: 'Insert User', value: 'add' }, | ||
{ label: 'Upsert User', value: 'upsert' } | ||
] | ||
}, | ||
perform: (request, { settings, payload, syncMode }) => { | ||
if (syncMode === 'add' || syncMode === 'upsert') { | ||
return request(`${settings.endpoint}/users/identify`, { | ||
method: 'post', | ||
json: { | ||
aliases_to_identify: [ | ||
{ | ||
external_id: payload.external_id, | ||
user_alias: payload.user_alias | ||
} | ||
], | ||
...(payload.merge_behavior !== undefined && { merge_behavior: payload.merge_behavior }) | ||
} | ||
}) | ||
} | ||
|
||
throw new IntegrationError('syncMode must be "add" or "upsert"', 'Invalid syncMode', 400) | ||
} | ||
} | ||
|
||
export default action |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters