/************************************************************************************************
* * Hubspot - Netsuite Integration **
* Library
*
* **********************************************************************************************
*
* Author: Jobin And Jismi
*
* Date Created : 16.06.2022
*
* Created By: Jobin And Jismi
*
***********************************************************************************************/
define(['N/config','N/runtime', 'N/record', 'N/search', 'N/format', 'N/error', 'N/encode', 'N/https', 'N/email'],
function (config,runtime, record, search, format, error, encode, https, email) {
var Library;
(function () {
var scriptContext;
function setContext(context) {
scriptContext = context;
}
var HUBSPOT_API_REQUESTS = {
/*DEALS*/
POST_DEALS: "https://api.hubapi.com/crm/v3/objects/deals/search?archived=false",
GET_DEAL_ASSOCIATIONS_ITEM: "https://api.hubapi.com/crm/v3/objects/deals/{dealId}/associations/line_items?archived=false",
GET_DEAL_ASSOCIATIONS_COMPANY: "https://api.hubapi.com/crm/v3/objects/deals/{dealId}/associations/companies?archived=false",
UPDATE_DEAL_STAGE: "https://api.hubapi.com/crm/v3/objects/deals/{dealId}?archived=false",
UPDATE_DEAL_PROPERTY: "https://api.hubapi.com/deals/v1/deal/{dealId}",
/*ITEM_LINE & PRODUCT*/
GET_LINE_ITEM: "https://api.hubapi.com/crm/v3/objects/line_items/{line_item}?properties=hs_product_id&properties=hs_sku&properties=name&properties=quantity&properties=amount&properties=price&properties=description&archived=false",
POST_LINE_ITEM: "https://api.hubapi.com/crm/v3/objects/line_items?archived=false",
POST_PRODUCT: "https://api.hubapi.com/crm/v3/objects/products",
/*COMPANY*/
GET_COMPANY: "https://api.hubapi.com/crm/v3/objects/companies/{companyid}?properties=netsuite_customer_id&properties=state&properties=city&properties=phone&properties=email&properties=name&properties=address&properties=address2&properties=zip&properties=country&archived=false",
POST_COMPANY: "https://api.hubapi.com/crm/v3/objects/companies?archived=false",
PUT_COMPANY: "https://api.hubapi.com/companies/v2/companies/{companyid}",
/*PIPELINE*/
GET_PIPELINES: "https://api.hubapi.com/crm/v3/pipelines/deals",
/*API_LIMIT*/
GET_APILIMIT: "https://api.hubapi.com/integrations/v1/limit/daily"
}
try {
Library = new HubspotLibrary();
} catch (e) {
log.error("error HubspotLibrary", e.message);
}
/**
* Class to encapsulate Hubspot Library functionality.
*/
function HubspotLibrary() {
this.setContext = setContext;
this.HUBSPOT_API_REQUESTS = HUBSPOT_API_REQUESTS ;
this.getRequestResults = getRequestResults;
this.getRequesHeaders = getRequestHeaders;
}
/**
* Get all the results from a Hubspot request.
*
* @scope Private
* @param {String} url - The URL to request and get the results from.
* @param {Object} parameters - The parameters for the request.
* @param {Object} headers - The headers for the request.
* @param {String} httpMethod - The method for the request.
* @return {Any} Results of request.
*/
function getRequestResults(url, parameters, httpMethod) {
var isMoreResults = false;
var response = null;
var responseBody = null;
var headers = getRequestHeaders()
log.debug("header", headers)
var results = [];
try {
do {
// reset definition of whether there are more results.
isMoreResults = false;
var requestObj = {
"method": httpMethod,
"url": url,
"headers": headers
}
if (parameters)
requestObj['body'] = parameters;
log.debug("requestObj", requestObj);
// request Hubspot.
response = https.request(requestObj);
log.debug('response', response)
// get the body of request.
responseBody = response.body;
// convert body to JSON.
if (responseBody && typeof responseBody === "string" && (responseBody.charAt(0) == "{" || responseBody.charAt(0) == "[")) {
responseBody = JSON.parse(responseBody);
}
// if request was successful.
if (response.code === 200 || response.code === 204 || response.code === 201) {
// if pagination was returned.
if (responseBody && responseBody.results) {
// add results to existing.
results = results.concat(responseBody.results);
log.debug('results',results)
// get url for next request.
}
// if errors were returned.
else if (responseBody && responseBody.errors && responseBody.errors[0]) {
throw error.create({
name: 'UNEXPECTED_ERROR',
message: responseBody.errors[0].join("<br>")
});
} else {
results = responseBody;
}
} else if (response.code === 400) {
throw error.create({
name: response.code.toString() + "_BAD_REQUEST",
message: responseBody.detail,
});
} else if (response.code === 401) {
throw error.create({
name: response.code.toString() + "_UNAUTHORISED_REQUEST",
message: responseBody.detail
});
} else if (response.code === 404) {
throw error.create({
name: response.code.toString() + "_NOT_FOUND",
message: "An unexpected error occurred."
});
}
else if (response.code === 500) {
if (responseBody && responseBody.detail) {
throw error.create({
name: response.code.toString() + "_INTERNAL_SERVER_ERROR",
message: responseBody.detail
});
} else {
throw error.create({
name: response.code.toString() + "_INTERNAL_SERVER_ERROR",
message: "An unexpected error occurred."
});
}
} else {
var response_data = JSON.parse(response.body)
if (response.body && response_data["errors"]) {
throw error.create({
name: "Hubspot error",
message: response_data["errors"]
});
}
else if (response.body && response.body.detail) {
throw error.create({
name: response.code.toString() + "_ERROR",
message: response.body.detail
});
} else {
throw error.create({
name: response.code.toString() + "_ERROR",
message: "An unexpected error occurred.",
});
}
}
}
while (url && isMoreResults); // while a url is provided and there are more results to get.
} catch (e) {
log.error('getRequestHubspotResults', e)
results = e;
}
return [results, response.code, JSON.parse(response.body)];
}
/**
* Get headers for Hubspot request.
*
* @scope Private
* Basic headers need to added for the request creation
*/
function getRequestHeaders() {
var headers = {};
try {
headers = { "Authorization": "Bearer pat-na1-********-****-****-****-************", "Content-Type": "application/json", "Accept":"application/json" }
} catch (e) {
Log.error("Error@getRequestHeaders", e);
}
return headers;
}
})();
return {
Library: Library
}
});