Skip to content

Commit

Permalink
feat(HighLevel Node): Add support for calendar items (n8n-io#10820)
Browse files Browse the repository at this point in the history
  • Loading branch information
Stamsy authored Jan 8, 2025
1 parent 9dd0686 commit 6e189fd
Show file tree
Hide file tree
Showing 24 changed files with 2,214 additions and 76 deletions.
10 changes: 10 additions & 0 deletions packages/nodes-base/credentials/HighLevelOAuth2Api.credentials.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,15 @@ export class HighLevelOAuth2Api implements ICredentialType {
type: 'hidden',
default: 'body',
},
{
displayName:
'Make sure your credentials include the required OAuth scopes for all actions this node performs.',
name: 'notice',
type: 'notice',
default: '',
displayOptions: {
hideOnCloud: true,
},
},
];
}
106 changes: 84 additions & 22 deletions packages/nodes-base/nodes/HighLevel/v2/GenericFunctions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { ToISOTimeOptions } from 'luxon';
import { DateTime } from 'luxon';
import type { ToISOTimeOptions } from 'luxon';
import type {
DeclarativeRestApiSettings,
IDataObject,
Expand All @@ -16,7 +16,7 @@ import type {
IPollFunctions,
IWebhookFunctions,
} from 'n8n-workflow';
import { NodeApiError } from 'n8n-workflow';
import { ApplicationError, NodeApiError } from '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,}))$/;
Expand All @@ -31,7 +31,7 @@ export function isPhoneValid(phone: string): boolean {
return VALID_PHONE_REGEX.test(String(phone));
}

function dateToIsoSupressMillis(dateTime: string) {
export function dateToIsoSupressMillis(dateTime: string) {
const options: ToISOTimeOptions = { suppressMilliseconds: true };
return DateTime.fromISO(dateTime).toISO(options);
}
Expand Down Expand Up @@ -63,7 +63,7 @@ export async function dueDatePreSendAction(
);
}
const dueDate = dateToIsoSupressMillis(dueDateParam);
requestOptions.body = (requestOptions.body || {}) as object;
requestOptions.body = (requestOptions.body ?? {}) as object;
Object.assign(requestOptions.body, { dueDate });
return requestOptions;
}
Expand All @@ -72,7 +72,7 @@ export async function contactIdentifierPreSendAction(
this: IExecuteSingleFunctions,
requestOptions: IHttpRequestOptions,
): Promise<IHttpRequestOptions> {
requestOptions.body = (requestOptions.body || {}) as object;
requestOptions.body = (requestOptions.body ?? {}) as object;
let identifier = this.getNodeParameter('contactIdentifier', null) as string;
if (!identifier) {
const fields = this.getNodeParameter('updateFields') as { contactIdentifier: string };
Expand All @@ -92,7 +92,7 @@ export async function validEmailAndPhonePreSendAction(
this: IExecuteSingleFunctions,
requestOptions: IHttpRequestOptions,
): Promise<IHttpRequestOptions> {
const body = (requestOptions.body || {}) as { email?: string; phone?: string };
const body = (requestOptions.body ?? {}) as { email?: string; phone?: string };

if (body.email && !isEmailValid(body.email)) {
const message = `email "${body.email}" has invalid format`;
Expand All @@ -111,7 +111,7 @@ export async function dateTimeToEpochPreSendAction(
this: IExecuteSingleFunctions,
requestOptions: IHttpRequestOptions,
): Promise<IHttpRequestOptions> {
const qs = (requestOptions.qs || {}) as {
const qs = (requestOptions.qs ?? {}) as {
startDate?: string | number;
endDate?: string | number;
};
Expand All @@ -133,22 +133,22 @@ export async function addLocationIdPreSendAction(

if (resource === 'contact') {
if (operation === 'getAll') {
requestOptions.qs = requestOptions.qs || {};
requestOptions.qs = requestOptions.qs ?? {};
Object.assign(requestOptions.qs, { locationId });
}
if (operation === 'create') {
requestOptions.body = requestOptions.body || {};
requestOptions.body = requestOptions.body ?? {};
Object.assign(requestOptions.body, { locationId });
}
}

if (resource === 'opportunity') {
if (operation === 'create') {
requestOptions.body = requestOptions.body || {};
requestOptions.body = requestOptions.body ?? {};
Object.assign(requestOptions.body, { locationId });
}
if (operation === 'getAll') {
requestOptions.qs = requestOptions.qs || {};
requestOptions.qs = requestOptions.qs ?? {};
Object.assign(requestOptions.qs, { location_id: locationId });
}
}
Expand Down Expand Up @@ -179,7 +179,7 @@ export async function highLevelApiRequest(
method,
body,
qs,
url: url || `https://services.leadconnectorhq.com${resource}`,
url: url ?? `https://services.leadconnectorhq.com${resource}`,
json: true,
};
if (!Object.keys(body).length) {
Expand All @@ -192,11 +192,42 @@ export async function highLevelApiRequest(
return await this.helpers.httpRequestWithAuthentication.call(this, 'highLevelOAuth2Api', options);
}

export const addNotePostReceiveAction = async function (
this: IExecuteSingleFunctions,
items: INodeExecutionData[],
response: IN8nHttpFullResponse,
): Promise<INodeExecutionData[]> {
const note = this.getNodeParameter('additionalFields.notes', 0) as string;

if (!note) {
return items;
}

const contact: IDataObject = (response.body as IDataObject).contact as IDataObject;

// Ensure there is a valid response and extract contactId and userId
if (!response || !response.body || !contact) {
throw new 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;
};

export async function taskUpdatePreSendAction(
this: IExecuteSingleFunctions,
requestOptions: IHttpRequestOptions,
): Promise<IHttpRequestOptions> {
const body = (requestOptions.body || {}) as { title?: string; dueDate?: string };
const body = (requestOptions.body ?? {}) as { title?: string; dueDate?: string };
if (!body.title || !body.dueDate) {
const contactId = this.getNodeParameter('contactId');
const taskId = this.getNodeParameter('taskId');
Expand All @@ -214,7 +245,7 @@ export async function splitTagsPreSendAction(
this: IExecuteSingleFunctions,
requestOptions: IHttpRequestOptions,
): Promise<IHttpRequestOptions> {
const body = (requestOptions.body || {}) as IDataObject;
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());
Expand All @@ -236,7 +267,7 @@ export async function highLevelApiPagination(
};
const rootProperty = resourceMapping[resource];

requestData.options.qs = requestData.options.qs || {};
requestData.options.qs = requestData.options.qs ?? {};
if (returnAll) requestData.options.qs.limit = 100;

let responseTotal = 0;
Expand Down Expand Up @@ -344,11 +375,42 @@ export async function getUsers(this: ILoadOptionsFunctions): Promise<INodeProper
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[];
export async function addCustomFieldsPreSendAction(
this: IExecuteSingleFunctions,
requestOptions: IHttpRequestOptions,
): Promise<IHttpRequestOptions> {
const requestBody = requestOptions.body as IDataObject;

if (requestBody && requestBody.customFields) {
const rawCustomFields = requestBody.customFields as IDataObject;

// Define the structure of fieldId
interface FieldIdType {
value: unknown;
cachedResultName?: string;
}

// Ensure rawCustomFields.values is an array of objects with fieldId and fieldValue
if (rawCustomFields && Array.isArray(rawCustomFields.values)) {
const formattedCustomFields = rawCustomFields.values.map((field: unknown) => {
// Assert that field is of the expected shape
const typedField = field as { fieldId: FieldIdType; fieldValue: unknown };

const fieldId = typedField.fieldId;

if (typeof fieldId === 'object' && fieldId !== null && 'value' in fieldId) {
return {
id: fieldId.value,
key: fieldId.cachedResultName ?? 'default_key',
field_value: typedField.fieldValue,
};
} else {
throw new ApplicationError('Error processing custom fields.');
}
});
requestBody.customFields = formattedCustomFields;
}
}

return requestOptions;
}
89 changes: 87 additions & 2 deletions packages/nodes-base/nodes/HighLevel/v2/HighLevelV2.node.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
import type {
IDataObject,
ILoadOptionsFunctions,
INodeListSearchItems,
INodeListSearchResult,
INodeProperties,
INodeType,
INodeTypeBaseDescription,
INodeTypeDescription,
} from 'n8n-workflow';
import { NodeConnectionType } from 'n8n-workflow';

import { calendarFields, calendarOperations } from './description/CalendarDescription';
import { contactFields, contactNotes, contactOperations } from './description/ContactDescription';
import { opportunityFields, opportunityOperations } from './description/OpportunityDescription';
import { taskFields, taskOperations } from './description/TaskDescription';
import {
getContacts,
getPipelines,
getPipelineStages,
getTimezones,
getUsers,
highLevelApiPagination,
} from './GenericFunctions';
Expand All @@ -37,6 +41,10 @@ const resources: INodeProperties[] = [
name: 'Task',
value: 'task',
},
{
name: 'Calendar',
value: 'calendar',
},
],
default: 'contact',
required: true,
Expand Down Expand Up @@ -82,6 +90,8 @@ const versionDescription: INodeTypeDescription = {
...opportunityFields,
...taskOperations,
...taskFields,
...calendarOperations,
...calendarFields,
],
};

Expand All @@ -101,7 +111,82 @@ export class HighLevelV2 implements INodeType {
getContacts,
getPipelineStages,
getUsers,
getTimezones,
},
listSearch: {
async searchCustomFields(
this: ILoadOptionsFunctions,
filter?: string,
): Promise<INodeListSearchResult> {
const { locationId } =
((await this.getCredentials('highLevelOAuth2Api'))?.oauthTokenData as IDataObject) ?? {};

const responseData: IDataObject = (await this.helpers.httpRequestWithAuthentication.call(
this,
'highLevelOAuth2Api',
{
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
url: `https://services.leadconnectorhq.com/locations/${locationId}/customFields?model=contact`,
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
Version: '2021-07-28',
},
},
)) as IDataObject;

const customFields = responseData.customFields as Array<{ name: string; id: string }>;

const results: INodeListSearchItems[] = customFields
.map((a) => ({
name: a.name,
value: a.id,
}))
.filter((a) => !filter || a.name.toLowerCase().includes(filter.toLowerCase()))
.sort((a, b) => {
if (a.name.toLowerCase() < b.name.toLowerCase()) return -1;
if (a.name.toLowerCase() > b.name.toLowerCase()) return 1;
return 0;
});

return { results };
},
async searchTimezones(
this: ILoadOptionsFunctions,
filter?: string,
): Promise<INodeListSearchResult> {
const { locationId } =
((await this.getCredentials('highLevelOAuth2Api'))?.oauthTokenData as IDataObject) ?? {};

const responseData: IDataObject = (await this.helpers.httpRequestWithAuthentication.call(
this,
'highLevelOAuth2Api',
{
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
url: `https://services.leadconnectorhq.com/locations/${locationId}/timezones`,
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
Version: '2021-07-28',
},
},
)) as IDataObject;

const timezones = responseData.timeZones as string[];

const results: INodeListSearchItems[] = timezones
.map((zone) => ({
name: zone.trim(),
value: zone.trim(),
}))
.filter((zone) => !filter || zone.name.toLowerCase().includes(filter.toLowerCase()))
.sort((a, b) => {
if (a.name.toLowerCase() < b.name.toLowerCase()) return -1;
if (a.name.toLowerCase() > b.name.toLowerCase()) return 1;
return 0;
});

return { results };
},
},
};
}
Loading

0 comments on commit 6e189fd

Please sign in to comment.