-
Notifications
You must be signed in to change notification settings - Fork 256
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add profile to list in upsertProfile (#1706)
* Add profile to list in upsertProfile -> Add list Id mapping in upsertProfile action -> Add profile to list if list Id is provided -> Update test case * Update snapshot tests * Update mapping description
- Loading branch information
1 parent
d995b53
commit 37fdeb0
Showing
7 changed files
with
218 additions
and
4 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
50 changes: 50 additions & 0 deletions
50
packages/destination-actions/src/destinations/klaviyo/functions.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,50 @@ | ||
import { RequestClient, DynamicFieldResponse, APIError } from '@segment/actions-core' | ||
import { API_URL, REVISION_DATE } from './config' | ||
import { GetListResultContent } from './types' | ||
import { Settings } from './generated-types' | ||
|
||
export async function getListIdDynamicData(request: RequestClient, settings: Settings): Promise<DynamicFieldResponse> { | ||
try { | ||
const result = await request(`${API_URL}/lists/`, { | ||
method: 'get', | ||
headers: { | ||
Authorization: `Klaviyo-API-Key ${settings.api_key}`, | ||
Accept: 'application/json', | ||
revision: REVISION_DATE | ||
}, | ||
skipResponseCloning: true | ||
}) | ||
const parsedContent = JSON.parse(result.content) as GetListResultContent | ||
const choices = parsedContent.data.map((list: { id: string; attributes: { name: string } }) => { | ||
return { value: list.id, label: list.attributes.name } | ||
}) | ||
return { | ||
choices | ||
} | ||
} catch (err) { | ||
return { | ||
choices: [], | ||
nextPage: '', | ||
error: { | ||
message: (err as APIError).message ?? 'Unknown error', | ||
code: (err as APIError).status + '' ?? 'Unknown error' | ||
} | ||
} | ||
} | ||
} | ||
|
||
export async function addProfileToList(request: RequestClient, profileId: string, listId: string) { | ||
const listData = { | ||
data: [ | ||
{ | ||
type: 'profile', | ||
id: profileId | ||
} | ||
] | ||
} | ||
|
||
await request(`${API_URL}/lists/${listId}/relationships/profiles/`, { | ||
method: 'POST', | ||
json: listData | ||
}) | ||
} |
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 |
---|---|---|
|
@@ -2,6 +2,7 @@ import nock from 'nock' | |
import { IntegrationError, createTestEvent, createTestIntegration } from '@segment/actions-core' | ||
import Definition from '../../index' | ||
import { API_URL } from '../../config' | ||
import * as Functions from '../../functions' | ||
|
||
const testDestination = createTestIntegration(Definition) | ||
|
||
|
@@ -11,7 +12,16 @@ export const settings = { | |
api_key: apiKey | ||
} | ||
|
||
jest.mock('../../functions', () => ({ | ||
...jest.requireActual('../../functions'), | ||
addProfileToList: jest.fn(() => Promise.resolve()) | ||
})) | ||
|
||
describe('Upsert Profile', () => { | ||
afterEach(() => { | ||
jest.resetAllMocks() | ||
}) | ||
|
||
it('should throw error if no email, phone_number, or external_id is provided', async () => { | ||
const event = createTestEvent({ | ||
type: 'track', | ||
|
@@ -138,4 +148,118 @@ describe('Upsert Profile', () => { | |
testDestination.testAction('upsertProfile', { event, settings, useDefaultMappings: true }) | ||
).rejects.toThrowError('An error occurred while processing the request') | ||
}) | ||
|
||
it('should add a profile to a list if list_id is provided', async () => { | ||
const listId = 'abc123' | ||
const profileId = '123' | ||
const mapping = { list_id: 'abc123' } | ||
|
||
const requestBody = { | ||
data: { | ||
type: 'profile', | ||
attributes: { | ||
email: '[email protected]', | ||
first_name: 'John', | ||
last_name: 'Doe', | ||
location: {}, | ||
properties: {} | ||
} | ||
} | ||
} | ||
|
||
nock(`${API_URL}`) | ||
.post('/profiles/', requestBody) | ||
.reply( | ||
200, | ||
JSON.stringify({ | ||
data: { | ||
id: profileId | ||
} | ||
}) | ||
) | ||
|
||
nock(`${API_URL}`).post(`/lists/${listId}/relationships/profiles/`).reply(200) | ||
|
||
const event = createTestEvent({ | ||
type: 'track', | ||
userId: '123', | ||
traits: { | ||
email: '[email protected]', | ||
firstName: 'John', | ||
lastName: 'Doe', | ||
list_id: listId | ||
} | ||
}) | ||
|
||
await expect( | ||
testDestination.testAction('upsertProfile', { event, mapping, settings, useDefaultMappings: true }) | ||
).resolves.not.toThrowError() | ||
|
||
expect(Functions.addProfileToList).toHaveBeenCalledWith(expect.anything(), profileId, listId) | ||
}) | ||
|
||
it('should add an existing profile to a list if list_id is provided', async () => { | ||
const listId = 'abc123' | ||
const profileId = '123' | ||
const mapping = { list_id: 'abc123' } | ||
|
||
const requestBody = { | ||
data: { | ||
type: 'profile', | ||
attributes: { | ||
email: '[email protected]', | ||
first_name: 'John', | ||
last_name: 'Doe', | ||
location: {}, | ||
properties: {} | ||
} | ||
} | ||
} | ||
|
||
const errorResponse = JSON.stringify({ | ||
errors: [ | ||
{ | ||
meta: { | ||
duplicate_profile_id: profileId | ||
} | ||
} | ||
] | ||
}) | ||
|
||
nock(`${API_URL}`).post('/profiles/', requestBody).reply(409, errorResponse) | ||
|
||
const updateRequestBody = { | ||
data: { | ||
type: 'profile', | ||
id: profileId, | ||
attributes: { | ||
email: '[email protected]', | ||
first_name: 'John', | ||
last_name: 'Doe', | ||
location: {}, | ||
properties: {} | ||
} | ||
} | ||
} | ||
nock(`${API_URL}`).patch(`/profiles/${profileId}`, updateRequestBody).reply(200, {}) | ||
|
||
nock(`${API_URL}`).post(`/lists/${listId}/relationships/profiles/`).reply(200) | ||
|
||
const event = createTestEvent({ | ||
type: 'track', | ||
userId: '123', | ||
traits: { | ||
email: '[email protected]', | ||
firstName: 'John', | ||
lastName: 'Doe', | ||
list_id: listId | ||
} | ||
}) | ||
|
||
await expect( | ||
testDestination.testAction('upsertProfile', { event, mapping, settings, useDefaultMappings: true }) | ||
).resolves.not.toThrowError() | ||
|
||
expect(Functions.addProfileToList).toHaveBeenCalledWith(expect.anything(), profileId, listId) | ||
}) | ||
}) |
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
4 changes: 4 additions & 0 deletions
4
packages/destination-actions/src/destinations/klaviyo/upsertProfile/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