crm-api-template-generator/backend/utils/createCRMContact.js

41 lines
1.4 KiB
JavaScript

const lookupContactByPhone = require('./lookupCRMContactByPhone')
const createContact = require('./createContact')
const loadCRM = require('./loadCRM')
const lookupContactByEmail = require('./lookupCRMContactByEmail')
async function createCRMContact(companyId, crmFirstName, crmPhone = '', crmEmail = '', crmLastName = '', dynamicBodyRequest = {}) {
const crmFiles = await loadCRM(companyId)
for (const crmConfig of crmFiles) {
const { crmRest: rest, authentication } = crmConfig.crm
let contact = null
if (crmPhone) {
contact = await lookupContactByPhone(rest, authentication, crmPhone, companyId)
}
// Por enquanto não esta verificando contato por email para cliente gradezco 4953
if (!contact?.exist && crmEmail && companyId != "4953") {
contact = await lookupContactByEmail(rest, authentication, crmEmail, companyId)
}
if (contact?.exist) {
continue
}
// const contact = await lookupContactByPhone(rest, authentication, crmPhone, companyId)
// if (contact.exist) continue
console.log("==============> contact: ", JSON.stringify(contact, null, 6))
await createContact(companyId, rest, authentication, crmPhone, crmFirstName, crmLastName, crmEmail, {}, dynamicBodyRequest, contact?.flow || 1)
}
}
module.exports = createCRMContact