/** * iDoklad: odeslani vydane faktury e-mailem. * * Sluzba: https://services.csbot.cz/apps/idoklad * Endpoint: POST /mail/issued-invoices/send * * Vzor akce, u ktere **odpoved sluzby nic nevraci**. Vystup se proto sklada * z toho, co skript posilal, ne z toho, co prislo zpatky. Bez toho by strom * za timhle krokem nemel na cem stavet podminku. * * Zaroven je to vzor toho, jak nahradit jeden vstup dvema chovanimi: kdyz je * vyplneny e-mail, posle se na nej. Kdyz neni, posle se na adresu odberatele * vedenou v iDokladu. */ export const manifest = { id: 'idoklad.send-invoice-email', name: 'Odeslat fakturu e-mailem', description: 'Odešle vydanou fakturu e-mailem. Bez zadané adresy jde na e-mail ' + 'odběratele vedený v iDokladu.', timeoutMs: 30000, inputs: [ { id: 'invoiceId', label: 'ID faktury v iDokladu', type: 'number', required: true }, { id: 'email', label: 'E-mail příjemce', type: 'string', required: false, pattern: '^[^@\\s]+@[^@\\s]+\\.[A-Za-z]{2,}$', hint: 'Nevyplněno = adresa odběratele z iDokladu.', }, { id: 'subject', label: 'Předmět', type: 'string', required: false }, { id: 'body', label: 'Text e-mailu', type: 'string', required: false, multiline: true }, { id: 'sendAttachment', label: 'Přiložit PDF faktury', type: 'boolean', required: false, default: true, }, { id: 'sendToSelf', label: 'Poslat kopii sobě', type: 'boolean', required: false, default: false, }, ], outputs: [ { id: 'sent', label: 'Odesláno', type: 'boolean', required: true }, { id: 'recipient', label: 'Komu se odeslalo', type: 'string', required: true }, ], }; export async function run(inputs, ctx) { const toGivenAddress = Boolean(inputs.email); const payload = { documentId: inputs.invoiceId, // Vsechna tri pole jsou u iDokladu povinna, i kdyz jsou nepravdiva. sendToPartner: !toGivenAddress, sendToAccountant: false, sendToSelf: inputs.sendToSelf ?? false, sendAttachment: inputs.sendAttachment ?? true, ...(toGivenAddress ? { otherRecipients: [inputs.email] } : {}), ...(inputs.subject ? { emailSubject: inputs.subject } : {}), ...(inputs.body ? { emailBody: inputs.body } : {}), }; // Nektere instance vraci 204 bez tela, jine 200 s potvrzenim. Obojí je uspech. const { status } = await ctx.http.post('/mail/issued-invoices/send', payload); const recipient = toGivenAddress ? String(inputs.email) : 'odběratel z iDokladu'; ctx.log(`Faktura ${inputs.invoiceId} odeslána (${recipient}), HTTP ${status}.`); return { sent: true, recipient }; }