Skip to content

Commit

Permalink
chore: Best offer first (#1184)
Browse files Browse the repository at this point in the history
  • Loading branch information
tlgimenes authored Mar 23, 2022
1 parent 3481f23 commit 66c8f36
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 12 deletions.
41 changes: 31 additions & 10 deletions packages/api/src/platforms/vtex/resolvers/aggregateOffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,38 @@ import type { Simulation } from '../clients/commerce/types/Simulation'

type Resolvers = (root: Simulation & { product: EnhancedSku }) => unknown

const inStock = (item: Simulation['items'][0]) =>
item.availability === 'available'

// Smallest Available Selling Price First
export const sortOfferByPrice = (
items: Simulation['items']
): Simulation['items'] =>
items.sort((a, b) => {
if (inStock(a) && !inStock(b)) {
return -1
}

if (!inStock(a) && inStock(b)) {
return 1
}

return a.sellingPrice - b.sellingPrice
})

export const StoreAggregateOffer: Record<string, Resolvers> = {
highPrice: ({ items }) =>
items.reduce(
(acc, curr) => (acc > curr.sellingPrice ? acc : curr.sellingPrice),
items[0]?.sellingPrice ?? 0
) / 1e2,
lowPrice: ({ items }) =>
items.reduce(
(acc, curr) => (acc < curr.sellingPrice ? acc : curr.sellingPrice),
items[0]?.sellingPrice ?? 0
) / 1e2,
highPrice: ({ items }) => {
const availableItems = items.filter(inStock)
const highPrice = availableItems.pop()?.sellingPrice

return (highPrice ?? 0) / 1e2
},
lowPrice: ({ items }) => {
const availableItems = items.filter(inStock)
const lowPrice = availableItems[0]?.sellingPrice

return (lowPrice ?? 0) / 1e2
},
offerCount: ({ items }) => items.length,
priceCurrency: () => '',
offers: ({ items, product }) => items.map((item) => ({ ...item, product })),
Expand Down
9 changes: 7 additions & 2 deletions packages/api/src/platforms/vtex/resolvers/product.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { slugify } from '../utils/slugify'
import type { Resolver } from '..'
import type { EnhancedSku } from '../utils/enhanceSku'
import { slugify } from '../utils/slugify'
import { sortOfferByPrice } from './aggregateOffer'

type Root = EnhancedSku

Expand Down Expand Up @@ -81,7 +82,11 @@ export const StoreProduct: Record<string, Resolver<Root>> = {

const simulation = await simulationLoader.load(items)

return { ...simulation, product }
return {
...simulation,
items: sortOfferByPrice(simulation.items),
product,
}
},
isVariantOf: ({ isVariantOf }) => isVariantOf,
additionalProperty: ({ attributes = [] }) =>
Expand Down
51 changes: 51 additions & 0 deletions packages/api/test/vtex.aggregateOffer.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { sortOfferByPrice } from '../src/platforms/vtex/resolvers/aggregateOffer'

describe('AggregateOffer', () => {
it('Should return best offers first', () => {
const sorted = sortOfferByPrice([
{
sellingPrice: 10,
availability: 'available',
},
{
sellingPrice: 20,
availability: 'unavailable',
},
{
sellingPrice: 30,
availability: 'available',
},
{
sellingPrice: 10,
availability: 'unavailable',
},
{
sellingPrice: 1,
availability: 'available',
},
] as any)

expect(sorted).toEqual([
{
sellingPrice: 1,
availability: 'available',
},
{
sellingPrice: 10,
availability: 'available',
},
{
sellingPrice: 30,
availability: 'available',
},
{
sellingPrice: 10,
availability: 'unavailable',
},
{
sellingPrice: 20,
availability: 'unavailable',
},
])
})
})

0 comments on commit 66c8f36

Please sign in to comment.