Files
dashback/src/defaultApplets/article-lookup.applet.js
2026-10-12 14:18:54 +02:00

45 lines
2.0 KiB
JavaScript

// @applet-input-schema {"type":"object","properties":{"sku":{"type":"string","title":"Article SKU","description":"Exact SKU to inspect (e.g. \"42000009\")."}},"required":["sku"]}
// @applet-output-schema key:string;value:string
// @applet-description "Article lookup — the raw GET /article/{sku} payload as a key/value sheet."
//
// Article lookup — the raw `GET /article/{sku}` payload as a key/value sheet.
//
// Demonstrates: returning a single object (one row per key) and embedding `console.log` for a
// human-readable summary alongside the machine-readable table.
// Endpoints: GET /article/{sku}.
//
// Tip: this applet is small on purpose — copy it as a starting point for your own lookups.
module.exports = async function (applet_input) {
const sku = String(applet_input.sku || '').trim();
if (!sku) {
throw new Error('Please provide a SKU (e.g. "42000009").');
}
const res = await fetch(`/article/${encodeURIComponent(sku)}`);
if (res.status === 404) throw new Error(`Article "${sku}" was not found.`);
if (!res.ok) throw new Error(`GET /article/${sku} failed: ${res.status}`);
const { article } = await res.json();
const price = (article.prices || [])[0];
const brand = article.brand?.name || '(no brand)';
const categoryIds = (article.categoryIds || []).join(', ') || '(none)';
console.log(`${sku}: ${article.name} — brand ${brand}, ${article.eans?.length || 0} EAN(s).`);
const rows = [
{ key: 'sku', value: article.sku },
{ key: 'name', value: article.name },
{ key: 'brand', value: brand },
{ key: 'categoryIds', value: categoryIds },
{ key: 'unit', value: article.unit?.si ?? '' },
{ key: 'unitamount', value: article.unitamount ?? '' },
{ key: 'weight', value: article.weight ?? '' },
{ key: 'track_stock', value: article.track_stock },
{ key: 'net', value: price?.net ?? '' },
{ key: 'taxRate', value: price?.taxRate ?? '' },
{ key: 'eans', value: (article.eans || []).join(', ') },
{ key: 'suppliers', value: (article.suppliers || []).length },
];
return rows;
};