2023-11-29 20:05:48 +00:00
|
|
|
const lookupContactByPhone = require('./lookupCRMContactByPhone')
|
|
|
|
const createContact = require('./createContact')
|
|
|
|
const sendMessageSocket = require('./sendMessageSocket')
|
|
|
|
const findProperty = require('./findProperty')
|
|
|
|
const journalingRequest = require('./journalingRequest')
|
|
|
|
|
2024-09-24 12:10:18 +00:00
|
|
|
async function templateValidator(crmPhoneTest, crm, companyId) {
|
2023-11-29 20:05:48 +00:00
|
|
|
|
|
|
|
let phoneTest = crmPhoneTest ? crmPhoneTest : '5511900112233'
|
|
|
|
|
|
|
|
let contact = await lookupContactByPhone(
|
|
|
|
rest = crm.crmRest,
|
|
|
|
authentication = crm.authentication,
|
|
|
|
crmPhone = phoneTest,
|
|
|
|
companyId,
|
2024-09-24 12:10:18 +00:00
|
|
|
test = { testing: true })
|
2023-11-29 20:05:48 +00:00
|
|
|
|
|
|
|
if (!contact.exist) {
|
|
|
|
|
|
|
|
console.log('O CONTATO SER CRIADO')
|
|
|
|
|
|
|
|
contact = await createContact(companyId,
|
|
|
|
rest = crm.crmRest,
|
|
|
|
authentication = crm.authentication,
|
|
|
|
crmPhone = phoneTest,
|
2024-07-19 00:41:03 +00:00
|
|
|
crmFirstName = 'unnamed',
|
2023-11-29 20:05:48 +00:00
|
|
|
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
|