n8n-nodes-highlevelv2
Version:
n8n community node for HighLevel v2 API integration - comprehensive CRM, marketing automation, and business management platform
429 lines • 18.8 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.addNotePostReceiveAction = void 0;
exports.isEmailValid = isEmailValid;
exports.isPhoneValid = isPhoneValid;
exports.dateToIsoSupressMillis = dateToIsoSupressMillis;
exports.taskPostReceiceAction = taskPostReceiceAction;
exports.dueDatePreSendAction = dueDatePreSendAction;
exports.contactIdentifierPreSendAction = contactIdentifierPreSendAction;
exports.validEmailAndPhonePreSendAction = validEmailAndPhonePreSendAction;
exports.dateTimeToEpochPreSendAction = dateTimeToEpochPreSendAction;
exports.addLocationIdPreSendAction = addLocationIdPreSendAction;
exports.addCompanyIdPreSendAction = addCompanyIdPreSendAction;
exports.highLevelApiRequest = highLevelApiRequest;
exports.taskUpdatePreSendAction = taskUpdatePreSendAction;
exports.splitTagsPreSendAction = splitTagsPreSendAction;
exports.highLevelApiPagination = highLevelApiPagination;
exports.getPipelineStages = getPipelineStages;
exports.getPipelines = getPipelines;
exports.getContacts = getContacts;
exports.getUsers = getUsers;
exports.getSocialAccounts = getSocialAccounts;
exports.addCustomFieldsPreSendAction = addCustomFieldsPreSendAction;
exports.getSubAccounts = getSubAccounts;
const luxon_1 = require("luxon");
const n8n_workflow_1 = require("n8n-workflow");
const VALID_EMAIL_REGEX = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
const VALID_PHONE_REGEX = /((?:\+|00)[17](?: |\-)?|(?:\+|00)[1-9]\d{0,2}(?: |\-)?|(?:\+|00)1\-\d{3}(?: |\-)?)?(0\d|\([0-9]{3}\)|[1-9]{0,3})(?:((?: |\-)[0-9]{2}){4}|((?:[0-9]{2}){4})|((?: |\-)[0-9]{3}(?: |\-)[0-9]{4})|([0-9]{7}))/;
function isEmailValid(email) {
return VALID_EMAIL_REGEX.test(String(email).toLowerCase());
}
function isPhoneValid(phone) {
return VALID_PHONE_REGEX.test(String(phone));
}
function dateToIsoSupressMillis(dateTime) {
var _a;
const options = { suppressMilliseconds: true };
return (_a = luxon_1.DateTime.fromISO(dateTime).toISO(options)) !== null && _a !== void 0 ? _a : undefined;
}
async function taskPostReceiceAction(items, _response) {
const contactId = this.getNodeParameter('contactId');
items.forEach((item) => (item.json.contactId = contactId));
return items;
}
async function dueDatePreSendAction(requestOptions) {
var _a;
let dueDateParam = this.getNodeParameter('dueDate', null);
if (!dueDateParam) {
const fields = this.getNodeParameter('updateFields');
dueDateParam = fields.dueDate;
}
if (!dueDateParam) {
throw new n8n_workflow_1.NodeApiError(this.getNode(), {}, { message: 'dueDate is required', description: 'dueDate is required' });
}
const dueDate = dateToIsoSupressMillis(dueDateParam);
requestOptions.body = ((_a = requestOptions.body) !== null && _a !== void 0 ? _a : {});
Object.assign(requestOptions.body, { dueDate });
return requestOptions;
}
async function contactIdentifierPreSendAction(requestOptions) {
var _a;
requestOptions.body = ((_a = requestOptions.body) !== null && _a !== void 0 ? _a : {});
let identifier = this.getNodeParameter('contactIdentifier', null);
if (!identifier) {
const fields = this.getNodeParameter('updateFields');
identifier = fields.contactIdentifier;
}
if (isEmailValid(identifier)) {
Object.assign(requestOptions.body, { email: identifier });
}
else if (isPhoneValid(identifier)) {
Object.assign(requestOptions.body, { phone: identifier });
}
else {
Object.assign(requestOptions.body, { contactId: identifier });
}
return requestOptions;
}
async function validEmailAndPhonePreSendAction(requestOptions) {
var _a;
const body = ((_a = requestOptions.body) !== null && _a !== void 0 ? _a : {});
if (body.email && !isEmailValid(body.email)) {
const message = `email "${body.email}" has invalid format`;
throw new n8n_workflow_1.NodeApiError(this.getNode(), {}, { message, description: message });
}
if (body.phone && !isPhoneValid(body.phone)) {
const message = `phone "${body.phone}" has invalid format`;
throw new n8n_workflow_1.NodeApiError(this.getNode(), {}, { message, description: message });
}
return requestOptions;
}
async function dateTimeToEpochPreSendAction(requestOptions) {
var _a;
const qs = ((_a = requestOptions.qs) !== null && _a !== void 0 ? _a : {});
const toEpoch = (dt) => new Date(dt).getTime();
if (qs.startDate)
qs.startDate = toEpoch(qs.startDate);
if (qs.endDate)
qs.endDate = toEpoch(qs.endDate);
return requestOptions;
}
async function addLocationIdPreSendAction(requestOptions) {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s;
const { locationId } = (_b = (_a = (await this.getCredentials('highLevelOAuth2Api'))) === null || _a === void 0 ? void 0 : _a.oauthTokenData) !== null && _b !== void 0 ? _b : {};
const resource = this.getNodeParameter('resource');
const operation = this.getNodeParameter('operation');
if (resource === 'contact') {
if (operation === 'getAll') {
requestOptions.qs = (_c = requestOptions.qs) !== null && _c !== void 0 ? _c : {};
Object.assign(requestOptions.qs, { locationId });
}
if (operation === 'create') {
requestOptions.body = (_d = requestOptions.body) !== null && _d !== void 0 ? _d : {};
Object.assign(requestOptions.body, { locationId });
}
}
if (resource === 'opportunity') {
if (operation === 'create') {
requestOptions.body = (_e = requestOptions.body) !== null && _e !== void 0 ? _e : {};
Object.assign(requestOptions.body, { locationId });
}
if (operation === 'getAll') {
requestOptions.qs = (_f = requestOptions.qs) !== null && _f !== void 0 ? _f : {};
Object.assign(requestOptions.qs, { location_id: locationId });
}
}
if (resource === 'media') {
if (operation === 'getFiles') {
requestOptions.qs = (_g = requestOptions.qs) !== null && _g !== void 0 ? _g : {};
Object.assign(requestOptions.qs, { locationId });
}
if (operation === 'uploadFile') {
requestOptions.body = (_h = requestOptions.body) !== null && _h !== void 0 ? _h : {};
Object.assign(requestOptions.body, { locationId });
}
}
if (resource === 'redirect') {
if (operation === 'getAll') {
requestOptions.qs = (_j = requestOptions.qs) !== null && _j !== void 0 ? _j : {};
Object.assign(requestOptions.qs, { locationId });
}
if (operation === 'create') {
requestOptions.body = (_k = requestOptions.body) !== null && _k !== void 0 ? _k : {};
Object.assign(requestOptions.body, { locationId });
}
if (operation === 'update') {
requestOptions.body = (_l = requestOptions.body) !== null && _l !== void 0 ? _l : {};
Object.assign(requestOptions.body, { locationId });
}
}
if (resource === 'funnel') {
if (['getFunnels', 'getFunnelPages', 'getPageCount'].includes(operation)) {
requestOptions.qs = (_m = requestOptions.qs) !== null && _m !== void 0 ? _m : {};
Object.assign(requestOptions.qs, { locationId });
}
}
if (resource === 'notes') {
if (['getAll'].includes(operation)) {
requestOptions.qs = (_o = requestOptions.qs) !== null && _o !== void 0 ? _o : {};
Object.assign(requestOptions.qs, { locationId });
}
if (['create', 'update'].includes(operation)) {
requestOptions.body = (_p = requestOptions.body) !== null && _p !== void 0 ? _p : {};
Object.assign(requestOptions.body, { locationId });
}
}
if (resource === 'tags') {
if (['addTags', 'removeTags'].includes(operation)) {
requestOptions.body = (_q = requestOptions.body) !== null && _q !== void 0 ? _q : {};
Object.assign(requestOptions.body, { locationId });
}
}
if (resource === 'customValues') {
if (['getAll'].includes(operation)) {
requestOptions.qs = (_r = requestOptions.qs) !== null && _r !== void 0 ? _r : {};
Object.assign(requestOptions.qs, { locationId });
}
if (['create', 'update'].includes(operation)) {
requestOptions.body = (_s = requestOptions.body) !== null && _s !== void 0 ? _s : {};
Object.assign(requestOptions.body, { locationId });
}
}
return requestOptions;
}
async function addCompanyIdPreSendAction(requestOptions) {
var _a, _b, _c;
const { companyId } = (_b = (_a = (await this.getCredentials('highLevelOAuth2Api'))) === null || _a === void 0 ? void 0 : _a.oauthTokenData) !== null && _b !== void 0 ? _b : {};
const resource = this.getNodeParameter('resource');
const operation = this.getNodeParameter('operation');
if (resource === 'saas') {
if (['getLocationsByStripeId', 'getAgencyPlans', 'getSaasLocations', 'getSaasPlan', 'bulkEnableSaas'].includes(operation)) {
requestOptions.qs = (_c = requestOptions.qs) !== null && _c !== void 0 ? _c : {};
Object.assign(requestOptions.qs, { companyId });
}
}
return requestOptions;
}
async function highLevelApiRequest(method, resource, body = {}, qs = {}, url, option = {}) {
let options = {
headers: {
'Content-Type': 'application/json',
Version: '2021-07-28',
},
method,
body,
qs,
url: url !== null && url !== void 0 ? url : `https://services.leadconnectorhq.com${resource}`,
json: true,
};
if (!Object.keys(body).length) {
delete options.body;
}
if (!Object.keys(qs).length) {
delete options.qs;
}
options = Object.assign({}, options, option);
return await this.helpers.httpRequestWithAuthentication.call(this, 'highLevelOAuth2Api', options);
}
const addNotePostReceiveAction = async function (items, response) {
const note = this.getNodeParameter('additionalFields.notes', 0);
if (!note) {
return items;
}
const contact = response.body.contact;
if (!response || !response.body || !contact) {
throw new n8n_workflow_1.ApplicationError('No response data available to extract contact ID and user ID.');
}
const contactId = contact.id;
const userId = contact.locationId;
const requestBody = {
userId,
body: note,
};
await highLevelApiRequest.call(this, 'POST', `/contacts/${contactId}/notes`, requestBody, {});
return items;
};
exports.addNotePostReceiveAction = addNotePostReceiveAction;
async function taskUpdatePreSendAction(requestOptions) {
var _a, _b;
const body = ((_a = requestOptions.body) !== null && _a !== void 0 ? _a : {});
if (!body.title || !body.dueDate) {
const contactId = this.getNodeParameter('contactId');
const taskId = this.getNodeParameter('taskId');
const resource = `/contacts/${contactId}/tasks/${taskId}`;
const responseData = await highLevelApiRequest.call(this, 'GET', resource);
body.title = body.title || responseData.title;
body.dueDate = body.dueDate || (responseData.dueDate ? (_b = dateToIsoSupressMillis(responseData.dueDate)) !== null && _b !== void 0 ? _b : undefined : undefined);
requestOptions.body = body;
}
return requestOptions;
}
async function splitTagsPreSendAction(requestOptions) {
var _a;
const body = ((_a = requestOptions.body) !== null && _a !== void 0 ? _a : {});
if (body.tags) {
if (Array.isArray(body.tags))
return requestOptions;
body.tags = body.tags.split(',').map((tag) => tag.trim());
}
return requestOptions;
}
async function highLevelApiPagination(requestData) {
var _a;
const responseData = [];
const resource = this.getNodeParameter('resource');
const returnAll = this.getNodeParameter('returnAll', false);
const resourceMapping = {
auth: 'auth',
blog: 'blogs',
contact: 'contacts',
conversation: 'conversations',
customValues: 'custom-values',
email: 'emails',
form: 'forms',
funnel: 'funnels',
media: 'files',
notes: 'notes',
opportunity: 'opportunities',
redirect: 'redirects',
saas: 'saas',
survey: 'surveys',
tags: 'tags',
user: 'users',
};
const rootProperty = resourceMapping[resource];
requestData.options.qs = (_a = requestData.options.qs) !== null && _a !== void 0 ? _a : {};
if (returnAll)
requestData.options.qs.limit = 100;
let responseTotal = 0;
do {
const pageResponseData = await this.makeRoutingRequest(requestData);
const items = pageResponseData[0].json[rootProperty];
items.forEach((item) => responseData.push({ json: item }));
const meta = pageResponseData[0].json.meta;
const startAfterId = meta.startAfterId;
const startAfter = meta.startAfter;
requestData.options.qs = { startAfterId, startAfter };
responseTotal = meta.total || 0;
} while (returnAll && responseTotal > responseData.length);
return responseData;
}
async function getPipelineStages() {
var _a, _b;
const operation = this.getNodeParameter('operation');
let pipelineId = '';
if (operation === 'create') {
pipelineId = this.getCurrentNodeParameter('pipelineId');
}
if (operation === 'update') {
pipelineId = this.getNodeParameter('updateFields.pipelineId');
}
if (operation === 'getAll') {
pipelineId = this.getNodeParameter('filters.pipelineId');
}
const { locationId } = (_b = (_a = (await this.getCredentials('highLevelOAuth2Api'))) === null || _a === void 0 ? void 0 : _a.oauthTokenData) !== null && _b !== void 0 ? _b : {};
const pipelines = (await highLevelApiRequest.call(this, 'GET', '/opportunities/pipelines', undefined, {
locationId,
})).pipelines;
const pipeline = pipelines.find((p) => p.id === pipelineId);
if (pipeline) {
const options = pipeline.stages.map((stage) => {
const name = stage.name;
const value = stage.id;
return { name, value };
});
return options;
}
return [];
}
async function getPipelines() {
var _a, _b;
const { locationId } = (_b = (_a = (await this.getCredentials('highLevelOAuth2Api'))) === null || _a === void 0 ? void 0 : _a.oauthTokenData) !== null && _b !== void 0 ? _b : {};
const responseData = await highLevelApiRequest.call(this, 'GET', '/opportunities/pipelines', undefined, { locationId });
const pipelines = responseData.pipelines;
const options = pipelines.map((pipeline) => {
const name = pipeline.name;
const value = pipeline.id;
return { name, value };
});
return options;
}
async function getContacts() {
var _a, _b;
const { locationId } = (_b = (_a = (await this.getCredentials('highLevelOAuth2Api'))) === null || _a === void 0 ? void 0 : _a.oauthTokenData) !== null && _b !== void 0 ? _b : {};
const responseData = await highLevelApiRequest.call(this, 'GET', '/contacts/', undefined, {
locationId,
});
const contacts = responseData.contacts;
const options = contacts.map((contact) => {
const name = contact.email;
const value = contact.id;
return { name, value };
});
return options;
}
async function getUsers() {
var _a, _b;
const { locationId } = (_b = (_a = (await this.getCredentials('highLevelOAuth2Api'))) === null || _a === void 0 ? void 0 : _a.oauthTokenData) !== null && _b !== void 0 ? _b : {};
const responseData = await highLevelApiRequest.call(this, 'GET', '/users/', undefined, {
locationId,
});
const users = responseData.users;
const options = users.map((user) => {
const name = user.name;
const value = user.id;
return { name, value };
});
return options;
}
async function getSocialAccounts() {
var _a, _b;
const { locationId } = (_b = (_a = (await this.getCredentials('highLevelOAuth2Api'))) === null || _a === void 0 ? void 0 : _a.oauthTokenData) !== null && _b !== void 0 ? _b : {};
try {
const responseData = await highLevelApiRequest.call(this, 'GET', `/locations/${locationId}/social-planner/accounts`, undefined, {});
const socialAccounts = responseData.socialAccounts;
const options = socialAccounts
.filter((account) => account.status === 'connected')
.map((account) => {
const name = `${account.accountName} (${account.platform})`;
const value = account.id;
return { name, value };
});
return options;
}
catch (error) {
return [];
}
}
async function addCustomFieldsPreSendAction(requestOptions) {
const requestBody = requestOptions.body;
if (requestBody && requestBody.customFields) {
const rawCustomFields = requestBody.customFields;
if (rawCustomFields && Array.isArray(rawCustomFields.values)) {
const formattedCustomFields = rawCustomFields.values.map((field) => {
var _a;
const typedField = field;
const fieldId = typedField.fieldId;
if (typeof fieldId === 'object' && fieldId !== null && 'value' in fieldId) {
return {
id: fieldId.value,
key: (_a = fieldId.cachedResultName) !== null && _a !== void 0 ? _a : 'default_key',
field_value: typedField.fieldValue,
};
}
else {
throw new n8n_workflow_1.ApplicationError('Error processing custom fields.');
}
});
requestBody.customFields = formattedCustomFields;
}
}
return requestOptions;
}
async function getSubAccounts() {
try {
const responseData = await highLevelApiRequest.call(this, 'GET', '/locations', undefined, {});
const subAccounts = responseData.locations;
const options = subAccounts.map((account) => {
const name = `${account.businessName} (${account.city}, ${account.state})`;
const value = account.id;
return { name, value };
});
return options;
}
catch (error) {
return [];
}
}
//# sourceMappingURL=GenericFunctions.js.map