Skip to content

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.

feat(content): check each draft's shopping links after a catalog sync and show it in the Library (00352, shopping path, part 7a) (#1615)

Cihana6d3b07a10ca
diff --git a/server/src/lib/catalog/__fixtures__/fake-db.js b/server/src/lib/catalog/__fixtures__/fake-db.js
index 2d8778844..adf75ad8c 100644
--- a/server/src/lib/catalog/__fixtures__/fake-db.js
+++ b/server/src/lib/catalog/__fixtures__/fake-db.js
@@ -49,6 +49,7 @@ export function fakeDb({ maxRows = Infinity } = {}) {
if (table === 'catalog_categories') return `${row.brand_id}|${row.name}`;
if (table === 'catalog_source_credentials') return row.source_id;
if (table === 'catalog_category_pages') return `${row.brand_id}|${row.category}`;
+ if (table === 'content_shopping_checks') return row.draft_id;
return `${row.brand_id}|${row.source}|${row.external_id}`;
};
// The unique keys' rows, by table, as the database's unique indexes find them.
@@ -76,6 +77,7 @@ export function fakeDb({ maxRows = Infinity } = {}) {
if (kind === 'eq') return at === value;
if (kind === 'neq') return at !== value;
if (kind === 'is') return value === null ? at == null : at === value;
+ if (kind === 'not_is') return value === null ? at != null : at !== value;
// SQL's: a null is never less.
if (kind === 'lt') return at != null && at < value;
return value.includes(at);
@@ -152,6 +154,12 @@ export function fakeDb({ maxRows = Infinity } = {}) {
state.filters.push(['is', column, value]);
return builder;
},
+ // Only `.not(column, 'is', value)`: a draft with a shopping path.
+ not: (column, operator, value) => {
+ if (operator !== 'is') throw new Error(`unexpected not.${operator}`);
+ state.filters.push(['not_is', column, value]);
+ return builder;
+ },
lt: (column, value) => {
state.filters.push(['lt', column, value]);
return builder;
diff --git a/server/src/lib/catalog/sources.js b/server/src/lib/catalog/sources.js
index 105c79a1a..0c4a2548c 100644
--- a/server/src/lib/catalog/sources.js
+++ b/server/src/lib/catalog/sources.js
@@ -6,6 +6,7 @@ import { FeedFormatError, parseFeed } from '../feeds/index.js';
import { isComposioConfigured } from '../composio.js';
import { PageFetchError } from '../safe-fetch.js';
import { logger } from '../logger.js';
+import { queueShoppingCheck } from '../shopping-check.js';
import { CredentialsKeyError, readCredentials } from './credentials.js';
import { saveItemIssues } from './issues.js';
import { signInRequest as workspaceSignIn, workspaceEntity } from './sign-in.js';
@@ -150,13 +151,16 @@ async function record(db, id, at, outcome) {
* A source being read already isn't read again at once: the answer says so,
* and nothing is recorded. A store's calls stop after `readMs` (STORE_READ_MS).
*
+ * After a sync that read the catalog, the brand's pages' shopping links are
+ * checked against it (shopping-check.js), behind the answer.
+ *
* @param {{ id: string, brand_id: string, kind: string, format: string, url?: string|null, account?: string|null }} source
- * @param {{ text?: string, db?: object, now?: Date, fetch?: Function, request?: Function, signInRequest?: Function, connectors?: object, readMs?: number }} [options]
+ * @param {{ text?: string, db?: object, now?: Date, fetch?: Function, request?: Function, signInRequest?: Function, connectors?: object, readMs?: number, afterSync?: Function }} [options]
* `fetch` (a feed's), `request` (a store's HTTP client), `signInRequest`
- * (the workspace sign-in's, sign-in.js), `connectors` and `readMs` are
- * the tests' stand-ins
+ * (the workspace sign-in's, sign-in.js), `connectors`, `readMs` and
+ * `afterSync` (the shopping check's queue) are the tests' stand-ins
*/
-export async function syncSource(source, options = {}) {
+export async function syncSource(source, { afterSync = queueShoppingCheck, ...options } = {}) {
if (reading.has(source.id)) {
return {
status: 'failed',
@@ -164,11 +168,14 @@ export async function syncSource(source, options = {}) {
};
}
reading.add(source.id);
+ let outcome;
try {
- return await syncOnce(source, options);
+ outcome = await syncOnce(source, options);
} finally {
reading.delete(source.id);
}
+ if (outcome.status === 'ok') afterSync(source.brand_id, options.db ? { db: options.db } : {});
+ return outcome;
}
async function syncOnce(
diff --git a/server/src/lib/catalog/sources.test.js b/server/src/lib/catalog/sources.test.js
index 3ef1876bd..92fdfa47c 100644
--- a/server/src/lib/catalog/sources.test.js
+++ b/server/src/lib/catalog/sources.test.js
@@ -119,6 +119,25 @@ describe('syncSource', () => {
// A broken feed doesn't mark the catalog removed.
expect(fake.rows('catalog_products').every((p) => p.removed_at === null)).toBe(true);
});
+
+ it("checks the brand's pages' shopping links after a sync that read the catalog, only then", async () => {
+ const afterSync = vi.fn();
+ await syncSource(source, {
+ db: fake.db,
+ now: NOW,
+ fetch: async () => ({ html: fixture('google-feed.xml') }),
+ afterSync,
+ });
+ expect(afterSync).toHaveBeenCalledWith('brand-1', { db: fake.db });
+ afterSync.mockClear();
+ await syncSource(source, {
+ db: fake.db,
+ now: NOW,
+ fetch: async () => ({ html: 'name,email\nAda,[email protected]' }),
+ afterSync,
+ });
+ expect(afterSync).not.toHaveBeenCalled();
+ });
});
describe('syncSource for a store (00282)', () => {
diff --git a/server/src/lib/shopping-check.js b/server/src/lib/shopping-check.js
new file mode 100644
index 000000000..5fe81a019
--- /dev/null
+++ b/server/src/lib/shopping-check.js
@@ -0,0 +1,435 @@
+import * as cheerio from 'cheerio';
+import supabaseAdmin from '../config/supabase.js';
+import { isBrandActive } from './active-subscriptions.js';
+import { onBrandDomain } from './audit/host.js';
+import { brandSite } from './catalog/category-pages.js';
+import { isInStock } from './catalog/stock.js';
+import { selectInChunks } from './chunked-in.js';
+import { linkKey } from './content-links.js';
+import { logger } from './logger.js';
+import { readAllRpcRows } from './rpc-pages.js';
+import { fetchPage } from './safe-fetch.js';
+import { readIn, readShoppingPath, refuses } from './shopping-path.js';
+
+/**
+ * Whether a page's shopping links still sell (shopping path, part 7, 00352).
+ *
+ * After each catalog sync, every draft of the brand with a shopping path is
+ * read again: the live page where it is published, else the draft. Its links
+ * are matched to the path's category pages and products (by link-targets'
+ * key, as the editor's Rules and Before you publish match them), and each
+ * linked product is read as the catalog has it now: sold out (no variant in
+ * stock), refused (Merchant Center refuses every variant in stock in its free
+ * listings) or gone from the catalog. What it finds is kept per draft in
+ * content_shopping_checks, for the Content Library's Shopping path column.
+ *
+ * A live page is read only on the brand's site (brandSite), and only when
+ * it reads as the page: one with fewer than MIN_WORDS words (an empty
+ * shell, bot protection's page), or that links none of the path while its
+ * draft does, is read from its draft instead.
+ *
+ * Only the path's products are checked: a product linked by hand from
+ * outside the path isn't known to be one.
+ */
+
+/** Drafts read a request: their HTML comes with them. */
+export const DRAFT_BATCH = 50;
+/** The most drafts with a path a brand's check reads, in id order. */
+export const MAX_DRAFTS = 1000;
+/** Live pages one check reads at most, one at a time; the rest are read from their draft. */
+export const MAX_LIVE_PAGES = 50;
+/** A live page's read: its links are in its first megabyte. */
+const PAGE_TIMEOUT_MS = 10_000;
+const PAGE_BYTES = 1024 * 1024;
+/**
+ * A live page with fewer words than this isn't the page: an empty shell, or
+ * bot protection's (as the Action Center's automatic checks read one).
+ */
+export const MIN_WORDS = 20;
+/** A check stops reading live pages after this, and reads the rest from their drafts. */
+export const LIVE_PAGES_DEADLINE_MS = 3 * 60 * 1000;
+
+/** A link longer than this isn't read: no page's address is (as content_drafts caps its own). */
+export const MAX_HREF_CHARS = 2048;
+
+const TITLE_CHARS = 200;
+const MAX_BAD = 40;
+
+/**
+ * Every link's address in a page's HTML, and in the HTML blocks the editor
+ * keeps base64-encoded (`div[data-html-block]`, a product grid among them).
+ * A relative address is read against `base`, the live page's own; without
+ * one it is dropped, as a draft's links are full addresses. A link longer
+ * than MAX_HREF_CHARS is dropped too.
+ */
+export function pageHrefs(html, base = null) {
+ const hrefs = [];
+ const read = (markup, nested) => {
+ const $ = cheerio.load(markup);
+ $('a[href]').each((_, a) => {
+ const href = String($(a).attr('href') ?? '').trim();
+ if (!href || href.length > MAX_HREF_CHARS) return;
+ try {
+ hrefs.push(base ? new URL(href, base).toString() : new URL(href).toString());
+ } catch {
+ // Not an address: a broken href, or a relative one without a base.
+ }
+ });
+ if (nested) return;
+ $('[data-html-block]').each((_, el) => {
+ const code = Buffer.from(String($(el).attr('data-html-block') ?? ''), 'base64').toString(
+ 'utf8',
+ );
+ if (code) read(code, true);
+ });
+ };
+ read(String(html ?? ''), false);
+ return hrefs;
+}
+
+/** A page's words outside its code (scripts, styles, frames). */
+export function pageWords(html) {
+ const $ = cheerio.load(String(html ?? ''));
+ $('script, style, noscript, template, iframe, svg').remove();
+ const body = $('body');
+ return (body.length ? body : $.root())
+ .text()
+ .split(/[^\p{L}\p{N}]+/u)
+ .filter(Boolean).length;
+}
+
+/**
+ * Each product's state as the catalog has it now, by id: 'gone' (not in the
+ * brand's catalog, or removed from it), 'sold_out' (no variant in stock),
+ * 'refused' (Merchant Center refuses every variant in stock) or 'ok'; and
+ * its page and title in the catalog (its link, else a variant's), for a
+ * product the path keeps neither for (one the person removed).
+ *
+ * @returns {Promise<{ states: Map<string, string>, pages: Map<string, string>,
+ * titles: Map<string, string> }>}
+ */
+export async function productStates(db, brandId, ids) {
+ const unique = [...new Set(ids)];
+ const states = new Map(unique.map((id) => [id, 'gone']));
+ const pages = new Map();
+ const titles = new Map();
+ if (!unique.length) return { states, pages, titles };
+ const products = await readIn(
+ db,
+ 'catalog_products',
+ 'id, title, url, removed_at',
+ 'id',
+ unique,
+ {
+ order: 'id',
+ more: (q) => q.eq('brand_id', brandId),
+ },
+ );
+ const live = products.filter((p) => !p.removed_at).map((p) => p.id);
+ const variants = await readIn(
+ db,
+ 'catalog_variants',
+ 'id, product_id, external_id, url, availability',
+ 'product_id',
+ live,
+ { order: 'id', more: (q) => q.eq('brand_id', brandId).is('removed_at', null) },
+ );
+ for (const product of products) {
+ const url = product.url ?? variants.find((v) => v.product_id === product.id && v.url)?.url;
+ if (url) pages.set(product.id, url);
+ if (product.title) titles.set(product.id, product.title);
+ }
+ const inStock = variants.filter((v) => isInStock(v.availability));
+ const issues = await readIn(
+ db,
+ 'catalog_item_issues',
+ 'id, external_id, severity, reporting_context',
+ 'external_id',
+ [...new Set(inStock.map((v) => v.external_id))],
+ { order: 'id', more: (q) => q.eq('brand_id', brandId).eq('severity', 'disapproved') },
+ );
+ const refused = new Set(issues.filter(refuses).map((i) => i.external_id));
+ for (const id of live) {
+ const offered = inStock.filter((v) => v.product_id === id);
+ states.set(
+ id,
+ !offered.length
+ ? 'sold_out'
+ : offered.every((v) => refused.has(v.external_id))
+ ? 'refused'
+ : 'ok',
+ );
+ }
+ return { states, pages, titles };
+}
+
+/** A path's products and the ones the person removed or it left out: every id to read. */
+export function pathProductIds(path) {
+ return [
+ ...new Set([
+ ...path.products.map((p) => p.id),
+ ...path.removed,
+ ...path.leftOut.map((p) => p.id),
+ ]),
+ ];
+}
+
+/**
+ * What a page's links do with its path, as the editor's Rules read them
+ * (web lib/content/shopping-check.ts): how many of its category pages and
+ * of its products that sell it links, and the products it links that no
+ * longer sell. A product the person removed or the path left out counts
+ * only when it no longer sells: the page may still link it.
+ *
+ * @param {{ states: Map<string, string>, pages: Map<string, string>,
+ * titles?: Map<string, string> }} catalog
+ */
+export function linkCheck(hrefs, path, { states, pages, titles = new Map() }) {
+ const keys = new Set(hrefs.map((href) => linkKey(href)).filter(Boolean));
+ const categories = [...new Set(path.categories.map((c) => linkKey(c.url)).filter(Boolean))];
+ const inPath = new Set(path.products.map((p) => p.id));
+ const leftOutTitles = new Map(path.leftOut.map((p) => [p.id, p.title]));
+ const checked = [
+ ...path.products.map((p) => ({ id: p.id, title: p.title, url: p.url, inPath: true })),
+ ...[...new Set([...path.removed, ...path.leftOut.map((p) => p.id)])]
+ .filter((id) => !inPath.has(id))
+ .map((id) => ({
+ id,
+ title: leftOutTitles.get(id) ?? titles.get(id) ?? null,
+ url: pages.get(id),
+ inPath: false,
+ })),
+ ];
+ const linked = [];
+ const seenPages = new Set();
+ for (const product of checked) {
+ const key = product.url ? linkKey(product.url) : null;
+ // One per page: the path's first, before a removed one with the same page.
+ if (!key || !keys.has(key) || seenPages.has(key)) continue;
+ seenPages.add(key);
+ const state = states.get(product.id) ?? 'gone';
+ if (!product.inPath && state === 'ok') continue;
+ linked.push({
+ id: product.id,
+ title: String(product.title ?? '').slice(0, TITLE_CHARS),
+ state,
+ inPath: product.inPath,
+ });
+ }
+ const selling = path.products.filter((p) => (states.get(p.id) ?? 'gone') === 'ok').length;
+ return {
+ categories: categories.length,
+ categoriesLinked: categories.filter((key) => keys.has(key)).length,
+ products: selling,
+ productsLinked: linked.filter((p) => p.inPath && p.state === 'ok').length,
+ bad: linked
+ .filter((p) => p.state !== 'ok')
+ .map(({ inPath: _inPath, ...p }) => p)
+ .slice(0, MAX_BAD),
+ };
+}
+
+/** Whether a check found any of the path's links: a category page, or a product it names. */
+export function linksPath(check) {
+ return check.categoriesLinked > 0 || check.productsLinked > 0 || check.bad.length > 0;
+}
+
+/** Where a draft is live: its CMS's address, or an optimized page's own, once published. */
+export function livePage(draft) {
+ if (draft.status !== 'published') return null;
+ const url = draft.published_url ?? (draft.method === 'optimize' ? draft.source_url : null);
+ return typeof url === 'string' && /^https?:\/\//i.test(url) ? url : null;
+}
+
+/**
+ * A live page's links, or null when it can't be read (the draft's are read
+ * instead): no answer, an error, a redirect off the brand's site, or too
+ * few words to be the page.
+ */
+async function liveHrefs(url, readPage, domains) {
+ try {
+ const page = await readPage(url, {
+ timeoutMs: PAGE_TIMEOUT_MS,
+ maxBytes: PAGE_BYTES,
+ truncate: true,
+ });
+ if (page.status >= 400) return null;
+ if (!onBrandDomain(page.url, domains)) return null;
+ if (pageWords(page.html) < MIN_WORDS) return null;
+ return pageHrefs(page.html, page.url);
+ } catch {
+ return null;
+ }
+}
+
+/** The brand's site (brandSite), for the live pages read; none when it can't be read. */
+async function siteDomains(db, brandId) {
+ try {
+ return await brandSite(db, brandId);
+ } catch {
+ return [];
+ }
+}
+
+/** The brand's primary site, as a base for a draft's relative links; null when none. */
+async function primarySite(db, brandId) {
+ const { data, error } = await db
+ .from('brand_domains')
+ .select('domain')
+ .eq('brand_id', brandId)
+ .eq('is_primary', true)
+ .maybeSingle();
+ if (error || !data?.domain) return null;
+ return /^https?:\/\//i.test(data.domain) ? data.domain : `https://${data.domain}`;
+}
+
+/** The brand's drafts with a shopping path, a batch at a time, in id order. */
+async function draftBatch(db, brandId, from) {
+ const { data, error } = await db
+ .from('content_drafts')
+ .select('id, status, method, source_url, published_url, body_html, shopping_path')
+ .eq('brand_id', brandId)
+ .is('archived_at', null)
+ .not('shopping_path', 'is', null)
+ .order('id')
+ .range(from, from + DRAFT_BATCH - 1);
+ if (error) throw new Error(`content_drafts read: ${error.message}`);
+ return data ?? [];
+}
+
+/** Rows of drafts that lost their path or were archived since: not in `kept`. */
+async function dropStale(db, brandId, kept) {
+ const { data, error } = await readAllRpcRows('content_shopping_checks', (from, to) =>
+ db
+ .from('content_shopping_checks')
+ .select('draft_id')
+ .eq('brand_id', brandId)
+ .order('draft_id')
+ .range(from, to),
+ );
+ if (error) throw new Error(`content_shopping_checks read: ${error.message}`);
+ const stale = data.map((r) => r.draft_id).filter((id) => !kept.has(id));
+ const { error: dropErr } = await selectInChunks(stale, (chunk) =>
+ db.from('content_shopping_checks').delete().eq('brand_id', brandId).in('draft_id', chunk),
+ );
+ if (dropErr) throw new Error(`content_shopping_checks delete: ${dropErr.message}`);
+ return stale.length;
+}
+
+/**
+ * Checks a brand's pages' shopping links and keeps what it finds.
+ *
+ * @param {string} brandId
+ * @param {{ db?: object, now?: Date, readPage?: Function, livePages?: number }} [opts]
+ * `readPage` reads a live page (safe-fetch's fetchPage); tests pass their own.
+ * @returns {Promise<{ drafts: number, live: number, attention: number, dropped: number }>}
+ */
+export async function checkShoppingLinks(
+ brandId,
+ { db = supabaseAdmin, now = new Date(), readPage = fetchPage, livePages = MAX_LIVE_PAGES } = {},
+) {
+ const started = Date.now();
+ const site = await primarySite(db, brandId);
+ const domains = await siteDomains(db, brandId);
+ const kept = new Set();
+ let live = 0;
+ let attention = 0;
+ // Whether every draft with a path was read: only then is a row no draft
+ // was read for a stale one.
+ let complete = false;
+ for (let from = 0; from < MAX_DRAFTS; from += DRAFT_BATCH) {
+ const drafts = await draftBatch(db, brandId, from);
+ const paths = drafts
+ .map((d) => ({ draft: d, path: readShoppingPath(d.shopping_path, { draftId: d.id }) }))
+ .filter((d) => d.path);
+ const catalog = await productStates(
+ db,
+ brandId,
+ paths.flatMap((d) => pathProductIds(d.path)),
+ );
+ const rows = [];
+ for (const { draft, path } of paths) {
+ const url = livePage(draft);
+ const canRead =
+ url &&
+ onBrandDomain(url, domains) &&
+ live < livePages &&
+ Date.now() - started < LIVE_PAGES_DEADLINE_MS;
+ const read = canRead ? await liveHrefs(url, readPage, domains) : null;
+ if (canRead) live += 1;
+ // A draft's relative link ("/products/x") is read against its page, else the brand's site.
+ const base = url ?? draft.source_url ?? site;
+ const inDraft = linkCheck(pageHrefs(draft.body_html, base), path, catalog);
+ const onPage = read ? linkCheck(read, path, catalog) : null;
+ // A live page that links none of the path while its draft does wasn't
+ // read as it is (a shell its script fills, a page served to bots): its
+ // draft is read.
+ const fromPage = Boolean(onPage) && (linksPath(onPage) || !linksPath(inDraft));
+ const check = fromPage ? onPage : inDraft;
+ if (check.bad.length) attention += 1;
+ kept.add(draft.id);
+ rows.push({
+ draft_id: draft.id,
+ brand_id: brandId,
+ read_from: fromPage ? 'page' : 'draft',
+ page_url: fromPage ? url : null,
+ categories: check.categories,
+ categories_linked: check.categoriesLinked,
+ products: check.products,
+ products_linked: check.productsLinked,
+ bad: check.bad,
+ checked_at: now.toISOString(),
+ });
+ }
+ if (rows.length) {
+ const { error } = await db
+ .from('content_shopping_checks')
+ .upsert(rows, { onConflict: 'draft_id' });
+ if (error) throw new Error(`content_shopping_checks write: ${error.message}`);
+ }
+ if (drafts.length < DRAFT_BATCH) {
+ complete = true;
+ break;
+ }
+ }
+ const dropped = complete ? await dropStale(db, brandId, kept) : 0;
+ return { drafts: kept.size, live, attention, dropped };
+}
+
+/** Brands waiting for their check, with the check's options. */
+const queued = new Map();
+let draining = null;
+
+/**
+ * Checks a brand's pages after a catalog sync, behind the API's other work:
+ * one brand at a time, and a brand synced again while it waits is checked
+ * once. A brand whose workspace's plan ended isn't checked (#1608). A
+ * failure is logged; the next sync checks again.
+ */
+export function queueShoppingCheck(brandId, opts = {}) {
+ queued.set(brandId, opts);
+ if (!draining) {
+ draining = (async () => {
+ while (queued.size) {
+ const [[next, options]] = queued;
+ queued.delete(next);
+ try {
+ // A workspace whose plan ended gets no check: it reads live pages (#1608).
+ if (!(await isBrandActive(next))) continue;
+ const result = await checkShoppingLinks(next, options);
+ if (result.drafts || result.dropped) {
+ logger.info({ brandId: next, ...result }, '[shopping-check] pages checked');
+ }
+ } catch (err) {
+ logger.error(
+ { err: { name: err?.name, message: err?.message }, brandId: next },
+ '[shopping-check] check failed',
+ );
+ }
+ }
+ })().finally(() => {
+ draining = null;
+ });
+ }
+ return draining;
+}
diff --git a/server/src/lib/shopping-check.test.js b/server/src/lib/shopping-check.test.js
new file mode 100644
index 000000000..7dc7b01d8
--- /dev/null
+++ b/server/src/lib/shopping-check.test.js
@@ -0,0 +1,706 @@
+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() },
+}));
+const isBrandActive = vi.hoisted(() => vi.fn(async () => true));
+vi.mock('./active-subscriptions.js', () => ({ isBrandActive }));
+
+import { fakeDb } from './catalog/__fixtures__/fake-db.js';
+import { logger } from './logger.js';
+import { htmlBlockElement } from './shopping-blocks.js';
+import {
+ DRAFT_BATCH,
+ LIVE_PAGES_DEADLINE_MS,
+ MAX_DRAFTS,
+ MAX_HREF_CHARS,
+ MAX_LIVE_PAGES,
+ MIN_WORDS,
+ checkShoppingLinks,
+ linkCheck,
+ linksPath,
+ livePage,
+ pageHrefs,
+ pageWords,
+ productStates,
+ queueShoppingCheck,
+} from './shopping-check.js';
+
+/** Whether a page's shopping links still sell (shopping path, part 7, 00352). */
+
+const BRAND = 'brand-1';
+const NOW = new Date('2026-10-27T09:00:00Z');
+
+const product = (id, extra = {}) => ({
+ id,
+ title: `Boot ${id}`,
+ url: `https://shop.example/products/${id}`,
+ price: 189,
+ salePrice: null,
+ currency: 'USD',
+ availability: 'in_stock',
+ image: null,
+ issues: [],
+ score: 1,
+ ...extra,
+});
+
+/** A path as a build leaves it (00351). */
+const path = (
+ products,
+ categories = [{ name: 'Boots', url: 'https://shop.example/c/boots', title: 'Boots' }],
+) => ({
+ version: 1,
+ builtAt: '2026-10-26T09:00:00.000Z',
+ pageType: 'buying_guide',
+ categories,
+ products,
+ leftOut: [],
+ locked: [],
+ removed: [],
+ added: [],
+});
+
+/** A catalog with each product's variants: [availability, external id]. */
+function catalog(fake, products) {
+ for (const [id, variants, removed = null] of products) {
+ fake.rows('catalog_products').push({
+ id,
+ brand_id: BRAND,
+ title: `Boot ${id}`,
+ removed_at: removed,
+ });
+ for (const [availability, externalId] of variants) {
+ fake.rows('catalog_variants').push({
+ id: `v-${externalId}`,
+ brand_id: BRAND,
+ product_id: id,
+ external_id: externalId,
+ availability,
+ removed_at: null,
+ });
+ }
+ }
+}
+
+const link = (id) =>
+ `<a href="https://shop.example/products/${id}?utm_source=guide">Boot ${id}</a>`;
+
+/** A live page as the brand's site serves it: words around its links. */
+const livePageHtml = (links) =>
+ `<html><head><script>var words = "${'x '.repeat(50)}";</script></head><body><nav>Home Shop</nav><article><h1>How to choose winter boots</h1><p>${'Warm boots keep feet dry on snow and ice. '.repeat(4)}</p><p>${links}</p></article></body></html>`;
+
+describe('pageHrefs', () => {
+ it("reads every link, the HTML blocks' too, and a live page's relative ones against it", () => {
+ const html = `<p>${link('p1')} <a href="/products/p2">two</a> <a href="mailto:[email protected]">mail</a></p>${htmlBlockElement(
+ '<div><a href="https://shop.example/products/p3">three</a></div>',
+ )}`;
+ expect(pageHrefs(html)).toEqual([
+ 'https://shop.example/products/p1?utm_source=guide',
+ 'mailto:[email protected]',
+ 'https://shop.example/products/p3',
+ ]);
+ expect(pageHrefs(html, 'https://shop.example/blog/boots')).toContain(
+ 'https://shop.example/products/p2',
+ );
+ });
+
+ it('skips a link longer than a page address can be, and checks a page of them quickly', () => {
+ const at = (href) => `<a href="${href}">x</a>`;
+ const longest = `https://shop.example/products/${'a'.repeat(MAX_HREF_CHARS - 30)}`;
+ const slashes = `https://shop.example${'/'.repeat(100_000)}x`;
+ const html = `<p>${at(longest)}${at(`${longest}a`)}${at(slashes)}${link('p1')}</p>`;
+ expect(pageHrefs(html)).toEqual([longest, 'https://shop.example/products/p1?utm_source=guide']);
+ const started = Date.now();
+ linkCheck(pageHrefs(html), path([product('p1')]), {
+ states: new Map([['p1', 'ok']]),
+ pages: new Map(),
+ });
+ expect(Date.now() - started).toBeLessThan(1000);
+ });
+
+ it('reads no link from nothing, or from a block that is no HTML', () => {
+ expect(pageHrefs(null)).toEqual([]);
+ expect(pageHrefs('<div data-html-block="!!!"></div>')).toEqual([]);
+ });
+});
+
+describe('pageWords', () => {
+ it("counts a page's words, not its scripts' or styles'", () => {
+ expect(pageWords('<p>Hello, wide world</p><script>one two three</script>')).toBe(3);
+ expect(pageWords(livePageHtml(''))).toBeGreaterThanOrEqual(MIN_WORDS);
+ expect(pageWords('<html><body><div id="root"></div></body></html>')).toBe(0);
+ expect(pageWords(null)).toBe(0);
+ });
+});
+
+describe('linksPath', () => {
+ it('is whether a check found a category page or a product of the path', () => {
+ const none = { categoriesLinked: 0, productsLinked: 0, bad: [] };
+ expect(linksPath(none)).toBe(false);
+ expect(linksPath({ ...none, categoriesLinked: 1 })).toBe(true);
+ expect(linksPath({ ...none, productsLinked: 1 })).toBe(true);
+ expect(linksPath({ ...none, bad: [{ id: 'p2', title: '', state: 'gone' }] })).toBe(true);
+ });
+});
+
+describe('productStates', () => {
+ it('reads each product as sold out, refused, gone or fine, by its variants in stock', async () => {
+ const fake = fakeDb();
+ catalog(fake, [
+ [
+ 'p-ok',
+ [
+ ['in stock', 'o1'],
+ ['out_of_stock', 'o2'],
+ ],
+ ],
+ ['p-limited', [['Limited-Availability', 'l1']]],
+ [
+ 'p-sold',
+ [
+ ['out_of_stock', 's1'],
+ ['preorder', 's2'],
+ ],
+ ],
+ ['p-refused', [['in_stock', 'r1']]],
+ ['p-ads', [['in_stock', 'a1']]],
+ ['p-removed', [['in_stock', 'x1']], '2026-10-26T00:00:00Z'],
+ ]);
+ fake.rows('catalog_item_issues').push(
+ {
+ id: 'i1',
+ brand_id: BRAND,
+ external_id: 'r1',
+ severity: 'disapproved',
+ reporting_context: 'FREE_LISTINGS',
+ },
+ // A disapproval for ads leaves the product in the free listings.
+ {
+ id: 'i2',
+ brand_id: BRAND,
+ external_id: 'a1',
+ severity: 'disapproved',
+ reporting_context: 'SHOPPING_ADS',
+ },
+ // Another brand's issue for the same offer id isn't this one's.
+ {
+ id: 'i3',
+ brand_id: 'brand-2',
+ external_id: 'o1',
+ severity: 'disapproved',
+ reporting_context: '',
+ },
+ );
+ const { states } = await productStates(fake.db, BRAND, [
+ 'p-ok',
+ 'p-limited',
+ 'p-sold',
+ 'p-refused',
+ 'p-ads',
+ 'p-removed',
+ 'p-unknown',
+ ]);
+ expect(Object.fromEntries(states)).toEqual({
+ 'p-ok': 'ok',
+ 'p-limited': 'ok',
+ 'p-sold': 'sold_out',
+ 'p-refused': 'refused',
+ 'p-ads': 'ok',
+ 'p-removed': 'gone',
+ 'p-unknown': 'gone',
+ });
+ });
+
+ it("reads past PostgREST's 1000-row cap", async () => {
+ const fake = fakeDb({ maxRows: 1000 });
+ catalog(fake, [
+ [
+ 'p1',
+ Array.from({ length: 1200 }, (_, i) => ['out_of_stock', `x${String(i).padStart(4, '0')}`]),
+ ],
+ ]);
+ // The one variant in stock sorts after the first 1000.
+ fake.rows('catalog_variants').push({
+ id: 'v-zzzz',
+ brand_id: BRAND,
+ product_id: 'p1',
+ external_id: 'zzzz',
+ availability: 'in_stock',
+ removed_at: null,
+ });
+ expect((await productStates(fake.db, BRAND, ['p1'])).states.get('p1')).toBe('ok');
+ });
+
+ it("reads each product's title in the catalog, a removed one's too", async () => {
+ const fake = fakeDb();
+ catalog(fake, [
+ ['p1', [['in_stock', 'a']]],
+ ['p-removed', [['in_stock', 'x']], '2026-10-26T00:00:00Z'],
+ ]);
+ const { titles } = await productStates(fake.db, BRAND, ['p1', 'p-removed', 'p-unknown']);
+ expect(Object.fromEntries(titles)).toEqual({ p1: 'Boot p1', 'p-removed': 'Boot p-removed' });
+ });
+});
+
+describe('linkCheck', () => {
+ it("counts the path's linked category pages and products, and the linked ones that don't sell", () => {
+ const states = new Map([
+ ['p1', 'ok'],
+ ['p2', 'sold_out'],
+ ['p3', 'refused'],
+ ]);
+ const check = linkCheck(
+ [
+ 'https://www.shop.example/c/boots/',
+ 'https://shop.example/products/p1',
+ 'https://shop.example/products/p2?utm_campaign=x',
+ 'https://shop.example/products/p2',
+ ],
+ path([product('p1'), product('p2'), product('p3'), product('p4')]),
+ { states, pages: new Map() },
+ );
+ // Of the four, p1 and p4 sell (p4 isn't read: gone); p1 is linked and sells.
+ expect(check).toEqual({
+ categories: 1,
+ categoriesLinked: 1,
+ products: 1,
+ productsLinked: 1,
+ bad: [{ id: 'p2', title: 'Boot p2', state: 'sold_out' }],
+ });
+ });
+
+ it('counts a removed or left-out product the page still links, only when it no longer sells', () => {
+ const removedPath = {
+ ...path([product('p1')]),
+ removed: ['r-sold', 'r-ok'],
+ leftOut: [{ id: 'l-gone', title: 'Old Boot', reason: 'gone' }],
+ };
+ const check = linkCheck(
+ [
+ 'https://shop.example/products/p1',
+ 'https://shop.example/products/r-sold',
+ 'https://shop.example/products/r-ok',
+ 'https://shop.example/products/l-gone',
+ ],
+ removedPath,
+ {
+ states: new Map([
+ ['p1', 'ok'],
+ ['r-sold', 'sold_out'],
+ ['r-ok', 'ok'],
+ ['l-gone', 'gone'],
+ ]),
+ pages: new Map([
+ ['r-sold', 'https://shop.example/products/r-sold'],
+ ['r-ok', 'https://shop.example/products/r-ok'],
+ ['l-gone', 'https://shop.example/products/l-gone'],
+ ]),
+ },
+ );
+ expect(check.productsLinked).toBe(1);
+ expect(check.bad).toEqual([
+ { id: 'r-sold', title: '', state: 'sold_out' },
+ { id: 'l-gone', title: 'Old Boot', state: 'gone' },
+ ]);
+ });
+
+ it("names a removed product by its catalog title, the path's own first", () => {
+ const removedPath = {
+ ...path([product('p1')]),
+ removed: ['r-sold', 'l-gone'],
+ leftOut: [{ id: 'l-gone', title: 'Old Boot', reason: 'gone' }],
+ };
+ const check = linkCheck(
+ ['https://shop.example/products/r-sold', 'https://shop.example/products/l-gone'],
+ removedPath,
+ {
+ states: new Map([
+ ['r-sold', 'sold_out'],
+ ['l-gone', 'gone'],
+ ]),
+ pages: new Map([
+ ['r-sold', 'https://shop.example/products/r-sold'],
+ ['l-gone', 'https://shop.example/products/l-gone'],
+ ]),
+ titles: new Map([
+ ['r-sold', 'Storm Boot'],
+ ['l-gone', 'Old Boot (catalog)'],
+ ]),
+ },
+ );
+ expect(check.bad).toEqual([
+ { id: 'r-sold', title: 'Storm Boot', state: 'sold_out' },
+ { id: 'l-gone', title: 'Old Boot', state: 'gone' },
+ ]);
+ });
+});
+
+describe('livePage', () => {
+ it("is a published page's CMS address, or an optimized page's own, and nothing before", () => {
+ const published = {
+ status: 'published',
+ method: 'create',
+ published_url: 'https://shop.example/blog/a',
+ };
+ expect(livePage(published)).toBe('https://shop.example/blog/a');
+ expect(livePage({ ...published, status: 'ready' })).toBeNull();
+ expect(
+ livePage({
+ status: 'published',
+ method: 'optimize',
+ published_url: null,
+ source_url: 'https://shop.example/b',
+ }),
+ ).toBe('https://shop.example/b');
+ expect(
+ livePage({
+ status: 'published',
+ method: 'create',
+ published_url: null,
+ source_url: 'https://x.example',
+ }),
+ ).toBeNull();
+ });
+});
+
+describe('checkShoppingLinks', () => {
+ function setup({ maxRows } = {}) {
+ const fake = fakeDb({ maxRows });
+ catalog(fake, [
+ ['p1', [['in_stock', 'a']]],
+ ['p2', [['out_of_stock', 'b']]],
+ ]);
+ fake.rows('brand_domains').push({ brand_id: BRAND, domain: 'shop.example', is_primary: true });
+ return fake;
+ }
+ const draft = (id, extra = {}) => ({
+ id,
+ brand_id: BRAND,
+ status: 'draft',
+ method: 'create',
+ source_url: null,
+ published_url: null,
+ archived_at: null,
+ body_html: `<p><a href="https://shop.example/c/boots">Boots</a> ${link('p1')} ${link('p2')}</p>`,
+ shopping_path: path([product('p1'), product('p2')]),
+ ...extra,
+ });
+
+ it("keeps each draft's check, read from its live page when published, else from the draft", async () => {
+ const fake = setup();
+ fake
+ .rows('content_drafts')
+ .push(
+ draft('d1'),
+ draft('d2', { status: 'published', published_url: 'https://shop.example/blog/boots' }),
+ draft('d3', { shopping_path: null }),
+ draft('d4', { archived_at: '2026-10-20T00:00:00Z' }),
+ draft('d5', { brand_id: 'brand-2' }),
+ );
+ // The live page was fixed in the CMS: it links only p1.
+ const readPage = vi.fn().mockResolvedValue({
+ url: 'https://shop.example/blog/boots',
+ status: 200,
+ html: livePageHtml('<a href="/products/p1">one</a>'),
+ });
+ const result = await checkShoppingLinks(BRAND, { db: fake.db, now: NOW, readPage });
+ expect(result).toEqual({ drafts: 2, live: 1, attention: 1, dropped: 0 });
+ expect(readPage).toHaveBeenCalledOnce();
+ expect(readPage.mock.calls[0][0]).toBe('https://shop.example/blog/boots');
+ const rows = Object.fromEntries(
+ fake.rows('content_shopping_checks').map((r) => [r.draft_id, r]),
+ );
+ expect(Object.keys(rows).sort()).toEqual(['d1', 'd2']);
+ expect(rows.d1).toMatchObject({
+ brand_id: BRAND,
+ read_from: 'draft',
+ page_url: null,
+ categories: 1,
+ categories_linked: 1,
+ // p2 sold out: one of the two sells, and the page links it.
+ products: 1,
+ products_linked: 1,
+ bad: [{ id: 'p2', title: 'Boot p2', state: 'sold_out' }],
+ checked_at: NOW.toISOString(),
+ });
+ expect(rows.d2).toMatchObject({
+ read_from: 'page',
+ page_url: 'https://shop.example/blog/boots',
+ categories_linked: 0,
+ products_linked: 1,
+ bad: [],
+ });
+ });
+
+ it("reads a draft's relative links against the brand's site", async () => {
+ const fake = setup();
+ fake.rows('content_drafts').push(draft('d1', { body_html: '<a href="/products/p2">two</a>' }));
+ await checkShoppingLinks(BRAND, { db: fake.db, now: NOW });
+ expect(fake.rows('content_shopping_checks')[0].bad).toEqual([
+ { id: 'p2', title: 'Boot p2', state: 'sold_out' },
+ ]);
+ });
+
+ it("reads a live page it can't reach, or one that answers an error, from its draft", async () => {
+ const fake = setup();
+ fake
+ .rows('content_drafts')
+ .push(
+ draft('d1', { status: 'published', published_url: 'https://shop.example/a' }),
+ draft('d2', { status: 'published', published_url: 'https://shop.example/b' }),
+ );
+ const readPage = vi
+ .fn()
+ .mockRejectedValueOnce(new Error('timeout'))
+ .mockResolvedValueOnce({ url: 'https://shop.example/b', status: 404, html: '' });
+ await checkShoppingLinks(BRAND, { db: fake.db, now: NOW, readPage });
+ expect(fake.rows('content_shopping_checks').map((r) => [r.read_from, r.bad.length])).toEqual([
+ ['draft', 1],
+ ['draft', 1],
+ ]);
+ });
+
+ it("reads a live page with too few words, or off the brand's site, from its draft", async () => {
+ const fake = setup();
+ fake
+ .rows('content_drafts')
+ .push(
+ draft('d1', { status: 'published', published_url: 'https://shop.example/a' }),
+ draft('d2', { status: 'published', published_url: 'https://shop.example/b' }),
+ draft('d3', { status: 'published', published_url: 'https://elsewhere.example/c' }),
+ );
+ const readPage = vi
+ .fn()
+ // Too short to be the page, though it links p1.
+ .mockResolvedValueOnce({
+ url: 'https://shop.example/a',
+ status: 200,
+ html: '<html><body><a href="/products/p1">Boot</a><script>render()</script></body></html>',
+ })
+ // Redirected off the brand's site.
+ .mockResolvedValueOnce({
+ url: 'https://parked.example/b',
+ status: 200,
+ html: livePageHtml(link('p1')),
+ });
+ const result = await checkShoppingLinks(BRAND, { db: fake.db, now: NOW, readPage });
+ // d3 is off the brand's site: never fetched.
+ expect(readPage.mock.calls.map(([url]) => url)).toEqual([
+ 'https://shop.example/a',
+ 'https://shop.example/b',
+ ]);
+ expect(result.live).toBe(2);
+ expect(fake.rows('content_shopping_checks').map((r) => [r.read_from, r.bad.length])).toEqual([
+ ['draft', 1],
+ ['draft', 1],
+ ['draft', 1],
+ ]);
+ });
+
+ it('reads from its draft a live page that links none of the path while the draft does', async () => {
+ const fake = setup();
+ fake.rows('content_drafts').push(
+ draft('d1', { status: 'published', published_url: 'https://shop.example/a' }),
+ draft('d2', {
+ status: 'published',
+ published_url: 'https://shop.example/b',
+ body_html: '<p>No links yet.</p>',
+ }),
+ );
+ // Words enough, but none of the path's links: a page served to bots, say.
+ const readPage = vi.fn(async (url) => ({
+ url,
+ status: 200,
+ html: livePageHtml('<a href="/about">About us</a>'),
+ }));
+ await checkShoppingLinks(BRAND, { db: fake.db, now: NOW, readPage });
+ const rows = Object.fromEntries(
+ fake.rows('content_shopping_checks').map((r) => [r.draft_id, r]),
+ );
+ // d1's draft links p2, sold out: the page isn't taken as fixed.
+ expect(rows.d1).toMatchObject({ read_from: 'draft', page_url: null });
+ expect(rows.d1.bad).toHaveLength(1);
+ // d2's draft links none either: the live page is read as it is.
+ expect(rows.d2).toMatchObject({
+ read_from: 'page',
+ page_url: 'https://shop.example/b',
+ bad: [],
+ });
+ });
+
+ it("reads no live page when the brand's site can't be read", async () => {
+ const fake = setup();
+ fake
+ .rows('content_drafts')
+ .push(draft('d1', { status: 'published', published_url: 'https://shop.example/a' }));
+ const from = fake.db.from;
+ const db = {
+ from: (table) =>
+ table === 'brand_domains'
+ ? {
+ select: () => ({
+ eq: () => ({
+ eq: () => ({ maybeSingle: async () => ({ data: null, error: null }) }),
+ then: (resolve) => resolve({ data: null, error: { message: 'down' } }),
+ }),
+ }),
+ }
+ : from(table),
+ };
+ const readPage = vi.fn();
+ await checkShoppingLinks(BRAND, { db, now: NOW, readPage });
+ expect(readPage).not.toHaveBeenCalled();
+ expect(fake.rows('content_shopping_checks')[0].read_from).toBe('draft');
+ });
+
+ it('reads at most its live pages a run; the rest from their drafts', async () => {
+ const fake = setup();
+ fake
+ .rows('content_drafts')
+ .push(
+ ...Array.from({ length: 3 }, (_, i) =>
+ draft(`d${i}`, { status: 'published', published_url: `https://shop.example/${i}` }),
+ ),
+ );
+ const readPage = vi.fn(async (url) => ({ url, status: 200, html: livePageHtml(link('p1')) }));
+ const result = await checkShoppingLinks(BRAND, {
+ db: fake.db,
+ now: NOW,
+ readPage,
+ livePages: 2,
+ });
+ expect(readPage).toHaveBeenCalledTimes(2);
+ expect(result.live).toBe(2);
+ expect(fake.rows('content_shopping_checks').map((r) => r.read_from)).toEqual([
+ 'page',
+ 'page',
+ 'draft',
+ ]);
+ expect(MAX_LIVE_PAGES).toBeGreaterThan(2);
+ });
+
+ it('stops reading live pages at its deadline; the rest from their drafts', async () => {
+ const fake = setup();
+ fake
+ .rows('content_drafts')
+ .push(
+ ...Array.from({ length: 3 }, (_, i) =>
+ draft(`d${i}`, { status: 'published', published_url: `https://shop.example/${i}` }),
+ ),
+ );
+ vi.useFakeTimers({ toFake: ['Date'] });
+ try {
+ const readPage = vi.fn(async (url) => {
+ // The first page takes the whole deadline.
+ vi.setSystemTime(Date.now() + LIVE_PAGES_DEADLINE_MS);
+ return { url, status: 200, html: livePageHtml(link('p1')) };
+ });
+ const result = await checkShoppingLinks(BRAND, { db: fake.db, now: NOW, readPage });
+ expect(readPage).toHaveBeenCalledOnce();
+ expect(result.live).toBe(1);
+ } finally {
+ vi.useRealTimers();
+ }
+ expect(fake.rows('content_shopping_checks').map((r) => r.read_from)).toEqual([
+ 'page',
+ 'draft',
+ 'draft',
+ ]);
+ });
+
+ it('drops no row when it stops at its most drafts: the others may still have a path', async () => {
+ const fake = setup();
+ fake
+ .rows('content_drafts')
+ .push(
+ ...Array.from({ length: MAX_DRAFTS }, (_, i) => draft(`d${String(i).padStart(4, '0')}`)),
+ );
+ // A draft past the cap, and its row from an earlier run.
+ fake.rows('content_drafts').push(draft('d9999'));
+ fake.rows('content_shopping_checks').push({ draft_id: 'd9999', brand_id: BRAND });
+ const result = await checkShoppingLinks(BRAND, { db: fake.db, now: NOW });
+ expect(result).toMatchObject({ drafts: MAX_DRAFTS, dropped: 0 });
+ expect(fake.rows('content_shopping_checks').map((r) => r.draft_id)).toContain('d9999');
+ });
+
+ it('drops the rows of drafts that lost their path or were archived, and keeps the others', async () => {
+ const fake = setup();
+ fake.rows('content_drafts').push(draft('d1'), draft('d2'));
+ await checkShoppingLinks(BRAND, { db: fake.db, now: NOW });
+ expect(fake.rows('content_shopping_checks')).toHaveLength(2);
+ fake.rows('content_drafts').find((d) => d.id === 'd2').shopping_path = null;
+ fake.rows('content_shopping_checks').push({ draft_id: 'other', brand_id: 'brand-2' });
+ const result = await checkShoppingLinks(BRAND, { db: fake.db, now: NOW });
+ expect(result.dropped).toBe(1);
+ expect(
+ fake
+ .rows('content_shopping_checks')
+ .map((r) => r.draft_id)
+ .sort(),
+ ).toEqual(['d1', 'other']);
+ });
+
+ it('reads the drafts a batch at a time, every one of them', async () => {
+ const fake = setup({ maxRows: 1000 });
+ fake
+ .rows('content_drafts')
+ .push(
+ ...Array.from({ length: DRAFT_BATCH + 3 }, (_, i) =>
+ draft(`d${String(i).padStart(3, '0')}`),
+ ),
+ );
+ const result = await checkShoppingLinks(BRAND, { db: fake.db, now: NOW });
+ expect(result.drafts).toBe(DRAFT_BATCH + 3);
+ expect(fake.rows('content_shopping_checks')).toHaveLength(DRAFT_BATCH + 3);
+ });
+
+ it("reads a path it can't read as none, and logs it", async () => {
+ const fake = setup();
+ fake
+ .rows('content_drafts')
+ .push(draft('d1', { shopping_path: { version: 1, products: [null] } }));
+ const result = await checkShoppingLinks(BRAND, { db: fake.db, now: NOW });
+ expect(result.drafts).toBe(0);
+ expect(logger.warn).toHaveBeenCalled();
+ });
+});
+
+describe('queueShoppingCheck', () => {
+ it("checks no brand whose workspace's plan ended (#1608)", async () => {
+ const fake = fakeDb();
+ fake.rows('content_drafts').push({
+ id: 'd1',
+ brand_id: 'lapsed',
+ status: 'draft',
+ archived_at: null,
+ body_html: '',
+ shopping_path: path([product('p1')]),
+ });
+ isBrandActive.mockImplementation(async (brandId) => brandId !== 'lapsed');
+ await queueShoppingCheck('lapsed', { db: fake.db });
+ expect(isBrandActive).toHaveBeenCalledWith('lapsed');
+ expect(fake.rows('content_shopping_checks')).toEqual([]);
+ expect(fake.writes).toEqual([]);
+ isBrandActive.mockImplementation(async () => true);
+ });
+
+ it('checks the brands one at a time, a brand queued twice once, and logs a failure', async () => {
+ const fake = fakeDb();
+ const broken = {
+ from: () => {
+ throw new Error('down');
+ },
+ };
+ queueShoppingCheck('brand-a', { db: fake.db });
+ // Queued twice while brand-a is checked: checked once.
+ queueShoppingCheck('brand-b', { db: broken });
+ await queueShoppingCheck('brand-b', { db: broken });
+ expect(logger.error).toHaveBeenCalledWith(
+ { err: { name: 'Error', message: 'down' }, brandId: 'brand-b' },
+ '[shopping-check] check failed',
+ );
+ expect(logger.error).toHaveBeenCalledTimes(1);
+ });
+});
diff --git a/server/src/lib/shopping-path.js b/server/src/lib/shopping-path.js
index ecbcb3241..7778e5cf5 100644
--- a/server/src/lib/shopping-path.js
+++ b/server/src/lib/shopping-path.js
@@ -263,7 +263,7 @@ async function readRows(what, page, limit = Infinity) {
* (chunked-in.js), each read to its end in `order` (a unique key, so no
* row is read twice or missed across pages).
*/
-async function readIn(db, table, columns, column, ids, { order, more = (q) => q }) {
+export async function readIn(db, table, columns, column, ids, { order, more = (q) => q }) {
const { data, error } = await selectInChunks(ids, (chunk) =>
readAllRpcRows(table, (from, to) =>
more(db.from(table).select(columns).in(column, chunk)).order(order).range(from, to),
diff --git a/supabase/migrations/00352_content_shopping_checks.sql b/supabase/migrations/00352_content_shopping_checks.sql
new file mode 100644
index 000000000..5a2f39cf1
--- /dev/null
+++ b/supabase/migrations/00352_content_shopping_checks.sql
@@ -0,0 +1,69 @@
+-- ============================================================================
+-- 00352: whether a page's shopping links still sell (shopping path, part 7)
+-- ============================================================================
+-- A buying guide, a best-of list or a comparison sends its readers, and the
+-- AI answers that quote it, to the category pages and the products of its
+-- shopping path (00351). A product sells out, Google Merchant Center refuses
+-- it, or it leaves the catalog, and the page goes on linking it. After each
+-- catalog sync the API reads every page with a shopping path again: the live
+-- page where it is published (content_drafts.published_url, or an optimized
+-- page's own address), else the draft, and keeps here what its links do with
+-- the path. The Content Library shows it in its Shopping path column.
+--
+-- * One row per draft with a shopping path; it goes with the draft, and
+-- the next check removes a row whose draft lost its path or was archived.
+-- * `bad`: the linked products that no longer sell, [{id, title, state}]
+-- with state sold_out, refused or gone, and `needs_attention` whether
+-- there are any.
+-- * Members read their brands' rows; the server writes them.
+
+create table if not exists public.content_shopping_checks (
+ draft_id uuid primary key references public.content_drafts(id) on delete cascade,
+ brand_id uuid not null references public.brands(id) on delete cascade,
+ -- Where the links were read: the live page, or the draft when it isn't
+ -- published or its page couldn't be read.
+ read_from text not null check (read_from in ('page', 'draft')),
+ page_url text check (
+ page_url is null or (char_length(page_url) <= 2048 and page_url ~* '^https?://')
+ ),
+ -- The path's category pages and products, and how many of them the page links.
+ categories smallint not null default 0 check (categories between 0 and 5),
+ categories_linked smallint not null default 0,
+ products smallint not null default 0 check (products between 0 and 40),
+ products_linked smallint not null default 0,
+ bad jsonb not null default '[]'::jsonb check (
+ jsonb_typeof(bad) = 'array' and jsonb_array_length(bad) <= 40
+ ),
+ needs_attention boolean generated always as (
+ jsonb_typeof(bad) = 'array' and jsonb_array_length(bad) > 0
+ ) stored,
+ checked_at timestamptz not null default now(),
+ constraint content_shopping_checks_linked_check check (
+ categories_linked between 0 and categories and products_linked between 0 and products
+ )
+);
+
+create index if not exists content_shopping_checks_brand_idx
+ on public.content_shopping_checks (brand_id, needs_attention);
+
+alter table public.content_shopping_checks enable row level security;
+revoke all on table public.content_shopping_checks from anon, authenticated;
+grant select on table public.content_shopping_checks to authenticated;
+grant all on table public.content_shopping_checks to service_role;
+
+drop policy if exists "content_shopping_checks: member select" on public.content_shopping_checks;
+create policy "content_shopping_checks: member select"
+ on public.content_shopping_checks
+ for select
+ using (
+ brand_id in (
+ select b.id from public.brands b
+ join public.profiles p on p.organization_id = b.organization_id
+ where p.id = auth.uid()
+ )
+ );
+
+comment on table public.content_shopping_checks is
+ 'Shopping path, part 7 (00352): what a page''s links do with its shopping path (00351) as the last catalog sync left the catalog: how many of its category pages and products it links, and the linked products that no longer sell (sold out, refused by Merchant Center, gone). Read from the live page when published, else the draft. Server writes, members read.';
+
+notify pgrst, 'reload schema';
diff --git a/supabase/schema.sql b/supabase/schema.sql
index 950462176..d9663b7f6 100644
--- a/supabase/schema.sql
+++ b/supabase/schema.sql
@@ -41787,3 +41787,76 @@ comment on column public.content_drafts.shopping_path is
notify pgrst, 'reload schema';
+-- ─────────────────────────────────────────────────────────────────────────
+-- migrations/00352_content_shopping_checks.sql
+-- ─────────────────────────────────────────────────────────────────────────
+-- ============================================================================
+-- 00352: whether a page's shopping links still sell (shopping path, part 7)
+-- ============================================================================
+-- A buying guide, a best-of list or a comparison sends its readers, and the
+-- AI answers that quote it, to the category pages and the products of its
+-- shopping path (00351). A product sells out, Google Merchant Center refuses
+-- it, or it leaves the catalog, and the page goes on linking it. After each
+-- catalog sync the API reads every page with a shopping path again: the live
+-- page where it is published (content_drafts.published_url, or an optimized
+-- page's own address), else the draft, and keeps here what its links do with
+-- the path. The Content Library shows it in its Shopping path column.
+--
+-- * One row per draft with a shopping path; it goes with the draft, and
+-- the next check removes a row whose draft lost its path or was archived.
+-- * `bad`: the linked products that no longer sell, [{id, title, state}]
+-- with state sold_out, refused or gone, and `needs_attention` whether
+-- there are any.
+-- * Members read their brands' rows; the server writes them.
+
+create table if not exists public.content_shopping_checks (
+ draft_id uuid primary key references public.content_drafts(id) on delete cascade,
+ brand_id uuid not null references public.brands(id) on delete cascade,
+ -- Where the links were read: the live page, or the draft when it isn't
+ -- published or its page couldn't be read.
+ read_from text not null check (read_from in ('page', 'draft')),
+ page_url text check (
+ page_url is null or (char_length(page_url) <= 2048 and page_url ~* '^https?://')
+ ),
+ -- The path's category pages and products, and how many of them the page links.
+ categories smallint not null default 0 check (categories between 0 and 5),
+ categories_linked smallint not null default 0,
+ products smallint not null default 0 check (products between 0 and 40),
+ products_linked smallint not null default 0,
+ bad jsonb not null default '[]'::jsonb check (
+ jsonb_typeof(bad) = 'array' and jsonb_array_length(bad) <= 40
+ ),
+ needs_attention boolean generated always as (
+ jsonb_typeof(bad) = 'array' and jsonb_array_length(bad) > 0
+ ) stored,
+ checked_at timestamptz not null default now(),
+ constraint content_shopping_checks_linked_check check (
+ categories_linked between 0 and categories and products_linked between 0 and products
+ )
+);
+
+create index if not exists content_shopping_checks_brand_idx
+ on public.content_shopping_checks (brand_id, needs_attention);
+
+alter table public.content_shopping_checks enable row level security;
+revoke all on table public.content_shopping_checks from anon, authenticated;
+grant select on table public.content_shopping_checks to authenticated;
+grant all on table public.content_shopping_checks to service_role;
+
+drop policy if exists "content_shopping_checks: member select" on public.content_shopping_checks;
+create policy "content_shopping_checks: member select"
+ on public.content_shopping_checks
+ for select
+ using (
+ brand_id in (
+ select b.id from public.brands b
+ join public.profiles p on p.organization_id = b.organization_id
+ where p.id = auth.uid()
+ )
+ );
+
+comment on table public.content_shopping_checks is
+ 'Shopping path, part 7 (00352): what a page''s links do with its shopping path (00351) as the last catalog sync left the catalog: how many of its category pages and products it links, and the linked products that no longer sell (sold out, refused by Merchant Center, gone). Read from the live page when published, else the draft. Server writes, members read.';
+
+notify pgrst, 'reload schema';
+
diff --git a/web/messages/en.json b/web/messages/en.json
index a78db8c14..4ff94a268 100644
--- a/web/messages/en.json
+++ b/web/messages/en.json
@@ -3512,6 +3512,30 @@
"published": "Published",
"empty": "No page in this tab matches the Last send filter."
},
+ "shopping": {
+ "label": "Shopping path",
+ "filters": {
+ "all": "All",
+ "attention": "Needs attention",
+ "ok": "OK",
+ "none": "None"
+ },
+ "empty": "No page listed here has a shopping path like this.",
+ "none": "None",
+ "unchecked": "Not checked yet",
+ "uncheckedHint": "Its links are checked against your catalog after the next catalog sync.",
+ "ok": "OK",
+ "soldOut": "{count} sold out",
+ "notForSale": "{count} not for sale",
+ "linked": "Links {categoriesLinked} of {categories, plural, one {# category page} other {# category pages}} and {productsLinked} of {products, plural, one {# product} other {# products}} in its shopping path.",
+ "states": {
+ "sold_out": "{title}: sold out",
+ "refused": "{title}: refused by Google Merchant Center",
+ "gone": "{title}: no longer in your catalog"
+ },
+ "checkedPage": "Checked on the live page after the catalog sync of {when}.",
+ "checkedDraft": "Checked in the draft after the catalog sync of {when}."
+ },
"title": "Library",
"description": "Everything you and the agents wrote or changed.",
"selectBrand": "Select a brand to see its content.",
@@ -3532,7 +3556,8 @@
"status": "Status",
"created": "Created",
"updated": "Updated",
- "menu": "Row actions"
+ "menu": "Row actions",
+ "shopping": "Shopping path"
},
"rowMenu": {
"label": "Actions for {title}",
diff --git a/web/src/app/[locale]/(dashboard)/dashboard/content/library/_library-table.tsx b/web/src/app/[locale]/(dashboard)/dashboard/content/library/_library-table.tsx
index f94b04657..6bff35415 100644
--- a/web/src/app/[locale]/(dashboard)/dashboard/content/library/_library-table.tsx
+++ b/web/src/app/[locale]/(dashboard)/dashboard/content/library/_library-table.tsx
@@ -52,6 +52,7 @@ import {
type LibraryTab,
} from '@/lib/content/drafts';
import type { CmsStatus } from '@/lib/content/cms-send-client';
+import type { DraftShopping } from '@/lib/content/shopping-check';
import { DRAFT_EXPORT_FORMATS, type DraftExportFormat } from '@/lib/content/draft-export';
import { formatDate, formatMonthDay } from '@/lib/format';
@@ -87,6 +88,7 @@ export interface LibrarySelection {
* editor; its menu sets the status and, for admins and managers, sends it
* to the CMS (#1331), archives or deletes it (00233). Beside its status, its
* last send to the CMS; with a selection, a box to pick rows to send together.
+ * An online shop's Library has a Shopping path column too (00352).
*/
export function LibraryTable({
drafts,
@@ -94,6 +96,7 @@ export function LibraryTable({
actions,
cms,
selection,
+ shopping = false,
}: {
drafts: ContentDraftListItem[];
tab: LibraryTab;
@@ -101,6 +104,8 @@ export function LibraryTable({
/** Each draft's last send to the CMS; none for a draft never sent. */
cms: ReadonlyMap<string, CmsStatus>;
selection?: LibrarySelection;
+ /** Whether to show the Shopping path column: the brand sells online. */
+ shopping?: boolean;
}) {
const t = useTranslations('contentEditor.library');
const tTypes = useTranslations('contentEditor.pageTypes');
@@ -137,6 +142,7 @@ export function LibraryTable({
<TableHead>{t('columns.method')}</TableHead>
<TableHead>{t('columns.by')}</TableHead>
<TableHead>{t('columns.status')}</TableHead>
+ {shopping && <TableHead>{t('columns.shopping')}</TableHead>}
<TableHead>{t('columns.created')}</TableHead>
<TableHead>{t('columns.updated')}</TableHead>
<TableHead className="w-8 pl-0 pr-4">
@@ -196,6 +202,11 @@ export function LibraryTable({
{sent && <CmsStatusMark status={sent} />}
</span>
</TableCell>
+ {shopping && (
+ <TableCell>
+ <ShoppingPathMark shopping={draft.shopping} />
+ </TableCell>
+ )}
<TableCell className="whitespace-nowrap text-xs text-muted-foreground tabular-nums">
<time dateTime={draft.createdAt} title={formatDate(draft.createdAt)}>
{tableDate(draft.createdAt)}
@@ -288,6 +299,84 @@ function RowMenu({ draft, actions }: { draft: ContentDraftListItem; actions: Lib
);
}
+const SHOPPING_BADGE = {
+ ok: 'border-green-500/30 bg-green-500/10 text-green-600 dark:text-green-400',
+ attention: 'border-amber-500/30 bg-amber-500/10 text-amber-700 dark:text-amber-400',
+} as const;
+
+/**
+ * A draft's shopping path (00352): OK, or how many of the products it links
+ * no longer sell; its tip says what it links, which ones and why, and where
+ * and when it was checked. None, or not checked yet, in grey.
+ */
+function ShoppingPathMark({ shopping }: { shopping: DraftShopping | undefined }) {
+ const t = useTranslations('contentEditor.library.shopping');
+ const format = useFormatter();
+ if (!shopping || shopping.state === 'none') {
+ return <span className="text-xs text-muted-foreground">{t('none')}</span>;
+ }
+ const label =
+ shopping.state === 'unchecked'
+ ? t('unchecked')
+ : shopping.state === 'ok'
+ ? t('ok')
+ : shopping.bad.every((link) => link.state === 'sold_out')
+ ? t('soldOut', { count: shopping.bad.length })
+ : t('notForSale', { count: shopping.bad.length });
+ return (
+ <Tooltip>
+ <TooltipTrigger
+ delay={300}
+ closeDelay={0}
+ render={
+ <button
+ type="button"
+ className="rounded focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring/50"
+ aria-label={label}
+ />
+ }
+ >
+ {shopping.state === 'unchecked' ? (
+ <span className="text-xs text-muted-foreground">{label}</span>
+ ) : (
+ <Badge
+ variant="outline"
+ className={`whitespace-nowrap ${SHOPPING_BADGE[shopping.state]}`}
+ >
+ {label}
+ </Badge>
+ )}
+ </TooltipTrigger>
+ <TooltipContent className="max-w-72 space-y-1">
+ {shopping.state === 'unchecked' ? (
+ <p>{t('uncheckedHint')}</p>
+ ) : (
+ <>
+ <p>
+ {t('linked', {
+ categoriesLinked: shopping.categoriesLinked,
+ categories: shopping.categories,
+ productsLinked: shopping.productsLinked,
+ products: shopping.products,
+ })}
+ </p>
+ {shopping.bad.map((link) => (
+ <p key={link.id} className="[overflow-wrap:anywhere]">
+ {t(`states.${link.state}`, { title: link.title })}
+ </p>
+ ))}
+ <p className="opacity-80">
+ {t(shopping.readFrom === 'page' ? 'checkedPage' : 'checkedDraft', {
+ when: format.dateTime(new Date(shopping.checkedAt), { dateStyle: 'medium' }),
+ })}
+ </p>
+ </>
+ )}
+ </TooltipContent>
+ </Tooltip>
+ );
+}
+
/**
* A draft's last send to the CMS, as an icon whose tip says when and how it
* went, and on wide screens its word: "In Webflow · draft", "Live"…
diff --git a/web/src/app/[locale]/(dashboard)/dashboard/content/library/page.test.tsx b/web/src/app/[locale]/(dashboard)/dashboard/content/library/page.test.tsx
index 15e09af60..e5ea778a9 100644
--- a/web/src/app/[locale]/(dashboard)/dashboard/content/library/page.test.tsx
+++ b/web/src/app/[locale]/(dashboard)/dashboard/content/library/page.test.tsx
@@ -1168,3 +1168,75 @@ describe('the Library’s Export and Created filter (CE-002)', () => {
expect(exportButton.disabled).toBe(true);
});
});
+
+describe('the Library’s Shopping path column (00352)', () => {
+ const checked = (bad: { id: string; title: string; state: 'sold_out' | 'refused' | 'gone' }[]) =>
+ ({
+ state: bad.length ? 'attention' : 'ok',
+ readFrom: 'page',
+ checkedAt: '2026-10-27T03:00:00Z',
+ categories: 1,
+ categoriesLinked: 1,
+ products: 4,
+ productsLinked: 4,
+ bad,
+ }) as const;
+ const drafts = [
+ row({
+ id: 'a',
+ title: 'Best rain jackets',
+ shopping: checked([
+ { id: 'p1', title: 'Storm Shell 2', state: 'sold_out' },
+ { id: 'p2', title: 'Pack-Lite Jacket', state: 'sold_out' },
+ ]),
+ }),
+ row({ id: 'b', title: 'How to choose boots', shopping: checked([]) }),
+ row({ id: 'c', title: 'How we test', shopping: { state: 'none' } }),
+ row({ id: 'd', title: 'Trail shoes', shopping: { state: 'unchecked' } }),
+ ];
+ const shop = () => {
+ brandState.brands = [{ id: 'brand-1', industry: 'Retail', shoppingModeEnabled: true }];
+ };
+ afterEach(() => {
+ brandState.brands = [
+ { id: 'brand-1', industry: 'Travel & Hospitality', shoppingModeEnabled: false },
+ ];
+ });
+
+ it('shows on an online shop what each page’s links do with its shopping path', async () => {
+ shop();
+ getContentDrafts.mockResolvedValue(drafts);
+ await renderPage();
+ const headers = [...container.querySelectorAll('th')].map((th) => th.textContent);
+ expect(headers).toContain('Shopping path');
+ const cells = [...container.querySelectorAll('tbody tr')].map(
+ (tr) => tr.querySelectorAll('td')[6]?.textContent,
+ );
+ expect(cells).toEqual(['2 sold out', 'OK', 'None', 'Not checked yet']);
+ });
+
+ it('lists the pages that need attention when the URL asks, and says so when there are none', async () => {
+ shop();
+ window.history.replaceState(null, '', '/dashboard/content/library?shopping=attention');
+ getContentDrafts.mockResolvedValue(drafts);
+ await renderPage();
+ expect(container.querySelectorAll('tbody tr')).toHaveLength(1);
+ expect(container.textContent).toContain('Best rain jackets');
+
+ getContentDrafts.mockResolvedValue(drafts.slice(1));
+ root.unmount();
+ root = createRoot(container);
+ await renderPage();
+ expect(container.textContent).toContain('No page listed here has a shopping path like this.');
+ });
+
+ it('isn’t there for a brand that doesn’t sell online', async () => {
+ window.history.replaceState(null, '', '/dashboard/content/library?shopping=attention');
+ getContentDrafts.mockResolvedValue(drafts);
+ await renderPage();
+ expect([...container.querySelectorAll('th')].map((th) => th.textContent)).not.toContain(
+ 'Shopping path',
+ );
+ expect(container.querySelectorAll('tbody tr')).toHaveLength(4);
+ });
+});
diff --git a/web/src/app/[locale]/(dashboard)/dashboard/content/library/page.tsx b/web/src/app/[locale]/(dashboard)/dashboard/content/library/page.tsx
index e07ee177f..8059973e2 100644
--- a/web/src/app/[locale]/(dashboard)/dashboard/content/library/page.tsx
+++ b/web/src/app/[locale]/(dashboard)/dashboard/content/library/page.tsx
@@ -57,6 +57,11 @@ import {
type SendFilter,
} from '@/lib/content/drafts';
import { siteTypeOf } from '@/lib/content/page-types';
+import {
+ SHOPPING_FILTERS,
+ draftsByShopping,
+ type ShoppingFilter,
+} from '@/lib/content/shopping-check';
import { formatNumber } from '@/lib/format';
import { readUrlChoice, readUrlParam, writeUrlParams } from '@/lib/url-state';
import { useBrandStore } from '@/stores/use-brand-store';
@@ -82,6 +87,8 @@ interface LibraryRead {
* send it to the CMS (#1331), archive, restore and delete. The tab lives in
* the URL (?tab=in_review), so a link can open "Library › In review";
* ?new=1 opens New content (Content › Editor with no draft to go back to).
+ * An online shop's pages show their shopping path's links, and a filter
+ * lists the ones that need attention (?shopping=attention, 00352).
*/
export default function ContentLibraryPage() {
const t = useTranslations('contentEditor.library');
@@ -93,6 +100,8 @@ export default function ContentLibraryPage() {
const [created, setCreated] = useState<CreatedRange>('all');
// How the listed drafts' last send to the CMS stands (#1331): ?sent=failed.
const [sent, setSent] = useState<SendFilter>('all');
+ // Which shopping paths are listed, on an online shop: ?shopping=attention.
+ const [shoppingFilter, setShoppingFilter] = useState<ShoppingFilter>('all');
const [read, setRead] = useState<LibraryRead | null>(null);
const [attempt, setAttempt] = useState(0);
const [starting, setStarting] = useState(false);
@@ -127,6 +136,7 @@ export default function ContentLibraryPage() {
setTab(readUrlChoice('tab', LIBRARY_TABS, 'all'));
setCreated(readUrlChoice('created', CREATED_RANGES, 'all'));
setSent(readUrlChoice('sent', SEND_FILTERS, 'all'));
+ setShoppingFilter(readUrlChoice('shopping', SHOPPING_FILTERS, 'all'));
// Once: closed, it doesn't open again on a reload.
if (readUrlParam('new') === '1') {
setStarting(true);
@@ -311,6 +321,11 @@ export default function ContentLibraryPage() {
clearPicked();
writeUrlParams({ sent: next }, { sent: 'all' });
};
+ const changeShopping = (next: ShoppingFilter) => {
+ setShoppingFilter(next);
+ clearPicked();
+ writeUrlParams({ shopping: next }, { shopping: 'all' });
+ };
if (!activeBrandId) {
return (
@@ -326,11 +341,14 @@ export default function ContentLibraryPage() {
const counts = current?.counts ?? null;
const inTab = drafts ? draftsInTab(drafts, tab) : [];
const createdIn = draftsCreatedIn(inTab, created);
+ // Shopping paths are an online shop's (shopping path, 00351).
+ const isShop = siteTypeOf(activeBrand ?? {}) === 'ecommerce';
+ const shopped = isShop ? draftsByShopping(createdIn, shoppingFilter) : createdIn;
// Filtered by their last send only once these drafts' sends are read (a
// list read again in another order needn't wait); till then, none listed
// to export or pick.
const sendsRead = sent === 'all' || !drafts || drafts.every((draft) => cmsReadFor.has(draft.id));
- const listed = sendsRead ? draftsLastSent(createdIn, cms, sent) : [];
+ const listed = sendsRead ? draftsLastSent(shopped, cms, sent) : [];
// Any of this brand's drafts sent: the sends of the last brand don't count.
const anySent = !!drafts?.some((draft) => cms.has(draft.id));
// Picked rows still listed (a read may have moved some), never archived ones.
@@ -435,6 +453,27 @@ export default function ContentLibraryPage() {
))}
</SelectContent>
</Select>
+ {isShop && (
+ <Select
+ value={shoppingFilter}
+ onValueChange={(value) => changeShopping(value as ShoppingFilter)}
+ items={SHOPPING_FILTERS.map((filter) => ({
+ value: filter,
+ label: `${t('shopping.label')}: ${t(`shopping.filters.${filter}`)}`,
+ }))}
+ >
+ <SelectTrigger size="sm" aria-label={t('shopping.label')}>
+ <SelectValue />
+ </SelectTrigger>
+ <SelectContent align="end">
+ {SHOPPING_FILTERS.map((filter) => (
+ <SelectItem key={filter} value={filter}>
+ {t(`shopping.filters.${filter}`)}
+ </SelectItem>
+ ))}
+ </SelectContent>
+ </Select>
+ )}
</div>
</div>
@@ -494,7 +533,13 @@ export default function ContentLibraryPage() {
</div>
) : listed.length === 0 && inTab.length > 0 ? (
<p className="px-6 py-12 text-center text-sm text-muted-foreground">
- {t(createdIn.length === 0 ? 'created.empty' : 'lastSend.empty')}
+ {t(
+ createdIn.length === 0
+ ? 'created.empty'
+ : shopped.length === 0
+ ? 'shopping.empty'
+ : 'lastSend.empty',
+ )}
</p>
) : (
<LibraryTable
@@ -503,6 +548,7 @@ export default function ContentLibraryPage() {
actions={rowActions}
cms={cms}
selection={selection}
+ shopping={isShop}
/>
)}
</CardContent>
@@ -573,14 +619,18 @@ export default function ContentLibraryPage() {
</DialogContent>
</Dialog>
{/* The cap's note: a filter that narrows the list isn't the cap. */}
- {counts && created === 'all' && sent === 'all' && listed.length < counts[tab] && (
- <p className="text-xs text-muted-foreground">
- {t('latestOnly', {
- shown: formatNumber(listed.length),
- count: formatNumber(counts[tab]),
- })}
- </p>
- )}
+ {counts &&
+ created === 'all' &&
+ sent === 'all' &&
+ (!isShop || shoppingFilter === 'all') &&
+ listed.length < counts[tab] && (
+ <p className="text-xs text-muted-foreground">
+ {t('latestOnly', {
+ shown: formatNumber(listed.length),
+ count: formatNumber(counts[tab]),
+ })}
+ </p>
+ )}
</div>
);
}
diff --git a/web/src/lib/actions/content-drafts-library.test.ts b/web/src/lib/actions/content-drafts-library.test.ts
index 852d49b30..2ebe5fa6c 100644
--- a/web/src/lib/actions/content-drafts-library.test.ts
+++ b/web/src/lib/actions/content-drafts-library.test.ts
@@ -8,6 +8,10 @@ import { beforeEach, describe, expect, it, vi } from 'vitest';
const BRAND = '33333333-3333-3333-3333-333333333333';
let rows: Record<string, unknown>[];
+// The shopping checks' read (00352): its rows, or the error it answers.
+let checkRows: Record<string, unknown>[];
+let checkError: { message: string } | null;
+let checkReads: string[][];
let counted: Record<string, number>;
let countReads: string[];
// The row menu's writes (00233): what each update or delete returns.
@@ -26,7 +30,22 @@ vi.mock('@/lib/content/action-drafts', async (importOriginal) => ({
vi.mock('@/lib/supabase/server', () => ({
createClient: vi.fn(async () => ({
auth: { getUser: async () => ({ data: { user: { id: 'u1' } } }) },
- from: () => {
+ from: (table: string) => {
+ if (table === 'content_shopping_checks') {
+ return {
+ select: () => ({
+ in: async (_col: string, ids: string[]) => {
+ checkReads.push(ids);
+ return checkError
+ ? { data: null, error: checkError }
+ : {
+ data: checkRows.filter((r) => ids.includes(r.draft_id as string)),
+ error: null,
+ };
+ },
+ }),
+ };
+ }
let head = false;
let status = 'all';
let archived = false;
@@ -79,6 +98,9 @@ const row = (i: number, status = 'draft') => ({
});
beforeEach(() => {
+ checkRows = [];
+ checkError = null;
+ checkReads = [];
countReads = [];
counted = { all: 812, draft: 300, in_review: 12, ready: 0, published: 500, archived: 7 };
writes = [];
@@ -111,6 +133,62 @@ describe('getContentDrafts', () => {
expect(counts).toMatchObject({ all: 1, published: 0, archived: 1 });
});
+ it("reads each draft's shopping path as the last catalog sync's check left it (00352)", async () => {
+ const check = {
+ read_from: 'page',
+ checked_at: '2026-10-27T03:00:00Z',
+ categories: 1,
+ categories_linked: 1,
+ products: 5,
+ products_linked: 4,
+ bad: [{ id: 'p2', title: 'Storm Shell 2', state: 'sold_out' }],
+ };
+ rows = [
+ row(1),
+ { ...row(2), shopping_built: '2026-10-26T09:00:00Z' },
+ { ...row(3), shopping_built: '2026-10-26T09:00:00Z' },
+ { ...row(4), shopping_built: '2026-10-26T09:00:00Z' },
+ ];
+ checkRows = [
+ { draft_id: 'd3', ...check },
+ { draft_id: 'd4', ...check, bad: [] },
+ ];
+ const { drafts } = await getContentDrafts(BRAND);
+ expect(drafts.map((d) => d.shopping?.state)).toEqual(['none', 'unchecked', 'attention', 'ok']);
+ // Only the drafts with a path are asked for.
+ expect(checkReads).toEqual([['d2', 'd3', 'd4']]);
+ expect(drafts[2].shopping).toEqual({
+ state: 'attention',
+ readFrom: 'page',
+ checkedAt: '2026-10-27T03:00:00Z',
+ categories: 1,
+ categoriesLinked: 1,
+ products: 5,
+ productsLinked: 4,
+ bad: [{ id: 'p2', title: 'Storm Shell 2', state: 'sold_out' }],
+ });
+ });
+
+ it("still lists the drafts when the shopping checks can't be read (00352 not applied)", async () => {
+ rows = [row(1), { ...row(2), shopping_built: '2026-10-26T09:00:00Z' }];
+ checkRows = [{ draft_id: 'd2', read_from: 'draft' }];
+ checkError = { message: 'relation "public.content_shopping_checks" does not exist' };
+ const { drafts } = await getContentDrafts(BRAND);
+ expect(drafts.map((d) => [d.id, d.shopping?.state])).toEqual([
+ ['d1', 'none'],
+ ['d2', 'unchecked'],
+ ]);
+ });
+
+ it('reads the shopping checks 100 drafts a request', async () => {
+ rows = Array.from({ length: 250 }, (_, i) => ({
+ ...row(i),
+ shopping_built: '2026-10-26T09:00:00Z',
+ }));
+ await getContentDrafts(BRAND);
+ expect(checkReads.map((ids) => ids.length)).toEqual([100, 100, 50]);
+ });
+
it('counts each tab on its own at the cap', async () => {
rows = Array.from({ length: 500 }, (_, i) => row(i));
const { drafts, counts } = await getContentDrafts(BRAND);
diff --git a/web/src/lib/actions/content-drafts.ts b/web/src/lib/actions/content-drafts.ts
index 3c6200406..4991e6f10 100644
--- a/web/src/lib/actions/content-drafts.ts
+++ b/web/src/lib/actions/content-drafts.ts
@@ -48,6 +48,7 @@ import {
type PageRead,
} from '@/lib/content/page-read';
import { MAX_SOURCE_SITES, type AiSourceClass } from '@/lib/content/source-class';
+import { draftShopping } from '@/lib/content/shopping-check';
import {
NO_DRAFT_RULES,
draftRulesFit,
@@ -154,6 +155,42 @@ async function countLibraryTabs(
return Object.fromEntries(counted) as Record<LibraryTab, number>;
}
+/** Ids one `in` filter takes: 100 UUIDs keep a request's address well under the edge's limit. */
+const IN_CHUNK = 100;
+
+/**
+ * The last shopping check of each of these drafts (00352), by draft id: one
+ * row a draft, so a chunk of ids is one request. Read apart from the drafts,
+ * so the Library still lists them when the checks can't be read (00352 not
+ * applied yet, or a read that fails): none is then kept, and a draft with a
+ * shopping path shows as not checked yet.
+ */
+async function readShoppingChecks(
+ supabase: Awaited<ReturnType<typeof createClient>>,
+ draftIds: string[],
+): Promise<Map<string, unknown>> {
+ const checks = new Map<string, unknown>();
+ const chunks: string[][] = [];
+ for (let i = 0; i < draftIds.length; i += IN_CHUNK) chunks.push(draftIds.slice(i, i + IN_CHUNK));
+ try {
+ const reads = await Promise.all(
+ chunks.map((chunk) =>
+ supabase
+ .from('content_shopping_checks')
+ .select(
+ 'draft_id, read_from, checked_at, categories, categories_linked, products, products_linked, bad',
+ )
+ .in('draft_id', chunk),
+ ),
+ );
+ if (reads.some((read) => read.error)) return new Map();
+ for (const read of reads) for (const row of read.data ?? []) checks.set(row.draft_id, row);
+ } catch {
+ return new Map();
+ }
+ return checks;
+}
+
/**
* The brand's drafts for the Library, the latest LIBRARY_LIMIT, with who
* started each, and each tab's count of all its drafts.
@@ -165,7 +202,7 @@ export async function getContentDrafts(brandId: string): Promise<ContentLibrary>
supabase
.from('content_drafts')
.select(
- 'id, title, page_type, method, status, author, created_by, agent_state, agent_state_at, created_at, updated_at, archived_at',
+ 'id, title, page_type, method, status, author, created_by, agent_state, agent_state_at, created_at, updated_at, archived_at, shopping_built:shopping_path->>builtAt',
)
.eq('brand_id', brandId)
.order('updated_at', { ascending: false })
@@ -175,13 +212,19 @@ export async function getContentDrafts(brandId: string): Promise<ContentLibrary>
const rows = draftsRes.data ?? [];
const me = userRes.data.user?.id ?? null;
- // A profile is readable only by its owner, so other members are named
- // through the same reader as Action Center's assignees.
- const people = await readAssignees(
- supabase,
- brandId,
- rows.flatMap((row) => (row.created_by ? [row.created_by] : [])),
- );
+ const [people, checks] = await Promise.all([
+ // A profile is readable only by its owner, so other members are named
+ // through the same reader as Action Center's assignees.
+ readAssignees(
+ supabase,
+ brandId,
+ rows.flatMap((row) => (row.created_by ? [row.created_by] : [])),
+ ),
+ readShoppingChecks(
+ supabase,
+ rows.flatMap((row) => (row.shopping_built ? [row.id] : [])),
+ ),
+ ]);
const drafts = rows.map(
(row): ContentDraftListItem => ({
@@ -197,6 +240,7 @@ export async function getContentDrafts(brandId: string): Promise<ContentLibrary>
createdAt: row.created_at,
updatedAt: row.updated_at,
archivedAt: row.archived_at,
+ shopping: draftShopping(row.shopping_built, checks.get(row.id) ?? null),
}),
);
// Under the cap the rows are every draft; at it, each tab is counted on its own.
diff --git a/web/src/lib/content/drafts.ts b/web/src/lib/content/drafts.ts
index 3af6eb0c0..f69c725a1 100644
--- a/web/src/lib/content/drafts.ts
+++ b/web/src/lib/content/drafts.ts
@@ -6,6 +6,7 @@
import type { SiteType } from './page-types';
import type { DraftRules } from '@/lib/content/rules';
+import type { DraftShopping } from '@/lib/content/shopping-check';
/** Draft → In review (an agent's draft waiting for a person) → Ready → Published. */
export const DRAFT_STATUSES = ['draft', 'in_review', 'ready', 'published'] as const;
@@ -135,6 +136,8 @@ export interface ContentDraftListItem {
updatedAt: string;
/** When an admin or manager archived it (00233); null when it isn't archived. */
archivedAt: string | null;
+ /** Its shopping path's links as the last catalog sync left them (00352); none when absent. */
+ shopping?: DraftShopping;
}
/**
diff --git a/web/src/lib/content/shopping-check.test.ts b/web/src/lib/content/shopping-check.test.ts
index cc2cf2efc..c3445356f 100644
--- a/web/src/lib/content/shopping-check.test.ts
+++ b/web/src/lib/content/shopping-check.test.ts
@@ -1,5 +1,7 @@
import { describe, expect, it } from 'vitest';
import {
+ draftShopping,
+ draftsByShopping,
listed,
shoppingLinks,
type ShoppingCheck,
@@ -142,3 +144,66 @@ describe('listed', () => {
expect(listed([1])).toEqual({ shown: [1], more: 0 });
});
});
+
+describe('draftShopping (00352)', () => {
+ const check = {
+ read_from: 'draft',
+ checked_at: '2026-10-27T03:00:00Z',
+ categories: 2,
+ categories_linked: 1,
+ products: 4,
+ products_linked: 3,
+ bad: [] as unknown[],
+ };
+
+ it('is none without a path, and not checked until a check came back', () => {
+ expect(draftShopping(null, check)).toEqual({ state: 'none' });
+ expect(draftShopping('2026-10-26T09:00:00Z', null)).toEqual({ state: 'unchecked' });
+ expect(draftShopping('2026-10-26T09:00:00Z', [])).toEqual({ state: 'unchecked' });
+ });
+
+ it('needs attention when a linked product no longer sells, and reads one embedded as a list', () => {
+ const bad = [{ id: 'p1', title: 'A', state: 'refused' }];
+ expect(draftShopping('x', { ...check, bad })).toMatchObject({ state: 'attention', bad });
+ expect(draftShopping('x', [check])).toMatchObject({
+ state: 'ok',
+ readFrom: 'draft',
+ categoriesLinked: 1,
+ productsLinked: 3,
+ });
+ });
+
+ it("reads a list of bad links it can't read as none", () => {
+ expect(draftShopping('x', { ...check, bad: [{ id: 1 }] })).toMatchObject({
+ state: 'ok',
+ bad: [],
+ });
+ });
+});
+
+describe('draftsByShopping', () => {
+ const drafts = [
+ { id: 'a', shopping: { state: 'none' as const } },
+ { id: 'b', shopping: { state: 'unchecked' as const } },
+ {
+ id: 'c',
+ shopping: draftShopping('x', {
+ read_from: 'page',
+ checked_at: 'y',
+ categories: 0,
+ categories_linked: 0,
+ products: 1,
+ products_linked: 1,
+ bad: [{ id: 'p', title: 'P', state: 'sold_out' }],
+ }),
+ },
+ { id: 'd' },
+ ];
+
+ it('lists each state, a path not checked yet only under All', () => {
+ expect(draftsByShopping(drafts, 'all').map((d) => d.id)).toEqual(['a', 'b', 'c', 'd']);
+ expect(draftsByShopping(drafts, 'attention').map((d) => d.id)).toEqual(['c']);
+ expect(draftsByShopping(drafts, 'ok').map((d) => d.id)).toEqual([]);
+ expect(draftsByShopping(drafts, 'none').map((d) => d.id)).toEqual(['a', 'd']);
+ });
+});
diff --git a/web/src/lib/content/shopping-check.ts b/web/src/lib/content/shopping-check.ts
index 81616afc5..6f24daddb 100644
--- a/web/src/lib/content/shopping-check.ts
+++ b/web/src/lib/content/shopping-check.ts
@@ -1,3 +1,4 @@
+import { z } from 'zod';
import { linkKey } from '@/lib/content/link-targets';
/**
@@ -6,7 +7,8 @@ import { linkKey } from '@/lib/content/link-targets';
* products as the catalog has them now. Read for the Rules tab's Shopping
* rules and for Before you publish. A product the page links is checked
* when it's one of the page's shopping path, or one the person removed from
- * it or the path left out: the page may still link it.
+ * it or the path left out: the page may still link it. The check after each
+ * catalog sync (00352) keeps the same for the Content Library.
*/
/**
@@ -155,3 +157,82 @@ export const LISTED_MAX = 3;
export function listed<T>(items: readonly T[], max = LISTED_MAX): { shown: T[]; more: number } {
return { shown: items.slice(0, max), more: Math.max(0, items.length - max) };
}
+
+/** A linked product that no longer sells, as the check after a catalog sync keeps it (00352). */
+export interface ShoppingBadLink {
+ id: string;
+ title: string;
+ state: Exclude<ShoppingLinkState, 'ok'>;
+}
+
+/**
+ * A draft's shopping path in the Content Library (00352): none, not checked
+ * since it was built (the next catalog sync checks it), or what the last
+ * check found on the live page or in the draft.
+ */
+export type DraftShopping =
+ | { state: 'none' }
+ | { state: 'unchecked' }
+ | {
+ state: 'ok' | 'attention';
+ readFrom: 'page' | 'draft';
+ checkedAt: string;
+ categories: number;
+ categoriesLinked: number;
+ products: number;
+ productsLinked: number;
+ bad: ShoppingBadLink[];
+ };
+
+/** The Library's Shopping path filter (?shopping=attention). */
+export const SHOPPING_FILTERS = ['all', 'attention', 'ok', 'none'] as const;
+export type ShoppingFilter = (typeof SHOPPING_FILTERS)[number];
+
+const checkRowSchema = z.object({
+ read_from: z.enum(['page', 'draft']),
+ checked_at: z.string(),
+ categories: z.number(),
+ categories_linked: z.number(),
+ products: z.number(),
+ products_linked: z.number(),
+ bad: z
+ .array(
+ z.object({
+ id: z.string(),
+ title: z.string(),
+ state: z.enum(['sold_out', 'refused', 'gone']),
+ }),
+ )
+ .catch([]),
+});
+
+/**
+ * A Library row's shopping path: `builtAt` its path's (null for none), and
+ * `check` its content_shopping_checks row, read on its own or as PostgREST
+ * embeds it.
+ */
+export function draftShopping(builtAt: string | null, check: unknown): DraftShopping {
+ if (!builtAt) return { state: 'none' };
+ const parsed = checkRowSchema.safeParse(Array.isArray(check) ? check[0] : check);
+ if (!parsed.success) return { state: 'unchecked' };
+ const row = parsed.data;
+ return {
+ state: row.bad.length ? 'attention' : 'ok',
+ readFrom: row.read_from,
+ checkedAt: row.checked_at,
+ categories: row.categories,
+ categoriesLinked: row.categories_linked,
+ products: row.products,
+ productsLinked: row.products_linked,
+ bad: row.bad,
+ };
+}
+
+/** The drafts the Shopping path filter lists: a path not checked yet only under All. */
+export function draftsByShopping<T extends { shopping?: DraftShopping }>(
+ drafts: T[],
+ filter: ShoppingFilter,
+): T[] {
+ if (filter === 'all') return drafts;
+ return drafts.filter((draft) => (draft.shopping?.state ?? 'none') === filter);
+}
diff --git a/web/src/types/supabase.ts b/web/src/types/supabase.ts
index d5f960f6d..a6b3e5422 100644
--- a/web/src/types/supabase.ts
+++ b/web/src/types/supabase.ts
@@ -1323,6 +1323,61 @@ export type Database = {
},
]
}
+ content_shopping_checks: {
+ Row: {
+ bad: Json
+ brand_id: string
+ categories: number
+ categories_linked: number
+ checked_at: string
+ draft_id: string
+ needs_attention: boolean
+ page_url: string | null
+ products: number
+ products_linked: number
+ read_from: string
+ }
+ Insert: {
+ bad?: Json
+ brand_id: string
+ categories?: number
+ categories_linked?: number
+ checked_at?: string
+ draft_id: string
+ page_url?: string | null
+ products?: number
+ products_linked?: number
+ read_from: string
+ }
+ Update: {
+ bad?: Json
+ brand_id?: string
+ categories?: number
+ categories_linked?: number
+ checked_at?: string
+ draft_id?: string
+ page_url?: string | null
+ products?: number
+ products_linked?: number
+ read_from?: string
+ }
+ Relationships: [
+ {
+ foreignKeyName: "content_shopping_checks_brand_id_fkey"
+ columns: ["brand_id"]
+ isOneToOne: false
+ referencedRelation: "brands"
+ referencedColumns: ["id"]
+ },
+ {
+ foreignKeyName: "content_shopping_checks_draft_id_fkey"
+ columns: ["draft_id"]
+ isOneToOne: true
+ referencedRelation: "content_drafts"
+ referencedColumns: ["id"]
+ },
+ ]
+ }
content_plan_marks: {
Row: {
brand_id: string