const lookupContactByPhone = require('./lookupCRMContactByPhone')
const createContact = require('./createContact')
const loadCRM = require('./loadCRM')

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

        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


