/** @typedef {{sku: string, name: string, priceCents: number, quantity: number, url: string}} CatalogItem */ /** @typedef {{schemaVersion: 1, sourceKind: 'local-fixture', revision: string, title: string, introduction: string, currency: 'USD', notes: string[], items: CatalogItem[]}} CatalogSnapshot */ /** @param {unknown} value @returns {value is Record} */ function isCatalogRecord_(value) { return typeof value === 'object' && value !== null && !Array.isArray(value); } /** @param {unknown} value @param {string} label @param {number} maximum */ function catalogText_(value, label, maximum) { if (typeof value !== 'string' || !value || value.length > maximum || value !== value.replace(/\s+/gu, ' ').trim() || /[\u0000-\u001f\u007f\ufffd]/u.test(value)) { throw new Error(label + ': missing, oversized, or non-normalized text.'); } return value; } /** @param {unknown} value @param {string} label @param {number} maximum */ function catalogInteger_(value, label, maximum) { if (typeof value !== 'number' || !Number.isInteger(value) || value < 0 || value > maximum) { throw new Error(label + ': expected an integer from 0 through ' + maximum + '.'); } return value; } /** @param {unknown} value @returns {CatalogSnapshot} */ function validateCatalogSnapshot_(value) { if (!isCatalogRecord_(value) || value.schemaVersion !== 1 || value.sourceKind !== 'local-fixture' || value.currency !== 'USD') { throw new Error('Expected a version-1 local-fixture catalog in USD.'); } const revision = catalogText_(value.revision, 'Revision', 32); if (!/^[a-z0-9-]+$/.test(revision)) { throw new Error('Revision must use lowercase letters, digits, and hyphens.'); } const title = catalogText_(value.title, 'Title', 120); const introduction = catalogText_(value.introduction, 'Introduction', 240); if (!Array.isArray(value.notes) || value.notes.length < 1 || value.notes.length > 5) { throw new Error('Expected 1 through 5 catalog notes.'); } /** @type {unknown[]} */ const noteValues = value.notes; const notes = noteValues.map(function (note, index) { return catalogText_(note, 'Note ' + (index + 1), 160); }); if (!Array.isArray(value.items) || value.items.length < 1 || value.items.length > 20) { throw new Error('Expected 1 through 20 catalog items.'); } /** @type {unknown[]} */ const itemValues = value.items; /** @type {CatalogItem[]} */ const items = []; /** @type {Set} */ const seen = new Set(); for (let index = 0; index < itemValues.length; index += 1) { const item = itemValues[index]; const label = 'Item ' + (index + 1); if (!isCatalogRecord_(item)) { throw new Error(label + ': expected an object.'); } const sku = catalogText_(item.sku, label + ' SKU', 7); if (!/^KIT-[0-9]{3}$/.test(sku) || seen.has(sku)) { throw new Error(label + ': SKU must be unique and have the form KIT-001.'); } seen.add(sku); const name = catalogText_(item.name, label + ' name', 120); const priceCents = catalogInteger_(item.priceCents, label + ' priceCents', 10000); const quantity = catalogInteger_(item.quantity, label + ' quantity', 100); const url = catalogText_(item.url, label + ' URL', 512); if (!/^https:\/\/catalog\.example\/stock\/items\/[a-z-]+\.html(?:\?[a-z0-9=&-]+)?$/.test(url)) { throw new Error(label + ': URL is outside the allowed canonical catalog link format.'); } items.push({ sku: sku, name: name, priceCents: priceCents, quantity: quantity, url: url }); } items.sort(function (a, b) { return a.sku < b.sku ? -1 : a.sku > b.sku ? 1 : 0; }); return { schemaVersion: 1, sourceKind: 'local-fixture', revision: revision, title: title, introduction: introduction, currency: 'USD', notes: notes, items: items }; } function logCatalogRestockPlan() { const json = PropertiesService.getScriptProperties().getProperty('HTML_CATALOG_JSON'); if (!json) { throw new Error('Generate output/catalog.json locally, then copy its complete contents into the HTML_CATALOG_JSON script property.'); } if (json.length > 8000 || Utilities.newBlob(json).getBytes().length > 8000) { throw new Error('HTML_CATALOG_JSON exceeds the 8000-byte transfer limit.'); } /** @type {unknown} */ let parsed; try { parsed = JSON.parse(json); } catch (error) { throw new Error( 'HTML_CATALOG_JSON is not valid JSON. Copy the complete contents of output/catalog.json into the script property.' ); } const snapshot = validateCatalogSnapshot_(parsed); let units = 0; let stockValueCents = 0; for (const item of snapshot.items) { units += item.quantity; stockValueCents += item.priceCents * item.quantity; } const restock = snapshot.items.filter(function (item) { return item.quantity < 5; }).map(function (item) { return { sku: item.sku, name: item.name, quantity: item.quantity, url: item.url }; }); Logger.log(JSON.stringify({ revision: snapshot.revision, title: snapshot.title, currency: snapshot.currency, itemCount: snapshot.items.length, units: units, stockValueCents: stockValueCents, restock: restock })); }