Open-source agentic AI Search Execution Platform. Track AI visibility, find opportunities, and let agents turn signals into prioritized actions and drafted fixes across ChatGPT, Claude, Gemini, Google AI Overviews, Google AI Mode, Perplexity, Grok, and Microsoft Copilot.
TypeScript aeoai-agentai-searchanswer-engine-optimizationbrand-monitoringchatgptclaudecopilotdockergeminigeogoogle-ai-overviewsgrokmcpnextjsopen-sourceperplexityseosupabasetypescript
feat(shopping): match each AI shopping card to the brand's catalog product it shows (Product Visibility, part 2) (#1637)
Cihan474f4aee27f9
diff --git a/server/src/lib/catalog/card-match.js b/server/src/lib/catalog/card-match.jsnew file mode 100644index 000000000..6aaa42254--- /dev/null+++ b/server/src/lib/catalog/card-match.js@@ -0,0 +1,473 @@+import { setImmediate as nextTurn } from 'timers/promises';+import supabaseAdmin from '../../config/supabase.js';+import { linkKey } from '../content-links.js';+import { logger } from '../logger.js';+import { readAllRpcRows } from '../rpc-pages.js';++/**+ * The catalog product behind an AI shopping card (product visibility, part 2;+ * ECOM-005, 00360). Product Visibility counts a brand's products on the AI+ * "shelf", so each product card the brand's own (matched_brand_role 'own')+ * is matched to one product of the brand's catalog (00278), first by what+ * names it exactly, then by its title:+ *+ * 1. the marketplace listing it links (an ASIN, an eBay item id), to a+ * variant's marketplace_item_id;+ * 2. a GTIN the card carries, to a variant's;+ * 3. its link, to a product's or a variant's page (with or without its+ * query);+ * 4. its title, when most of its words are one product's (TITLE_MATCH)+ * and clearly not another's (TITLE_MARGIN). The brand's own name and+ * filler words don't count. The products compared are those sharing a+ * word with the card that few of the catalog's titles have+ * (COMMON_WORD_SHARE), so a big catalog's "women" or "backpack" doesn't+ * make each card weigh most of it.+ *+ * A key two products share names neither. Cards are matched as they're+ * written (cloro-result-handler.js), against a catalog read at most every+ * INDEX_TTL_MS, and all of a brand's recent cards again after its catalog+ * syncs (sweepShoppingCardMatches), a slice at a time.+ */++/** The share of two titles' words in common (Dice) a title match needs… */+export const TITLE_MATCH = 0.6;+/** …and by how much it must beat the next product, so two lookalikes match neither. */+export const TITLE_MARGIN = 0.1;+/** How far back a brand's cards are matched again after a catalog sync. */+export const REMATCH_DAYS = 90;+/** Matches written a call. */+const WRITE_BATCH = 500;+/** How long a brand's catalog is kept for matching new cards. */+const INDEX_TTL_MS = 10 * 60 * 1000;+/**+ * Products kept across the brands' catalogs at most; the brand read least+ * recently goes first. One brand bigger than this is still kept, alone.+ */+export const MAX_INDEXED_PRODUCTS = 200_000;+/**+ * A word in more of a catalog's titles than this share (and than+ * COMMON_WORD_MIN) still counts in a title's score, but finds no products to+ * compare: it would make each card weigh most of a big catalog.+ */+export const COMMON_WORD_SHARE = 0.05;+/** …so a small catalog compares every product sharing a word, as before. */+export const COMMON_WORD_MIN = 500;+/** Cards matched between two breaths for the event loop. */+export const MATCH_SLICE = 500;+/**+ * A card title's first words a title match reads, at most: a product title+ * says what it is in far fewer, and each word more is compared with up to+ * every product it finds.+ */+export const MAX_CARD_TITLE_WORDS = 40;++const AMBIGUOUS = Symbol('ambiguous');++/** Words that say nothing about which product it is. */+const FILLER = new Set([+ 'a',+ 'an',+ 'and',+ 'by',+ 'for',+ 'in',+ 'of',+ 'on',+ 'the',+ 'to',+ 'with',+ 'new',+]);++const FOLD = { ı: 'i', ß: 'ss', ø: 'o', æ: 'ae', œ: 'oe', ł: 'l', đ: 'd', þ: 'th' };++/** A text's words: lowercased, accents and the letters Unicode keeps whole folded, split at the rest. */+export function titleWords(text, skip = FILLER) {+ const folded = String(text ?? '')+ .toLowerCase()+ .normalize('NFKD')+ .replace(/\p{M}/gu, '')+ .replace(/[ıßøæœłđþ]/g, (c) => FOLD[c]);+ return new Set(folded.split(/[^\p{L}\p{N}]+/u).filter((word) => word && !skip.has(word)));+}++/** A GTIN as digits, without the zeros GTIN-12, -13 and -14 pad it with. */+export function gtinKey(value) {+ const digits = String(value ?? '').replace(/\D/g, '');+ return digits.length >= 8 ? digits.replace(/^0+/, '') : null;+}++/** A page's host and path, without its query: a product page linked with its variant or tracking. */+export function pathKey(url) {+ try {+ const parsed = new URL(String(url ?? '').trim());+ if (parsed.protocol !== 'http:' && parsed.protocol !== 'https:') return null;+ // Its trailing slashes, cut by a walk back: a regex anchored at the end+ // tries each run of slashes from each of its slashes.+ let path = parsed.pathname;+ let end = path.length;+ while (end > 0 && path[end - 1] === '/') end -= 1;+ path = path.slice(0, end);+ return `${parsed.hostname.toLowerCase().replace(/^www\./, '')}${path.toLowerCase()}`;+ } catch {+ return null;+ }+}++/** The keys a card's provider gives its GTIN under, in the card's raw payload. */+const GTIN_KEYS = ['gtin', 'gtin13', 'gtin12', 'gtin14', 'ean', 'upc'];++/**+ * The GTIN a card's provider gave, if any: in its raw payload as it's+ * written, or read out of it by the sweep (CARD_GTIN_COLUMNS).+ */+function cardGtin(card) {+ const raw = card.raw && typeof card.raw === 'object' ? card.raw : {};+ for (const from of [card, raw]) {+ for (const key of GTIN_KEYS) {+ const value = from[key];+ if ((typeof value === 'string' && value.trim()) || typeof value === 'number') {+ return String(value);+ }+ }+ }+ return null;+}++/** The sweep's select of a card's GTIN keys, each read out of its raw payload. */+const CARD_GTIN_COLUMNS = GTIN_KEYS.map((key) => `${key}:raw->>${key}`).join(', ');++function post(postings, word, at) {+ if (!postings.has(word)) postings.set(word, []);+ postings.get(word).push(at);+}++function setOnce(map, key, productId) {+ if (!key) return;+ const had = map.get(key);+ if (had === undefined) map.set(key, productId);+ else if (had !== productId) map.set(key, AMBIGUOUS);+}++function pick(map, key) {+ if (!key) return null;+ const id = map.get(key);+ return id && id !== AMBIGUOUS ? id : null;+}++/**+ * A brand's catalog as the matcher reads it: its live products' titles and+ * pages, and their variants' listings, GTINs and pages.+ *+ * @param {{ products: { id: string, title: string|null, url: string|null }[], variants: { product_id: string, url: string|null, gtin: string|null, marketplace_item_id: string|null }[] }} catalog+ * @param {{ brandName?: string|null }} [opts] the brand's name: its words don't tell its products apart+ */+export function catalogIndex({ products, variants }, { brandName } = {}) {+ const skip = new Set([...FILLER, ...titleWords(brandName)]);+ const index = {+ skip,+ size: products.length,+ byListing: new Map(),+ byGtin: new Map(),+ byUrl: new Map(),+ byPath: new Map(),+ titles: [],+ postings: new Map(),+ common: new Set(),+ };+ const addPage = (url, productId) => {+ setOnce(index.byUrl, linkKey(url), productId);+ setOnce(index.byPath, pathKey(url), productId);+ };+ const live = new Set();+ // How many titles each word is in.+ const titlesWith = new Map();+ for (const product of products) {+ live.add(product.id);+ if (product.url) addPage(product.url, product.id);+ const words = titleWords(product.title, skip);+ if (!words.size) continue;+ index.titles.push({ id: product.id, words });+ for (const word of words) titlesWith.set(word, (titlesWith.get(word) ?? 0) + 1);+ }+ // A title is found by its words few titles have, or else by its rarest one.+ const common = Math.max(COMMON_WORD_MIN, index.titles.length * COMMON_WORD_SHARE);+ index.titles.forEach(({ words }, at) => {+ let rarest = null;+ let found = false;+ for (const word of words) {+ const count = titlesWith.get(word);+ if (count <= common) {+ found = true;+ post(index.postings, word, at);+ } else {+ index.common.add(word);+ if (rarest === null || count < titlesWith.get(rarest)) rarest = word;+ }+ }+ if (!found) post(index.postings, rarest, at);+ });+ for (const variant of variants) {+ if (!live.has(variant.product_id)) continue;+ setOnce(+ index.byListing,+ variant.marketplace_item_id?.trim().toUpperCase() || null,+ variant.product_id,+ );+ setOnce(index.byGtin, gtinKey(variant.gtin), variant.product_id);+ if (variant.url) addPage(variant.url, variant.product_id);+ }+ return index;+}++/**+ * The catalog product a card shows, and how it was found, or null.+ *+ * @param {{ product_title?: string|null, merchant_url?: string|null, listing_id?: string|null, gtin?: string|null, raw?: object|null }} card+ * @returns {{ productId: string, match: 'listing'|'gtin'|'url'|'title' } | null}+ */+export function matchCard(card, index) {+ const listing = pick(index.byListing, card.listing_id?.trim().toUpperCase() || null);+ if (listing) return { productId: listing, match: 'listing' };+ const gtin = pick(index.byGtin, gtinKey(cardGtin(card)));+ if (gtin) return { productId: gtin, match: 'gtin' };+ if (card.merchant_url) {+ const page =+ pick(index.byUrl, linkKey(card.merchant_url)) ??+ pick(index.byPath, pathKey(card.merchant_url));+ if (page) return { productId: page, match: 'url' };+ }+ const words = new Set(+ [...titleWords(card.product_title, index.skip)].slice(0, MAX_CARD_TITLE_WORDS),+ );+ if (!words.size) return null;+ const compared = new Set();+ for (const word of words) {+ for (const at of index.postings.get(word) ?? []) compared.add(at);+ }+ let best = null;+ let bestScore = 0;+ let second = 0;+ for (const at of compared) {+ const title = index.titles[at].words;+ let common = 0;+ for (const word of words) if (title.has(word)) common += 1;+ const score = (2 * common) / (words.size + title.size);+ if (score > bestScore) {+ second = bestScore;+ bestScore = score;+ best = index.titles[at].id;+ } else if (score > second) {+ second = score;+ }+ }+ // A product not compared shares only the card's common words, and has a+ // word the card hasn't: it scores at most `unseen`. One that close to the+ // best could be the lookalike that matches neither, so neither matches.+ let commonInCard = 0;+ for (const word of words) if (index.common.has(word)) commonInCard += 1;+ const unseen = commonInCard ? (2 * commonInCard) / (words.size + commonInCard + 1) : 0;+ return best && bestScore >= TITLE_MATCH && bestScore - Math.max(second, unseen) >= TITLE_MARGIN+ ? { productId: best, match: 'title' }+ : null;+}++async function readAll(what, page) {+ const { data, error } = await readAllRpcRows(what, page);+ if (error) throw new Error(`${what} read: ${error.message}`);+ return data;+}++/** A brand's live catalog, read for matching. */+export async function loadCatalogIndex(brandId, { db = supabaseAdmin, brandName = null } = {}) {+ const [products, variants] = await Promise.all([+ readAll('catalog_products', (from, to) =>+ db+ .from('catalog_products')+ .select('id, title, url')+ .eq('brand_id', brandId)+ .is('removed_at', null)+ .order('id')+ .range(from, to),+ ),+ readAll('catalog_variants', (from, to) =>+ db+ .from('catalog_variants')+ .select('id, product_id, url, gtin, marketplace_item_id')+ .eq('brand_id', brandId)+ .is('removed_at', null)+ .order('id')+ .range(from, to),+ ),+ ]);+ return catalogIndex({ products, variants }, { brandName });+}++/**+ * Brands' catalogs kept for matching the cards being written: each read+ * again after ttlMs, and, once they hold more than maxProducts products+ * between them, the brand read least recently dropped first.+ *+ * @param {{ maxProducts?: number, ttlMs?: number, load?: typeof loadCatalogIndex, now?: () => number }} [opts]+ */+export function catalogIndexCache({+ maxProducts = MAX_INDEXED_PRODUCTS,+ ttlMs = INDEX_TTL_MS,+ load = loadCatalogIndex,+ now = Date.now,+} = {}) {+ // brandId → { at, index (a promise), size }; a Map keeps the order its+ // keys went in, the least recently read first.+ const kept = new Map();+ const shrink = (keepId) => {+ let total = 0;+ for (const entry of kept.values()) total += entry.size;+ for (const [brandId, entry] of kept) {+ if (total <= maxProducts) break;+ if (brandId === keepId) continue;+ kept.delete(brandId);+ total -= entry.size;+ }+ };+ return {+ get(brandId, opts = {}) {+ const had = kept.get(brandId);+ kept.delete(brandId);+ if (had && now() - had.at < ttlMs) {+ kept.set(brandId, had);+ return had.index;+ }+ const entry = { at: now(), index: null, size: 0 };+ entry.index = load(brandId, opts).then(+ (index) => {+ if (kept.get(brandId) === entry) {+ entry.size = index.size ?? 0;+ shrink(brandId);+ }+ return index;+ },+ (err) => {+ if (kept.get(brandId) === entry) kept.delete(brandId);+ throw err;+ },+ );+ kept.set(brandId, entry);+ return entry.index;+ },+ drop(brandId) {+ kept.delete(brandId);+ },+ /** The brands kept, the least recently read first. */+ brands: () => [...kept.keys()],+ };+}++const indexes = catalogIndexCache();++/**+ * A brand's catalog for matching the cards being written, read again after+ * INDEX_TTL_MS, or when a sweep has matched the brand's cards since.+ */+export function catalogIndexFor(brandId, opts = {}) {+ return indexes.get(brandId, opts);+}++/**+ * Cards matched to a catalog, MATCH_SLICE at a time with a breath between,+ * so a big brand's sweep doesn't hold the event loop (tracking callbacks,+ * API requests) for its whole length.+ */+export async function matchCards(cards, index) {+ const found = [];+ for (let at = 0; at < cards.length; at += 1) {+ if (at > 0 && at % MATCH_SLICE === 0) await nextTurn();+ found.push(matchCard(cards[at], index));+ }+ return found;+}++/**+ * Matches a brand's own cards of the last REMATCH_DAYS to its catalog as it+ * is now, and writes the matches that changed.+ *+ * @returns {Promise<{ cards: number, matched: number, changed: number }>}+ */+export async function matchBrandShoppingCards(+ brandId,+ { db = supabaseAdmin, now = new Date() } = {},+) {+ const { data: brand, error: brandErr } = await db+ .from('brands')+ .select('name')+ .eq('id', brandId)+ .maybeSingle();+ if (brandErr) throw new Error(`brands read: ${brandErr.message}`);+ const index = await loadCatalogIndex(brandId, { db, brandName: brand?.name ?? null });+ indexes.drop(brandId);+ const since = new Date(now.getTime() - REMATCH_DAYS * 86_400_000).toISOString();+ const cards = await readAll('prompt_result_shopping_cards', (from, to) =>+ db+ .from('prompt_result_shopping_cards')+ .select(+ `id, product_title, merchant_url, listing_id, ${CARD_GTIN_COLUMNS}, catalog_product_id, catalog_match`,+ )+ .eq('brand_id', brandId)+ .eq('matched_brand_role', 'own')+ .gte('created_at', since)+ .order('id')+ .range(from, to),+ );+ const matches = await matchCards(cards, index);+ let matched = 0;+ const changes = [];+ for (const [at, card] of cards.entries()) {+ const found = matches[at];+ if (found) matched += 1;+ const productId = found?.productId ?? null;+ const match = found?.match ?? null;+ if (productId !== (card.catalog_product_id ?? null) || match !== (card.catalog_match ?? null)) {+ changes.push({ id: card.id, product_id: productId, match });+ }+ }+ let changed = 0;+ for (let at = 0; at < changes.length; at += WRITE_BATCH) {+ const { data, error } = await db.rpc('set_shopping_card_matches', {+ p_brand_id: brandId,+ p_matches: changes.slice(at, at + WRITE_BATCH),+ });+ if (error) throw new Error(`set_shopping_card_matches: ${error.message}`);+ changed += Number(data ?? 0);+ }+ return { cards: cards.length, matched, changed };+}++/** Catalogs read again within this long are matched again by the daily sweep. */+const SWEEP_SYNCED_WITHIN_MS = 26 * 60 * 60 * 1000;++/**+ * The daily run, after the catalog sweep: each brand whose catalog synced+ * in the last day has its recent cards matched again, one brand at a time.+ * A brand's failure is logged and the others go on.+ */+export async function sweepShoppingCardMatches({ db = supabaseAdmin, now = new Date() } = {}) {+ const since = new Date(now.getTime() - SWEEP_SYNCED_WITHIN_MS).toISOString();+ const { data, error } = await db+ .from('catalog_sources')+ .select('brand_id')+ .eq('last_status', 'ok')+ .gte('last_synced_at', since);+ if (error) throw new Error(`catalog_sources read: ${error.message}`);+ const brands = [...new Set((data ?? []).map((row) => row.brand_id))];+ let changed = 0;+ for (const brandId of brands) {+ try {+ changed += (await matchBrandShoppingCards(brandId, { db, now })).changed;+ } catch (err) {+ logger.error(+ { err: { name: err?.name, message: err?.message }, brandId },+ '[card-match] brand failed',+ );+ }+ }+ return { brands: brands.length, changed };+}diff --git a/server/src/lib/catalog/card-match.test.js b/server/src/lib/catalog/card-match.test.jsnew file mode 100644index 000000000..9de9ac558--- /dev/null+++ b/server/src/lib/catalog/card-match.test.js@@ -0,0 +1,468 @@+import { setImmediate } from 'timers';+import { describe, expect, it, vi } from 'vitest';++vi.mock('../../config/supabase.js', () => ({ default: {} }));+vi.mock('../logger.js', () => ({+ logger: { info: vi.fn(), warn: vi.fn(), error: vi.fn() },+}));++import { logger } from '../logger.js';+import {+ COMMON_WORD_MIN,+ COMMON_WORD_SHARE,+ MATCH_SLICE,+ MAX_CARD_TITLE_WORDS,+ REMATCH_DAYS,+ TITLE_MATCH,+ catalogIndex,+ catalogIndexCache,+ gtinKey,+ matchBrandShoppingCards,+ matchCard,+ matchCards,+ pathKey,+ sweepShoppingCardMatches,+ titleWords,+} from './card-match.js';++/** The catalog product behind an AI shopping card (product visibility, part 2, 00360). */++/**+ * The tables the matcher reads, with the PostgREST calls it makes: eq, is,+ * gte, order and range, max_rows (`maxRows`, as PostgREST's 1000) and rpc.+ * A select's `alias:raw->>key` reads that key out of the row's raw payload,+ * which isn't returned itself then.+ */+function fakeDb({ maxRows = Infinity } = {}) {+ const tables = new Map();+ const rows = (name) => {+ if (!tables.has(name)) tables.set(name, []);+ return tables.get(name);+ };+ const from = (table) => {+ const filters = [];+ const fromRaw = [];+ let range = null;+ let one = false;+ const run = () => {+ const found = rows(table)+ .filter((row) => filters.every((keep) => keep(row)))+ .sort((a, b) => (a.id < b.id ? -1 : a.id > b.id ? 1 : 0));+ const [start, end] = range ?? [0, found.length];+ const data = found+ .slice(start, end + 1)+ .slice(0, maxRows)+ .map((row) => {+ if (!fromRaw.length) return row;+ const { raw, ...rest } = row;+ for (const [alias, key] of fromRaw) rest[alias] = raw?.[key] ?? null;+ return rest;+ });+ return { data: one ? (data[0] ?? null) : data, error: null };+ };+ const q = {+ select: (columns = '*') => {+ for (const column of columns.split(',')) {+ const read = /^\s*(\w+):raw->>(\w+)\s*$/.exec(column);+ if (read) fromRaw.push([read[1], read[2]]);+ }+ return q;+ },+ eq: (column, value) => (filters.push((row) => row[column] === value), q),+ is: (column, value) => (filters.push((row) => (row[column] ?? null) === value), q),+ gte: (column, value) => (filters.push((row) => row[column] >= value), q),+ order: () => q,+ range: (start, end) => ((range = [start, end]), q),+ maybeSingle: () => ((one = true), q),+ then: (resolve, reject) => Promise.resolve(run()).then(resolve, reject),+ };+ return q;+ };+ return { db: { from, rpc: async () => ({ data: 0, error: null }) }, rows };+}++const BRAND = 'brand-1';+const NOW = new Date('2026-10-27T09:00:00Z');++const products = [+ {+ id: 'p-shoe',+ title: 'Northwind Trail Runner X',+ url: 'https://northwind.example/products/trail-runner-x',+ },+ {+ id: 'p-pack',+ title: 'Northwind Ridge 30L Backpack',+ url: 'https://northwind.example/products/ridge-30',+ },+ { id: 'p-pack40', title: 'Northwind Ridge 40L Backpack', url: null },+ { id: 'p-jacket', title: 'StormShell Jacket', url: null },+];+const variants = [+ { product_id: 'p-shoe', url: null, gtin: '0012345678905', marketplace_item_id: 'b0c123abcd' },+ {+ product_id: 'p-jacket',+ url: 'https://northwind.example/products/stormshell?color=blue',+ gtin: null,+ marketplace_item_id: null,+ },+ // A variant of a product the catalog removed isn't read.+ { product_id: 'p-gone', url: null, gtin: '999999999999', marketplace_item_id: null },+];+const index = catalogIndex({ products, variants }, { brandName: 'Northwind' });++describe('titleWords and the keys', () => {+ it('reads a title as words, accents and folded letters alike, without filler', () => {+ expect([...titleWords('Çanta for the Straße — Sırt 30L')]).toEqual([+ 'canta',+ 'strasse',+ 'sirt',+ '30l',+ ]);+ });++ it('reads a GTIN without its padding, and a page without its query', () => {+ expect(gtinKey('0012345678905')).toBe('12345678905');+ expect(gtinKey('12345678905')).toBe('12345678905');+ expect(gtinKey('12-34')).toBeNull();+ expect(pathKey('https://WWW.Shop.example/Products/A/?utm_source=x')).toBe(+ 'shop.example/products/a',+ );+ expect(pathKey('mailto:[email protected]')).toBeNull();+ });++ it('reads a link with a long run of slashes in time near its length', () => {+ const started = Date.now();+ expect(pathKey(`https://shop.example/${'/'.repeat(100_000)}a//`)).toBe(+ `shop.example/${'/'.repeat(100_000)}a`,+ );+ expect(pathKey(`https://shop.example/a${'/'.repeat(100_000)}`)).toBe('shop.example/a');+ expect(Date.now() - started).toBeLessThan(500);+ });+});++describe('matchCard', () => {+ it('matches by the listing it links, then a GTIN, then the page', () => {+ expect(matchCard({ listing_id: 'B0C123ABCD', product_title: 'Something else' }, index)).toEqual(+ {+ productId: 'p-shoe',+ match: 'listing',+ },+ );+ expect(matchCard({ product_title: 'x', raw: { gtin: '12345678905' } }, index)).toEqual({+ productId: 'p-shoe',+ match: 'gtin',+ });+ expect(+ matchCard(+ {+ merchant_url: 'https://www.northwind.example/products/ridge-30/?srsltid=abc',+ product_title: 'x',+ },+ index,+ ),+ ).toEqual({ productId: 'p-pack', match: 'url' });+ // A variant's page, with its own query.+ expect(+ matchCard(+ { merchant_url: 'https://northwind.example/products/stormshell?color=blue' },+ index,+ ),+ ).toEqual({ productId: 'p-jacket', match: 'url' });+ });++ it("matches by title when one product's words are clearly the closest, the brand's name aside", () => {+ expect(matchCard({ product_title: 'Trail Runner X by Northwind' }, index)).toEqual({+ productId: 'p-shoe',+ match: 'title',+ });+ expect(matchCard({ product_title: 'StormShell Jacket — Men' }, index)).toEqual({+ productId: 'p-jacket',+ match: 'title',+ });+ });++ it('matches neither of two lookalikes, nor a title too far from any', () => {+ // "Ridge Backpack" is as close to the 30L as to the 40L.+ expect(matchCard({ product_title: 'Northwind Ridge Backpack' }, index)).toBeNull();+ expect(matchCard({ product_title: 'Ridge 30L Backpack' }, index)).toEqual({+ productId: 'p-pack',+ match: 'title',+ });+ expect(matchCard({ product_title: 'Summit Daypack 30 W' }, index)).toBeNull();+ expect(matchCard({ product_title: '' }, index)).toBeNull();+ expect(TITLE_MATCH).toBeGreaterThan(0.5);+ });++ it('matches by no key two products share', () => {+ const shared = catalogIndex({+ products: [+ { id: 'a', title: 'Alpha', url: null },+ { id: 'b', title: 'Beta', url: null },+ ],+ variants: [+ { product_id: 'a', url: null, gtin: '12345678', marketplace_item_id: null },+ { product_id: 'b', url: null, gtin: '012345678', marketplace_item_id: null },+ ],+ });+ expect(matchCard({ raw: { ean: '12345678' }, product_title: 'Gamma' }, shared)).toBeNull();+ });++ describe('in a big catalog', () => {+ // 1,200 backpacks: "backpack" is in each title, "hiking" and "city" in+ // 600 each, more than COMMON_WORD_SHARE of them and COMMON_WORD_MIN.+ const big = Array.from({ length: 1200 }, (_, i) => ({+ id: `b${i}`,+ title: `${i % 2 ? 'Hiking' : 'City'} Backpack Model${i}`,+ url: null,+ }));+ const pack = (id, title) => ({ id, title, url: null });++ it('compares only the products sharing a word few titles have, and scores the common words too', () => {+ const bigIndex = catalogIndex({ products: big, variants: [] });+ expect(1200 * COMMON_WORD_SHARE).toBeLessThan(600);+ expect(COMMON_WORD_MIN).toBeLessThan(600);+ expect([...bigIndex.common].sort()).toEqual(['backpack', 'city', 'hiking']);+ // Found by "model7"; "hiking" and "backpack" count in its score (1.0).+ expect(matchCard({ product_title: 'Hiking Backpack Model7' }, bigIndex)).toEqual({+ productId: 'b7',+ match: 'title',+ });+ // A title of common words only is still found, by its rarest one.+ const withPlain = catalogIndex({+ products: [...big, pack('plain', 'City Hiking Backpack')],+ variants: [],+ });+ expect(matchCard({ product_title: 'City Hiking Backpack' }, withPlain)).toEqual({+ productId: 'plain',+ match: 'title',+ });+ });++ it('matches no product that one not compared could be as close to', () => {+ // "Hiking Backpack Grey" scores 0.67 with "Grey Hiking Strap", found+ // by "grey", and as much with each "Hiking Backpack Model…", which no+ // word few titles have finds. Compared, they would tie.+ const bigIndex = catalogIndex({+ products: [...big, pack('strap', 'Grey Hiking Strap')],+ variants: [],+ });+ expect(matchCard({ product_title: 'Hiking Backpack Grey' }, bigIndex)).toBeNull();+ // Its own title is far enough from them: 1.0 against 0.4 at most.+ expect(matchCard({ product_title: 'Grey Hiking Strap' }, bigIndex)).toEqual({+ productId: 'strap',+ match: 'title',+ });+ });++ it(`reads a long card title's first ${MAX_CARD_TITLE_WORDS} words, in time near its length`, () => {+ // 5,000 products, each found by its own number; a card title of 20,000+ // numbers would otherwise compare each product with each of its words.+ const numbered = catalogIndex({+ products: Array.from({ length: 5000 }, (_, i) => pack(`n${i}`, `Daypack ${i}`)),+ variants: [],+ });+ const long = Array.from({ length: 20_000 }, (_, i) => String(i)).join(' ');+ const started = Date.now();+ expect(matchCard({ product_title: long }, numbered)).toBeNull();+ expect(Date.now() - started).toBeLessThan(500);+ expect(matchCard({ product_title: 'Daypack 7' }, numbered)).toEqual({+ productId: 'n7',+ match: 'title',+ });+ });+ });+});++describe('matchCards', () => {+ it(`matches cards ${MATCH_SLICE} at a time, letting the event loop run between`, async () => {+ const cards = Array.from({ length: MATCH_SLICE * 2 + 1 }, () => ({+ listing_id: 'B0C123ABCD',+ }));+ let ticks = 0;+ let done = false;+ const tick = () => {+ ticks += 1;+ if (!done) setImmediate(tick);+ };+ setImmediate(tick);+ const found = await matchCards(cards, index);+ done = true;+ expect(found).toHaveLength(cards.length);+ expect(found.every((match) => match?.productId === 'p-shoe')).toBe(true);+ // A breath after each full slice.+ expect(ticks).toBe(2);+ });+});++describe('catalogIndexCache', () => {+ function setup() {+ const sizes = { a: 3, b: 2, c: 2, d: 9 };+ const load = vi.fn(async (brandId) => ({ size: sizes[brandId] }));+ let clock = 0;+ const cache = catalogIndexCache({ maxProducts: 5, ttlMs: 100, load, now: () => clock });+ return { cache, load, tick: (ms) => (clock += ms) };+ }+ const loads = (load) => load.mock.calls.map(([brandId]) => brandId);++ it('keeps the brands up to a number of products, dropping the one read least recently first', async () => {+ const { cache, load } = setup();+ await cache.get('a');+ await cache.get('b');+ // Reading a keeps it: b is now the least recently read.+ await cache.get('a');+ await cache.get('c');+ expect(cache.brands()).toEqual(['a', 'c']);+ await cache.get('a');+ await cache.get('b');+ expect(loads(load)).toEqual(['a', 'b', 'c', 'b']);+ // One brand over the limit is kept alone.+ await cache.get('d');+ expect(cache.brands()).toEqual(['d']);+ });++ it('reads a brand again after its time, or after a failed read, or once dropped', async () => {+ const { cache, load, tick } = setup();+ await cache.get('a');+ tick(100);+ await cache.get('a');+ load.mockRejectedValueOnce(new Error('down'));+ tick(100);+ await expect(cache.get('a')).rejects.toThrow('down');+ expect(cache.brands()).toEqual([]);+ await cache.get('a');+ cache.drop('a');+ await cache.get('a');+ expect(loads(load)).toEqual(['a', 'a', 'a', 'a', 'a']);+ });+});++describe('matchBrandShoppingCards', () => {+ function setup({ maxRows } = {}) {+ const fake = fakeDb({ maxRows });+ const rpc = vi.fn(async (_name, { p_matches }) => ({ data: p_matches.length, error: null }));+ fake.db.rpc = rpc;+ fake.rows('brands').push({ id: BRAND, name: 'Northwind' });+ for (const p of products)+ fake.rows('catalog_products').push({ ...p, brand_id: BRAND, removed_at: null });+ for (const [i, v] of variants.slice(0, 2).entries()) {+ fake.rows('catalog_variants').push({ id: `v${i}`, ...v, brand_id: BRAND, removed_at: null });+ }+ return { fake, rpc };+ }+ const card = (id, over) => ({+ id,+ brand_id: BRAND,+ matched_brand_role: 'own',+ created_at: '2026-10-20T00:00:00Z',+ product_title: null,+ merchant_url: null,+ listing_id: null,+ catalog_product_id: null,+ catalog_match: null,+ ...over,+ });++ it("writes only the matches that changed, of the brand's own recent cards", async () => {+ const { fake, rpc } = setup();+ fake.rows('prompt_result_shopping_cards').push(+ card('c1', { listing_id: 'B0C123ABCD' }),+ // Already matched as it is: not written.+ card('c2', {+ product_title: 'StormShell Jacket',+ catalog_product_id: 'p-jacket',+ catalog_match: 'title',+ }),+ // Its product left the catalog's titles: cleared.+ card('c3', {+ product_title: 'Unknown Thing',+ catalog_product_id: 'p-old',+ catalog_match: 'title',+ }),+ card('c4', { product_title: 'Trail Runner X', matched_brand_role: 'competitor' }),+ card('c5', { listing_id: 'B0C123ABCD', created_at: '2026-01-01T00:00:00Z' }),+ card('c6', { listing_id: 'B0C123ABCD', brand_id: 'brand-2' }),+ );+ const result = await matchBrandShoppingCards(BRAND, { db: fake.db, now: NOW });+ expect(result).toEqual({ cards: 3, matched: 2, changed: 2 });+ expect(rpc).toHaveBeenCalledWith('set_shopping_card_matches', {+ p_brand_id: BRAND,+ p_matches: [+ { id: 'c1', product_id: 'p-shoe', match: 'listing' },+ { id: 'c3', product_id: null, match: null },+ ],+ });+ expect(REMATCH_DAYS).toBe(90);+ });++ it("reads past PostgREST's 1000-row cap, and writes in batches", async () => {+ const { fake, rpc } = setup({ maxRows: 1000 });+ fake+ .rows('prompt_result_shopping_cards')+ .push(+ ...Array.from({ length: 1200 }, (_, i) =>+ card(`c${String(i).padStart(4, '0')}`, { listing_id: 'B0C123ABCD' }),+ ),+ );+ const result = await matchBrandShoppingCards(BRAND, { db: fake.db, now: NOW });+ expect(result.cards).toBe(1200);+ expect(rpc.mock.calls.map(([, args]) => args.p_matches.length)).toEqual([500, 500, 200]);+ });++ it('reads a GTIN under each key the cards are written with', async () => {+ const { fake, rpc } = setup();+ fake+ .rows('prompt_result_shopping_cards')+ .push(+ card('c1', { raw: { ean: '0012345678905' } }),+ card('c2', { raw: { upc: 12345678905 } }),+ card('c3', { raw: { gtin13: '0012345678905', title: 'x' } }),+ card('c4', { raw: { gtin: '999' } }),+ );+ const result = await matchBrandShoppingCards(BRAND, { db: fake.db, now: NOW });+ expect(result).toEqual({ cards: 4, matched: 3, changed: 3 });+ expect(rpc.mock.calls[0][1].p_matches).toEqual([+ { id: 'c1', product_id: 'p-shoe', match: 'gtin' },+ { id: 'c2', product_id: 'p-shoe', match: 'gtin' },+ { id: 'c3', product_id: 'p-shoe', match: 'gtin' },+ ]);+ });+});++describe('sweepShoppingCardMatches', () => {+ it('matches each brand whose catalog synced in the last day once, and goes on past a failure', async () => {+ const fake = fakeDb();+ fake.db.rpc = vi.fn(async () => ({ data: 0, error: null }));+ fake+ .rows('catalog_sources')+ .push(+ { id: 's1', brand_id: 'b1', last_status: 'ok', last_synced_at: '2026-10-27T03:00:00Z' },+ { id: 's2', brand_id: 'b1', last_status: 'ok', last_synced_at: '2026-10-27T04:00:00Z' },+ { id: 's3', brand_id: 'b2', last_status: 'ok', last_synced_at: '2026-10-26T20:00:00Z' },+ { id: 's4', brand_id: 'b3', last_status: 'failed', last_synced_at: '2026-10-27T05:00:00Z' },+ { id: 's5', brand_id: 'b4', last_status: 'ok', last_synced_at: '2026-10-20T05:00:00Z' },+ );+ const brandsRead = [];+ const from = fake.db.from;+ fake.db.from = (table) => {+ const q = from(table);+ if (table !== 'brands') return q;+ const eq = q.eq;+ q.eq = (column, value) => {+ brandsRead.push(value);+ if (value === 'b2') throw new Error('down');+ return eq(column, value);+ };+ return q;+ };+ expect(await sweepShoppingCardMatches({ db: fake.db, now: NOW })).toEqual({+ brands: 2,+ changed: 0,+ });+ expect(brandsRead).toEqual(['b1', 'b2']);+ expect(logger.error).toHaveBeenCalledWith(+ { err: { name: 'Error', message: 'down' }, brandId: 'b2' },+ '[card-match] brand failed',+ );+ });+});diff --git a/server/src/lib/cloro-result-handler.js b/server/src/lib/cloro-result-handler.jsindex ee4a282ad..7749ceaa1 100644--- a/server/src/lib/cloro-result-handler.js+++ b/server/src/lib/cloro-result-handler.js@@ -26,6 +26,7 @@ import { updateTargetUrlStats } from './target-url-stats.js';import { persistCitationRows } from './citation-rows.js';import { logger } from './logger.js';import { classifyMerchant } from './merchant-detection.js';+import { catalogIndexFor, matchCard } from './catalog/card-match.js';/*** @param {object} args@@ -182,12 +183,41 @@ export async function persistShoppingCards({};});+ // The brand's own cards, matched to its catalog's products (00360): what+ // Product Visibility counts on the shelf. The catalog may be unreadable;+ // the cards are written all the same, and the next sweep matches them.+ const own = rows.filter((row) => row.matched_brand_role === 'own');+ if (own.length) {+ const index = await catalogIndexFor(brandId, { brandName: brandInfo.brandName }).catch(+ (err) => {+ logger.warn({ err: err?.message, brandId }, '[cloro-result] catalog unreadable for cards');+ return null;+ },+ );+ for (const row of index ? own : []) {+ const found = matchCard(row, index);+ if (found) {+ row.catalog_product_id = found.productId;+ row.catalog_match = found.match;+ }+ }+ }+// `onConflict: 'prompt_result_id,position' ignoreDuplicates: true` keeps// the call idempotent — re-running the backfill or a retry on the same// prompt_result won't duplicate or error.- const { error } = await supabaseAdmin- .from('prompt_result_shopping_cards')- .upsert(rows, { onConflict: 'prompt_result_id,position', ignoreDuplicates: true });+ const write = (values) =>+ supabaseAdmin+ .from('prompt_result_shopping_cards')+ .upsert(values, { onConflict: 'prompt_result_id,position', ignoreDuplicates: true });+ let { error } = await write(rows);+ // An API deployed before its migration (00360): the cards go in without+ // their matches rather than not at all.+ if (error && /catalog_(product_id|match)/.test(error.message ?? '')) {+ ({ error } = await write(+ rows.map(({ catalog_product_id: _p, catalog_match: _m, ...row }) => row),+ ));+ }if (error) {logger.error(diff --git a/server/src/lib/cloro-result-handler.test.js b/server/src/lib/cloro-result-handler.test.jsindex 91819f8cc..277883d5c 100644--- a/server/src/lib/cloro-result-handler.test.js+++ b/server/src/lib/cloro-result-handler.test.js@@ -7,9 +7,17 @@ vi.mock('../config/supabase.js', () => ({default: { from: () => ({ upsert }) },}));vi.mock('./ai-tracker.js', () => ({ analyzeSentimentAI: vi.fn() }));+const catalogIndexFor = vi.hoisted(() => vi.fn());+vi.mock('./catalog/card-match.js', async (importOriginal) => ({+ ...(await importOriginal()),+ catalogIndexFor,+}));+import { catalogIndex } from './catalog/card-match.js';import { persistShoppingCards } from './cloro-result-handler.js';+const noCatalog = catalogIndex({ products: [], variants: [] });+const base = {promptResultId: 'result-1',brandId: 'brand-1',@@ -23,6 +31,7 @@ describe('persistShoppingCards – ChatGPT inline products (#1183)', () => {beforeEach(() => {upsert.mockReset();upsert.mockResolvedValue({ error: null });+ catalogIndexFor.mockReset().mockResolvedValue(noCatalog);});it('stores inline products after the carousel cards', async () => {@@ -107,3 +116,71 @@ describe('persistShoppingCards – ChatGPT inline products (#1183)', () => {expect(upsert).not.toHaveBeenCalled();});});++describe('persistShoppingCards – the catalog product behind an own card (00360)', () => {+ const catalog = catalogIndex(+ {+ products: [+ {+ id: 'p-runner',+ title: 'Acme Trail Runner X',+ url: 'https://acme.com/products/trail-runner-x',+ },+ { id: 'p-pack', title: 'Acme Ridge 30L Backpack', url: null },+ ],+ variants: [+ { product_id: 'p-pack', url: null, gtin: null, marketplace_item_id: 'B0C123ABCD' },+ ],+ },+ { brandName: 'Acme' },+ );+ const cards = [+ {+ products: [+ {+ title: 'Acme Trail Runner X',+ url: 'https://acme.com/products/trail-runner-x?variant=4',+ price: '$129.00',+ },+ { title: 'Acme Ridge Pack', url: 'https://www.amazon.com/dp/B0C123ABCD', price: '$119.00' },+ { title: 'Summit Daypack 30 W', url: 'https://summit.example/p/daypack', price: '$110.00' },+ ],+ },+ ];++ beforeEach(() => {+ upsert.mockReset();+ upsert.mockResolvedValue({ error: null });+ catalogIndexFor.mockReset().mockResolvedValue(catalog);+ });++ it("matches the brand's own cards by their link or listing, and leaves a rival's alone", async () => {+ await persistShoppingCards({ ...base, rawCards: cards });+ expect(catalogIndexFor).toHaveBeenCalledWith('brand-1', { brandName: 'Acme' });+ const rows = upsert.mock.calls[0][0];+ expect(rows.map((row) => [row.catalog_product_id ?? null, row.catalog_match ?? null])).toEqual([+ ['p-runner', 'url'],+ ['p-pack', 'listing'],+ [null, null],+ ]);+ });++ it("writes the cards without matches when the catalog can't be read", async () => {+ catalogIndexFor.mockRejectedValue(new Error('down'));+ expect(await persistShoppingCards({ ...base, rawCards: cards })).toBe(3);+ expect(upsert.mock.calls[0][0].some((row) => 'catalog_product_id' in row)).toBe(false);+ });++ it('writes the cards again without their matches before the migration is in', async () => {+ upsert+ .mockResolvedValueOnce({+ error: {+ message: "Could not find the 'catalog_match' column of 'prompt_result_shopping_cards'",+ },+ })+ .mockResolvedValueOnce({ error: null });+ expect(await persistShoppingCards({ ...base, rawCards: cards })).toBe(3);+ expect(upsert).toHaveBeenCalledTimes(2);+ expect(upsert.mock.calls[1][0].some((row) => 'catalog_product_id' in row)).toBe(false);+ });+});diff --git a/server/src/server.js b/server/src/server.jsindex f9bc38352..b548485b4 100644--- a/server/src/server.js+++ b/server/src/server.js@@ -32,6 +32,7 @@ import { sweepImproveOutcomes } from './lib/action-center/improve-outcome.js';import { sweepBrandFacts } from './lib/brand-facts-sweep.js';import { purgeRemovedCatalogProducts } from './lib/catalog/purge.js';import { sweepCatalogSources } from './lib/catalog/sources.js';+import { sweepShoppingCardMatches } from './lib/catalog/card-match.js';import { sweepWebflowPublished } from './lib/cms/webflow-status.js';import { generateBriefForOpportunity } from './routes/content.js';import webhookContentCallbackRoutes from './routes/webhook-content-callback.js';@@ -316,6 +317,8 @@ async function runDailyTracking() {// Catalog feed URLs (#1263): read again once a day, never blocking tracking.// Then the products removed from their source over 90 days ago are// deleted: after the sweep, so it isn't deleting while a sync brings one back.+ // Then the brands' AI shopping cards are matched to their catalogs as they+ // are now (product visibility, 00360).sweepCatalogSources().catch((err) => {logger.error({ err }, '[catalog] daily sweep failed');@@ -323,6 +326,13 @@ async function runDailyTracking() {.then(() => purgeRemovedCatalogProducts()).catch((err) => {logger.error({ err }, '[catalog] daily purge failed');+ })+ .then(() => sweepShoppingCardMatches())+ .catch((err) => {+ logger.error(+ { err: { name: err?.name, message: err?.message } },+ '[card-match] daily sweep failed',+ );});// Pages sent to Webflow, published there since (#1331): read once a day.diff --git a/supabase/migrations/00360_shopping_card_catalog_match.sql b/supabase/migrations/00360_shopping_card_catalog_match.sqlnew file mode 100644index 000000000..8281a162c--- /dev/null+++ b/supabase/migrations/00360_shopping_card_catalog_match.sql@@ -0,0 +1,80 @@+-- ============================================================================+-- 00360: the catalog product behind an AI shopping card (product visibility, part 2)+-- ============================================================================+-- Product Visibility's Shelf, Products and Categories (ECOM-005…008) count a+-- brand's products on the AI "shelf": which catalog product (00278) each+-- product card in an AI answer (prompt_result_shopping_cards) shows. A card+-- the brand's own (matched_brand_role 'own') is matched to the brand's+-- catalog when it's written, and again after the catalog changes:+--+-- * by the marketplace listing it links (an ASIN, an eBay item id) to a+-- variant's marketplace_item_id;+-- * by a GTIN the card carries, to a variant's;+-- * by its link, to a product's or a variant's page;+-- * else by its title, close enough to one product's and to no other.+--+-- * catalog_product_id: the product, null for none (a rival's card, or a+-- product the catalog doesn't have); catalog_match: how it was found,+-- read only with a product. A product deleted from the catalog leaves+-- its cards' catalog_match behind until the next sweep clears it.+-- * set_shopping_card_matches: the API writes a brand's matches in bulk+-- after a catalog sync. Members read the cards as before (RLS); only+-- the server writes these.++alter table public.prompt_result_shopping_cards+ add column if not exists catalog_product_id uuid+ references public.catalog_products(id) on delete set null,+ add column if not exists catalog_match text+ check (catalog_match is null or catalog_match in ('listing', 'gtin', 'url', 'title'));++create index if not exists prompt_result_shopping_cards_catalog_product_idx+ on public.prompt_result_shopping_cards (brand_id, catalog_product_id)+ where catalog_product_id is not null;++-- A catalog product's cards by the product alone: what `on delete set null`+-- looks up when a product is deleted, which the index above can't serve.+create index if not exists prompt_result_shopping_cards_catalog_product_id_idx+ on public.prompt_result_shopping_cards (catalog_product_id)+ where catalog_product_id is not null;++comment on column public.prompt_result_shopping_cards.catalog_product_id is+ 'Product visibility (00360): the brand''s catalog product this card shows, matched by its listing, GTIN, link or title; null for none.';+comment on column public.prompt_result_shopping_cards.catalog_match is+ 'How catalog_product_id was found: listing, gtin, url or title (00360).';++-- A brand's cards' matches, in bulk: [{"id", "product_id", "match"}], a null+-- product_id clearing one. Only the brand's own cards change, and only those+-- whose match changed; returns how many did.+create or replace function public.set_shopping_card_matches(p_brand_id uuid, p_matches jsonb)+returns integer+language sql+security definer+set search_path = public+as $$+ with m as (+ select+ (e->>'id')::uuid as id,+ nullif(e->>'product_id', '')::uuid as product_id,+ nullif(e->>'match', '') as match+ from jsonb_array_elements(coalesce(p_matches, '[]'::jsonb)) as e+ ),+ changed as (+ update public.prompt_result_shopping_cards c+ set catalog_product_id = m.product_id,+ catalog_match = case when m.product_id is null then null else m.match end+ from m+ where c.id = m.id+ and c.brand_id = p_brand_id+ and (+ c.catalog_product_id is distinct from m.product_id+ or c.catalog_match is distinct from (case when m.product_id is null then null else m.match end)+ )+ returning 1+ )+ select count(*)::integer from changed;+$$;++revoke all on function public.set_shopping_card_matches(uuid, jsonb) from public, anon, authenticated;+grant execute on function public.set_shopping_card_matches(uuid, jsonb) to service_role;++notify pgrst, 'reload schema';diff --git a/supabase/schema.sql b/supabase/schema.sqlindex 9b4b53da6..df098bd88 100644--- a/supabase/schema.sql+++ b/supabase/schema.sql@@ -41860,6 +41860,90 @@ comment on table public.content_shopping_checks isnotify pgrst, 'reload schema';+-- ─────────────────────────────────────────────────────────────────────────+-- migrations/00360_shopping_card_catalog_match.sql+-- ─────────────────────────────────────────────────────────────────────────+-- ============================================================================+-- 00360: the catalog product behind an AI shopping card (product visibility, part 2)+-- ============================================================================+-- Product Visibility's Shelf, Products and Categories (ECOM-005…008) count a+-- brand's products on the AI "shelf": which catalog product (00278) each+-- product card in an AI answer (prompt_result_shopping_cards) shows. A card+-- the brand's own (matched_brand_role 'own') is matched to the brand's+-- catalog when it's written, and again after the catalog changes:+--+-- * by the marketplace listing it links (an ASIN, an eBay item id) to a+-- variant's marketplace_item_id;+-- * by a GTIN the card carries, to a variant's;+-- * by its link, to a product's or a variant's page;+-- * else by its title, close enough to one product's and to no other.+--+-- * catalog_product_id: the product, null for none (a rival's card, or a+-- product the catalog doesn't have); catalog_match: how it was found,+-- read only with a product. A product deleted from the catalog leaves+-- its cards' catalog_match behind until the next sweep clears it.+-- * set_shopping_card_matches: the API writes a brand's matches in bulk+-- after a catalog sync. Members read the cards as before (RLS); only+-- the server writes these.++alter table public.prompt_result_shopping_cards+ add column if not exists catalog_product_id uuid+ references public.catalog_products(id) on delete set null,+ add column if not exists catalog_match text+ check (catalog_match is null or catalog_match in ('listing', 'gtin', 'url', 'title'));++create index if not exists prompt_result_shopping_cards_catalog_product_idx+ on public.prompt_result_shopping_cards (brand_id, catalog_product_id)+ where catalog_product_id is not null;++-- A catalog product's cards by the product alone: what `on delete set null`+-- looks up when a product is deleted, which the index above can't serve.+create index if not exists prompt_result_shopping_cards_catalog_product_id_idx+ on public.prompt_result_shopping_cards (catalog_product_id)+ where catalog_product_id is not null;++comment on column public.prompt_result_shopping_cards.catalog_product_id is+ 'Product visibility (00360): the brand''s catalog product this card shows, matched by its listing, GTIN, link or title; null for none.';+comment on column public.prompt_result_shopping_cards.catalog_match is+ 'How catalog_product_id was found: listing, gtin, url or title (00360).';++-- A brand's cards' matches, in bulk: [{"id", "product_id", "match"}], a null+-- product_id clearing one. Only the brand's own cards change, and only those+-- whose match changed; returns how many did.+create or replace function public.set_shopping_card_matches(p_brand_id uuid, p_matches jsonb)+returns integer+language sql+security definer+set search_path = public+as $$+ with m as (+ select+ (e->>'id')::uuid as id,+ nullif(e->>'product_id', '')::uuid as product_id,+ nullif(e->>'match', '') as match+ from jsonb_array_elements(coalesce(p_matches, '[]'::jsonb)) as e+ ),+ changed as (+ update public.prompt_result_shopping_cards c+ set catalog_product_id = m.product_id,+ catalog_match = case when m.product_id is null then null else m.match end+ from m+ where c.id = m.id+ and c.brand_id = p_brand_id+ and (+ c.catalog_product_id is distinct from m.product_id+ or c.catalog_match is distinct from (case when m.product_id is null then null else m.match end)+ )+ returning 1+ )+ select count(*)::integer from changed;+$$;++revoke all on function public.set_shopping_card_matches(uuid, jsonb) from public, anon, authenticated;+grant execute on function public.set_shopping_card_matches(uuid, jsonb) to service_role;++notify pgrst, 'reload schema';+-- ─────────────────────────────────────────────────────────────────────────-- migrations/00365_action_outcome_mixed.sql-- ─────────────────────────────────────────────────────────────────────────diff --git a/web/src/types/supabase.ts b/web/src/types/supabase.tsindex 7af3610ca..aa221971a 100644--- a/web/src/types/supabase.ts+++ b/web/src/types/supabase.ts@@ -3424,6 +3424,8 @@ export type Database = {prompt_result_shopping_cards: {Row: {brand_id: string+ catalog_match: string | null+ catalog_product_id: string | nullcreated_at: stringid: stringimage_url: string | null@@ -3448,6 +3450,8 @@ export type Database = {}Insert: {brand_id: string+ catalog_match?: string | null+ catalog_product_id?: string | nullcreated_at?: stringid?: stringimage_url?: string | null@@ -3472,6 +3476,8 @@ export type Database = {}Update: {brand_id?: string+ catalog_match?: string | null+ catalog_product_id?: string | nullcreated_at?: stringid?: stringimage_url?: string | null@@ -3495,6 +3501,13 @@ export type Database = {review_count?: number | null}Relationships: [+ {+ foreignKeyName: "prompt_result_shopping_cards_catalog_product_id_fkey"+ columns: ["catalog_product_id"]+ isOneToOne: false+ referencedRelation: "catalog_products"+ referencedColumns: ["id"]+ },{foreignKeyName: "prompt_result_shopping_cards_brand_id_fkey"columns: ["brand_id"]