fix: Small adjustments

This commit is contained in:
Brennan Wilkes (Text Groove) 2026-01-29 11:59:02 -08:00
parent f1b5b7d36a
commit 7254acb946
4 changed files with 19 additions and 20 deletions

View file

@ -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 {

View file

@ -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;

View file

@ -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
}

View file

@ -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) {