Skip to content
Open
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

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 @@ -24,6 +24,14 @@ const destination: DestinationDefinition<Settings> = {
description: 'Created under Settings > API Integration in the Topsort Manager Platform.',
type: 'password',
required: true
},
skipZeroPricePurchases: {
label: 'Skip Zero Price Purchases',
description:
'When enabled, purchase events with items that have zero or missing unit price will be filtered out.',
type: 'boolean',
required: false,
default: false
}
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,141 @@ describe('Topsort.purchase', () => {
})
).rejects.toThrowError(AggregateAjvError)
})

it('should filter out items with zero price when skipZeroPricePurchases is enabled', async () => {
nock(/.*/).persist().post(/.*/).reply(200)

const event = createTestEvent({
properties: {
products: [
{
product_id: '123',
price: 100,
quantity: 1
},
{
product_id: '456',
price: 0,
quantity: 2
},
{
product_id: '789',
price: 50,
quantity: 1
}
]
}
})

const responses = await testDestination.testAction('purchase', {
event,
settings: {
api_key: 'bar',
skipZeroPricePurchases: true
},
useDefaultMappings: true
})

expect(responses.length).toBe(1)
expect(responses[0].status).toBe(200)
expect(responses[0].options.json).toMatchObject({
purchases: expect.arrayContaining([
expect.objectContaining({
items: [
{
productId: '123',
unitPrice: 100,
quantity: 1
},
{
productId: '789',
unitPrice: 50,
quantity: 1
}
]
})
])
})
})

it('should skip entire purchase when all items have zero price and skipZeroPricePurchases is enabled', async () => {
const event = createTestEvent({
properties: {
products: [
{
product_id: '123',
price: 0,
quantity: 1
},
{
product_id: '456',
price: 0,
quantity: 2
}
]
}
})

const responses = await testDestination.testAction('purchase', {
event,
settings: {
api_key: 'bar',
skipZeroPricePurchases: true
},
useDefaultMappings: true
})

expect(responses.length).toBe(0)
})

it('should send all items including zero price when skipZeroPricePurchases is disabled', async () => {
nock(/.*/).persist().post(/.*/).reply(200)

const event = createTestEvent({
properties: {
products: [
{
product_id: '123',
price: 100,
quantity: 1
},
{
product_id: '456',
price: 0,
quantity: 2
}
]
}
})

const responses = await testDestination.testAction('purchase', {
event,
settings: {
api_key: 'bar',
skipZeroPricePurchases: false
},
useDefaultMappings: true
})

expect(responses.length).toBe(1)
expect(responses[0].status).toBe(200)
expect(responses[0].options.json).toMatchObject({
purchases: expect.arrayContaining([
expect.objectContaining({
items: [
{
productId: '123',
unitPrice: 100,
quantity: 1
},
{
productId: '456',
unitPrice: 0,
quantity: 2
}
]
})
])
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,15 @@ describe(`Testing snapshot for ${destinationSlug}'s ${actionSlug} destination ac
const responses = await testDestination.testAction(actionSlug, {
event: event,
mapping: event.properties,
settings: settingsData,
settings: { ...settingsData, skipZeroPricePurchases: false },
auth: undefined
})

if (!responses[0].request) {
expect(responses[0].options.json).toMatchSnapshot()
return
}

const request = responses[0].request
const rawBody = await request.text()

Expand Down Expand Up @@ -57,10 +62,15 @@ describe(`Testing snapshot for ${destinationSlug}'s ${actionSlug} destination ac
const responses = await testDestination.testAction(actionSlug, {
event: event,
mapping: event.properties,
settings: settingsData,
settings: { ...settingsData, skipZeroPricePurchases: false },
auth: undefined
})

if (!responses[0].request) {
expect(responses[0].options.json).toMatchSnapshot()
return
}

const request = responses[0].request
const rawBody = await request.text()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,17 @@ const action: ActionDefinition<Settings, Payload> = {
},
perform: (request, { payload, settings }) => {
const client = new TopsortAPIClient(request, settings)

if (settings.skipZeroPricePurchases) {
const filteredItems = payload.items.filter((item) => item.unitPrice != null && item.unitPrice > 0)

if (filteredItems.length === 0) {
return
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @FelipeRiveraC ,

You could throw a PayloadValidationError here if you liked - it provides feedback to the customer about what happened.

It's OK not to also. Up to you.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, we actually dont want to provide feedback, so its ok. Thanks!

}

payload.items = filteredItems
}

return client.sendEvent({
purchases: [payload]
})
Expand Down
Loading