← back to Dw Sku Integrity

maharam-real-code-helpers.mjs

34 lines

// Pure helpers for TK-10900 Maharam manufacturer-code recovery.

export function publishedCodes(body, sourceUrl) {
  const codes = [...new Set(
    [...String(body).matchAll(/\b(\d{6})[–-](\d{3})\b/g)]
      .map((match) => `${match[1]}-${match[2]}`),
  )];
  const urlColor = (String(sourceUrl).match(/\/colors\/(\d{3})(?:-|$)/) || [])[1];
  return {
    codes,
    urlColor,
    valid: urlColor ? codes.filter((code) => code.endsWith(`-${urlColor}`)) : [],
  };
}

export function validateRecoveryEntries(entries) {
  const ids = new Set(entries.map((entry) => entry.shopify_id));
  const codes = new Set(entries.map((entry) => entry.new));
  const badShape = entries.filter((entry) => !/^\d{6}-\d{3}$/.test(entry.new));
  const badUrlSuffix = entries.filter((entry) => {
    const color = (String(entry.source_url).match(/\/colors\/(\d{3})(?:-|$)/) || [])[1];
    return !color || !entry.new.endsWith(`-${color}`);
  });
  return {
    rows: entries.length,
    unique_ids: ids.size,
    unique_codes: codes.size,
    bad_shape: badShape.length,
    bad_url_suffix: badUrlSuffix.length,
    ok: ids.size === entries.length && codes.size === entries.length
      && badShape.length === 0 && badUrlSuffix.length === 0,
  };
}