const lookupContactByPhone = require('./lookupCRMContactByPhone') const createContact = require('./createContact') const sendMessageSocket = require('./sendMessageSocket') const findProperty = require('./findProperty') const journalingRequest = require('./journalingRequest') async function templateValidator(crmPhoneTest, crm, companyId) { let phoneTest = crmPhoneTest ? crmPhoneTest : '5511900112233' let contact = await lookupContactByPhone( rest = crm.crmRest, authentication = crm.authentication, crmPhone = phoneTest, companyId, test = { testing: true }) if (!contact.exist) { console.log('O CONTATO SER CRIADO') contact = await createContact(companyId, rest = crm.crmRest, authentication = crm.authentication, crmPhone = phoneTest, crmFirstName = 'Username', crmLastName = '', crmEmail = '', test = { testing: true }) sendMessageSocket({ companyId, status: 'processing', data: { msg } }) } else { msg = `Contato de numero ${phoneTest} já existe no crm` sendMessageSocket({ companyId, status: 'processing', data: { msg } }) } let { request, calls, response } = findProperty(crm.crmRest, 'callJournaling') for (const operation of calls) { msg = `Tentando inserir registro de chamada(call journal) ${Object.keys(operation)[0]} para o contato ${phoneTest}` await journalingRequest(request, body = operation[Object.keys(operation)[0]], crmCallDuration = "900", contact, crmAgent = "2245", crmPhone = phoneTest, authentication = crm.authentication, test = { testing: true, msg, companyId }) msg = `Registro de chamada inserido com sucesso ${Object.keys(operation)[0]} para o contato ${phoneTest}` sendMessageSocket({ companyId, status: 'processing', data: { msg } }) } sendMessageSocket({ companyId, status: 'finished', data: { crmPhoneTest: phoneTest } }) } module.exports = templateValidator