-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement offer enhancement and fetching logic with aggregation
- Loading branch information
1 parent
4d5dc0d
commit 23aa36c
Showing
8 changed files
with
293 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { Item, Seller } from '@faststore/api' | ||
import { EnhancedCommercialOffer } from './enhance' | ||
import { inStock, price } from './sort' | ||
|
||
type Root = EnhancedCommercialOffer<Seller, Item> | ||
|
||
const withTax = ( | ||
price: number, | ||
tax: number = 0, | ||
unitMultiplier: number = 1 | ||
) => { | ||
const unitTax = tax / unitMultiplier | ||
return Math.round((price + unitTax) * 100) / 100 | ||
} | ||
|
||
const getHighPrice = ( | ||
offers: Root[], | ||
options: { includeTaxes: boolean } = { includeTaxes: false } | ||
) => { | ||
const availableOffers = offers.filter(inStock) | ||
const highOffer = availableOffers[availableOffers.length - 1] | ||
const highPrice = highOffer ? price(highOffer) : 0 | ||
if (!options.includeTaxes) { | ||
return highPrice | ||
} | ||
|
||
return withTax(highPrice, highOffer?.Tax, highOffer?.product?.unitMultiplier) | ||
} | ||
|
||
const getLowPrice = ( | ||
offers: Root[], | ||
options: { includeTaxes: boolean } = { includeTaxes: false } | ||
) => { | ||
const [lowOffer] = offers.filter(inStock) | ||
|
||
const lowPrice = lowOffer ? price(lowOffer) : 0 | ||
|
||
if (!options.includeTaxes) { | ||
return lowPrice | ||
} | ||
|
||
return withTax(lowPrice, lowOffer?.Tax, lowOffer?.product?.unitMultiplier) | ||
} | ||
|
||
export function aggregateOffer(offers: Root[]) { | ||
return { | ||
highPrice: getHighPrice(offers), | ||
lowPrice: getLowPrice(offers), | ||
lowPriceWithTaxes: getLowPrice(offers, { includeTaxes: true }), | ||
offerCount: offers.length, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { CommertialOffer } from '@faststore/api' | ||
|
||
export type EnhancedCommercialOffer<S, P> = CommertialOffer & { | ||
seller: S | ||
product: P | ||
} | ||
|
||
export const enhanceCommercialOffer = <S, P>({ | ||
offer, | ||
seller, | ||
product, | ||
}: { | ||
offer: CommertialOffer | ||
seller: S | ||
product: P | ||
}): EnhancedCommercialOffer<S, P> => ({ | ||
...offer, | ||
product, | ||
seller, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { ProductSearchResult } from '@faststore/api' | ||
import { api, storeUrl } from '../../../faststore.config' | ||
|
||
const IS_PROD = process.env.NODE_ENV === 'production' | ||
|
||
export function getUrl(skuId: string) { | ||
const base = IS_PROD | ||
? storeUrl | ||
: `https://${api.storeId}.${api.environment}.com.br` | ||
const url = new URL(`${base}/api/intelligent-search/product_search`) | ||
url.searchParams.append('query', `sku.id:${skuId}`) | ||
return url.toString() | ||
} | ||
|
||
export async function fetcher(skuId: string) { | ||
return fetch(getUrl(skuId)).then((res) => | ||
res.json() | ||
) as Promise<ProductSearchResult> | ||
} |
Oops, something went wrong.