feat: added dynamic body request to create lead/contact
parent
61783eecf6
commit
f2814269ac
|
@ -36,11 +36,11 @@ const getSalesforceUser = require('../utils/getSalesforceUser')
|
|||
|
||||
const contactCreate = async (req, res) => {
|
||||
|
||||
const { companyId, crmFirstName, crmLastName, crmPhone, crmEmail } = req.body
|
||||
const { companyId, crmFirstName, crmLastName, crmPhone, crmEmail, dynamicBodyRequest } = req.body
|
||||
|
||||
mustContainProperties(req, ['companyId', 'crmPhone',])
|
||||
|
||||
await createCRMContact(companyId, crmFirstName, crmPhone, crmEmail, crmLastName)
|
||||
|
||||
await createCRMContact(companyId, crmFirstName, crmPhone, crmEmail, crmLastName, dynamicBodyRequest)
|
||||
|
||||
res.status(StatusCodes.OK).send()
|
||||
|
||||
|
|
|
@ -2,8 +2,8 @@ const lookupContactByPhone = require('./lookupCRMContactByPhone')
|
|||
const createContact = require('./createContact')
|
||||
const loadCRM = require('./loadCRM')
|
||||
|
||||
async function createCRMContact(companyId, crmFirstName, crmPhone, crmEmail = '', crmLastName = '',) {
|
||||
|
||||
async function createCRMContact(companyId, crmFirstName, crmPhone, crmEmail = '', crmLastName = '', dynamicBodyRequest = {}) {
|
||||
|
||||
const crmFiles = await loadCRM(companyId)
|
||||
|
||||
for (const crmConfig of crmFiles) {
|
||||
|
@ -11,11 +11,11 @@ async function createCRMContact(companyId, crmFirstName, crmPhone, crmEmail = ''
|
|||
const { crmRest: rest, authentication } = crmConfig.crm
|
||||
|
||||
const contact = await lookupContactByPhone(rest, authentication, crmPhone, companyId)
|
||||
|
||||
if (contact.exist) continue
|
||||
|
||||
await createContact(companyId, rest, authentication, crmPhone, crmFirstName, crmLastName, crmEmail,)
|
||||
|
||||
if (contact.exist) continue
|
||||
|
||||
await createContact(companyId, rest, authentication, crmPhone, crmFirstName, crmLastName, crmEmail, {}, dynamicBodyRequest)
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -8,7 +8,7 @@ const CRM = require('../models/CRM')
|
|||
const requestConfigHeader = require('./requestConfigHeader')
|
||||
const sendMessageSocket = require('./sendMessageSocket')
|
||||
|
||||
async function createContact(companyId, rest, authentication, crmPhone, crmFirstName = 'Username', crmLastName = 'Last name', crmEmail = '', test = {}) {
|
||||
async function createContact(companyId, rest, authentication, crmPhone, crmFirstName = 'Username', crmLastName = 'Last name', crmEmail = '', test = {}, dynamicBodyRequest = {}) {
|
||||
let { request, body, response } = findProperty(rest, 'createContactRecord')
|
||||
|
||||
const { requestContentType, requestEncoding, requestType, responseType, url } = request
|
||||
|
@ -42,7 +42,7 @@ async function createContact(companyId, rest, authentication, crmPhone, crmFirst
|
|||
const { type, userName, passWord, token, crmClientId } = authentication
|
||||
|
||||
//url, crmPhone, requestType, requestContentType, type, userName, passWord, token, crmClientId, data = '', ticketId = '', companyId
|
||||
const config = await requestConfigHeader(url, crmPhone, requestType, requestContentType, type, userName, passWord, token, crmClientId, body, '', companyId)
|
||||
let config = await requestConfigHeader(url, crmPhone, requestType, requestContentType, type, userName, passWord, token, crmClientId, body, '', companyId)
|
||||
|
||||
if (test?.testing) {
|
||||
msg = `Tentanto criar contato de numero ${crmPhone} no crm`
|
||||
|
@ -51,8 +51,15 @@ async function createContact(companyId, rest, authentication, crmPhone, crmFirst
|
|||
|
||||
console.log('#####################')
|
||||
console.log('CREATE CONTACT PAYLOAD: ', JSON.stringify(config, null, 6))
|
||||
console.log('#####################')
|
||||
|
||||
console.log('#####################')
|
||||
|
||||
if (dynamicBodyRequest && Object.keys(dynamicBodyRequest) !== 0) {
|
||||
config.data = { ...config.data, ...dynamicBodyRequest }
|
||||
console.log('#####################')
|
||||
console.log('CREATE CONTACT PAYLOAD UPDATED BY DYNAMIC BODY REQUEST: ', JSON.stringify(config, null, 6))
|
||||
console.log('#####################')
|
||||
}
|
||||
|
||||
let data
|
||||
try {
|
||||
let { data: _data } = await axios(config)
|
||||
|
@ -72,8 +79,8 @@ async function createContact(companyId, rest, authentication, crmPhone, crmFirst
|
|||
}
|
||||
|
||||
throw error
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// let { data } = await axios(config)
|
||||
|
||||
data = flatten(data)
|
||||
|
|
Loading…
Reference in New Issue