/** * iDoklad: zalozeni odberatele. * * Sluzba: https://services.csbot.cz/apps/idoklad * Endpoint: POST /contacts * * **Nic nedohledava.** Kdyz uz kontakt existuje, vznikne druhy se stejnym ICO - * a to je spravne chovani teto operace, ne chyba. Hledani je vlastni krok * (`Najit kontakt`), takze si strom sam rekne, kdy hledat a kdy zakladat: * * 1. Najit kontakt (podle ICO) * 2. podminka: nenalezeno? * ano -> Najit kontakt (podle e-mailu) * 3. podminka: porad nenalezeno? * ano -> Zalozit kontakt * * Kdyz nekomu staci "chci mit jistotu, ze tam je", pouzije jednim krokem * `Najit nebo zalozit kontakt`. Obojí ma smysl a ani jedno nenahrazuje druhe. */ export const manifest = { id: 'idoklad.create-contact', name: 'Založit kontakt', description: 'Založí v iDokladu nového odběratele a vrátí jeho ID. Nic nedohledává - ' + 'na to je krok Najít kontakt.', timeoutMs: 25000, inputs: [ { id: 'companyName', label: 'Název odběratele', type: 'string', required: true, hint: 'U firmy název, u fyzické osoby jméno.', }, { id: 'identificationNumber', label: 'IČO', type: 'string', required: false, pattern: '^[0-9]{6,12}$', hint: 'Jen číslice. U fyzické osoby zůstane prázdné.', }, { id: 'vatNumber', label: 'DIČ', type: 'string', required: false }, { id: 'email', label: 'E-mail', type: 'string', required: false }, { id: 'phone', label: 'Telefon', type: 'string', required: false }, { id: 'street', label: 'Ulice', type: 'string', required: false }, { id: 'city', label: 'Město', type: 'string', required: false }, { id: 'postalCode', label: 'PSČ', type: 'string', required: false }, { id: 'countryCode', label: 'Země', type: 'string', required: false, default: 'CZ', hint: 'Dvoupísmenný kód. Nevyplněno = CZ.', }, ], outputs: [ { id: 'contactId', label: 'ID kontaktu', type: 'number', required: true }, { id: 'companyName', label: 'Název odběratele', type: 'string', required: true }, { id: 'contact', label: 'Celý kontakt', type: 'object', required: true }, ], }; export async function run(inputs, ctx) { const { unwrap, pick, text, num, need } = ctx.util; const companyName = text(inputs.companyName); if (!companyName) { ctx.fail('Bez názvu odběratele iDoklad kontakt nezaloží.'); } /* * Posilaji se jen vyplnena pole. Prazdny retezec neni totez co "neuvedeno" - * u DIC by z nej iDoklad udelal platce DPH s prazdnym cislem. */ const body = { CompanyName: companyName, CountryId: null, ...(text(inputs.identificationNumber) ? { IdentificationNumber: text(inputs.identificationNumber) } : {}), ...(text(inputs.vatNumber) ? { VatIdentificationNumber: text(inputs.vatNumber) } : {}), ...(text(inputs.email) ? { Email: text(inputs.email) } : {}), ...(text(inputs.phone) ? { Mobile: text(inputs.phone) } : {}), ...(text(inputs.street) ? { Street: text(inputs.street) } : {}), ...(text(inputs.city) ? { City: text(inputs.city) } : {}), ...(text(inputs.postalCode) ? { PostalCode: text(inputs.postalCode) } : {}), ...(text(inputs.countryCode) ? { CountryCode: text(inputs.countryCode) } : {}), }; const response = await ctx.http.post('/contacts', body); const created = unwrap(response.body); ctx.log(`Kontakt ${companyName} založen.`); return { contactId: need(num(pick(created, 'id')), 'ID založeného kontaktu'), companyName: text(pick(created, 'companyName', 'name')) || companyName, contact: created && typeof created === 'object' ? created : {}, }; }