← back to Fineartamerica Price Sync

lib/parse-sku.js

36 lines

// Parse a Designer-Wallcoverings Shopify variant SKU that encodes a Fine Art
// America order configuration, e.g.:
//   artworkid[65269183]-productid[printframed]-imagewidth[20]-imageheight[24]
//   -paperid[archivalmattepaper]-frameid[CRQ13]-mat1id[PM918]-mat1width[2]
//   -finishid[0.125acrylic]
//
// The SKU IS the contract between Shopify and FAA — every field needed to price
// the item on FAA is embedded here, so cost lookup is deterministic (no fuzzy
// title matching). Returns null for SKUs that carry no FAA signature.

function parseSku(sku) {
  if (!sku || !/artworkid\[/i.test(sku) || !/productid\[/i.test(sku)) return null;
  const cfg = {};
  const re = /([a-z0-9]+)\[([^\]]*)\]/gi;
  let m;
  while ((m = re.exec(sku))) cfg[m[1].toLowerCase()] = m[2];
  cfg._raw = sku;
  return cfg;
}

// A stable cache key for one FAA production configuration (artwork-independent
// fields collapse re-fetches across identical frame/size/mat combos).
function configKey(cfg) {
  return [
    cfg.artworkid, cfg.productid, cfg.imagewidth, cfg.imageheight,
    cfg.paperid, cfg.frameid, cfg.mat1id, cfg.mat1width, cfg.finishid,
  ].join('|');
}

// Human label for logs / dry-run tables.
function configLabel(cfg) {
  return `${cfg.productid} ${cfg.imagewidth}x${cfg.imageheight} frame:${cfg.frameid || '-'} mat:${cfg.mat1id || '-'} finish:${cfg.finishid || '-'}`;
}

module.exports = { parseSku, configKey, configLabel };