From 2a44dde9cb3cef5e5d05eb453a4eec357b106062 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Carneiro?= Date: Fri, 22 Nov 2024 15:48:44 +0000 Subject: [PATCH] Refactor extractPricingEntitiesBySlug --- src/pricing.ts | 55 +++++++++++++++++++++++++------------------------- 1 file changed, 27 insertions(+), 28 deletions(-) diff --git a/src/pricing.ts b/src/pricing.ts index 819ca7e..67e10b1 100644 --- a/src/pricing.ts +++ b/src/pricing.ts @@ -159,39 +159,38 @@ const getPriceComponents = (priceItem: CompositePriceItemDto): PriceComponent[] * @returns the product and price relations from the price items grouped by their slug. */ export const extractPricingEntitiesBySlug = ( - priceItems?: (PriceItem | CompositePriceItem)[], + priceItems: Array = [], ): PricingEntitiesExtractResult => { - const productRelations = [] as RelationAttributeValue['$relation']; - const priceRelations = [] as RelationAttributeValue['$relation']; - const priceLookup: Record = {}; - const productLookup: Record = {}; - const pricingTags: string[] = []; - - priceItems?.forEach((item) => { - if (item?.product_id && !productLookup[item.product_id]) { - productRelations.push({ - entity_id: item.product_id, - _schema: 'product', - _tags: [], - }); - pricingTags.push(...(item._product?._tags || [])); - productLookup[item.product_id] = true; + const productSet = new Set(); + const priceSet = new Set(); + const tagSet = new Set(); + + for (const item of priceItems) { + /** + * Account for possibility that function might be called with + * array containing nullish items, even though it's typed not to + */ + if (!item) continue; + + if (item.product_id) productSet.add(item.product_id); + if (item.price_id) priceSet.add(item.price_id); + + const productTags = item._product?._tags?.filter(Boolean); + const priceTags = item._price?._tags?.filter(Boolean); + + for (const tag of productTags ?? []) { + tagSet.add(tag); } - if (item?.price_id && !priceLookup[item.price_id]) { - priceRelations.push({ - entity_id: item.price_id, - _schema: 'price', - _tags: [], - }); - pricingTags.push(...(item._price?._tags || [])); - priceLookup[item.price_id] = true; + + for (const tag of priceTags ?? []) { + tagSet.add(tag); } - }); + } return { - product: { $relation: productRelations }, - price: { $relation: priceRelations }, - _tags: [...new Set(pricingTags)], + product: { $relation: Array.from(productSet).map((id) => ({ entity_id: id, _schema: 'product', _tags: [] })) }, + price: { $relation: Array.from(priceSet).map((id) => ({ entity_id: id, _schema: 'price', _tags: [] })) }, + _tags: Array.from(tagSet), }; };