Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Recombee (Actions)] Fix bugs #2564

Merged
merged 1 commit into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,40 +23,24 @@ Object {

exports[`Testing snapshot for actions-recombee destination: addCartAddition action - all fields 1`] = `
Object {
"requests": Array [
Object {
"method": "POST",
"params": Object {
"additionalData": Object {
"testType": "fSK%!$(3gz2peZ!&@L@D",
},
"amount": 85450379273175.05,
"cascadeCreate": true,
"itemId": "fSK%!$(3gz2peZ!&@L@D",
"price": 85450379273175.05,
"recommId": "fSK%!$(3gz2peZ!&@L@D",
"timestamp": "fSK%!$(3gz2peZ!&@L@D",
"userId": "fSK%!$(3gz2peZ!&@L@D",
},
"path": "/cartadditions/",
},
],
"additionalData": Object {
"testType": "fSK%!$(3gz2peZ!&@L@D",
},
"amount": 85450379273175.05,
"cascadeCreate": true,
"itemId": "fSK%!$(3gz2peZ!&@L@D",
"price": 85450379273175.05,
"recommId": "fSK%!$(3gz2peZ!&@L@D",
"timestamp": "fSK%!$(3gz2peZ!&@L@D",
"userId": "fSK%!$(3gz2peZ!&@L@D",
}
`;

exports[`Testing snapshot for actions-recombee destination: addCartAddition action - required fields 1`] = `
Object {
"requests": Array [
Object {
"method": "POST",
"params": Object {
"cascadeCreate": true,
"itemId": "fSK%!$(3gz2peZ!&@L@D",
"userId": "fSK%!$(3gz2peZ!&@L@D",
},
"path": "/cartadditions/",
},
],
"cascadeCreate": true,
"itemId": "fSK%!$(3gz2peZ!&@L@D",
"userId": "fSK%!$(3gz2peZ!&@L@D",
}
`;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { interactionFields } from '../commonFields'
const action: ActionDefinition<Settings, Payload> = {
title: 'Add Bookmark',
description: 'Adds a bookmark of the given item made by the given user.',
defaultSubscription: 'type = "track" and ( event = "Product Added to Wishlist" or event = "Product Shared" )',
defaultSubscription: 'type = "track" and event = "Product Added to Wishlist"',
fields: {
userId: {
label: 'User ID',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,23 @@

exports[`Testing snapshot for Recombee's addCartAddition destination action: all fields 1`] = `
Object {
"requests": Array [
Object {
"method": "POST",
"params": Object {
"additionalData": Object {
"testType": "E$o]!yX^hLQ^46#)",
},
"amount": 34128252129771.52,
"cascadeCreate": true,
"itemId": "E$o]!yX^hLQ^46#)",
"price": 34128252129771.52,
"recommId": "E$o]!yX^hLQ^46#)",
"timestamp": "E$o]!yX^hLQ^46#)",
"userId": "E$o]!yX^hLQ^46#)",
},
"path": "/cartadditions/",
},
],
"additionalData": Object {
"testType": "E$o]!yX^hLQ^46#)",
},
"amount": 34128252129771.52,
"cascadeCreate": true,
"itemId": "E$o]!yX^hLQ^46#)",
"price": 34128252129771.52,
"recommId": "E$o]!yX^hLQ^46#)",
"timestamp": "E$o]!yX^hLQ^46#)",
"userId": "E$o]!yX^hLQ^46#)",
}
`;

exports[`Testing snapshot for Recombee's addCartAddition destination action: required fields 1`] = `
Object {
"requests": Array [
Object {
"method": "POST",
"params": Object {
"cascadeCreate": true,
"itemId": "E$o]!yX^hLQ^46#)",
"userId": "E$o]!yX^hLQ^46#)",
},
"path": "/cartadditions/",
},
],
"cascadeCreate": true,
"itemId": "E$o]!yX^hLQ^46#)",
"userId": "E$o]!yX^hLQ^46#)",
}
`;
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const SETTINGS: Settings = {
describe('addCartAddition', () => {
it('should validate action fields', async () => {
nock('https://rapi-eu-west.recombee.com/')
.post(`/${DATABASE_ID}/batch/`)
.post(`/${DATABASE_ID}/cartadditions/`)
.query({
hmac_timestamp: /.*/,
hmac_sign: /.*/
Expand All @@ -38,24 +38,16 @@ describe('addCartAddition', () => {
})

expect(await response[0].request.json()).toMatchObject({
requests: [
{
method: 'POST',
path: '/cartadditions/',
params: {
userId: 'user-id',
itemId: 'product-id',
timestamp: '2021-09-01T00:00:00.000Z',
cascadeCreate: true
}
}
]
userId: 'user-id',
itemId: 'product-id',
timestamp: '2021-09-01T00:00:00.000Z',
cascadeCreate: true
})
})

it('should validate action fields with recommId and additionalData', async () => {
nock('https://rapi-eu-west.recombee.com/')
.post(`/${DATABASE_ID}/batch/`)
.post(`/${DATABASE_ID}/cartadditions/`)
.query({
hmac_timestamp: /.*/,
hmac_sign: /.*/
Expand Down Expand Up @@ -90,108 +82,15 @@ describe('addCartAddition', () => {
})

expect(await response[0].request.json()).toMatchObject({
requests: [
{
method: 'POST',
path: '/cartadditions/',
params: {
userId: 'user-id',
itemId: 'product-id',
timestamp: '2021-09-01T00:00:00.000Z',
cascadeCreate: true,
recommId,
additionalData: {
region: 'region'
}
}
}
]
})
})

it('should validate action fields with multiple items', async () => {
nock('https://rapi-eu-west.recombee.com/')
.post(`/${DATABASE_ID}/batch/`)
.query({
hmac_timestamp: /.*/,
hmac_sign: /.*/
})
.reply(200, [
{ code: 200, json: 'ok' },
{ code: 200, json: 'ok' }
])

const event = createTestEvent({
userId: 'user-id',
properties: {
products: [
{
product_id: 'item-1',
quantity: 1,
price: 100
},
{
product_id: 'item-2',
quantity: 2,
price: 200
}
]
},
timestamp: '2021-09-01T00:00:00.000Z'
})

const response = await testDestination.testAction('addCartAddition', {
event,
settings: SETTINGS,
useDefaultMappings: true,
mapping: {
items: {
'@arrayPath': [
'$.properties.products',
{
itemId: {
'@path': '$.product_id'
},
amount: {
'@path': '$.quantity'
},
price: {
'@path': '$.price'
}
}
]
}
itemId: 'product-id',
timestamp: '2021-09-01T00:00:00.000Z',
cascadeCreate: true,
recommId,
additionalData: {
region: 'region'
}
})

expect(await response[0].request.json()).toMatchObject({
requests: [
{
method: 'POST',
path: '/cartadditions/',
params: {
userId: 'user-id',
itemId: 'item-1',
timestamp: '2021-09-01T00:00:00.000Z',
cascadeCreate: true,
amount: 1,
price: 100
}
},
{
method: 'POST',
path: '/cartadditions/',
params: {
userId: 'user-id',
itemId: 'item-2',
timestamp: '2021-09-01T00:00:00.000Z',
cascadeCreate: true,
amount: 2,
price: 200
}
}
]
})
})

it('should throw an error when fields are not mapped', async () => {
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 @@ -22,11 +22,10 @@ const action: ActionDefinition<Settings, Payload> = {
}
}
},
items: {
label: 'Items',
description: 'The items that were added to the cart.',
item: {
label: 'Item',
description: 'The item that was added to the cart.',
type: 'object',
multiple: true,
required: true,
properties: {
itemId: {
Expand All @@ -51,25 +50,15 @@ const action: ActionDefinition<Settings, Payload> = {
}
},
default: {
// Product Added is an event with a single product
'@arrayPath': [
'$.properties',
{
itemId: {
'@if': {
exists: { '@path': '$.product_id' },
then: { '@path': '$.product_id' },
else: { '@path': '$.asset_id' }
}
},
amount: {
'@path': '$.quantity'
},
price: {
'@path': '$.price'
}
itemId: {
'@if': {
exists: { '@path': '$.properties.product_id' },
then: { '@path': '$.properties.product_id' },
else: { '@path': '$.properties.sku' }
}
]
},
amount: { '@path': '$.properties.quantity' },
price: { '@path': '$.properties.price' }
}
},
timestamp: {
Expand All @@ -83,25 +72,22 @@ const action: ActionDefinition<Settings, Payload> = {
},
perform: async (request, data) => {
const client = new RecombeeApiClient(data.settings, request)
await client.send(new Batch(payloadToInteractions(data.payload)))
await client.send(payloadToInteraction(data.payload))
},
performBatch: async (request, data) => {
const client = new RecombeeApiClient(data.settings, request)
await client.send(new Batch(data.payload.flatMap(payloadToInteractions)))
await client.send(new Batch(data.payload.map(payloadToInteraction)))
}
}

function payloadToInteractions(payload: Payload): AddCartAddition[] {
return payload.items.map(
(item) =>
new AddCartAddition({
userId: payload.userId,
...item,
timestamp: payload.timestamp,
recommId: payload.recommId,
additionalData: payload.additionalData
})
)
function payloadToInteraction(payload: Payload): AddCartAddition {
return new AddCartAddition({
userId: payload.userId,
...payload.item,
timestamp: payload.timestamp,
recommId: payload.recommId,
additionalData: payload.additionalData
})
}

export default action
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ export function interactionFields(interactionName: string): Record<string, Input
export const ecommerceIdMapping = {
itemId: {
'@if': {
exists: { '@path': '$.product_id' },
then: { '@path': '$.product_id' },
else: { '@path': '$.sku' }
exists: { '@path': '$.properties.product_id' },
then: { '@path': '$.properties.product_id' },
else: { '@path': '$.properties.sku' }
}
}
}
Expand Down
Loading
Loading