Skip to content

Commit

Permalink
CCP-67: Make userId optional for engage sms and email (#1040)
Browse files Browse the repository at this point in the history
* make userId input optional, add thrown error for missing userid with no traits

* improve error logging, add unit tests

* changes as per comments

* add console log for test

* try removing console log and throwing other error instead

* better error handling

* try a different way of passing userId

* pass in an empty string on missing user id

* fake user id for custom args

* make userid undefined again

* make userId required false

* move integration error out of catch block

* fix failing test
  • Loading branch information
zawadzkip authored Feb 28, 2023
1 parent ff8ba87 commit 5b7678b
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,26 @@ describe.each(['stage', 'production'])('%s environment', (environment) => {

expect(sendGridRequest.isDone()).toEqual(false)
})
it('should throw error and not send email with no trait enrichment and no user id', async () => {
const mapping = getDefaultMapping({
userId: undefined,
traitEnrichment: false
})
await expect(
sendgrid.testAction('sendEmail', {
event: createMessagingTestEvent({
timestamp,
event: 'Audience Entered',
userId: undefined
}),
settings,
mapping
})
).rejects.toThrow('Unable to process email, no userId provided and trait enrichment disabled')

const sendGridRequest = nock('https://api.sendgrid.com').post('/v3/mail/send', sendgridRequestBody).reply(200, {})
expect(sendGridRequest.isDone()).toEqual(false)
})

it('should not send an email when send field not in payload', async () => {
const responses = await sendgrid.testAction('sendEmail', {
Expand Down

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
Expand Up @@ -151,7 +151,7 @@ const action: ActionDefinition<Settings, Payload> = {
label: 'User ID',
description: 'User ID in Segment',
type: 'string',
required: true,
required: false,
default: { '@path': '$.userId' }
},
toEmail: {
Expand Down Expand Up @@ -355,6 +355,13 @@ const action: ActionDefinition<Settings, Payload> = {
if (payload.traitEnrichment) {
traits = payload?.traits ? payload?.traits : JSON.parse('{}')
} else {
if (!payload.userId) {
throw new IntegrationError(
'Unable to process email, no userId provided and trait enrichment disabled',
'Invalid parameters',
400
)
}
traits = await fetchProfileTraits(request, settings, payload.userId, statsClient, tags)
}

Expand Down Expand Up @@ -430,7 +437,7 @@ const action: ActionDefinition<Settings, Payload> = {
...payload.customArgs,
source_id: settings.sourceId,
space_id: settings.spaceId,
user_id: payload.userId,
user_id: payload.userId ?? undefined,
// This is to help disambiguate in the case it's email or email_address.
__segment_internal_external_id_key__: EXTERNAL_ID_KEY,
__segment_internal_external_id_value__: profile[EXTERNAL_ID_KEY]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,23 @@ describe.each(['stage', 'production'])('%s environment', (environment) => {

expect(responses.length).toEqual(0)
})
it('should throw error with no userId and no trait enrichment', async () => {
const mapping = getDefaultMapping({
userId: undefined,
traitEnrichment: false
})
await expect(
twilio.testAction('sendSms', {
event: createMessagingTestEvent({
timestamp,
event: 'Audience Entered',
userId: 'jane'
}),
settings,
mapping
})
).rejects.toThrowError('Unable to process sms, no userId provided and no traits provided')
})

it('should send SMS', async () => {
const expectedTwilioRequest = new URLSearchParams({
Expand Down

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
Expand Up @@ -13,7 +13,6 @@ const action: ActionDefinition<Settings, Payload> = {
label: 'User ID',
description: 'User ID in Segment',
type: 'string',
required: true,
default: { '@path': '$.userId' }
},
toNumber: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class SmsMessageSender extends MessageSender<Payload> {
}

const profile = {
user_id: this.payload.userId,
user_id: this.payload.userId ?? undefined,
phone,
traits
}
Expand All @@ -55,6 +55,13 @@ export class SmsMessageSender extends MessageSender<Payload> {
}

private getProfileTraits = async () => {
if (!this.payload.userId) {
throw new IntegrationError(
'Unable to process sms, no userId provided and no traits provided',
'Invalid parameters',
400
)
}
try {
const endpoint = `https://profiles.segment.${
this.settings.profileApiEnvironment === 'production' ? 'com' : 'build'
Expand Down

0 comments on commit 5b7678b

Please sign in to comment.