/** * RAYNET CRM: dohledani firmy. * * Sluzba: https://services.csbot.cz/apps/raynet * Endpoint: GET /company?fulltext=... * * Vzor **predvalidace**: nenalezeno tady neni chyba. Prave podle toho se strom * vetvi na "znameho zakaznika" a "noveho zajemce", takze chyba by rozhodnuti * zahodila misto aby ho umoznila. */ export const manifest = { id: 'raynet.find-company', name: 'Dohledat firmu', description: 'Zjistí, jestli je odesílatel v CRM. Hledá podle IČO, e-mailu nebo názvu. ' + 'Nic nezakládá, výsledek slouží k větvení stromu.', inputs: [ { id: 'regNumber', label: 'IČO', type: 'string', required: false, pattern: '^[0-9]{6,12}$', hint: 'Jen číslice. Nejpřesnější, hledá se podle něj první.', }, { id: 'email', label: 'E-mail', type: 'string', required: false, hint: 'Například {{from}} u e-mailu.', }, { id: 'name', label: 'Název firmy', type: 'string', required: false, hint: 'Použije se jako poslední, název se píše pokaždé jinak.', }, ], outputs: [ { id: 'customerKnown', label: 'Firma nalezena', type: 'boolean', required: true }, { id: 'companyId', label: 'ID firmy', type: 'number', required: false }, { id: 'companyName', label: 'Název firmy', type: 'string', required: false }, { id: 'regNumber', label: 'IČO', type: 'string', required: false }, { id: 'ownerName', label: 'Vlastník záznamu', type: 'string', required: false }, { id: 'matchedBy', label: 'Podle čeho se našla', type: 'string', required: true }, ], }; const notFound = { customerKnown: false, companyId: null, companyName: null, regNumber: null, ownerName: null, matchedBy: 'nenalezeno', }; export async function run(inputs, ctx) { const { unwrap, pick, first, text, num } = ctx.util; if (!inputs.regNumber && !inputs.email && !inputs.name) { ctx.fail('Vyplňte IČO, e-mail nebo název firmy, jinak není podle čeho hledat.'); } /** Jedno hledani. RAYNET ma na vsechno jeden fulltext, filtr podle pole nema. */ async function search(value) { const { body } = await ctx.http.get('/company', { query: { fulltext: value, limit: 5 } }); const items = unwrap(body); return Array.isArray(items) ? items : []; } // Poradi je zamer: ICO je jednoznacne, e-mail muze mit vic firem stejny // a nazev se v kazdem systemu pise jinak. const attempts = []; if (inputs.regNumber) attempts.push([inputs.regNumber, 'IČO']); if (inputs.email) attempts.push([inputs.email, 'e-mail']); if (inputs.name) attempts.push([inputs.name, 'název']); for (const [value, label] of attempts) { const items = await search(value); const company = first(items); if (!company) { ctx.log(`Podle ${label} se nic nenašlo.`); continue; } if (items.length > 1) { ctx.log(`Podle ${label} vyšlo ${items.length} záznamů, bere se první.`); } return { customerKnown: true, companyId: num(pick(company, 'id')), companyName: text(pick(company, 'name', 'companyName')), regNumber: text(pick(company, 'regNumber', 'identificationNumber')), ownerName: text(pick(pick(company, 'owner'), 'fullName', 'name')), matchedBy: label, }; } return notFound; }