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

[1.x] fix: Return price as offer's Price instead of spotPrice #2603

Draft
wants to merge 1 commit into
base: 1.x
Choose a base branch
from
Draft
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
8 changes: 4 additions & 4 deletions packages/api/src/platforms/vtex/utils/productStock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ import type { CommertialOffer } from '../clients/search/types/ProductSearchResul
export const inStock = (offer: Pick<CommertialOffer, 'AvailableQuantity'>) =>
offer.AvailableQuantity > 0

export const price = (offer: Pick<CommertialOffer, 'spotPrice'>) =>
offer.spotPrice ?? 0
export const price = (offer: Pick<CommertialOffer, 'Price'>) => offer.Price ?? 0

export const sellingPrice = (offer: CommertialOffer) => offer.Price ?? 0

export const availability = (available: boolean) =>
available ? 'https://schema.org/InStock' : 'https://schema.org/OutOfStock'

// Smallest Available Spot Price First
export const bestOfferFirst = (
a: Pick<CommertialOffer, 'AvailableQuantity' | 'spotPrice'>,
b: Pick<CommertialOffer, 'AvailableQuantity' | 'spotPrice'>
a: Pick<CommertialOffer, 'AvailableQuantity' | 'Price'>,
b: Pick<CommertialOffer, 'AvailableQuantity' | 'Price'>
) => {
if (inStock(a) && !inStock(b)) {
return -1
Expand Down
22 changes: 11 additions & 11 deletions packages/api/test/vtex.aggregateOffer.test.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
import { bestOfferFirst } from '../src/platforms/vtex/utils/productStock'
import type { CommertialOffer } from '../src/platforms/vtex/clients/search/types/ProductSearchResult'

type TestItem = Pick<CommertialOffer, 'AvailableQuantity' | 'spotPrice'>
type TestItem = Pick<CommertialOffer, 'AvailableQuantity' | 'Price'>

describe('AggregateOffer', () => {
it('Should return best offers first', () => {
const testItems: TestItem[] = [
{ AvailableQuantity: 1, spotPrice: 10 },
{ AvailableQuantity: 0, spotPrice: 20 },
{ AvailableQuantity: 1, spotPrice: 30 },
{ AvailableQuantity: 0, spotPrice: 10 },
{ AvailableQuantity: 1, spotPrice: 1 },
{ AvailableQuantity: 1, Price: 10 },
{ AvailableQuantity: 0, Price: 20 },
{ AvailableQuantity: 1, Price: 30 },
{ AvailableQuantity: 0, Price: 10 },
{ AvailableQuantity: 1, Price: 1 },
]

const sorted = testItems.sort(bestOfferFirst)

expect(sorted).toEqual([
{ AvailableQuantity: 1, spotPrice: 1 },
{ AvailableQuantity: 1, spotPrice: 10 },
{ AvailableQuantity: 1, spotPrice: 30 },
{ AvailableQuantity: 0, spotPrice: 10 },
{ AvailableQuantity: 0, spotPrice: 20 },
{ AvailableQuantity: 1, Price: 1 },
{ AvailableQuantity: 1, Price: 10 },
{ AvailableQuantity: 1, Price: 30 },
{ AvailableQuantity: 0, Price: 10 },
{ AvailableQuantity: 0, Price: 20 },
])
})
})
Loading