forked from segmentio/action-destinations
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Sendgrid Audiences] - adding user attribute and custom field support (…
…segmentio#2604) * adding user attribute support * saving progress * adding file * adding support for custom_fields * updating description for custom field * coercing all user attributes to strings * updating types
- Loading branch information
1 parent
2d219c9
commit b31888c
Showing
8 changed files
with
332 additions
and
2 deletions.
There are no files selected for viewing
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
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 |
---|---|---|
|
@@ -61,6 +61,17 @@ const mapping = { | |
anonymous_id: { '@path': '$.anonymousId' }, | ||
external_id: { '@path': '$.traits.external_id' }, | ||
phone_number_id: { '@path': '$.traits.phone' }, | ||
user_attributes: { | ||
first_name: { '@path': '$.traits.first_name' }, | ||
last_name: { '@path': '$.traits.last_name' }, | ||
address_line_1: { '@path': '$.traits.street' }, | ||
address_line_2: { '@path': '$.traits.address_line_2' }, | ||
city: { '@path': '$.traits.city' }, | ||
state_province_region: { '@path': '$.traits.state' }, | ||
country: { '@path': '$.traits.country' }, | ||
postal_code: { '@path': '$.traits.postal_code' } | ||
}, | ||
custom_fields: { '@path': '$.traits.custom_fields' }, | ||
enable_batching: true, | ||
batch_size: 200 | ||
} | ||
|
@@ -97,6 +108,65 @@ describe('SendgridAudiences.syncAudience', () => { | |
expect(responses[0].status).toBe(200) | ||
}) | ||
|
||
it('should upsert a single Contact with user attributes and custom fields, and add it to a Sendgrid list correctly', async () => { | ||
const event = createTestEvent({ | ||
...addPayload, | ||
traits: { | ||
...addPayload.traits, | ||
first_name: 'fname', | ||
last_name: 'lname', | ||
street: '123 Main St', | ||
address_line_2: 123456, // should be stringified | ||
city: 'SF', | ||
state: 'CA', | ||
country: 'US', | ||
postal_code: "N88EU", | ||
custom_fields: { | ||
custom_field_1: 'custom_field_1_value', | ||
custom_field_2: 2345, | ||
custom_field_3: '2024-01-01T00:00:00.000Z', | ||
custom_field_4: false, // should be removed | ||
custom_field_5: null // should be removed | ||
} | ||
} | ||
}) | ||
|
||
const addExpectedPayloadWithAttributes = { | ||
...addExpectedPayload, | ||
contacts: [ | ||
{ | ||
email: '[email protected]', | ||
external_id: 'some_external_id', | ||
phone_number_id: '+353123456789', | ||
anonymous_id: 'some_anonymous_id', | ||
first_name: 'fname', | ||
last_name: 'lname', | ||
address_line_1: '123 Main St', | ||
address_line_2: '123456', | ||
city: 'SF', | ||
state_province_region: 'CA', | ||
country: 'US', | ||
postal_code: "N88EU", | ||
custom_fields: { | ||
custom_field_1: 'custom_field_1_value', | ||
custom_field_2: 2345, | ||
custom_field_3: '2024-01-01T00:00:00.000Z' | ||
} | ||
} | ||
] | ||
} | ||
|
||
nock('https://api.sendgrid.com').put('/v3/marketing/contacts', addExpectedPayloadWithAttributes).reply(200, {}) | ||
const responses = await testDestination.testAction('syncAudience', { | ||
event, | ||
settings, | ||
useDefaultMappings: true, | ||
mapping | ||
}) | ||
expect(responses.length).toBe(1) | ||
expect(responses[0].status).toBe(200) | ||
}) | ||
|
||
it('should upsert a single Contact with just email, and add it to a Sendgrid list correctly', async () => { | ||
const addPayloadCopy = JSON.parse(JSON.stringify(addPayload)) | ||
|
||
|
56 changes: 56 additions & 0 deletions
56
...es/destination-actions/src/destinations/sendgrid-audiences/syncAudience/dynamic-fields.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,56 @@ | ||
|
||
import { RequestClient } from '@segment/actions-core' | ||
import { DynamicFieldResponse } from '@segment/actions-core' | ||
import { GET_CUSTOM_FIELDS_URL } from '../constants' | ||
import { Payload } from './generated-types' | ||
|
||
export async function dynamicCustomFields(request: RequestClient, payload: Payload): Promise<DynamicFieldResponse> { | ||
interface ResultItem { | ||
id: string | ||
name: string | ||
field_type: string | ||
} | ||
interface ResponseType { | ||
data: { | ||
custom_fields: Array<ResultItem> | ||
} | ||
} | ||
interface ResultError { | ||
response: { | ||
data: { | ||
errors: Array<{ message: string }> | ||
}, | ||
status: number | ||
} | ||
} | ||
|
||
try { | ||
const response: ResponseType = await request(GET_CUSTOM_FIELDS_URL, { | ||
method: 'GET', | ||
skipResponseCloning: true | ||
}) | ||
|
||
const allFields = response.data.custom_fields.map(field => field.name) | ||
const selectedFields = new Set(Object.keys(payload.custom_fields ?? {})) | ||
const availableFields = allFields.filter(field => !selectedFields.has(field)) | ||
|
||
return { | ||
choices: availableFields.map( | ||
fieldName => ({ | ||
label: fieldName, | ||
value: fieldName | ||
}) | ||
) | ||
} | ||
|
||
} catch (err) { | ||
const error = err as ResultError | ||
return { | ||
choices: [], | ||
error: { | ||
message: error.response.data.errors.map((e) => e.message).join(';') ?? 'Unknown error: dynamicCustomFields', | ||
code: String(error.response.status) | ||
} | ||
} | ||
} | ||
} |
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
43 changes: 43 additions & 0 deletions
43
...s/destination-actions/src/destinations/sendgrid-audiences/syncAudience/generated-types.ts
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
Oops, something went wrong.