/** * PPL CPL: stav zasilky. * * Sluzba: https://services.csbot.cz/apps/pplcplapi * Endpoint: GET /shipments?shipmentNumbers=... * * Vzor **cteciho kroku, u ktereho nenalezeno neni chyba**. Zasilka, kterou PPL * jeste nenacetl, je bezny stav prvnich minut po zalozeni, ne selhani. */ export const manifest = { id: 'ppl.track', name: 'Zjistit stav zásilky', description: 'Vrátí aktuální stav zásilky a historii jejích událostí. Nic nemění.', inputs: [ { id: 'shipmentNumber', label: 'Číslo zásilky', type: 'string', required: false, hint: 'Vrací ho krok Vytvořit zásilku. Vyplňte tohle, nebo referenci.', }, { id: 'reference', label: 'Reference', type: 'string', required: false, hint: 'Vaše číslo zadané při zakládání, typicky číslo objednávky.', }, ], outputs: [ { id: 'found', label: 'Zásilka nalezena', type: 'boolean', required: true }, { id: 'shipmentNumber', label: 'Číslo zásilky', type: 'string', required: false }, { id: 'state', label: 'Stav', type: 'string', required: false }, { id: 'stateDate', label: 'Datum stavu', type: 'string', required: false }, { id: 'delivered', label: 'Doručeno', type: 'boolean', required: true }, { id: 'shipment', label: 'Celý záznam zásilky', type: 'object', required: false }, ], }; const notFound = { found: false, shipmentNumber: null, state: null, stateDate: null, delivered: false, shipment: null, }; export async function run(inputs, ctx) { const { pick, first, text } = ctx.util; if (!inputs.shipmentNumber && !inputs.reference) { ctx.fail('Vyplňte číslo zásilky nebo referenci, jinak není co hledat.'); } const { body } = await ctx.http.get('/shipments', { query: { shipmentNumbers: inputs.shipmentNumber ?? undefined, customerReferences: inputs.reference ?? undefined, limit: 5, }, }); const items = Array.isArray(body) ? body : pick(body, 'shipments', 'data', 'items'); const shipment = first(Array.isArray(items) ? items : []); if (!shipment) { ctx.log('PPL o zásilce zatím nic neví. Krátce po založení je to běžné.'); return notFound; } // Posledni udalost je aktualni stav. PPL vraci historii vzestupne. const events = pick(shipment, 'shipmentEvents', 'events', 'states'); const last = Array.isArray(events) && events.length > 0 ? events[events.length - 1] : null; const state = text(pick(last, 'code', 'name', 'statusCode') ?? pick(shipment, 'statusCode', 'state')); const stateDate = text(pick(last, 'date', 'eventDate') ?? pick(shipment, 'statusDate')); return { found: true, shipmentNumber: text(pick(shipment, 'shipmentNumber')), state, stateDate, // Kod doruceno je u PPL DELIVERED. Porovnava se bez ohledu na velikost pismen. delivered: (state ?? '').toUpperCase().includes('DELIVER'), shipment, }; }