Skip to content

Commit

Permalink
remove subscribe_sms and subscribe_email fields andult to subscribing…
Browse files Browse the repository at this point in the history
… if email or phone is present
  • Loading branch information
tcgilbert committed Apr 19, 2024
1 parent a021b25 commit a23756e
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 169 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ exports[`Testing snapshot for actions-klaviyo destination: subscribeProfile acti
Object {
"data": Object {
"attributes": Object {
"custom_source": "Segment Klaviyo (Actions) Destination",
"custom_source": "TsKr#",
"profiles": Object {
"data": Array [
Object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,107 +20,10 @@ describe('Subscribe Profile', () => {
).rejects.toThrowError(PayloadValidationError)
})

it('should throw error if both subscribe_email and subscribe_sms are false', async () => {
const event = createTestEvent({
type: 'track',
context: {
traits: {
email: 'segment@test.com',
phone_number: '+17065802344'
}
}
})
// subscribe_email: false, subscribe_sms: false,
const mapping = {
klaviyo_id: '',
subscribe_email: false,
subscribe_sms: false,
list_id: '',
consented_at: {
'@path': '$.timestamp'
},
email: {
'@path': '$.context.traits.email'
},
phone_number: {
'@path': '$.context.traits.phone_number'
}
}

await expect(testDestination.testAction('subscribeProfile', { event, settings, mapping })).rejects.toThrowError(
PayloadValidationError
)
})

it('should throw error if (email = "", email_subscribed = true; phone = "+17065802344", subscribe_sms = false)', async () => {
const event = createTestEvent({
type: 'track',
context: {
traits: {
email: '',
phone_number: '+17065802344'
}
}
})
// subscribe_email: true, subscribe_sms: false,
const mapping = {
klaviyo_id: '',
subscribe_email: true,
subscribe_sms: false,
list_id: '',
consented_at: {
'@path': '$.timestamp'
},
email: {
'@path': '$.context.traits.email'
},
phone_number: {
'@path': '$.context.traits.phone_number'
}
}

await expect(testDestination.testAction('subscribeProfile', { event, settings, mapping })).rejects.toThrowError(
PayloadValidationError
)
})

it('should throw error if (email = "valid@email.com", email_subscribed = false; phone = "", subscribe_sms = true)', async () => {
const event = createTestEvent({
type: 'track',
context: {
traits: {
email: 'valid@email.com',
phone_number: ''
}
}
})
// subscribe_email: false, subscribe_sms: true,
const mapping = {
klaviyo_id: '',
subscribe_email: false,
subscribe_sms: true,
list_id: '',
consented_at: {
'@path': '$.timestamp'
},
email: {
'@path': '$.context.traits.email'
},
phone_number: {
'@path': '$.context.traits.phone_number'
}
}
await expect(testDestination.testAction('subscribeProfile', { event, settings, mapping })).rejects.toThrowError(
PayloadValidationError
)
})

it('formats the correct request body when list id is empty', async () => {
const payload = {
email: 'segment@email.com',
phone_number: '+17067675219',
subscribe_email: true,
subscribe_sms: true,
list_id: '',
klaviyo_id: '6789',
timestamp: '2024-04-01T18:37:06.558Z'
Expand Down Expand Up @@ -176,8 +79,6 @@ describe('Subscribe Profile', () => {

const mapping = {
klaviyo_id: payload.klaviyo_id,
subscribe_email: payload.subscribe_email,
subscribe_sms: payload.subscribe_sms,
list_id: payload.list_id,
consented_at: {
'@path': '$.timestamp'
Expand All @@ -199,8 +100,6 @@ describe('Subscribe Profile', () => {
const payload = {
email: 'segment@email.com',
phone_number: '+17067675219',
subscribe_email: true,
subscribe_sms: true,
list_id: '12345',
klaviyo_id: '6789',
timestamp: '2024-04-01T18:37:06.558Z'
Expand Down Expand Up @@ -264,8 +163,6 @@ describe('Subscribe Profile', () => {

const mapping = {
klaviyo_id: payload.klaviyo_id,
subscribe_email: payload.subscribe_email,
subscribe_sms: payload.subscribe_sms,
list_id: payload.list_id,
consented_at: {
'@path': '$.timestamp'
Expand All @@ -283,12 +180,9 @@ describe('Subscribe Profile', () => {
).resolves.not.toThrowError()
})

it('formats the correct request body when only email channel is subscribed', async () => {
it('formats the correct request body when only email is provided', async () => {
const payload = {
email: 'segment@email.com',
phone_number: '+17067675219',
subscribe_email: true,
subscribe_sms: false,
list_id: '12345',
klaviyo_id: '6789',
timestamp: '2024-04-01T18:37:06.558Z'
Expand All @@ -306,7 +200,6 @@ describe('Subscribe Profile', () => {
attributes: {
id: payload.klaviyo_id,
email: payload.email,
phone_number: payload.phone_number,
subscriptions: {
email: {
marketing: {
Expand Down Expand Up @@ -338,16 +231,13 @@ describe('Subscribe Profile', () => {
timestamp: payload.timestamp,
context: {
traits: {
email: payload.email,
phone_number: payload.phone_number
email: payload.email
}
}
})

const mapping = {
klaviyo_id: payload.klaviyo_id,
subscribe_email: payload.subscribe_email,
subscribe_sms: payload.subscribe_sms,
list_id: payload.list_id,
consented_at: {
'@path': '$.timestamp'
Expand All @@ -365,12 +255,9 @@ describe('Subscribe Profile', () => {
).resolves.not.toThrowError()
})

it('formats the correct request body when only sms channel is subscribed', async () => {
it('formats the correct request body when only phone number is provided', async () => {
const payload = {
email: 'segment@email.com',
phone_number: '+17067675219',
subscribe_email: false,
subscribe_sms: true,
list_id: '12345',
klaviyo_id: '6789',
timestamp: '2024-04-01T18:37:06.558Z'
Expand All @@ -387,7 +274,6 @@ describe('Subscribe Profile', () => {
type: 'profile',
attributes: {
id: payload.klaviyo_id,
email: payload.email,
phone_number: payload.phone_number,
subscriptions: {
sms: {
Expand Down Expand Up @@ -420,16 +306,13 @@ describe('Subscribe Profile', () => {
timestamp: payload.timestamp,
context: {
traits: {
email: payload.email,
phone_number: payload.phone_number
}
}
})

const mapping = {
klaviyo_id: payload.klaviyo_id,
subscribe_email: payload.subscribe_email,
subscribe_sms: payload.subscribe_sms,
list_id: payload.list_id,
consented_at: {
'@path': '$.timestamp'
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 @@ -9,8 +9,7 @@ import { SubscribeProfile, SubscribeEventData } from '../types'

const action: ActionDefinition<Settings, Payload> = {
title: 'Subscribe Profile',
description: 'Subscribe profiles to Email marketing, SMS marketing, or both.',
defaultSubscription: 'type = "track"',
description: 'Subscribe Klaviyo profiles to Email marketing, SMS marketing, or both.',
fields: {
klaviyo_id: {
label: 'Klaviyo Id',
Expand All @@ -19,37 +18,29 @@ const action: ActionDefinition<Settings, Payload> = {
},
email: {
label: 'Email',
description: `The email address to subscribe or to set on the profile if the email channel is omitted.`,
description: `The email address to subscribe. If provided, the associated profile will be subscribed to Email marketing.`,
type: 'string',
format: 'email',
default: { '@path': '$.context.traits.email' }
},
subscribe_email: {
label: 'Subscribe Profile to Email Marketing',
description: `Controls the subscription status for email marketing. If set to "yes", the profile's consent preferences for email marketing are set to "SUBSCRIBED"; otherwise, the email channel is omitted.`,
type: 'boolean',
required: true,
default: true
},
phone_number: {
label: 'Phone Number',
description: `The phone number to subscribe or to set on the profile if SMS channel is omitted. This must be in E.164 format.`,
description: `The phone number to subscribe. This must be in E.164 format. If provided, the associated profile will be subscribed to SMS marketing.`,
type: 'string',
default: { '@path': '$.context.traits.phone' }
},
subscribe_sms: {
label: 'Subscribe Profile to SMS Marketing',
description: `Controls the subscription status for SMS marketing. If set to "yes", the profile's consent preferences for SMS marketing are set to "SUBSCRIBED"; otherwise, the SMS channel is omitted.`,
type: 'boolean',
required: true,
default: true
},
list_id: {
label: 'List Id',
description: `The Klaviyo list to add the newly subscribed profiles to. If no List Id is present, the opt-in process used to subscribe the profile depends on the account's default opt-in settings.`,
type: 'string',
dynamic: true
},
custom_source: {
label: 'Custom Source',
description: 'A custom method or source to detail source of consent preferences (e.g., "Marketing Event").',
type: 'string',
default: 'Segment Klaviyo (Actions) Destination'
},
consented_at: {
label: 'Consented At',
description: `The timestamp of when the profile's consent was gathered.`,
Expand All @@ -65,30 +56,18 @@ const action: ActionDefinition<Settings, Payload> = {
}
},
perform: async (request, { payload }) => {
const { email, klaviyo_id, phone_number, consented_at, list_id, subscribe_email, subscribe_sms } = payload
const { email, klaviyo_id, phone_number, consented_at, list_id, custom_source } = payload
if (!email && !phone_number) {
throw new PayloadValidationError('Phone Number or Email is required.')
}

// Ensure the properties for at least one marketing channel subscription (Email or SMS) are available
if ((!email || subscribe_email !== true) && (!phone_number || subscribe_sms !== true)) {
throw new PayloadValidationError('At least one marketing channel (Email or SMS) needs to be subscribed to.')
}

const profileToSubscribe = formatSubscribeProfile(
email,
phone_number,
klaviyo_id,
consented_at,
subscribe_sms,
subscribe_email
)
const profileToSubscribe = formatSubscribeProfile(email, phone_number, klaviyo_id, consented_at)

const eventData: SubscribeEventData = {
data: {
type: 'profile-subscription-bulk-create-job',
attributes: {
custom_source: 'Segment Klaviyo (Actions) Destination',
custom_source: custom_source || 'Segment Klaviyo (Actions) Destination', // Use custom_source if populated, otherwise fallback to default value
profiles: {
data: [profileToSubscribe]
}
Expand Down Expand Up @@ -122,9 +101,7 @@ function formatSubscribeProfile(
email: string | undefined,
phone_number: string | undefined,
klaviyo_id: string | undefined,
consented_at: string | number | undefined,
subscribe_sms: boolean,
subscribe_email: boolean
consented_at: string | number | undefined
) {
const profileToSubscribe: SubscribeProfile = {
type: 'profile',
Expand All @@ -136,7 +113,7 @@ function formatSubscribeProfile(
}
}

if (email && subscribe_email) {
if (email) {
profileToSubscribe.attributes.subscriptions.email = {
marketing: {
consent: 'SUBSCRIBED',
Expand All @@ -145,7 +122,7 @@ function formatSubscribeProfile(
}
}

if (phone_number && subscribe_sms) {
if (phone_number) {
profileToSubscribe.attributes.subscriptions.sms = {
marketing: {
consent: 'SUBSCRIBED',
Expand Down

0 comments on commit a23756e

Please sign in to comment.