-
Notifications
You must be signed in to change notification settings - Fork 151
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #232 from dikoga/forrester
Forrester
- Loading branch information
Showing
5 changed files
with
226 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
src/rules/create-new-contact-add-to-contact-list-hubspot.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/** | ||
* @title Add New Contact to HubSpot for Marketing | ||
* @overview Add New Contact to HubSpot then add to a List for marketing | ||
* @gallery true | ||
* @category webhook | ||
* | ||
* This rule will add a New Contact to HubSpot if they don't already exist, and then add that Contact to a List for marketing. | ||
* | ||
* This is useful for cases where you want to enroll new users in an email list related to your application. | ||
* You will need to set two values HUBSPOT_API_KEY and HUBSPOT_NEW_MEMBER_LIST_ID | ||
* For more details about the Rules configuration settings, see here https://auth0.com/docs/rules/guides/configuration | ||
* For more information about Hubspot API keys see here https://knowledge.hubspot.com/integrations/how-do-i-get-my-hubspot-api-key | ||
* Use 1 as the value for HUBSPOT_NEW_MEMBER_LIST_ID for the default list in Hubspot. Otherwise, you can see the ID of any list in HubSpot visiting it, and looking at the URL. It will have this format https://app.hubspot.com/contacts/:portalId/lists/:listId where :listId is the value you want. | ||
*/ | ||
|
||
function createNewContactAndAddToContactListHubSpot(user, context, callback) { | ||
const request = require('request'); | ||
user.app_metadata = user.app_metadata || {}; | ||
|
||
//Populate the variables below with appropriate values | ||
const apiKey = configuration.HUBSPOT_API_KEY; // For more information about HubSpot API keys https://knowledge.hubspot.com/integrations/how-do-i-get-my-hubspot-api-key | ||
const newMemberListId = configuration.HUBSPOT_NEW_MEMBER_LIST_ID; //Use 1 for default list, otherwise You can see the ID of any list in HubSpot visiting it and looking at the URL. It will have this format https://app.hubspot.com/contacts/:portalId/lists/:listId | ||
|
||
//************** CREATE A NEW CONTACT IN HUBSPOT **********************/ | ||
const contactData = JSON.stringify({ | ||
"properties": [ | ||
{ | ||
"property": "email", | ||
"value": user.email | ||
}, | ||
{ | ||
"property": "firstname", | ||
"value": user.given_name || "" | ||
}, | ||
{ | ||
"property": "lastname", | ||
"value": user.family_name || "" | ||
} | ||
] | ||
}); | ||
|
||
const contactOptions = { | ||
url: 'https://api.hubapi.com/contacts/v1/contact/?hapikey=' + apiKey, | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json' | ||
}, | ||
body: contactData | ||
}; | ||
request(contactOptions, function (err, response, body) { | ||
if (err || (response.statusCode !== 200 && response.statusCode !== 409)) { | ||
console.log('NOTIFY YOUR MONITOR APPLICATION OF AN ERROR ADDING A NEW CONTACT'); | ||
user.app_metadata.hubSpotContactCreated = false; | ||
} else { | ||
console.log('[NEW CONTACT] HANDLE ANY POSSIBLE INFORMATION YOU MIGHT WANT TO STORE IN THE USERS PROFILE'); | ||
const newContactId = JSON.parse(body).vid; | ||
user.app_metadata.hubSpotContactCreated = true; | ||
user.app_metadata.hubSpotContactId = newContactId; | ||
|
||
//************** ADD NEW CONTACT TO AN EXISTING E-MAIL LIST IN HUBSPOT **********************/ | ||
const subscribeData = JSON.stringify({ "vids": [newContactId] }); | ||
//************** NOTE THIS USES LIST NUMBER AND HUBSPOT API KEY THE URL BELOW **********************/ | ||
const subscribeOptions = { | ||
url: 'https://api.hubapi.com/contacts/v1/lists/' + newMemberListId + '/add?hapikey=' + apiKey, | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json' | ||
}, | ||
body: subscribeData | ||
}; | ||
|
||
request(subscribeOptions, function (err, response, body) { | ||
if (err || (response.statusCode !== 200 && response.statusCode !== 409)) { | ||
console.log('NOTIFY YOUR MONITOR APPLICATION OF AN ERROR ON ADDING CONTACT TO A EMAIL LIST'); | ||
console.log(err); | ||
user.app_metadata.hubSpotContactAddedToList = false; | ||
} else { | ||
user.app_metadata.hubSpotContactAddedToList = true; | ||
console.log('[EMAIL LIST] HANDLE ANY POSSIBLE INFORMATION YOU MIGHT WANT TO STORE IN THE USERS PROFILE'); | ||
} | ||
|
||
auth0.users.updateAppMetadata(user.user_id, user.app_metadata); | ||
}); | ||
} | ||
auth0.users.updateAppMetadata(user.user_id, user.app_metadata); | ||
}); | ||
|
||
return callback(null, user, context); | ||
} |
125 changes: 125 additions & 0 deletions
125
test/rules/create-new-contact-add-to-contact-list-hubspot.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
'use strict'; | ||
|
||
const loadRule = require('../utils/load-rule'); | ||
const ContextBuilder = require('../utils/contextBuilder'); | ||
const UserBuilder = require('../utils/userBuilder'); | ||
|
||
const ruleName = 'create-new-contact-add-to-contact-list-hubspot'; | ||
describe(ruleName + ' - sunny path', () => { | ||
let rule; | ||
let context; | ||
let user; | ||
let globals; | ||
let stubs = {}; | ||
|
||
const newHubSpotContactId = 123; | ||
|
||
beforeEach(() => { | ||
globals = { | ||
auth0: { | ||
users: { | ||
updateAppMetadata: jest.fn() | ||
} | ||
}, | ||
configuration: { | ||
HUBSPOT_API_KEY: 'API_KEY', | ||
HUBSPOT_NEW_MEMBER_LIST_ID: '1', | ||
} | ||
}; | ||
|
||
stubs['request'] = jest.fn().mockImplementationOnce((obj, cb) => { | ||
cb(null, { statusCode: 200 }, JSON.stringify({ vid: newHubSpotContactId })); | ||
}) | ||
.mockImplementationOnce((obj, cb) => { | ||
cb(null, { statusCode: 200 }, { | ||
"updated": [ | ||
newHubSpotContactId | ||
], | ||
"discarded": [], | ||
"invalidVids": [], | ||
"invalidEmails": [] | ||
}); | ||
}); | ||
|
||
user = new UserBuilder().build(); | ||
user.given_name = "Given"; | ||
user.family_name = "Family"; | ||
|
||
context = new ContextBuilder().build(); | ||
rule = loadRule(ruleName, globals, stubs); | ||
}); | ||
|
||
it('should create a hubspot contact, add to a contact list and set app metadata', (done) => { | ||
rule(user, context, () => { }); | ||
|
||
// First POST is to get the access token | ||
const newTokenPostOptions = stubs.request.mock.calls[0][0]; | ||
const newTokenPostBody = JSON.parse(newTokenPostOptions.body); | ||
expect(newTokenPostOptions.url).toBe('https://api.hubapi.com/contacts/v1/contact/?hapikey=' + globals.configuration.HUBSPOT_API_KEY); | ||
expect(newTokenPostBody.properties[0].value).toBe(user.email); | ||
expect(newTokenPostBody.properties[1].value).toBe(user.given_name); | ||
expect(newTokenPostBody.properties[2].value).toBe(user.family_name); | ||
|
||
// Second POST is to create the lead | ||
const addContactToListPostOptions = stubs.request.mock.calls[1][0]; | ||
const addContactToListPostBody = JSON.parse(addContactToListPostOptions.body); | ||
expect(addContactToListPostOptions.url).toBe('https://api.hubapi.com/contacts/v1/lists/' + globals.configuration.HUBSPOT_NEW_MEMBER_LIST_ID + '/add?hapikey=' + globals.configuration.HUBSPOT_API_KEY); | ||
expect(addContactToListPostBody.vids[0]).toBe(newHubSpotContactId); | ||
|
||
const updateAppMetadataCall = globals.auth0.users.updateAppMetadata.mock.calls[0]; | ||
expect(updateAppMetadataCall[0]).toBe(user.user_id); | ||
expect(updateAppMetadataCall[1].hubSpotContactCreated).toBe(true); | ||
expect(updateAppMetadataCall[1].hubSpotContactId).toBe(newHubSpotContactId); | ||
expect(updateAppMetadataCall[1].hubSpotContactAddedToList).toBe(true); | ||
|
||
done(); | ||
}); | ||
|
||
describe('fail to create a contact', () => { | ||
beforeEach(() => { | ||
|
||
stubs['request'] = jest.fn().mockImplementationOnce((obj, cb) => { | ||
cb(null, { statusCode: 500 }, null); | ||
}) | ||
.mockImplementationOnce((obj, cb) => { | ||
cb(null, { statusCode: 500 }, null); | ||
}); | ||
}); | ||
|
||
it('should fail create a hubspot contact', (done) => { | ||
rule(user, context, () => { }); | ||
|
||
const updateAppMetadataCall = globals.auth0.users.updateAppMetadata.mock.calls[0]; | ||
expect(updateAppMetadataCall[0]).toBe(user.user_id); | ||
expect(updateAppMetadataCall[1].hubSpotContactCreated).toBe(false); | ||
|
||
done(); | ||
}); | ||
}); | ||
|
||
|
||
describe('fail to add the contact to a list', () => { | ||
beforeEach(() => { | ||
|
||
stubs['request'] = jest.fn().mockImplementationOnce((obj, cb) => { | ||
cb(null, { statusCode: 200 }, JSON.stringify({ vid: newHubSpotContactId })); | ||
}) | ||
.mockImplementationOnce((obj, cb) => { | ||
cb(null, { statusCode: 500 }, null); | ||
}); | ||
}); | ||
|
||
it('should fail create a hubspot contact', (done) => { | ||
rule(user, context, () => { }); | ||
|
||
const updateAppMetadataCall = globals.auth0.users.updateAppMetadata.mock.calls[0]; | ||
expect(updateAppMetadataCall[0]).toBe(user.user_id); | ||
expect(updateAppMetadataCall[1].hubSpotContactCreated).toBe(true); | ||
expect(updateAppMetadataCall[1].hubSpotContactId).toBe(newHubSpotContactId); | ||
expect(updateAppMetadataCall[1].hubSpotContactAddedToList).toBe(false); | ||
|
||
done(); | ||
}); | ||
}); | ||
}); | ||
|