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

37 lines
1.2 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)
}
if (!contact?.exist && crmEmail) {
contact = await lookupContactByEmail(rest, authentication, crmEmail, companyId)
}
if (contact?.exist) {
continue
}
// const contact = await lookupContactByPhone(rest, authentication, crmPhone, companyId)
// if (contact.exist) continue
await createContact(companyId, rest, authentication, crmPhone, crmFirstName, crmLastName, crmEmail, {}, dynamicBodyRequest)
}
}
module.exports = createCRMContact