diff --git a/src/stores/coop.js b/src/stores/coop.js index 5bea951..8d4da2e 100644 --- a/src/stores/coop.js +++ b/src/stores/coop.js @@ -1,6 +1,6 @@ "use strict"; -const { normalizeCspc } = require("../utils/sku"); +const { normalizeSkuKey } = require("../utils/sku"); const { humanBytes } = require("../utils/bytes"); const { padLeft, padRight } = require("../utils/string"); @@ -163,10 +163,9 @@ function productFromApi(p) { (Number.isFinite(p?.Price) ? `$${Number(p.Price).toFixed(2)}` : ""); const upc = String(p.UPC || "").trim(); - const sku = - upc || // use UPC if present - String(p.ProductStoreID); // fallback to store-specific ID - + const rawKey = upc || String(p.ProductStoreID || "").trim() || String(p.ProductID || "").trim(); + const sku = normalizeSkuKey(rawKey, { storeLabel: "Co-op World of Whisky", url }); + const img = normalizeAbsUrl(p.ImageURL); return { diff --git a/src/tracker/merge.js b/src/tracker/merge.js index be06f08..9f44bdd 100644 --- a/src/tracker/merge.js +++ b/src/tracker/merge.js @@ -1,7 +1,7 @@ // src/tracker/merge.js "use strict"; -const { normalizeCspc } = require("../utils/sku"); +const { normalizeSkuKey } = require("../utils/sku"); const { normPrice } = require("../utils/price"); function normImg(v) { @@ -15,12 +15,10 @@ function isRealSku(v) { return Boolean(normalizeCspc(v)); } -function normalizeSkuPreserve(raw) { - const s = String(raw || "").trim(); - const c = normalizeCspc(s); - return c || s; // CSPC if present, else keep UPC/ProductStoreID/etc +function normalizeSkuForDb(raw, { storeLabel, url } = {}) { + return normalizeSkuKey(raw, { storeLabel, url }); } - + function mergeDiscoveredIntoDb(prevDb, discovered) { const merged = new Map(prevDb.byUrl); @@ -108,7 +106,7 @@ function mergeDiscoveredIntoDb(prevDb, discovered) { if (!prev) { const now = { ...nowRaw, - sku: normalizeSkuPreserve(nowRaw.sku), + sku: normalizeSkuForDb(nowRaw.sku, { storeLabel: nowRaw.storeLabel, url }), img: normImg(nowRaw.img), removed: false, }; @@ -121,7 +119,7 @@ function mergeDiscoveredIntoDb(prevDb, discovered) { if (prevUrlForThisItem === url && prev.removed) { const now = { ...nowRaw, - sku: normalizeSkuPreserve(nowRaw.sku) || normalizeSkuPreserve(prev.sku), + sku: normalizeSkuForDb(nowRaw.sku, { storeLabel: nowRaw.storeLabel, url }) || normalizeSkuForDb(prev.sku, { storeLabel: prev.storeLabel, url: prev.url }), img: normImg(nowRaw.img) || normImg(prev.img), removed: false, }; @@ -139,9 +137,9 @@ function mergeDiscoveredIntoDb(prevDb, discovered) { const prevPrice = normPrice(prev.price); const nowPrice = normPrice(nowRaw.price); - const prevSku = normalizeSkuPreserve(prev.sku); - const nowSku = normalizeSkuPreserve(nowRaw.sku) || prevSku; - + const prevSku = normalizeSkuForDb(prev.sku, { storeLabel: prev.storeLabel, url: prev.url }); + const nowSku = normalizeSkuForDb(nowRaw.sku, { storeLabel: nowRaw.storeLabel, url }) || prevSku; + const prevImg = normImg(prev.img); let nowImg = normImg(nowRaw.img); if (!nowImg) nowImg = prevImg; diff --git a/src/utils/sku_map.js b/src/utils/sku_map.js index 3a45d4f..9385725 100644 --- a/src/utils/sku_map.js +++ b/src/utils/sku_map.js @@ -56,8 +56,9 @@ function isNumericSku(k) { } function isUpcSku(k) { - // UPC-A/EAN/GTIN-ish (most common: 12 or 13; sometimes 14) - return /^\d{12,14}$/.test(String(k || "").trim()); + const s = String(k || "").trim(); + if (s.startsWith("upc:")) return true; + return /^\d{12,14}$/.test(s); // keep legacy support } diff --git a/viz/app/linker_page.js b/viz/app/linker_page.js index 94f9a58..f76ba12 100644 --- a/viz/app/linker_page.js +++ b/viz/app/linker_page.js @@ -233,8 +233,9 @@ function isSoftSkuKey(k) { function isUpcSkuKey(k) { - return /^\d{12,14}$/.test(String(k || "").trim()); -} + const s = String(k || "").trim(); + return s.startsWith("upc:") || /^\d{12,14}$/.test(s); + } function isABStoreLabel(label) {