Skip to content

Commit 988b27d

Browse files
joe-ayoub-segmentnick-Ag
authored andcommitted
[Attentive] - minor bug fix (#3083)
* [Attentive] - bug fix * preset update * fixing tests
1 parent 4a4e7e0 commit 988b27d

File tree

5 files changed

+130
-138
lines changed

5 files changed

+130
-138
lines changed

packages/destination-actions/src/destinations/attentive/ecommEvent/__tests__/__snapshots__/snapshot.test.ts.snap

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ Object {
55
"items": Array [
66
Object {
77
"name": "Test Product",
8-
"price": Object {
9-
"currency": "USD",
10-
"value": 100,
11-
},
8+
"price": Array [
9+
100,
10+
"USD",
11+
],
1212
"productId": "test-product-id",
1313
"productImage": "https://example.com/image.jpg",
1414
"productUrl": "https://example.com/product",
@@ -32,10 +32,10 @@ Object {
3232
"items": Array [
3333
Object {
3434
"name": "Test Product",
35-
"price": Object {
36-
"currency": "USD",
37-
"value": 100,
38-
},
35+
"price": Array [
36+
100,
37+
"USD",
38+
],
3939
"productId": "test-product-id",
4040
"productImage": "https://example.com/image.jpg",
4141
"productUrl": "https://example.com/product",

packages/destination-actions/src/destinations/attentive/ecommEvent/__tests__/index.test.ts

Lines changed: 70 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -55,37 +55,31 @@ describe('eCommerce API - Product View, Add to Cart, Purchase', () => {
5555
clientUserId: { '@path': '$.userId' }
5656
}
5757
}
58-
59-
const event = createTestEvent(
60-
basePayload
61-
)
58+
59+
const event = createTestEvent(basePayload)
6260

6361
nock('https://api.attentivemobile.com')
64-
.post('/v1/events/ecommerce/product-view',
65-
{
66-
items: [
67-
{
68-
productId: 'prod_1',
69-
productVariantId: 'variant_1',
70-
price: {
71-
value: 19.99,
72-
currency: 'USD'
73-
},
74-
productImage: 'https://example.com/image.png',
75-
productUrl: 'https://example.com/product/prod_1',
76-
name: 'Test Product',
77-
quantity: 1
78-
}
79-
],
80-
user: {
81-
phone: '+12345556789',
82-
83-
externalIdentifiers: {
84-
clientUserId: 'testuser123'
85-
}
86-
},
87-
occurredAt: '2024-01-08T13:52:50.212Z'
88-
})
62+
.post('/v1/events/ecommerce/product-view', {
63+
items: [
64+
{
65+
productId: 'prod_1',
66+
productVariantId: 'variant_1',
67+
price: [19.99, 'USD'],
68+
productImage: 'https://example.com/image.png',
69+
productUrl: 'https://example.com/product/prod_1',
70+
name: 'Test Product',
71+
quantity: 1
72+
}
73+
],
74+
user: {
75+
phone: '+12345556789',
76+
77+
externalIdentifiers: {
78+
clientUserId: 'testuser123'
79+
}
80+
},
81+
occurredAt: '2024-01-08T13:52:50.212Z'
82+
})
8983
.reply(200, {})
9084

9185
const responses = await testDestination.testAction('ecommEvent', {
@@ -119,37 +113,31 @@ describe('eCommerce API - Product View, Add to Cart, Purchase', () => {
119113
clientUserId: { '@path': '$.userId' }
120114
}
121115
}
122-
123-
const event = createTestEvent(
124-
basePayload
125-
)
116+
117+
const event = createTestEvent(basePayload)
126118

127119
nock('https://api.attentivemobile.com')
128-
.post('/v1/events/ecommerce/add-to-cart',
129-
{
130-
items: [
131-
{
132-
productId: 'prod_1',
133-
productVariantId: 'variant_1',
134-
price: {
135-
value: 19.99,
136-
currency: 'USD'
137-
},
138-
productImage: 'https://example.com/image.png',
139-
productUrl: 'https://example.com/product/prod_1',
140-
name: 'Test Product',
141-
quantity: 1
142-
}
143-
],
144-
user: {
145-
phone: '+12345556789',
146-
147-
externalIdentifiers: {
148-
clientUserId: 'testuser123'
149-
}
150-
},
151-
occurredAt: '2024-01-08T13:52:50.212Z'
152-
})
120+
.post('/v1/events/ecommerce/add-to-cart', {
121+
items: [
122+
{
123+
productId: 'prod_1',
124+
productVariantId: 'variant_1',
125+
price: [19.99, 'USD'],
126+
productImage: 'https://example.com/image.png',
127+
productUrl: 'https://example.com/product/prod_1',
128+
name: 'Test Product',
129+
quantity: 1
130+
}
131+
],
132+
user: {
133+
phone: '+12345556789',
134+
135+
externalIdentifiers: {
136+
clientUserId: 'testuser123'
137+
}
138+
},
139+
occurredAt: '2024-01-08T13:52:50.212Z'
140+
})
153141
.reply(200, {})
154142

155143
const responses = await testDestination.testAction('ecommEvent', {
@@ -183,37 +171,31 @@ describe('eCommerce API - Product View, Add to Cart, Purchase', () => {
183171
clientUserId: { '@path': '$.userId' }
184172
}
185173
}
186-
187-
const event = createTestEvent(
188-
basePayload
189-
)
174+
175+
const event = createTestEvent(basePayload)
190176

191177
nock('https://api.attentivemobile.com')
192-
.post('/v1/events/ecommerce/purchase',
193-
{
194-
items: [
195-
{
196-
productId: 'prod_1',
197-
productVariantId: 'variant_1',
198-
price: {
199-
value: 19.99,
200-
currency: 'USD'
201-
},
202-
productImage: 'https://example.com/image.png',
203-
productUrl: 'https://example.com/product/prod_1',
204-
name: 'Test Product',
205-
quantity: 1
206-
}
207-
],
208-
user: {
209-
phone: '+12345556789',
210-
211-
externalIdentifiers: {
212-
clientUserId: 'testuser123'
213-
}
214-
},
215-
occurredAt: '2024-01-08T13:52:50.212Z'
216-
})
178+
.post('/v1/events/ecommerce/purchase', {
179+
items: [
180+
{
181+
productId: 'prod_1',
182+
productVariantId: 'variant_1',
183+
price: [19.99, 'USD'],
184+
productImage: 'https://example.com/image.png',
185+
productUrl: 'https://example.com/product/prod_1',
186+
name: 'Test Product',
187+
quantity: 1
188+
}
189+
],
190+
user: {
191+
phone: '+12345556789',
192+
193+
externalIdentifiers: {
194+
clientUserId: 'testuser123'
195+
}
196+
},
197+
occurredAt: '2024-01-08T13:52:50.212Z'
198+
})
217199
.reply(200, {})
218200

219201
const responses = await testDestination.testAction('ecommEvent', {
@@ -235,4 +217,4 @@ describe('eCommerce API - Product View, Add to Cart, Purchase', () => {
235217
expect(responses.length).toBe(1)
236218
expect(responses[0].status).toBe(200)
237219
})
238-
})
220+
})

packages/destination-actions/src/destinations/attentive/functions.ts

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
import { PayloadValidationError } from '@segment/actions-core'
2-
import { Item, User, EcommEventJSON, CustomEventJSON, UpsertUserAttributesJSON, SubscribeUserJSON, SubscriptionType } from './types'
2+
import {
3+
Item,
4+
User,
5+
EcommEventJSON,
6+
CustomEventJSON,
7+
UpsertUserAttributesJSON,
8+
SubscribeUserJSON,
9+
SubscriptionType
10+
} from './types'
311
import { Payload as CustomEvent } from './customEvents/generated-types'
412
import { Payload as EcommEvent } from './ecommEvent/generated-types'
513
import { Payload as CustomAttributesEvent } from './upsertUserAttributes/generated-types'
614
import { Payload as SubscribeUserEvent } from './subscribeUser/generated-types'
715

8-
type UserIdentifiers =
9-
| CustomEvent['userIdentifiers']
10-
| EcommEvent['userIdentifiers']
16+
type UserIdentifiers = CustomEvent['userIdentifiers'] | EcommEvent['userIdentifiers']
1117

1218
type Items = EcommEvent['items']
1319

@@ -19,11 +25,11 @@ export function validate(payload: CustomEvent | EcommEvent | CustomAttributesEve
1925
if (!email && !phone && !clientUserId && Object.keys(customIdentifiers).length === 0) {
2026
throw new PayloadValidationError('At least one user identifier is required.')
2127
}
22-
}
28+
}
2329

2430
export function validateSubscribeUser(payload: SubscribeUserEvent) {
25-
const { userIdentifiers, locale } = payload
26-
if (!userIdentifiers && !locale) {
31+
const { signUpSourceId, locale } = payload
32+
if (!signUpSourceId && !locale) {
2733
throw new PayloadValidationError('Either locale or signUpSourceId is required.')
2834
}
2935
}
@@ -47,15 +53,12 @@ function formatUser(userIdentifiers: UserIdentifiers): User {
4753
function formatItems(items: Items): Array<Item> {
4854
return items.map(({ value, currency, ...rest }) => ({
4955
...rest,
50-
price: {
51-
value,
52-
currency,
53-
}
56+
price: currency !== undefined && currency !== null && currency !== '' ? [value, currency] : [value]
5457
}))
5558
}
5659

57-
function formatLocale(locale?: string): { language: string, country: string } {
58-
if(!locale) {
60+
function formatLocale(locale?: string): { language: string; country: string } {
61+
if (!locale) {
5962
throw new PayloadValidationError('Locale Signup Source ID is required.')
6063
}
6164
const parts = locale.split('-')
@@ -76,15 +79,9 @@ export function formatEcommEventJSON(payload: EcommEvent): EcommEventJSON {
7679
}
7780

7881
export function formatCustomEventJSON(payload: CustomEvent): CustomEventJSON {
79-
const {
80-
externalEventId,
81-
type,
82-
properties,
83-
occurredAt,
84-
userIdentifiers
85-
} = payload
86-
87-
if (Object.values(properties ?? {}).some(value => Array.isArray(value))) {
82+
const { externalEventId, type, properties, occurredAt, userIdentifiers } = payload
83+
84+
if (Object.values(properties ?? {}).some((value) => Array.isArray(value))) {
8885
throw new PayloadValidationError('Properties cannot contain arrays.')
8986
}
9087

@@ -98,12 +95,9 @@ export function formatCustomEventJSON(payload: CustomEvent): CustomEventJSON {
9895
}
9996

10097
export function formatUpsertUserAttributesJSON(payload: CustomAttributesEvent): UpsertUserAttributesJSON {
101-
const {
102-
properties,
103-
userIdentifiers
104-
} = payload
98+
const { properties, userIdentifiers } = payload
10599

106-
if (Object.values(properties ?? {}).some(value => typeof value === 'object' || Array.isArray(value))) {
100+
if (Object.values(properties ?? {}).some((value) => typeof value === 'object' || Array.isArray(value))) {
107101
throw new PayloadValidationError('Properties cannot contain objects or arrays.')
108102
}
109103

@@ -124,4 +118,4 @@ export function formatSubscribeUserJSON(payload: SubscribeUserEvent): SubscribeU
124118
singleOptIn,
125119
user: formatUser(userIdentifiers)
126120
}
127-
}
121+
}

packages/destination-actions/src/destinations/attentive/index.ts

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import ecommEvent from './ecommEvent'
55
import upsertUserAttributes from './upsertUserAttributes'
66
import subscribeUser from './subscribeUser'
77

8-
98
const destination: DestinationDefinition<Settings> = {
109
name: 'Attentive',
1110
slug: 'actions-attentive',
@@ -58,29 +57,49 @@ const destination: DestinationDefinition<Settings> = {
5857
name: 'View Item',
5958
subscribe: 'event = "Product Viewed"',
6059
partnerAction: 'ecommEvent',
61-
mapping: {
60+
mapping: {
6261
...defaultValues(ecommEvent.fields),
63-
eventType: 'view-item',
62+
eventType: 'view-item',
63+
items: {
64+
productId: { '@path': '$.properties.product_id' },
65+
productVariantId: { '@path': '$.properties.variant' },
66+
value: { '@path': '$.properties.price' },
67+
productImage: { '@path': '$.properties.image_url' },
68+
productUrl: { '@path': '$.properties.url' },
69+
name: { '@path': '$.properties.name' },
70+
currency: { '@path': '$.properties.currency' },
71+
quantity: { '@path': '$.properties.quantity' }
72+
}
6473
},
6574
type: 'automatic'
6675
},
6776
{
6877
name: 'Add to Cart',
6978
subscribe: 'event = "Product Added"',
7079
partnerAction: 'ecommEvent',
71-
mapping: {
80+
mapping: {
7281
...defaultValues(ecommEvent.fields),
73-
eventType: 'add-to-cart',
82+
eventType: 'add-to-cart',
83+
items: {
84+
productId: { '@path': '$.properties.product_id' },
85+
productVariantId: { '@path': '$.properties.variant' },
86+
value: { '@path': '$.properties.price' },
87+
productImage: { '@path': '$.properties.image_url' },
88+
productUrl: { '@path': '$.properties.url' },
89+
name: { '@path': '$.properties.name' },
90+
currency: { '@path': '$.properties.currency' },
91+
quantity: { '@path': '$.properties.quantity' }
92+
}
7493
},
7594
type: 'automatic'
7695
},
7796
{
7897
name: 'Purchase',
7998
subscribe: 'event = "Order Completed"',
8099
partnerAction: 'ecommEvent',
81-
mapping: {
100+
mapping: {
82101
...defaultValues(ecommEvent.fields),
83-
eventType: 'purchase',
102+
eventType: 'purchase'
84103
},
85104
type: 'automatic'
86105
}

0 commit comments

Comments
 (0)