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

chore: Best offer first #1184

Merged
merged 3 commits into from
Mar 23, 2022
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
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',
},
])
})
})