Skip to content
Open
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import type { IntegrationError, ActionDefinition } from '@segment/actions-core'
import type { ActionDefinition } from '@segment/actions-core'
import type { Settings } from '../generated-types'
import type { Payload } from './generated-types'
import { external_id, lookup_field, data, enable_batching, batch_size, event_name } from '../properties'
import { addToList, addToListBatch, createList, getList } from '../functions'
import { addToList, addToListBatch } from '../functions'
import { retlOnMappingSaveHook } from '../retlOnMappingSaveHook'

const action: ActionDefinition<Settings, Payload> = {
title: 'Add to List',
Expand All @@ -17,80 +18,7 @@ const action: ActionDefinition<Settings, Payload> = {
event_name: { ...event_name }
},
hooks: {
retlOnMappingSave: {
label: 'Connect to a static list in Marketo',
description: 'When saving this mapping, we will create a static list in Marketo using the fields you provided.',
inputFields: {
list_id: {
type: 'string',
label: 'Existing List ID',
description:
'The ID of the Marketo Static List that users will be synced to. If defined, we will not create a new list.',
required: false
},
list_name: {
type: 'string',
label: 'List Name',
description: 'The name of the Marketo Static List that you would like to create.',
required: false
}
},
outputTypes: {
id: {
type: 'string',
label: 'ID',
description: 'The ID of the created Marketo Static List that users will be synced to.',
required: false
},
name: {
type: 'string',
label: 'List Name',
description: 'The name of the created Marketo Static List that users will be synced to.',
required: false
}
},
performHook: async (request, { settings, hookInputs, statsContext }) => {
if (hookInputs.list_id) {
try {
return getList(request, settings, hookInputs.list_id)
} catch (e) {
const message = (e as IntegrationError).message || JSON.stringify(e) || 'Failed to get list'
const code = (e as IntegrationError).code || 'GET_LIST_FAILURE'
return {
error: {
message,
code
}
}
}
}

try {
const input = {
audienceName: hookInputs.list_name,
settings: settings
}
const listId = await createList(request, input, statsContext)

return {
successMessage: `List '${hookInputs.list_name}' (id: ${listId}) created successfully!`,
savedData: {
id: listId,
name: hookInputs.list_name
}
}
} catch (e) {
const message = (e as IntegrationError).message || JSON.stringify(e) || 'Failed to create list'
const code = (e as IntegrationError).code || 'CREATE_LIST_FAILURE'
return {
error: {
message,
code
}
}
}
}
}
retlOnMappingSave: retlOnMappingSaveHook<Payload>()
},
perform: async (request, { settings, payload, statsContext, hookOutputs }) => {
statsContext?.statsClient?.incr('addToAudience', 1, statsContext?.tags)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export async function addToList(
settings: Settings,
payload: AddToListPayload,
statsContext?: StatsContext,
hookOutputs?: { id: string; name: string }
hookOutputs?: { id?: string; name?: string }
) {
// If the list ID is provided in the hook outputs, use it
const list_id = hookOutputs?.id ?? payload.external_id
Expand Down Expand Up @@ -86,7 +86,7 @@ export async function addToListBatch(
settings: Settings,
payloads: AddToListPayload[],
statsContext?: StatsContext,
hookOutputs?: { id: string; name: string }
hookOutputs?: { id?: string; name?: string }
) {
// If the list ID is provided in the hook outputs, use it
const list_id = hookOutputs?.id ?? payloads[0].external_id
Expand Down Expand Up @@ -154,10 +154,14 @@ export async function removeFromList(
request: RequestClient,
settings: Settings,
payload: RemoveFromListPayload,
statsContext?: StatsContext
statsContext?: StatsContext,
hookOutputs?: { id?: string; name?: string }
) {
if (!payload.external_id) {
throw new IntegrationError('No "external_id" found in payload', ErrorCodes.PAYLOAD_VALIDATION_FAILED, 400)
// If the list ID is provided in the hook outputs, use it
const list_id = hookOutputs?.id ?? payload.external_id

if (!list_id) {
throw new IntegrationError('No list ID found in payload', ErrorCodes.PAYLOAD_VALIDATION_FAILED, 400)
}

const api_endpoint = formatEndpoint(settings.api_endpoint)
Expand Down Expand Up @@ -186,7 +190,7 @@ export async function removeFromList(
const leadIds = extractLeadIds(getLeadsResponse.data.result)

const deleteLeadsUrl =
api_endpoint + REMOVE_USERS_ENDPOINT.replace('listId', payload.external_id).replace('idsToDelete', leadIds)
api_endpoint + REMOVE_USERS_ENDPOINT.replace('listId', list_id).replace('idsToDelete', leadIds)

// DELETE lead ids from list in Marketo
const deleteLeadsResponse = await request<MarketoDeleteLeadsResponse>(deleteLeadsUrl, {
Expand All @@ -208,13 +212,17 @@ export async function removeFromListBatch(
request: RequestClient,
settings: Settings,
payloads: RemoveFromListPayload[],
statsContext?: StatsContext
statsContext?: StatsContext,
hookOutputs?: { id?: string; name?: string }
) {
if (!payloads[0].external_id) {
// If the list ID is provided in the hook outputs, use it
const list_id = hookOutputs?.id ?? payloads[0].external_id

if (!list_id) {
return buildMultiStatusErrorResponse(payloads.length, {
status: 400,
errortype: ErrorCodes.PAYLOAD_VALIDATION_FAILED,
errormessage: 'No "external_id" found in payload'
errormessage: 'No list ID found in payload'
})
}

Expand Down Expand Up @@ -248,7 +256,7 @@ export async function removeFromListBatch(
const leadIds = extractLeadIds(getLeadsResponse.data.result)

const deleteLeadsUrl =
api_endpoint + REMOVE_USERS_ENDPOINT.replace('listId', payloads[0].external_id).replace('idsToDelete', leadIds)
api_endpoint + REMOVE_USERS_ENDPOINT.replace('listId', list_id).replace('idsToDelete', leadIds)

// DELETE lead ids from list in Marketo
const deleteLeadsResponse = await request<MarketoDeleteLeadsResponse>(deleteLeadsUrl, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { Settings } from './generated-types'

import addToList from './addToList'
import removeFromList from './removeFromList'
import syncList from './syncList'
import { MarketoListResponse, GET_LIST_ENDPOINT } from './constants'
import { createList, formatEndpoint, getAccessToken } from './functions'

Expand Down Expand Up @@ -96,7 +97,8 @@ const destination: AudienceDestinationDefinition<Settings> = {
},
actions: {
addToList,
removeFromList
removeFromList,
syncList
},
presets: [
{
Expand Down
Loading
Loading