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

feat: vendorId in purchase #21

Merged
merged 4 commits into from
Feb 20, 2025
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 @@ -15,3 +15,35 @@ Headers {
},
}
`;

exports[`Topsort.purchase should be successful with default mappings and products object, including brand as vendorId 1`] = `
Headers {
Symbol(map): Object {
"authorization": Array [
"Bearer bar",
],
"content-type": Array [
"application/json",
],
"user-agent": Array [
"Segment (Actions)",
],
},
}
`;

exports[`Topsort.purchase should be successful with default mappings and products object, including custom vendorId 1`] = `
Headers {
Symbol(map): Object {
"authorization": Array [
"Bearer bar",
],
"content-type": Array [
"application/json",
],
"user-agent": Array [
"Segment (Actions)",
],
},
}
`;
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Object {
"productId": "4mevd8V",
"quantity": -6621698275147776,
"unitPrice": -66216982751477.76,
"vendorId": "4mevd8V",
},
],
"occurredAt": "2021-02-01T00:00:00.000Z",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,13 @@ describe('Topsort.purchase', () => {
nock(/.*/).persist().post(/.*/).reply(200)

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

const responses = await testDestination.testAction('purchase', {
Expand Down Expand Up @@ -50,6 +48,94 @@ describe('Topsort.purchase', () => {
})
})

it('should be successful with default mappings and products object, including brand as vendorId', async () => {
nock(/.*/).persist().post(/.*/).reply(200)

const event = createTestEvent({
products: [
{
product_id: '123',
price: 100,
quantity: 1,
brand: 'v123'
}
]
})

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

expect(responses.length).toBe(1)
expect(responses[0].status).toBe(200)
expect(responses[0].options.headers).toMatchSnapshot()
expect(responses[0].options.json).toMatchObject({
purchases: expect.arrayContaining([
expect.objectContaining({
id: expect.any(String),
occurredAt: expect.any(String),
opaqueUserId: expect.any(String),
items: [
{
productId: '123',
unitPrice: 100,
quantity: 1,
vendorId: 'v123'
}
]
})
])
})
})

it('should be successful with default mappings and products object, including custom vendorId', async () => {
nock(/.*/).persist().post(/.*/).reply(200)

const event = createTestEvent({
products: [
{
product_id: '123',
price: 100,
quantity: 1,
vendorId: 'v123'
}
]
})

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

expect(responses.length).toBe(1)
expect(responses[0].status).toBe(200)
expect(responses[0].options.headers).toMatchSnapshot()
expect(responses[0].options.json).toMatchObject({
purchases: expect.arrayContaining([
expect.objectContaining({
id: expect.any(String),
occurredAt: expect.any(String),
opaqueUserId: expect.any(String),
items: [
{
productId: '123',
unitPrice: 100,
quantity: 1,
vendorId: 'v123'
}
]
})
])
})
})

it('should fail because it misses a required field (products)', async () => {
nock(/.*/).persist().post(/.*/).reply(200)

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 @@ -61,15 +61,28 @@ const action: ActionDefinition<Settings, Payload> = {
description: 'Count of products purchased.',
type: 'integer',
required: false
},
vendorId: {
label: 'Vendor ID',
description: 'The vendor ID of the product being purchased.',
type: 'string',
required: false
}
},
default: {
'@arrayPath': [
'$.properties.products',
'$.products',
{
productId: { '@path': '$.product_id' },
unitPrice: { '@path': '$.price' },
quantity: { '@path': '$.quantity' }
quantity: { '@path': '$.quantity' },
vendorId: {
'@if': {
exists: { '@path': '$.vendorId' },
then: { '@path': '$.vendorId' },
else: { '@path': '$.brand' }
}
}
}
]
}
Expand Down
Loading