const WeavyURL = 'WEAVY_URL';
const WeavyAPI = '********************';
const HubSpotAccessToken = '-*-*-*-*-*-*-*-*-*-*-*-*';
exports.main = async (context, sendResponse) => {
var fullName = 'Demo User';
var email = 'demo@user.com';
var uid = 'uid-demouser';
try {
let hsContact = await fetch('https://api.hubapi.com/crm/v3/objects/contacts/' + context.contact.vid, {
method: 'GET',
headers: { 'Authorization': 'Bearer ' + HubSpotAccessToken }
});
if (hsContact.ok) {
let hsContactData = await hsContact.json();
var fullName = hsContactData.properties.firstname + ' ' + hsContactData.properties.lastname;
var email = hsContactData.properties.email;
var uid = 'uid-' + hsContactData.properties.hs_object_id;
}
} catch(e) {}
let user = await fetch(WeavyURL + '/api/users/' + uid, {
method: 'PUT',
headers: {
'content-type': 'application/json',
'Authorization': 'Bearer ' + WeavyAPI
},
body: JSON.stringify({
"name": fullName,
"email": email
})
});
if (user.ok) {
let response = await fetch(WeavyURL + '/api/users/' + uid + '/tokens', {
method: 'POST',
headers: {
'content-type': 'application/json',
'Authorization': 'Bearer ' + WeavyAPI
}
});
if (response.ok) {
let data = await response.json();
sendResponse({
body: JSON.stringify(data),
statusCode: 200
});
} else {
sendResponse({ body: { error: response.statusText }, statusCode: 500 });
}
}
};