From 599197c4834bddc87978fb65a0f2b8b4366269f5 Mon Sep 17 00:00:00 2001 From: EJ Mercado Date: Fri, 29 May 2020 15:53:48 +0800 Subject: [PATCH] feat: get lowest priced offers for sku --- src/sections/products/codec.ts | 74 +++++++++++++++---- test/unit/__snapshots__/products.test.ts.snap | 23 +++--- 2 files changed, 74 insertions(+), 23 deletions(-) diff --git a/src/sections/products/codec.ts b/src/sections/products/codec.ts index 5cf05e41..85073e28 100644 --- a/src/sections/products/codec.ts +++ b/src/sections/products/codec.ts @@ -212,20 +212,34 @@ export const GetLowestOfferListingsForASINResponse = Codec.interface({ * This has attributes instead of children, how do we handle that? * http://docs.developer.amazonservices.com/en_CA/products/Products_Datatypes.html#OfferCount */ -// const OfferCountType = number +const OfferCountType = Codec.interface({ + OfferCount: number, +}) -// const LowestPrice +const LowestPrice = Codec.interface({ + LandedPrice: MoneyType, + ListingPrice: MoneyType, + Shipping: MoneyType, + Points: optional(Points), +}) -// const Summary = Codec.interface({ -// TotalOfferCount: number, -// NumberOfOffers: OfferCountType, -// LowestPrices: optional(ensureArray('LowestPrice', LowestPrice)), -// BuyBoxPrices: optional(ensureArray('BuyBoxPrice', BuyBoxPrice)), -// ListPrice: optional(MoneyType), -// SuggestedLowerPricePlusShipping: optional(MoneyType), -// BuyBoxEligibleOffers: optional(OfferCountType), -// OffersAvailableTime: optional(mwsDate) -// }) +const BuyBoxPrice = Codec.interface({ + LandedPrice: MoneyType, + ListingPrice: MoneyType, + Shipping: MoneyType, + Points: optional(Points), +}) + +const Summary = Codec.interface({ + TotalOfferCount: number, + NumberOfOffers: OfferCountType, + LowestPrices: optional(ensureArray('LowestPrice', LowestPrice)), + BuyBoxPrices: optional(ensureArray('BuyBoxPrice', BuyBoxPrice)), + ListPrice: optional(MoneyType), + SuggestedLowerPricePlusShipping: optional(MoneyType), + BuyBoxEligibleOffers: optional(OfferCountType), + OffersAvailableTime: optional(mwsDate), +}) const Identifier = Codec.interface({ MarketplaceId: string, @@ -234,10 +248,39 @@ const Identifier = Codec.interface({ TimeOfOfferChange: optional(mwsDate), }) +const SellerFeedbackRating = Codec.interface({ + SellerPositiveFeedbackRating: optional(number), + FeedbackCount: number, +}) + +/** + * Does not have elements, but is defined by attributes + */ +const DetailedShippingTimeType = optional(unknown) + +const ShipsFrom = Codec.interface({ + State: optional(string), + Country: optional(string), +}) + +const Offer = Codec.interface({ + MyOffer: optional(boolean), + SubCondition: string, + SellerFeedbackRating: optional(SellerFeedbackRating), + ShippingTime: DetailedShippingTimeType, + ListingPrice: MoneyType, + Points: optional(Points), + Shipping: MoneyType, + ShipsFrom: optional(ShipsFrom), + IsFulfilledByAmazon: boolean, + IsBuyBoxWinner: optional(boolean), + IsFeaturedMerchant: optional(boolean), +}) + const GetLowestPricedOffersForSKU = Codec.interface({ Identifier, - Summary: unknown, - Offers: unknown, + Summary, + Offers: ensureArray('Offer', Offer), }) export const GetLowestPricedOffersForSKUResponse = Codec.interface({ @@ -245,8 +288,11 @@ export const GetLowestPricedOffersForSKUResponse = Codec.interface({ GetLowestPricedOffersForSKUResult: GetLowestPricedOffersForSKU, }), }) + /** + * * Types derived from codecs + * */ export type GetMatchingProductForIdResponse = GetInterface diff --git a/test/unit/__snapshots__/products.test.ts.snap b/test/unit/__snapshots__/products.test.ts.snap index cbc470e8..c52d87e4 100644 --- a/test/unit/__snapshots__/products.test.ts.snap +++ b/test/unit/__snapshots__/products.test.ts.snap @@ -399,8 +399,8 @@ Array [ "SellerSKU": "GE Product", "TimeOfOfferChange": 2015-07-19T23:15:11.859Z, }, - "Offers": Object { - "Offer": Object { + "Offers": Array [ + Object { "IsBuyBoxWinner": true, "IsFeaturedMerchant": true, "IsFulfilledByAmazon": true, @@ -409,6 +409,7 @@ Array [ "CurrencyCode": "USD", }, "MyOffer": false, + "Points": undefined, "SellerFeedbackRating": Object { "FeedbackCount": 1, "SellerPositiveFeedbackRating": 100, @@ -418,15 +419,16 @@ Array [ "CurrencyCode": "USD", }, "ShippingTime": "", + "ShipsFrom": undefined, "SubCondition": "new", }, - }, + ], "Summary": Object { "BuyBoxEligibleOffers": Object { "OfferCount": 1, }, - "BuyBoxPrices": Object { - "BuyBoxPrice": Object { + "BuyBoxPrices": Array [ + Object { "LandedPrice": Object { "Amount": 32.99, "CurrencyCode": "USD", @@ -435,18 +437,19 @@ Array [ "Amount": 32.99, "CurrencyCode": "USD", }, + "Points": undefined, "Shipping": Object { "Amount": 0, "CurrencyCode": "USD", }, }, - }, + ], "ListPrice": Object { "Amount": 58.34, "CurrencyCode": "USD", }, - "LowestPrices": Object { - "LowestPrice": Object { + "LowestPrices": Array [ + Object { "LandedPrice": Object { "Amount": 32.99, "CurrencyCode": "USD", @@ -455,15 +458,17 @@ Array [ "Amount": 32.99, "CurrencyCode": "USD", }, + "Points": undefined, "Shipping": Object { "Amount": 0, "CurrencyCode": "USD", }, }, - }, + ], "NumberOfOffers": Object { "OfferCount": 1, }, + "OffersAvailableTime": undefined, "SuggestedLowerPricePlusShipping": Object { "Amount": 32.99, "CurrencyCode": "USD",