ChemPal Documentation - v1.18.0
    Preparing search index...

    Function collectNativePrices

    • Collects a product's prices by price-history series: each size, plus the headline when no size has its price. Sizes that can't be told apart are left out, since their order may change.

      Parameters

      • product: Product

        The product, with all its sizes

      Returns Record<string, PriceSnapshot>

      Prices by series id

      collectNativePrices(product); // { '3f9c2a::500g': { title: 'Acetone 500 g', price: 24, … } }
      
      export function collectNativePrices(product: Product): Record<string, PriceSnapshot> {
      const prices: Record<string, PriceSnapshot> = {};
      const ambiguous = new Set<string>();

      for (const variant of product.variants ?? []) {
      const id = variantSeriesKey(product, variant);
      if (id === undefined || !isRecordablePrice(variant.price)) continue;
      if (id in prices) ambiguous.add(id);
      prices[id] = {
      title: variantRowTitle(product, variant),
      price: round2(variant.price),
      currencyCode: variant.currencyCode ?? product.currencyCode,
      quantity: variant.quantity,
      uom: variant.uom,
      };
      }
      for (const id of ambiguous) delete prices[id];
      const variantPrices = new Set(Object.values(prices).map(({ price }) => price));

      const id = productSeriesKey(product);
      if (id === undefined || !isRecordablePrice(product.price)) return prices;
      const price = round2(product.price);
      if (variantPrices.has(price)) return prices;

      prices[id] = {
      title: product.title,
      price,
      currencyCode: product.currencyCode,
      quantity: product.quantity,
      uom: product.uom,
      };
      return prices;
      }