ihs-auth0-integrations
Version:
auth0 actions and more
45 lines (34 loc) • 2.02 kB
JavaScript
/*
* npm module for auth0-HubSpot integration
*/
const _ = require('lodash');
const { executeHSAPI } = require('../helpers/hubspot-api');
const { checkAppMetadata } = require('../helpers/api-helpers');
exports.syncHSContacts = (event, api) => {
if (checkAppMetadata(event, api)) {
const hubspotAPIBody = {
email: _.get(event.user, 'email'),
firstname: _.get(event.user, 'given_name', ''),
lastname: _.get(event.user, 'family_name', '')
}
executeHSAPI('contacts', 'create', event, api, hubspotAPIBody);
}
else {
if (_.get(event.user.app_metadata, 'hubspot.hubSpotContactId') <= 0) {
// maybe to raise a ticket here
console.log(`HS-INTEGRATION: Something is not right! This user should be on HubSpot already (${_.get(event.user, 'email')}) but his app_metadata is wrong on Auth0`);
if (event.secrets.HUBSPOT_INTEGRATION_RAISE_TICKETS) {
const hubspotAPIBody = {
hs_pipeline: _.get(event.secrets, 'HUBSPOT_SUPPORT_PIPELINE_ID'),
hs_pipeline_stage: _.get(event.secrets, 'HUBSPOT_TICKET_INITITAL_STAGE'),
hs_ticket_priority: _.get(event.secrets, 'HUBSPOT_TICKET_PRIORITY'),
subject: "Generated by Auth0 Login Flow",
content: `This user should be on HubSpot already (${_.get(event.user, 'email')}) but his app_metadata is wrong on Auth0\n\n app_metadata = ${JSON.stringify(_.get(event.user, 'app_metadata'))}\n\n To solve this, go to Auth0 User Management, select the user and remove hubSpotContactId key from app_metadata`
}
executeHSAPI('tickets', 'create', event, api, hubspotAPIBody);
}
}
else
console.log(`HS-INTEGRATION: No need to do anything, this user is on HubSpot already (${_.get(event.user, 'email')} HubSpot Contact Id: ${_.get(event.user.app_metadata, 'hubspot.hubSpotContactId')} )`);
}
}