2024-11-05 18:37:36 +00:00
|
|
|
const loadCRM = require('./loadCRM')
|
|
|
|
const lookupContactByPhone = require('./lookupCRMContactByPhone')
|
|
|
|
const findProperty = require('./findProperty')
|
|
|
|
const axios = require('axios')
|
|
|
|
const requestConfigHeader = require('./requestConfigHeader')
|
|
|
|
|
|
|
|
async function sfcase(companyId, crmPhone) {
|
|
|
|
|
|
|
|
const crmFiles = await loadCRM(companyId)
|
|
|
|
|
|
|
|
let res = { contactId: "" }
|
|
|
|
|
|
|
|
for (const crmConfig of crmFiles) {
|
|
|
|
|
|
|
|
const { crmRest: rest, authentication } = crmConfig.crm
|
|
|
|
|
|
|
|
let createCase = findProperty(rest, 'createCase')
|
|
|
|
|
|
|
|
console.log('===============> createCase: ', createCase)
|
|
|
|
|
|
|
|
if (createCase) {
|
|
|
|
|
|
|
|
let contact = await lookupContactByPhone(rest, authentication, crmPhone, companyId, {}, false)
|
|
|
|
|
|
|
|
console.log('==========> contact: ', contact)
|
|
|
|
|
|
|
|
if (!contact?.exist) {
|
|
|
|
console.log('===============> ContactExist: ', JSON.stringify(contact, null, 6))
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2024-11-28 12:17:54 +00:00
|
|
|
const { contactId, created, accountId } = contact
|
2024-11-05 18:37:36 +00:00
|
|
|
|
|
|
|
let { request, body } = createCase
|
|
|
|
|
|
|
|
console.log("====> request: ", request)
|
|
|
|
console.log("====> body: ", body)
|
|
|
|
|
|
|
|
const { type, userName, passWord, token, crmClientId } = authentication
|
|
|
|
const { requestContentType, requestEncoding, requestType, responseType, url } = request
|
|
|
|
|
2024-11-28 12:17:54 +00:00
|
|
|
let config = await requestConfigHeader(url, crmPhone, requestType, requestContentType, type, userName, passWord, token, crmClientId, body)
|
2024-11-05 18:37:36 +00:00
|
|
|
|
2024-11-28 12:17:54 +00:00
|
|
|
console.log("====> accountId: ", accountId, " | contactId: ", contactId)
|
|
|
|
|
|
|
|
|
|
|
|
if (accountId && contactId)
|
|
|
|
config.data = {
|
|
|
|
...config.data, ...{
|
|
|
|
ContactId: contactId,
|
|
|
|
AccountId: accountId
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log("====> create case request config: ", config)
|
2024-11-05 18:37:36 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
const { data } = await axios(config)
|
|
|
|
|
|
|
|
console.log('Data from case created: ', JSON.stringify(data, null, 6))
|
|
|
|
|
|
|
|
res.contactId = contactId
|
|
|
|
res.caseId = data.id
|
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
console.log(`CASE CREATE ERROR: `, error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
break
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return res
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = sfcase
|