All files / src/suppliers SupplierBaseMySimpleStore.ts

84.97% Statements 198/233
45.94% Branches 17/37
100% Functions 10/10
84.97% Lines 198/233

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 4011x 1x 1x 1x 1x 1x 1x 1x 1x 1x                                   1x 1x 1x           1x 1x 1x 1x 1x 1x 1x   1x                   1x 1x 6x 6x   1x                       1x 1x 3x 3x   1x                   1x 1x 19x 19x   1x                 1x 1x 34x 34x   1x                       1x 1x 19x 19x   1x                           1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x               1x   1x 1x 1x 1x 1x   1x 1x   1x                         1x 1x 2x 19x 19x   19x 19x 19x 19x 19x 19x 19x 19x 19x   19x 19x 19x   19x 19x 2x   1x                             1x 1x 6x     6x 6x   1x                             1x 1x 4x   4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x   4x 4x 4x 4x 4x 4x   4x 4x 4x 4x 4x 4x   4x 4x 4x 4x   4x 4x   1x                           1x 1x 1x 1x 1x 1x 1x         1x 1x 1x 1x         1x 1x 1x 1x 1x 1x 1x 1x                     1x               1x 1x 1x 1x 1x 1x 1x 1x 5x 5x 5x 1x 5x 4x 4x 5x 1x 1x 1x   1x 1x 1x 3x 3x 3x 3x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                 1x 1x 1x   1x 1x 1x 1x  
import { UOM } from '@/constants/common';
import { parsePrice } from '@/helpers/currency';
import { parseQuantity, toBaseQuantity } from '@/helpers/quantity';
import { htmlToAscii, mapDefined } from '@/helpers/utils';
import { ProductBuilder } from '@/utils/ProductBuilder';
import { isValidSearchResponse, isProductDetail } from '@/utils/typeGuards/mysimplestore';
import { isValidVariant } from '@/utils/typeGuards/productbuilder';
import { SupplierBase } from './SupplierBase';
import { getErrorMessage } from '@/helpers/exceptions';
/**
 * Abstract base for suppliers running on the MySimpleStore / GoDaddy "Online Store"
 * platform (a Spree-based JSON storefront). The API lives at
 * `https://{storeId}.mysimplestore.com/api/v2`, keyed by the store's uuid, while the
 * human-facing product pages live on the supplier's own `baseURL`.
 *
 * This is a two-phase supplier: the search/list endpoint returns products without
 * their variants (only a `variant_count`), so {@link queryProducts} builds partial
 * products from the listing and {@link getProductData} fetches each product's detail
 * endpoint to enumerate the per-size variants.
 *
 * Concrete suppliers provide only their identity (`supplierName`, `baseURL`,
 * `storeId`) and shipping/payment metadata.
 * @typeParam MySimpleStoreListProduct - The raw search-list item type
 * @typeParam Product - The common Product type all suppliers map to
 * @abstract
 * @category Suppliers
 * @source
 */
export abstract class SupplierBaseMySimpleStore
  extends SupplierBase<MySimpleStoreListProduct, Product>
  implements ISupplier
{
  /** The MySimpleStore store id (uuid) that keys every API request */
  public abstract readonly storeId: string;
 
  /** Default values for products */
  protected productDefaults = {
    uom: UOM.EA,
    quantity: 1,
    currencyCode: 'USD',
    currencySymbol: '$',
  };
 
  /**
   * The MySimpleStore API host derived from the store id. All API requests set
   * this as the `host` override so the request targets the storefront API while
   * the referrer/base stays on the supplier's own site.
   * @returns The API hostname (e.g. "7692587b-...-a6ebcdb36c13.mysimplestore.com")
   * @example
   * ```typescript
   * this.apiHost; // "7692587b-61ba-4b63-b329-a6ebcdb36c13.mysimplestore.com"
   * ```
   * @source
   */
  protected get apiHost(): string {
    return `${this.storeId}.mysimplestore.com`;
  }
 
  /**
   * All host origin patterns required for this supplier, extending the base
   * (which contributes `baseURL`) with the MySimpleStore API host. The concrete
   * `{storeId}.mysimplestore.com` host is covered at runtime by the wildcard
   * `https://*.mysimplestore.com/*` manifest permission.
   * @returns The list of `https://host/*` permission patterns
   * @example
   * ```typescript
   * this.requiredHosts;
   * // ["https://orbitnaturalproductderivatives.com/*", "https://7692587b-....mysimplestore.com/*"]
   * ```
   * @source
   */
  public override get requiredHosts(): string[] {
    return [...super.requiredHosts, `https://${this.apiHost}/*`];
  }
 
  /**
   * Derives the unique product key from a MySimpleStore search item: its stable
   * catalog uuid (the same value passed to `.setID`).
   * @param data - The raw MySimpleStore search-list product
   * @returns The product's id
   * @example
   * ```typescript
   * this.getUniqueProductKey(product); // "019dda5e-dae6-756d-90db-a579bb95294f"
   * ```
   * @source
   */
  protected getUniqueProductKey(data: MySimpleStoreListProduct): string {
    return String(data.id);
  }
 
  /**
   * Selects the title of a product from the search response.
   * @param data - Product object from the search response
   * @returns The product name
   * @example
   * ```typescript
   * this.titleSelector(product); // "Thymol"
   * ```
   * @source
   */
  protected titleSelector(data: MySimpleStoreListProduct): string {
    return data.name;
  }
 
  /**
   * Builds the human-facing product-page URL from a search item's storefront
   * path. MySimpleStore serves product pages under `/products` on the supplier's
   * own domain, so the API's `relative_url` (`/ols/products/<slug>`) is prefixed
   * with `/products`.
   * @param product - The raw MySimpleStore search-list product
   * @returns The absolute product-page URL
   * @example
   * ```typescript
   * this.productUrl(product); // "https://onpd.com/products/ols/products/geraniol-60"
   * ```
   * @source
   */
  protected productUrl(product: MySimpleStoreListProduct): string {
    return `${this.baseURL}/products${product.relative_url}`;
  }
 
  /**
   * Queries products from the MySimpleStore search endpoint. Issues a single GET
   * to `/api/v2/products` with the keyword filters the storefront's own search UI
   * uses, fuzzy-filters the returned products against the query, and initializes
   * partial product builders (variants are filled in later by {@link getProductData}).
   * @param query - The search term to query products for
   * @param limit - The maximum number of products to return
   * @returns Promise resolving to an array of ProductBuilder instances, or void if the search fails
   * @example
   * ```typescript
   * const results = await this.queryProducts("thymol", 10);
   * if (results) console.log(`Found ${results.length} products`);
   * ```
   * @source
   */
  protected async queryProducts(
    query: string,
    limit: number = this.limit,
  ): Promise<ProductBuilder<Product>[] | void> {
    const searchResponse = await this.httpGetJson({
      host: this.apiHost,
      path: '/api/v2/products',
      params: {
        app: 'vnext',
        page: 1,
        per_page: limit,
        'q[keywords]': query,
        'q[name_or_description_text_cont]': query,
        'q[descend_by_match]': 'true',
      },
    });
 
    if (!isValidSearchResponse(searchResponse)) {
      this.logger.error('Invalid or empty MySimpleStore search response', {
        query,
        searchResponse,
      });
      return;
    }
 
    const fuzzResults = this.fuzzyFilterAst<MySimpleStoreListProduct>(searchResponse.products);
 
    this.logger.debug('Applied fuzzy filter to products', {
      query,
      products: searchResponse.products,
      fuzzResults,
    });
 
    return this.initProductBuilders(fuzzResults.slice(0, limit));
  }
 
  /**
   * Initializes partial product builders from MySimpleStore search-list items.
   * The search response carries no variants, so only the product-wide fields are
   * set here (title, URL, listing price, image, description, CAS, identity); the
   * per-size variants and quantity are populated by {@link getProductData}.
   * @param products - Array of raw MySimpleStore search-list products
   * @returns Array of ProductBuilder instances seeded with listing data
   * @example
   * ```typescript
   * const builders = this.initProductBuilders(searchResponse.products);
   * console.log(builders[0].get("title")); // "Thymol"
   * ```
   * @source
   */
  protected initProductBuilders(products: MySimpleStoreListProduct[]): ProductBuilder<Product>[] {
    return mapDefined(products, (product) => {
      const builder = new ProductBuilder<Product>(this.baseURL);
      const image = product.image_list?.[0]?.url ?? product.default_asset_url;
 
      builder
        .setBasicInfo(product.name, this.productUrl(product), this.supplierName)
        .setMatchPercentage(this.matchScoreOf(product))
        .setID(product.id)
        .setCacheKey(this.getUniqueProductKey(product))
        .setImage(image, product.name)
        .setDescription(product.description_raw)
        // CAS lives in the free-form description copy; setCAS extracts it.
        .setCAS(`${product.name}\n${product.description_raw ?? ''}`);
 
      if (product.price) {
        builder.setPricing(product.price.numeric, product.price.currency ?? 'USD', '$');
      }
 
      return builder;
    });
  }
 
  /**
   * Reduces a variant's quantity + uom to a single comparable scalar so variants
   * can be sorted ascending by chemical content. Mass units collapse to
   * milligrams and volume units to millilitres via {@link toBaseQuantity}; the two
   * families aren't comparable to each other, but a product's variants always
   * share a family. Missing/unknown quantities sort last.
   * @param variant - The variant to rank
   * @returns The canonical magnitude, or +Infinity when it can't be derived
   * @example
   * ```typescript
   * this.variantSortRank({ quantity: 1, uom: "l" });   // 1000
   * this.variantSortRank({ quantity: 500, uom: "g" }); // 500000
   * this.variantSortRank({});                          // Infinity
   * ```
   * @source
   */
  protected variantSortRank(variant: Partial<Variant>): number {
    if (typeof variant.quantity !== 'number' || !variant.uom) {
      return Number.POSITIVE_INFINITY;
    }
    return toBaseQuantity(variant.quantity, variant.uom);
  }
 
  /**
   * Parses a single MySimpleStore variant into a `Partial<Variant>`. The size and
   * unit come from `options_text` ("Size: 1 LITER"), the price from the string
   * `price` field, the currency symbol from `display_price`, and the variant URL
   * from the product URL plus the sku.
   * @param variant - The raw MySimpleStore variant
   * @param productUrl - The product's canonical page URL, used to build the variant URL
   * @returns The parsed partial variant
   * @example
   * ```typescript
   * this.parseVariant(rawVariant, productUrl);
   * // { title: "1 LITER", quantity: 1, uom: "l", price: 60, currencySymbol: "$",
   * //   sku: "GRN-60-1-LTR", id: "019da6a4-...", url: ".../v/GRN-60-1-LTR" }
   * ```
   * @source
   */
  protected parseVariant(variant: MySimpleStoreVariant, productUrl: string): Partial<Variant> {
    const parsed: Partial<Variant> = { id: variant.id };
 
    // `options_text` is "Size: 1 LITER"; the size label is the part after the colon.
    const sizeLabel = (variant.options_text ?? variant.option_values?.[0]?.name ?? '')
      .split(':')
      .pop()
      ?.trim();
    if (sizeLabel) {
      parsed.title = sizeLabel;
      const quantity = parseQuantity(sizeLabel);
      if (quantity) {
        parsed.quantity = quantity.quantity;
        parsed.uom = quantity.uom;
      }
    }
 
    if (variant.price) {
      const price = Number(variant.price);
      if (!Number.isNaN(price)) {
        parsed.price = price;
      }
    }
 
    // Currency code/symbol come from the pre-formatted display price ("$60.00").
    const displayPrice = variant.display_price ? parsePrice(variant.display_price) : undefined;
    if (displayPrice) {
      parsed.currencyCode = displayPrice.currencyCode;
      parsed.currencySymbol = displayPrice.currencySymbol;
    }
 
    if (variant.sku) {
      parsed.sku = variant.sku;
      parsed.url = `${productUrl}/v/${variant.sku}`;
    }
 
    return parsed;
  }
 
  /**
   * Enriches a partial product (from {@link queryProducts}) with its detail-page
   * data. Fetches `/api/v2/products/{slug}`, parses every variant, sorts them
   * ascending by chemical quantity, promotes the smallest to the parent-level
   * price/quantity/sku, and records the full variant list. A failed detail fetch
   * keeps the product with its listing data rather than dropping it.
   * @param product - The ProductBuilder to enrich
   * @returns Promise resolving to the enriched ProductBuilder, or void if it can't be built
   * @example
   * ```typescript
   * const enriched = await this.getProductData(builder);
   * // enriched.get("variants")?.length === 4  (Geraniol 60: 1/4/5/20 LITER)
   * ```
   * @source
   */
  protected async getProductData(
    product: ProductBuilder<Product>,
  ): Promise<ProductBuilder<Product> | void> {
    return this.getProductDataWithCache(product, async (builder) => {
      const url = builder.get('url');
      if (typeof url !== 'string') {
        this.logger.error('[MySimpleStore] Invalid product URL', { url });
        return;
      }
 
      // The product page URL ends in the slug (".../ols/products/{slug}"); the
      // detail endpoint is keyed by that slug.
      const slug = url.split('/').pop();
      if (!slug) {
        this.logger.error('[MySimpleStore] Could not derive slug from URL', { url });
        return builder;
      }
 
      let detail: unknown;
      try {
        detail = await this.httpGetJson({
          host: this.apiHost,
          path: `/api/v2/products/${slug}`,
          params: { app: 'vnext' },
        });
      } catch (error) {
        this.logger.warn(
          `[MySimpleStore] Detail fetch failed; keeping listing data: ${getErrorMessage(error)}`,
          {
            error,
            slug,
          },
        );
        return builder;
      }
 
      if (!isProductDetail(detail)) {
        this.logger.warn('[MySimpleStore] Invalid product detail; keeping listing data', {
          slug,
          detail,
        });
        return builder;
      }
 
      // Enrich product-wide fields from the detail response.
      if (detail.description) {
        builder.setDescription(htmlToAscii(detail.description));
      }
      builder.setCAS(detail.description_text ?? detail.name);
      // Add every product photo as a gallery image so the detail-panel carousel can cycle them.
      // The first asset becomes the primary image; the rest are appended.
      (detail.assets ?? []).forEach((asset, index) => {
        const src = asset.large_url ?? asset.small_url;
        if (!src) return;
        if (index === 0) {
          builder.setImage(src, detail.name);
        } else {
          builder.addImage(src, detail.name);
        }
      });
      if (typeof detail.in_stock === 'boolean') {
        builder.setAvailability(detail.in_stock);
      }
 
      const variants = (detail.variants ?? [])
        .map((variant) => this.parseVariant(variant, url))
        .sort((a, b) => {
          const rankDiff = this.variantSortRank(a) - this.variantSortRank(b);
          if (rankDiff !== 0) return rankDiff;
          return (a.price ?? Number.POSITIVE_INFINITY) - (b.price ?? Number.POSITIVE_INFINITY);
        });
 
      const primary = variants[0];
      if (primary) {
        if (typeof primary.price === 'number') builder.setPrice(primary.price);
        if (primary.currencyCode) builder.setCurrencyCode(primary.currencyCode);
        if (primary.currencySymbol) builder.setCurrencySymbol(primary.currencySymbol);
        if (typeof primary.quantity === 'number' && primary.uom) {
          builder.setQuantity(primary.quantity, primary.uom);
        }
        if (primary.sku) builder.setSku(primary.sku);
      } else {
        // Single-size products carry no variants; recover the size from the name
        // or description so the product isn't dropped for lacking a quantity.
        const fallback = parseQuantity(detail.name) ?? parseQuantity(detail.description_text ?? '');
        if (fallback) {
          builder.setQuantity(fallback.quantity, fallback.uom);
        }
      }
 
      if (variants.length > 1) {
        builder.setVariants(variants.filter(isValidVariant));
      }
 
      return builder;
    });
  }
}