Skip to content

Commit

Permalink
fix: Return price as offer.Price instead of offer.spotPrice
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfp13 committed Dec 19, 2024
1 parent 6a034d8 commit 67c48e6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
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 },
])
})
})

0 comments on commit 67c48e6

Please sign in to comment.