Skip to content

Commit

Permalink
Added multi-status response and unit tests for sync-mode
Browse files Browse the repository at this point in the history
  • Loading branch information
sayan-das-in committed Sep 30, 2024
1 parent b1113ba commit 69d3141
Show file tree
Hide file tree
Showing 5 changed files with 228 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -698,4 +698,206 @@ describe('MultiStatus', () => {
])
})
})

describe('syncMode', () => {
it('trackEvent2 - should return a multiStatus error response when syncMode is not set', async () => {
nock(settings.endpoint).post('/users/track').reply(201, {})

const mapping = {
name: {
'@path': '$.traits.name'
},
time: receivedAt,
email: {
'@path': '$.traits.email'
},
external_id: {
'@path': '$.traits.externalId'
},
braze_id: {
'@path': '$.traits.brazeId'
}
}

const events: SegmentEvent[] = [
// Valid Event
createTestEvent({
type: 'identify',
receivedAt,
traits: {
name: 'Example User One',
externalId: 'test-external-id-1'
}
}),
// Event without any user identifier
createTestEvent({
type: 'identify',
receivedAt,
traits: {
name: 'Example User Two',
externalId: 'test-external-id-2'
}
})
]

const response = await testDestination.executeBatch('trackEvent2', {
events,
settings,
mapping
})

expect(response).toMatchObject([
{
errormessage: 'Invalid syncMode, must be set to "add" or "update"',
errorreporter: 'DESTINATION',
errortype: 'PAYLOAD_VALIDATION_FAILED',
status: 400
},
{
errormessage: 'Invalid syncMode, must be set to "add" or "update"',
errorreporter: 'DESTINATION',
errortype: 'PAYLOAD_VALIDATION_FAILED',
status: 400
}
])
})

it('trackPurchase2 - should return a multiStatus error response when syncMode is not set', async () => {
nock(settings.endpoint).post('/users/track').reply(201, {})

const mapping = {
time: receivedAt,
external_id: {
'@path': '$.properties.externalId'
},
products: {
'@path': '$.properties.products'
}
}

const events: SegmentEvent[] = [
// Valid Event
createTestEvent({
event: 'Order Completed',
type: 'track',
receivedAt,
properties: {
externalId: 'test-external-id',
products: [
{
product_id: 'test-product-id',
currency: 'USD',
price: 99.99,
quantity: 1
},
{
product_id: 'test-product-id',
currency: 'USD',
price: 99.99,
quantity: 1
}
]
}
}),
// Event with no product
createTestEvent({
event: 'Order Completed',
type: 'track',
receivedAt,
properties: {
externalId: 'test-external-id',
products: []
}
})
]

const response = await testDestination.executeBatch('trackPurchase2', {
events,
settings,
mapping
})

expect(response).toMatchObject([
{
errormessage: 'Invalid syncMode, must be set to "add" or "update"',
errorreporter: 'DESTINATION',
errortype: 'PAYLOAD_VALIDATION_FAILED',
status: 400
},
{
errormessage: 'Invalid syncMode, must be set to "add" or "update"',
errorreporter: 'DESTINATION',
errortype: 'PAYLOAD_VALIDATION_FAILED',
status: 400
}
])
})

it('updateUserProfile2 - should return a multiStatus error response when syncMode is not set', async () => {
nock(settings.endpoint).post('/users/track').reply(201, {})

const mapping = {
first_name: {
'@path': '$.traits.firstName'
},
last_name: {
'@path': '$.traits.lastName'
},
time: receivedAt,
email: {
'@path': '$.traits.email'
},
external_id: {
'@path': '$.traits.externalId'
},
braze_id: {
'@path': '$.traits.brazeId'
}
}

const events: SegmentEvent[] = [
// Valid Event
createTestEvent({
type: 'identify',
receivedAt,
traits: {
firstName: 'Example',
lastName: 'User',
externalId: 'test-external-id-1'
}
}),
// Valid Event
createTestEvent({
type: 'identify',
receivedAt,
traits: {
firstName: 'Example',
lastName: 'User',
externalId: 'test-external-id-2'
}
})
]

const response = await testDestination.executeBatch('updateUserProfile2', {
events,
settings,
mapping
})

expect(response).toMatchObject([
{
errormessage: 'Invalid syncMode, must be set to "add" or "update"',
errorreporter: 'DESTINATION',
errortype: 'PAYLOAD_VALIDATION_FAILED',
status: 400
},
{
errormessage: 'Invalid syncMode, must be set to "add" or "update"',
errorreporter: 'DESTINATION',
errortype: 'PAYLOAD_VALIDATION_FAILED',
status: 400
}
])
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ 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'
import { sendTrackEvent, sendBatchedTrackEvent } from '../utils'
import { sendTrackEvent, sendBatchedTrackEvent, generateMultiStatusError } from '../utils'

const action: ActionDefinition<Settings, Payload> = {
title: 'Track Event V2',
Expand Down Expand Up @@ -114,7 +114,9 @@ const action: ActionDefinition<Settings, Payload> = {
if (syncMode === 'add' || syncMode === 'update') {
return sendBatchedTrackEvent(request, settings, payload, syncMode)
}
throw new IntegrationError('syncMode must be "add" or "update"', 'Invalid syncMode', 400)

// Return a multi-status error if the syncMode is invalid
return generateMultiStatusError(payload.length, 'Invalid syncMode, must be set to "add" or "update"')
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ 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'
import { sendBatchedTrackPurchase, sendTrackPurchase } from '../utils'
import { generateMultiStatusError, sendBatchedTrackPurchase, sendTrackPurchase } from '../utils'

const action: ActionDefinition<Settings, Payload> = {
title: 'Track Purchase V2',
Expand Down Expand Up @@ -132,7 +132,9 @@ const action: ActionDefinition<Settings, Payload> = {
if (syncMode === 'add' || syncMode === 'update') {
return sendBatchedTrackPurchase(request, settings, payload, syncMode)
}
throw new IntegrationError('syncMode must be "add" or "update"', 'Invalid syncMode', 400)

// Return a multi-status error if the syncMode is invalid
return generateMultiStatusError(payload.length, 'Invalid syncMode, must be set to "add" or "update"')
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { ActionDefinition } from '@segment/actions-core'
import type { Settings } from '../generated-types'
import type { Payload } from './generated-types'
import { updateUserProfile, updateBatchedUserProfile } from '../utils'
import { updateUserProfile, updateBatchedUserProfile, generateMultiStatusError } from '../utils'
import { IntegrationError } from '@segment/actions-core'

const action: ActionDefinition<Settings, Payload> = {
Expand Down Expand Up @@ -307,7 +307,9 @@ const action: ActionDefinition<Settings, Payload> = {
if (syncMode === 'update') {
return updateBatchedUserProfile(request, settings, payload, syncMode)
}
throw new IntegrationError(`Sync mode ${syncMode} is not supported`, 'Invalid syncMode', 400)

// Return a multi-status error if the syncMode is invalid
return generateMultiStatusError(payload.length, 'Invalid syncMode, must be set to "add" or "update"')
}
}

Expand Down
14 changes: 14 additions & 0 deletions packages/destination-actions/src/destinations/braze/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -591,3 +591,17 @@ async function handleBrazeAPIResponse(
function transformPayloadsType(obj: object[]) {
return obj as JSONLikeObject[]
}

export function generateMultiStatusError(batchSize: number, errorMessage: string): MultiStatusResponse {
const multiStatusResponse = new MultiStatusResponse()

for (let i = 0; i < batchSize; i++) {
multiStatusResponse.pushErrorResponse({
status: 400,
errortype: 'PAYLOAD_VALIDATION_FAILED',
errormessage: errorMessage
})
}

return multiStatusResponse
}

0 comments on commit 69d3141

Please sign in to comment.