63 lines
2.4 KiB
JavaScript
63 lines
2.4 KiB
JavaScript
const loadCRM = require('./loadCRM')
|
|
const lookupContactByPhone = require('./lookupCRMContactByPhone')
|
|
const createContact = require('./createContact')
|
|
const findProperty = require('./findProperty')
|
|
|
|
const journalingRequest = require('./journalingRequest')
|
|
const ticketCRM = require('./ticketCRM')
|
|
|
|
async function journaling(companyId, operation, crmPhone, crmAgent, crmCallDuration = 0, crmFirstName = 'Username', crmLastName = 'Last name') {
|
|
|
|
const crmFiles = await loadCRM(companyId)
|
|
const crmTicketLinks = []
|
|
|
|
for (const crmConfig of crmFiles) {
|
|
|
|
const { crmRest: rest, authentication } = crmConfig.crm
|
|
|
|
let contact = await lookupContactByPhone(rest, authentication, crmPhone, companyId)
|
|
|
|
console.log("===========> journaling contat ", JSON.stringify(contact, null, 6))
|
|
|
|
if (!contact.exist) {
|
|
// createContact(companyId, rest, authentication, crmPhone = '', crmFirstName = 'Username', crmLastName = 'Last name', crmEmail = '', test = {}, dynamicBodyRequest = {})
|
|
contact = await createContact(companyId, rest, authentication, crmPhone, crmFirstName, crmLastName, crmEmail = '', test = {}, dynamicBodyRequest = {}, contact?.flow)
|
|
}
|
|
|
|
let { request, calls, response } = findProperty(rest, 'callJournaling')
|
|
|
|
let body = findProperty(calls, operation)
|
|
|
|
await journalingRequest(request, body, crmCallDuration, contact, crmAgent, crmPhone, authentication, rest, companyId)
|
|
|
|
// create ticket crm sap cliente gradezco. To each call is created or open a ticket
|
|
if (companyId == "4953") {
|
|
|
|
console.log("============> journaling contact?.flow: ", contact?.flow)
|
|
|
|
const crmTicketLinks = await ticketCRM(companyId, crmPhone, crmAgent, crmFirstName, contact?.flow)
|
|
.catch(function (error) {
|
|
|
|
console.error(`Error on create jounaling ticket: companyID ${companyId} | crmPhone: ${crmPhone}`)
|
|
console.error(error)
|
|
console.error(error?.response?.data)
|
|
console.error(error?.response?.status)
|
|
console.error(error?.response?.headers)
|
|
|
|
throw new Error(`Error on create jounaling ticket: companyID ${companyId} | crmPhone: ${crmPhone}`)
|
|
})
|
|
|
|
// console.log('jounaling crmTicketLinks: ', crmTicketLinks)
|
|
return crmTicketLinks
|
|
|
|
}
|
|
|
|
return []
|
|
}
|
|
|
|
}
|
|
|
|
module.exports = journaling
|
|
|
|
|