Skip to content

Commit

Permalink
Squashed commit of the following:
Browse files Browse the repository at this point in the history
commit 199765a87a153b1aadd3e8598f1ef16de28d938b
Merge: 3ea1444 f0e5ac2
Author: Nick Aguilar <[email protected]>
Date:   Mon Jul 22 17:16:35 2024 -0700

    Merge branch 'main' into fbca-retl-hook

commit 3ea1444
Author: Nick Aguilar <[email protected]>
Date:   Mon Jul 22 17:15:11 2024 -0700

    Splits off engage and retl AdAccountId settings, fun fact: these cannot share a slug

commit f0e5ac2
Author: Joe Ayoub <[email protected]>
Date:   Fri Jul 19 16:19:18 2024 +0200

    aws s3 bug fixes (#2200)

commit 0a1285a
Author: Nick Aguilar <[email protected]>
Date:   Thu Jul 18 15:08:29 2024 -0700

    Generates types

commit 8f5f5ed
Author: Nick Aguilar <[email protected]>
Date:   Thu Jul 18 15:04:14 2024 -0700

    WIP on an inexplicably not passing unit test

commit fc4f5ed
Author: Nick Aguilar <[email protected]>
Date:   Thu Jul 18 14:05:48 2024 -0700

    Checks for existence of user selected audience. Saves audience name and ID when returning from hook

commit a2a642c
Author: Nick Aguilar <[email protected]>
Date:   Thu Jul 18 12:58:03 2024 -0700

    WIP - verifying selected audience exists and saving it's ID as output

commit a022704
Author: Nick Aguilar <[email protected]>
Date:   Thu Jul 18 12:47:32 2024 -0700

    Removes description param from creating an audience per PRD

commit 49add30
Author: Nick Aguilar <[email protected]>
Date:   Thu Jul 18 12:44:07 2024 -0700

    Pulls down 200 most recent audiences in dynamic field response

commit c2f3e32
Author: Nick Aguilar <[email protected]>
Date:   Thu Jul 18 12:35:50 2024 -0700

    Removes pagination related code - that work will be saved for later :)

commit 6078690
Merge: b831f7b c9ecf66
Author: Nick Aguilar <[email protected]>
Date:   Thu Jul 18 12:31:29 2024 -0700

    Merge branch 'main' into fbca-retl-hook

commit b831f7b
Author: Nick Aguilar <[email protected]>
Date:   Wed Jul 17 15:14:15 2024 -0700

    Sets paging return such that the previous page was the current page

commit 365690e
Author: Nick Aguilar <[email protected]>
Date:   Wed Jul 17 15:09:14 2024 -0700

    Can pass dynamicFieldContext & paging into local server request

commit 037534c
Author: Nick Aguilar <[email protected]>
Date:   Wed Jul 17 14:37:23 2024 -0700

    Fixes build

commit 1e9d616
Author: Nick Aguilar <[email protected]>
Date:   Tue Jul 16 16:55:05 2024 -0700

    WIP on a pagination mechanism for dynamic fields

commit 151d26c
Author: Nick Aguilar <[email protected]>
Date:   Tue Jul 16 16:14:03 2024 -0700

    Adds a dynamic audience field, pulls down all customaudiences as choices

commit 27e4903
Author: Nick Aguilar <[email protected]>
Date:   Tue Jul 16 15:29:46 2024 -0700

    Adds required customer_file_source field

commit 60d2291
Author: Nick Aguilar <[email protected]>
Date:   Tue Jul 16 12:26:51 2024 -0700

    WIP - first draft of a FacebookClient class, including an untested createAudience hook for retl

commit 5ba6c09
Author: Nick Aguilar <[email protected]>
Date:   Tue Jul 16 12:05:23 2024 -0700

    Introduces shared adAccountId top-level setting and audienceSetting

commit 0a20b76
Author: Nick Aguilar <[email protected]>
Date:   Tue Jul 16 12:02:16 2024 -0700

    Removes delete action, add action. Introduces single sync action
  • Loading branch information
nick-Ag committed Jul 23, 2024
1 parent 1b0a42c commit 21b4f81
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 43 deletions.
78 changes: 49 additions & 29 deletions packages/destination-actions/src/destinations/aws-s3/operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,60 +25,60 @@ function generateFile(payloads: Payload[], audienceSettings: AudienceSettings):
const additionalColumns = payloads[0].additional_identifiers_and_traits_columns ?? []

Object.entries(columnsField).forEach(([_, value]) => {
if (value !== undefined) {
if (![undefined, null, ''].includes(value)) {
headers.push(value)
}
})

additionalColumns.forEach((additionalColumn) => {
headers.push(additionalColumn.value)
if (![undefined, null, ''].includes(additionalColumn.value)) {
headers.push(additionalColumn.value)
}
})

const headerString = `${headers.join(audienceSettings.delimiter === 'tab' ? '\t' : audienceSettings.delimiter)}\n`

const rows: string[] = [headerString]

payloads.forEach((payload, index, arr) => {
const action = payload.propertiesOrTraits[payload.audienceName]

const row: string[] = []
if (headers.includes('audience_name')) {
row.push(enquoteIdentifier(String(payload.audienceName ?? '')))
if (![undefined, null, ''].includes(columnsField.audience_name)) {
row.push(encodeString(String(payload.audienceName ?? '')))
}
if (headers.includes('audience_id')) {
row.push(enquoteIdentifier(String(payload.audienceId ?? '')))
if (![undefined, null, ''].includes(columnsField.audience_id)) {
row.push(encodeString(String(payload.audienceId ?? '')))
}
if (headers.includes('audience_action')) {
row.push(enquoteIdentifier(String(action ?? '')))
if (![undefined, null, ''].includes(columnsField.audience_action)) {
row.push(encodeString(String(action ?? '')))
}
if (headers.includes('email')) {
row.push(enquoteIdentifier(String(payload.email ?? '')))
if (![undefined, null, ''].includes(columnsField.email)) {
row.push(encodeString(String(payload.email ?? '')))
}
if (headers.includes('user_id')) {
row.push(enquoteIdentifier(String(payload.userId ?? '')))
if (![undefined, null, ''].includes(columnsField.user_id)) {
row.push(encodeString(String(payload.userId ?? '')))
}
if (headers.includes('anonymous_id')) {
row.push(enquoteIdentifier(String(payload.anonymousId ?? '')))
if (![undefined, null, ''].includes(columnsField.anonymous_id)) {
row.push(encodeString(String(payload.anonymousId ?? '')))
}
if (headers.includes('timestamp')) {
row.push(enquoteIdentifier(String(payload.timestamp ?? '')))
if (![undefined, null, ''].includes(columnsField.timestamp)) {
row.push(encodeString(String(payload.timestamp ?? '')))
}
if (headers.includes('message_id')) {
row.push(enquoteIdentifier(String(payload.messageId ?? '')))
if (![undefined, null, ''].includes(columnsField.message_id)) {
row.push(encodeString(String(payload.messageId ?? '')))
}
if (headers.includes('space_id')) {
row.push(enquoteIdentifier(String(payload.spaceId ?? '')))
if (![undefined, null, ''].includes(columnsField.space_id)) {
row.push(encodeString(String(payload.spaceId ?? '')))
}
if (headers.includes('integrations_object')) {
row.push(enquoteIdentifier(String(JSON.stringify(payload.integrationsObject) ?? '')))
if (![undefined, null, ''].includes(columnsField.integrations_object)) {
row.push(encodeString(String(JSON.stringify(payload.integrationsObject) ?? '')))
}
if (headers.includes('properties_or_traits')) {
row.push(enquoteIdentifier(String(JSON.stringify(payload.propertiesOrTraits) ?? '')))
if (![undefined, null, ''].includes(columnsField.properties_or_traits)) {
row.push(encodeString(String(JSON.stringify(payload.propertiesOrTraits) ?? '')))
}

additionalColumns.forEach((additionalColumn) => {
//row.push(enquoteIdentifier(String(JSON.stringify(payload.propertiesOrTraits[additionalColumn.key]) ?? '')))
row.push(enquoteIdentifier(String(JSON.stringify(additionalColumn.key) ?? '')))
row.push(encodeString(String(JSON.stringify(payload.propertiesOrTraits[additionalColumn.key]) ?? '')))
})

const isLastRow = arr.length === index + 1
Expand All @@ -92,8 +92,28 @@ function generateFile(payloads: Payload[], audienceSettings: AudienceSettings):
return rows.join('')
}

function enquoteIdentifier(str: string) {
function encodeString(str: string) {
return `"${String(str).replace(/"/g, '""')}"`
}

export { generateFile, enquoteIdentifier }
function validate(payloads: Payload[], audienceSettings: AudienceSettings) {
const delimiter = audienceSettings.delimiter
const columns = payloads[0].columns
const additionalIdentifierColumns = payloads[0].additional_identifiers_and_traits_columns

// ensure column names do not contain delimiter
Object.values(columns).forEach((columnName) => {
if (columnName.includes(delimiter)) {
throw new Error(`Column name ${columnName} cannot contain delimiter: ${delimiter}`)
}
})

// ensure additional identifier column names do not contain delimiter
additionalIdentifierColumns?.forEach((column) => {
if (column.value.includes(delimiter)) {
throw new Error(`Column name ${column.value} cannot contain delimiter: ${delimiter}`)
}
})
}

export { generateFile, validate }

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
@@ -1,7 +1,7 @@
import type { ActionDefinition } from '@segment/actions-core'
import type { Settings, AudienceSettings } from '../generated-types'
import type { Payload } from './generated-types'
import { generateFile } from '../operations'
import { generateFile, validate } from '../operations'
import { S3CSVClient } from './s3'

const action: ActionDefinition<Settings, Payload, AudienceSettings> = {
Expand Down Expand Up @@ -52,7 +52,7 @@ const action: ActionDefinition<Settings, Payload, AudienceSettings> = {
description: 'Name of column for timestamp for when the user was added or removed from the Audience',
type: 'string'
},
messageId: {
message_id: {
label: 'Message ID',
description: 'Name of column for the unique identifier for the message.',
type: 'string'
Expand Down Expand Up @@ -241,10 +241,9 @@ const action: ActionDefinition<Settings, Payload, AudienceSettings> = {
}

async function processData(payloads: Payload[], settings: Settings, audienceSettings?: AudienceSettings) {
validate(payloads, audienceSettings as AudienceSettings)
const fileContent = generateFile(payloads, audienceSettings as AudienceSettings)

const s3Client = new S3CSVClient(settings.s3_aws_region, settings.iam_role_arn, settings.iam_external_id)

await s3Client.uploadS3(settings, audienceSettings as AudienceSettings, fileContent)
}

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 @@ -16,7 +16,7 @@ const destination: AudienceDestinationDefinition<Settings, AudienceSettings> = {
authentication: {
scheme: 'oauth2',
fields: {
adAccountId
retlAdAccountId: adAccountId
},
refreshAccessToken: async () => {
return { accessToken: 'TODO: Implement this' }
Expand All @@ -30,7 +30,7 @@ const destination: AudienceDestinationDefinition<Settings, AudienceSettings> = {
}
},
audienceFields: {
adAccountId,
engageAdAccountId: adAccountId,
audienceDescription: {
type: 'string',
label: 'Description',
Expand All @@ -45,7 +45,7 @@ const destination: AudienceDestinationDefinition<Settings, AudienceSettings> = {
},
async createAudience(request, createAudienceInput) {
const audienceName = createAudienceInput.audienceName
const adAccountId = createAudienceInput.audienceSettings?.adAccountId
const adAccountId = createAudienceInput.audienceSettings?.engageAdAccountId
const audienceDescription = createAudienceInput.audienceSettings?.audienceDescription

if (!audienceName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ const action: ActionDefinition<Settings, Payload> = {
hooks: {
retlOnMappingSave: {
label: 'Select or create an audience in Facebook',
description: 'TODO: Create or select an audience in Facebook.',
description:
'When saving this mapping, Segment will either create a new audience in Facebook or connect to an existing one. To create a new audience, enter the name of the audience. To connect to an existing audience, select the audience ID from the dropdown.',
inputFields: {
audienceName: {
type: 'string',
label: 'Audience Name',
label: 'Audience Creation Name',
description: 'The name of the audience in Facebook.',
default: 'TODO: Model Name by default'
},
Expand All @@ -23,7 +24,7 @@ const action: ActionDefinition<Settings, Payload> = {
label: 'Existing Audience ID',
description: 'The ID of the audience in Facebook.',
dynamic: async (request, { settings }) => {
const fbClient = new FacebookClient(request, settings.adAccountId)
const fbClient = new FacebookClient(request, settings.retlAdAccountId)
const { choices, error } = await fbClient.getAllAudiences()

if (error) {
Expand Down Expand Up @@ -51,7 +52,7 @@ const action: ActionDefinition<Settings, Payload> = {
}
},
performHook: async (request, { settings, hookInputs }) => {
const fbClient = new FacebookClient(request, settings.adAccountId)
const fbClient = new FacebookClient(request, settings.retlAdAccountId)

if (hookInputs.existingAudienceId) {
const { data, error } = await fbClient.getSingleAudience(hookInputs.existingAudienceId)
Expand Down

0 comments on commit 21b4f81

Please sign in to comment.