Skip to content

Commit

Permalink
Update MC contacts upsert to handle multiple identifiers (#2032)
Browse files Browse the repository at this point in the history
* Update MC contacts upsert to handle multiple identifiers

* Update defaults for phone_number_id, anonymous_id

* Update default for anonymous_id

* Change anonymous_id to anonymousId
  • Loading branch information
msaunders-twilio authored Jun 4, 2024
1 parent 807258a commit e99e29f
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -1,32 +1,48 @@
import { tranformValueToAcceptedDataType } from '../sendgrid-properties'
import { tranformValueToAcceptedDataType, validatePayload } from '../sendgrid-properties'

describe('tranformValueToAcceptedDataType', () => {
it('should transform a boolean value into a string', () => {
expect(tranformValueToAcceptedDataType(true)).toBe('true')
})
describe('sendgrid-properties', () => {
describe('tranformValueToAcceptedDataType', () => {
it('should transform a boolean value into a string', () => {
expect(tranformValueToAcceptedDataType(true)).toBe('true')
})

it('should transform an array value into a string', () => {
expect(tranformValueToAcceptedDataType([1, 2, 3])).toBe('[1,2,3]')
})
it('should transform an array value into a string', () => {
expect(tranformValueToAcceptedDataType([1, 2, 3])).toBe('[1,2,3]')
})

it('should transform an object value into a string', () => {
expect(tranformValueToAcceptedDataType({ a: 1 })).toBe('{"a":1}')
})
it('should transform an object value into a string', () => {
expect(tranformValueToAcceptedDataType({ a: 1 })).toBe('{"a":1}')
})

it('should transform nested arrays and objects into a string', () => {
const data = [[1, 2, 3], { a: 1, b: 2, c: { d: 3, e: ['f', 'g'] } }]
expect(tranformValueToAcceptedDataType(data)).toBe('[[1,2,3],{"a":1,"b":2,"c":{"d":3,"e":["f","g"]}}]')
})
it('should transform nested arrays and objects into a string', () => {
const data = [[1, 2, 3], { a: 1, b: 2, c: { d: 3, e: ['f', 'g'] } }]
expect(tranformValueToAcceptedDataType(data)).toBe('[[1,2,3],{"a":1,"b":2,"c":{"d":3,"e":["f","g"]}}]')
})

it('should return the value for number types', () => {
expect(tranformValueToAcceptedDataType(123)).toBe(123)
})
it('should return the value for number types', () => {
expect(tranformValueToAcceptedDataType(123)).toBe(123)
})

it('should return the value for string types', () => {
expect(tranformValueToAcceptedDataType('Hello, test')).toBe('Hello, test')
})

it('should return the value for string types', () => {
expect(tranformValueToAcceptedDataType('Hello, test')).toBe('Hello, test')
it('should return the value for date types', () => {
expect(tranformValueToAcceptedDataType('2022-11-01T00:00:00Z')).toBe('2022-11-01T00:00:00Z')
})
})

it('should return the value for date types', () => {
expect(tranformValueToAcceptedDataType('2022-11-01T00:00:00Z')).toBe('2022-11-01T00:00:00Z')
describe('validatePayload', () => {
it('should throw an error if no identifying field is included', () => {
const payload = {}
expect(() => validatePayload(payload)).toThrowError(
'Contact must have at least one identifying field included (email, phone_number_id, external_id, anonymous_id).'
)
})

it('should not throw an error if at least one identifying field is included', () => {
const payload = { anonymous_id: 'hip-hop-anonymous' }
expect(() => validatePayload(payload)).not.toThrow()
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,24 @@ const transformValuesToAcceptedDataTypes = (data: any): any => {
return tranformedData
}

// Validate the payload of each contact
export const validatePayload = (payload: Payload) => {
// Validate that 1 of the 4 identifier fields is included in the payload
if (!payload.primary_email && !payload.phone_number_id && !payload.external_id && !payload.anonymous_id) {
throw new IntegrationError(
'Contact must have at least one identifying field included (email, phone_number_id, external_id, anonymous_id).',
'Invalid value',
400
)
}
}

export const convertPayload = (payload: Payload, accountCustomFields: CustomField[]) => {
const { state, primary_email, enable_batching, customFields, ...rest } = payload

// Validate that each contact payload is correct (i.e. contains 1 of the 4 identifier fields)
validatePayload(payload)

// If there are any custom fields, convert their key from sendgrid Name to sendgrid ID if needed
const updatedCustomFields = customFields
? convertCustomFieldNamesToIds(customFields, accountCustomFields)
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 @@ -201,8 +201,8 @@ const action: ActionDefinition<Settings, Payload> = {
label: 'Email Address',
description: `The contact's email address.`,
type: 'string',
allowNull: false,
required: true,
allowNull: true,
required: false,
default: {
'@if': {
exists: { '@path': '$.traits.email' },
Expand All @@ -211,6 +211,42 @@ const action: ActionDefinition<Settings, Payload> = {
}
}
},
phone_number_id: {
label: 'Phone Number ID',
description: `The contact's Phone Number ID. This must be a valid phone number.`,
type: 'string',
allowNull: true,
required: false,
default: {
'@if': {
exists: { '@path': '$.traits.phone' },
then: { '@path': '$.traits.phone' },
else: { '@path': '$.properties.phone' }
}
}
},
external_id: {
label: 'External ID',
description: `The contact's External ID.`,
type: 'string',
allowNull: true,
required: false,
default: {
'@if': {
exists: { '@path': '$.traits.external_id' },
then: { '@path': '$.traits.external_id' },
else: { '@path': '$.properties.external_id' }
}
}
},
anonymous_id: {
label: 'Anonymous ID ',
description: `The contact's Anonymous ID.`,
type: 'string',
allowNull: true,
required: false,
default: { '@path': '$.anonymousId' }
},
customFields: customFields
},

Expand Down

0 comments on commit e99e29f

Please sign in to comment.