n8n/packages/nodes-base/nodes/HighLevel/GenericFunctions.ts
Marcus c2e97a89f9
feat(HighLevel Node): Add HighLevel node
* HighLevel Node & Credentials with basic Get/GetAll contacts

* moved contact description into subfolder

* improving all  HighLevel contact crud operations

* Get All Contacts request filters and options

* Get All Contacts request operation pagination

* Get All Contacts pagination with rootProperty

* fixing contact operations lint error node-param-options-type-unsorted-items

* Get All Contact pagination using response total to terminate loop

* Get All Contacts pagination using qs startAfterId and startAfter instead of nextPageUrl

* cleanup ContactDescription.ts operations

* added OpportunityDescription.ts Get All Operation

* added PipelineDescription.ts Get All pipelines (pagination no supported)

* GenericFunctions.ts created highLevelApiPagination method

* removed returnAll from getAll pipelines because pagination not supported

* minor improvement using IPostReceiveRootProperty Interface

* added TaskDescription.ts GetAll operation

* TaskDescription get operation

* task delete operation

* ContactDescription fixing duplicate additionalFields

* OpportunityDescription.ts adding filters for get all operation

* opportunity get and delete operation

* opportunity create additional fields

* task create with additionalFields

* create opportunity added missing status

* pagination resource mapping; create opportunity for contact email/phone/id

* create task format field dueDate to suppressMilliseconds

* opportunity's pipeline options with loadOptions

* fixed task dueDate; task update operation

* added contact dnd fields

* added contact tags

* add contact source parameter

* using returnAll Tasks with undocumented limit property

* add contact customField with loadOptions

* task dueDate preSendAction throw required error

* opportunity stageId using getPipelineStages method

* improve update contacts operation; renaming identifier to contactId

* contact tasks renamed contactIdentifier to contactId and identifier to taskId

* delete unused postReceiveAction

* getTaskPostReceiceAction to inject contactId into items

* remove source field from update contact because not supported

* renaming opportunities pipelineIdentifier to pipelineId

* opportunity parameter reordering

* opportunity update operation

* opportunity renamed "Identifier" to "Opporituny ID"

* create opportunity additional field tags

* opportunity create/update additional fields

* opportunity update contactIdentifier

* deleted pipeline getAll resource; cleanup generic functions;

* opportunity getAll option stageId use getPipelineStages

* opportunity assignedTo options use loadOptionsMethod getUsers

* added loadOptions description ''Choose from the list, or specify an ID [...]'

* getAll Tasks removing output maxResults

* highLevelApiRequest  add IPollFunctions

* add HighLevelTriggerNode

* add highLevelOAuth2Api credentials

* revert from v2 oauth2 back to apiKey v1 (incompatible client-oauth2)

* delete run.sh (was commited by accidant)

* removed HighLevelTrigger.node.ts (polling) until we support API V2 webhooks

* task additionalFields in alphabetical order; task assignedTo using loadOptions getUsers

* contact additionalFields/updateFields/filters in alphabetical order

* opportunity additionalFields/fields in alphabetical order

*  linter fixes, formating

*  fixed deprecated auth

*  lock file and auth fix

*  linter fixes

*  requestWithAuthentication in highLevelApiRequest

*  timeZones autoload and naming fixes

* contact property dnd - improved display name and description

* contact property dnd - improved display name and description

* contact create/update pre send action to validate email/phone

* updated description for contact lookup email and phone parameters

* opportunity monetary value - removed getAll filter (doesn't work) - added param description

* opportunity getAll startDate and endDate to type dateTime and dateTimeToEpochPreSendAction

* loadOption getTimezones() using /timezones and not moment.tz.names()

* improved description for opportunity parameter assignedTo

* added hint to opportunity param contactIdentifier "there can only be one..."

* added contact phone description and placeholder

* contact renamed operation "create" to "create or update" and added notice

* update opportunity - prefetch fields title/status required by api

* create/update task splitting up additionalFields to prep update changes

* update task- prefetch fields title/dueDate required by api

* linter fixes

*  improvements

*  updated autoloaded parameters names to follow convention

* update opportunity added hint "You cannot update an opportunity's pipeline ID."

Co-authored-by: Michael Kret <michael.k@radency.com>
2022-08-30 11:25:06 +02:00

283 lines
9.3 KiB
TypeScript

import {
DeclarativeRestApiSettings,
IDataObject,
IExecuteFunctions,
IExecutePaginationFunctions,
IExecuteSingleFunctions,
IHookFunctions,
IHttpRequestOptions,
ILoadOptionsFunctions,
IN8nHttpFullResponse,
INodeExecutionData,
INodePropertyOptions,
IPollFunctions,
IWebhookFunctions,
NodeApiError,
} from 'n8n-workflow';
import { OptionsWithUri } from 'request';
import { DateTime, ToISOTimeOptions } from 'luxon';
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}))/;
export function isEmailValid(email: string): boolean {
return VALID_EMAIL_REGEX.test(String(email).toLowerCase());
}
export function isPhoneValid(phone: string): boolean {
return VALID_PHONE_REGEX.test(String(phone));
}
function dateToIsoSupressMillis(dateTime: string) {
const options: ToISOTimeOptions = { suppressMilliseconds: true };
return DateTime.fromISO(dateTime).toISO(options);
}
export async function taskPostReceiceAction(
this: IExecuteSingleFunctions,
items: INodeExecutionData[],
response: IN8nHttpFullResponse,
): Promise<INodeExecutionData[]> {
const contactId = this.getNodeParameter('contactId');
items.forEach((item) => (item.json.contactId = contactId));
return items;
}
export async function dueDatePreSendAction(
this: IExecuteSingleFunctions,
requestOptions: IHttpRequestOptions,
): Promise<IHttpRequestOptions> {
let dueDateParam = this.getNodeParameter('dueDate', null) as string;
if (!dueDateParam) {
const fields = this.getNodeParameter('updateFields') as { dueDate: string };
dueDateParam = fields.dueDate;
}
if (!dueDateParam) {
throw new NodeApiError(
this.getNode(),
{},
{ message: 'dueDate is required', description: 'dueDate is required' },
);
}
const dueDate = dateToIsoSupressMillis(dueDateParam);
requestOptions.body = (requestOptions.body || {}) as object;
Object.assign(requestOptions.body, { dueDate });
return requestOptions;
}
export async function contactIdentifierPreSendAction(
this: IExecuteSingleFunctions,
requestOptions: IHttpRequestOptions,
): Promise<IHttpRequestOptions> {
requestOptions.body = (requestOptions.body || {}) as object;
let identifier = this.getNodeParameter('contactIdentifier', null) as string;
if (!identifier) {
const fields = this.getNodeParameter('updateFields') as { contactIdentifier: string };
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;
}
export async function validEmailAndPhonePreSendAction(
this: IExecuteSingleFunctions,
requestOptions: IHttpRequestOptions,
): Promise<IHttpRequestOptions> {
const body = (requestOptions.body || {}) as { email?: string; phone?: string };
if (body.email && !isEmailValid(body.email)) {
const message = `email "${body.email}" has invalid format`;
throw new NodeApiError(this.getNode(), {}, { message, description: message });
}
if (body.phone && !isPhoneValid(body.phone)) {
const message = `phone "${body.phone}" has invalid format`;
throw new NodeApiError(this.getNode(), {}, { message, description: message });
}
return requestOptions;
}
export async function dateTimeToEpochPreSendAction(
this: IExecuteSingleFunctions,
requestOptions: IHttpRequestOptions,
): Promise<IHttpRequestOptions> {
const qs = (requestOptions.qs || {}) as {
startDate?: string | number;
endDate?: string | number;
};
const toEpoch = (dt: string) => new Date(dt).getTime();
if (qs.startDate) qs.startDate = toEpoch(qs.startDate as string);
if (qs.endDate) qs.endDate = toEpoch(qs.endDate as string);
return requestOptions;
}
export async function opportunityUpdatePreSendAction(
this: IExecuteSingleFunctions,
requestOptions: IHttpRequestOptions,
): Promise<IHttpRequestOptions> {
const body = (requestOptions.body || {}) as { title?: string; status?: string };
if (!body.status || !body.title) {
const pipelineId = this.getNodeParameter('pipelineId');
const opportunityId = this.getNodeParameter('opportunityId');
const resource = `/pipelines/${pipelineId}/opportunities/${opportunityId}`;
const responseData = await highLevelApiRequest.call(this, 'GET', resource);
body.status = body.status || responseData.status;
body.title = body.title || responseData.name;
requestOptions.body = body;
}
return requestOptions;
}
export async function taskUpdatePreSendAction(
this: IExecuteSingleFunctions,
requestOptions: IHttpRequestOptions,
): Promise<IHttpRequestOptions> {
const body = (requestOptions.body || {}) as { title?: string; dueDate?: string };
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;
// the api response dueDate has to be formatted or it will error on update
body.dueDate = body.dueDate || dateToIsoSupressMillis(responseData.dueDate);
requestOptions.body = body;
}
return requestOptions;
}
export async function splitTagsPreSendAction(
this: IExecuteSingleFunctions,
requestOptions: IHttpRequestOptions,
): Promise<IHttpRequestOptions> {
const body = (requestOptions.body || {}) as IDataObject;
if (body.tags) {
if (Array.isArray(body.tags)) return requestOptions;
body.tags = (body.tags as string).split(',').map((tag) => tag.trim());
}
return requestOptions;
}
export async function highLevelApiPagination(
this: IExecutePaginationFunctions,
requestData: DeclarativeRestApiSettings.ResultOptions,
): Promise<INodeExecutionData[]> {
const responseData: INodeExecutionData[] = [];
const resource = this.getNodeParameter('resource') as string;
const returnAll = this.getNodeParameter('returnAll', false) as boolean;
const resourceMapping: { [key: string]: string } = {
contact: 'contacts',
opportunity: 'opportunities',
};
const rootProperty = resourceMapping[resource];
requestData.options.qs = requestData.options.qs || {};
if (returnAll) requestData.options.qs.limit = 100;
let responseTotal = 0;
do {
const pageResponseData: INodeExecutionData[] = await this.makeRoutingRequest(requestData);
const items = pageResponseData[0].json[rootProperty] as [];
items.forEach((item) => responseData.push({ json: item }));
const meta = pageResponseData[0].json.meta as IDataObject;
const startAfterId = meta.startAfterId as string;
const startAfter = meta.startAfter as number;
requestData.options.qs = { startAfterId, startAfter };
responseTotal = (meta.total as number) || 0;
} while (returnAll && responseTotal > responseData.length);
return responseData;
}
export async function highLevelApiRequest(
this:
| IExecuteFunctions
| IExecuteSingleFunctions
| IWebhookFunctions
| IPollFunctions
| IHookFunctions
| ILoadOptionsFunctions,
method: string,
resource: string,
body: IDataObject = {},
qs: IDataObject = {},
uri?: string,
option: IDataObject = {},
) {
let options: OptionsWithUri = {
method,
body,
qs,
uri: uri || `https://rest.gohighlevel.com/v1${resource}`,
json: true,
};
if (!Object.keys(body).length) {
delete options.body;
}
if (!Object.keys(qs).length) {
delete options.qs;
}
options = Object.assign({}, options, option);
try {
return await this.helpers.requestWithAuthentication.call(this, 'highLevelApi', options);
} catch (error) {
throw new NodeApiError(this.getNode(), error, {
message: error.message,
});
}
}
export async function getPipelineStages(
this: ILoadOptionsFunctions,
): Promise<INodePropertyOptions[]> {
const pipelineId = this.getCurrentNodeParameter('pipelineId') as string;
const responseData = await highLevelApiRequest.call(this, 'GET', '/pipelines');
const pipelines = responseData.pipelines as [
{ id: string; stages: [{ id: string; name: string }] },
];
const pipeline = pipelines.find((p) => p.id === pipelineId);
if (pipeline) {
const options: INodePropertyOptions[] = pipeline.stages.map((stage) => {
const name = stage.name;
const value = stage.id;
return { name, value };
});
return options;
}
return [];
}
export async function getUsers(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const responseData = await highLevelApiRequest.call(this, 'GET', '/users');
const users = responseData.users as [{ id: string; name: string; email: string }];
const options: INodePropertyOptions[] = users.map((user) => {
const name = user.name;
const value = user.id;
return { name, value };
});
return options;
}
export async function getTimezones(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const responseData = await highLevelApiRequest.call(this, 'GET', '/timezones');
const timezones = responseData.timezones as string[];
return timezones.map((zone) => ({
name: zone,
value: zone,
})) as INodePropertyOptions[];
}