mirror of
https://github.com/n8n-io/n8n.git
synced 2024-11-10 14:44:05 -08:00
2913e676e6
* 🎨 Change current defualt ressource to contact * Change display name for list membership * 🎨 Change display name for ressource properties * 🔥 Remove Name & ID in Display name * 🐛 Fix simplify data where behavior was wrong * 🎨 Move simplfy output to additional fields * Change data type from string to number for IDs * Add tooltip for contact ID * 🎨 Change tooltip for list of Id's * Change name of fields and reorgaized options * 🥅 Add clearer errors for some specific cases * 🎨 changed filters in Deal get all * Deprecate form ressource * ✨ add pipeline dropdown to auto fetch * 🎨 Change direction's name * 🎨 Change description of filter groups * 🎨 Add validation and errors for maximum filters * ✨ Add ressource locator to all ressources * 🎨 add tooltip for timestamp when using expressions * ✨add versioning * 🐛 fixes small bugs and ui problems * remove show options that was breaking custom prop * ✨ add ownership to deals * ✨ add notice for credential depreciation * 🎨 Refactor names for RLC * 🎨 Fixes to Deal * ✨ Inprove delete confirmation message * 🎨 Fixe titles of fields * 🎨 Fixe correct ressource hint * 🎨 Improve error when requesting non existing ressource * ⚡️ Improve get deal * ✨ Improve search by domain for companies * ✨ Improve getting recently created deals * 🎨 Improve versioning file structure * 🥅 Improve error for unknown ids * ✨ Add filters to recently search for companies * ✨ Uniformize including properties for contact * ✨ Improve get all operation for Companies * 🎨 Change filters name * 🎨 Remove useless descriptions * 🎨Improve description * ✨ Dynamically retrieve the correct operator for the properties * 🥅 Improve error handeling * 🥅 improve error when not finding the id * ⚡️Add searchable feature to Deal and contacts * 🚨 Fix linting issues * 🚨 Fix linting issues for V1 Hubspot * 🐛 Fix pairedItem for v2 hubspot * 🚨 Fix linting in HubSpot Description * 🚨 More linting fixes * Fix error handling * Update endpoint for deleting object * Fix label for recent actions * 🐛 fix listMemberships typo in V1 and V2 * docs: Stop assuming the user's gender in code comments (after merge)c87262a312
* fixing V1 GenericFunctions.ts api query limit as number * Update description for ticket * Add type to RLC * Revert "Add type to RLC" This reverts commit578dacfb97
. * Add type to RLC engagement --------- Co-authored-by: Marcus <marcus@n8n.io>
442 lines
13 KiB
TypeScript
442 lines
13 KiB
TypeScript
import type {
|
|
IHookFunctions,
|
|
IWebhookFunctions,
|
|
IDataObject,
|
|
ILoadOptionsFunctions,
|
|
INodePropertyOptions,
|
|
INodeType,
|
|
INodeTypeDescription,
|
|
IWebhookResponseData,
|
|
} from 'n8n-workflow';
|
|
import { NodeOperationError } from 'n8n-workflow';
|
|
|
|
import { hubspotApiRequest, propertyEvents } from './V1/GenericFunctions';
|
|
|
|
import { createHash } from 'crypto';
|
|
|
|
export class HubspotTrigger implements INodeType {
|
|
description: INodeTypeDescription = {
|
|
displayName: 'HubSpot Trigger',
|
|
name: 'hubspotTrigger',
|
|
icon: 'file:hubspot.svg',
|
|
group: ['trigger'],
|
|
version: 1,
|
|
description: 'Starts the workflow when HubSpot events occur',
|
|
defaults: {
|
|
name: 'HubSpot Trigger',
|
|
},
|
|
inputs: [],
|
|
outputs: ['main'],
|
|
credentials: [
|
|
{
|
|
name: 'hubspotDeveloperApi',
|
|
required: true,
|
|
},
|
|
],
|
|
webhooks: [
|
|
{
|
|
name: 'default',
|
|
httpMethod: 'POST',
|
|
responseMode: 'onReceived',
|
|
path: 'webhook',
|
|
},
|
|
{
|
|
name: 'setup',
|
|
httpMethod: 'GET',
|
|
responseMode: 'onReceived',
|
|
path: 'webhook',
|
|
},
|
|
],
|
|
properties: [
|
|
{
|
|
displayName: 'Events',
|
|
name: 'eventsUi',
|
|
type: 'fixedCollection',
|
|
typeOptions: {
|
|
multipleValues: true,
|
|
},
|
|
placeholder: 'Add Event',
|
|
default: {},
|
|
options: [
|
|
{
|
|
displayName: 'Event',
|
|
name: 'eventValues',
|
|
values: [
|
|
{
|
|
displayName: 'Name',
|
|
name: 'name',
|
|
type: 'options',
|
|
options: [
|
|
{
|
|
name: 'Company Created',
|
|
value: 'company.creation',
|
|
description:
|
|
"To get notified if any company is created in a customer's account",
|
|
},
|
|
{
|
|
name: 'Company Deleted',
|
|
value: 'company.deletion',
|
|
description:
|
|
"To get notified if any company is deleted in a customer's account",
|
|
},
|
|
{
|
|
name: 'Company Property Changed',
|
|
value: 'company.propertyChange',
|
|
description:
|
|
"To get notified if a specified property is changed for any company in a customer's account",
|
|
},
|
|
{
|
|
name: 'Contact Created',
|
|
value: 'contact.creation',
|
|
description:
|
|
"To get notified if any contact is created in a customer's account",
|
|
},
|
|
{
|
|
name: 'Contact Deleted',
|
|
value: 'contact.deletion',
|
|
description:
|
|
"To get notified if any contact is deleted in a customer's account",
|
|
},
|
|
{
|
|
name: 'Contact Privacy Deleted',
|
|
value: 'contact.privacyDeletion',
|
|
description:
|
|
'To get notified if a contact is deleted for privacy compliance reasons',
|
|
},
|
|
{
|
|
name: 'Contact Property Changed',
|
|
value: 'contact.propertyChange',
|
|
description:
|
|
"To get notified if a specified property is changed for any contact in a customer's account",
|
|
},
|
|
{
|
|
name: 'Conversation Creation',
|
|
value: 'conversation.creation',
|
|
description: 'To get notified if a new thread is created in an account',
|
|
},
|
|
{
|
|
name: 'Conversation Deletion',
|
|
value: 'conversation.deletion',
|
|
description:
|
|
'To get notified if a thread is archived or soft-deleted in an account',
|
|
},
|
|
{
|
|
name: 'Conversation New Message',
|
|
value: 'conversation.newMessage',
|
|
description: 'To get notified if a new message on a thread has been received',
|
|
},
|
|
{
|
|
name: 'Conversation Privacy Deletion',
|
|
value: 'conversation.privacyDeletion',
|
|
description: 'To get notified if a thread is permanently deleted in an account',
|
|
},
|
|
{
|
|
name: 'Conversation Property Change',
|
|
value: 'conversation.propertyChange',
|
|
description: 'To get notified if a property on a thread has been changed',
|
|
},
|
|
{
|
|
name: 'Deal Created',
|
|
value: 'deal.creation',
|
|
description: "To get notified if any deal is created in a customer's account",
|
|
},
|
|
{
|
|
name: 'Deal Deleted',
|
|
value: 'deal.deletion',
|
|
description: "To get notified if any deal is deleted in a customer's account",
|
|
},
|
|
{
|
|
name: 'Deal Property Changed',
|
|
value: 'deal.propertyChange',
|
|
description:
|
|
"To get notified if a specified property is changed for any deal in a customer's account",
|
|
},
|
|
],
|
|
default: 'contact.creation',
|
|
required: true,
|
|
},
|
|
{
|
|
displayName: 'Property Name or ID',
|
|
name: 'property',
|
|
type: 'options',
|
|
description:
|
|
'Choose from the list, or specify an ID using an <a href="https://docs.n8n.io/code-examples/expressions/">expression</a>',
|
|
typeOptions: {
|
|
loadOptionsDependsOn: ['contact.propertyChange'],
|
|
loadOptionsMethod: 'getContactProperties',
|
|
},
|
|
displayOptions: {
|
|
show: {
|
|
name: ['contact.propertyChange'],
|
|
},
|
|
},
|
|
default: '',
|
|
required: true,
|
|
},
|
|
{
|
|
displayName: 'Property Name or ID',
|
|
name: 'property',
|
|
type: 'options',
|
|
description:
|
|
'Choose from the list, or specify an ID using an <a href="https://docs.n8n.io/code-examples/expressions/">expression</a>',
|
|
typeOptions: {
|
|
loadOptionsDependsOn: ['company.propertyChange'],
|
|
loadOptionsMethod: 'getCompanyProperties',
|
|
},
|
|
displayOptions: {
|
|
show: {
|
|
name: ['company.propertyChange'],
|
|
},
|
|
},
|
|
default: '',
|
|
required: true,
|
|
},
|
|
{
|
|
displayName: 'Property Name or ID',
|
|
name: 'property',
|
|
type: 'options',
|
|
description:
|
|
'Choose from the list, or specify an ID using an <a href="https://docs.n8n.io/code-examples/expressions/">expression</a>',
|
|
typeOptions: {
|
|
loadOptionsDependsOn: ['deal.propertyChange'],
|
|
loadOptionsMethod: 'getDealProperties',
|
|
},
|
|
displayOptions: {
|
|
show: {
|
|
name: ['deal.propertyChange'],
|
|
},
|
|
},
|
|
default: '',
|
|
required: true,
|
|
},
|
|
],
|
|
},
|
|
],
|
|
},
|
|
{
|
|
displayName: 'Additional Fields',
|
|
name: 'additionalFields',
|
|
type: 'collection',
|
|
placeholder: 'Add Field',
|
|
default: {},
|
|
options: [
|
|
{
|
|
displayName: 'Max Concurrent Requests',
|
|
name: 'maxConcurrentRequests',
|
|
type: 'number',
|
|
typeOptions: {
|
|
minValue: 5,
|
|
},
|
|
default: 5,
|
|
},
|
|
],
|
|
},
|
|
],
|
|
};
|
|
|
|
methods = {
|
|
loadOptions: {
|
|
// Get all the available contacts to display them to user so that they can
|
|
// select them easily
|
|
async getContactProperties(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
const returnData: INodePropertyOptions[] = [];
|
|
const endpoint = '/properties/v2/contacts/properties';
|
|
const properties = await hubspotApiRequest.call(this, 'GET', endpoint, {});
|
|
for (const property of properties) {
|
|
const propertyName = property.label;
|
|
const propertyId = property.name;
|
|
returnData.push({
|
|
name: propertyName,
|
|
value: propertyId,
|
|
});
|
|
}
|
|
return returnData;
|
|
},
|
|
// Get all the available companies to display them to user so that they can
|
|
// select them easily
|
|
async getCompanyProperties(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
const returnData: INodePropertyOptions[] = [];
|
|
const endpoint = '/properties/v2/companies/properties';
|
|
const properties = await hubspotApiRequest.call(this, 'GET', endpoint, {});
|
|
for (const property of properties) {
|
|
const propertyName = property.label;
|
|
const propertyId = property.name;
|
|
returnData.push({
|
|
name: propertyName,
|
|
value: propertyId,
|
|
});
|
|
}
|
|
return returnData;
|
|
},
|
|
// Get all the available deals to display them to user so that they can
|
|
// select them easily
|
|
async getDealProperties(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
const returnData: INodePropertyOptions[] = [];
|
|
const endpoint = '/properties/v2/deals/properties';
|
|
const properties = await hubspotApiRequest.call(this, 'GET', endpoint, {});
|
|
for (const property of properties) {
|
|
const propertyName = property.label;
|
|
const propertyId = property.name;
|
|
returnData.push({
|
|
name: propertyName,
|
|
value: propertyId,
|
|
});
|
|
}
|
|
return returnData;
|
|
},
|
|
},
|
|
};
|
|
|
|
webhookMethods = {
|
|
default: {
|
|
async checkExists(this: IHookFunctions): Promise<boolean> {
|
|
// Check all the webhooks which exist already if it is identical to the
|
|
// one that is supposed to get created.
|
|
const currentWebhookUrl = this.getNodeWebhookUrl('default') as string;
|
|
const { appId } = await this.getCredentials('hubspotDeveloperApi');
|
|
|
|
try {
|
|
const { targetUrl } = await hubspotApiRequest.call(
|
|
this,
|
|
'GET',
|
|
`/webhooks/v3/${appId}/settings`,
|
|
{},
|
|
);
|
|
if (targetUrl !== currentWebhookUrl) {
|
|
throw new NodeOperationError(
|
|
this.getNode(),
|
|
`The APP ID ${appId} already has a target url ${targetUrl}. Delete it or use another APP ID before executing the trigger. Due to Hubspot API limitations, you can have just one trigger per APP.`,
|
|
);
|
|
}
|
|
} catch (error) {
|
|
if (error.statusCode === 404) {
|
|
return false;
|
|
}
|
|
}
|
|
// if the app is using the current webhook url. Delete everything and create it again with the current events
|
|
|
|
const { results: subscriptions } = await hubspotApiRequest.call(
|
|
this,
|
|
'GET',
|
|
`/webhooks/v3/${appId}/subscriptions`,
|
|
{},
|
|
);
|
|
|
|
// delete all subscriptions
|
|
for (const subscription of subscriptions) {
|
|
await hubspotApiRequest.call(
|
|
this,
|
|
'DELETE',
|
|
`/webhooks/v3/${appId}/subscriptions/${subscription.id}`,
|
|
{},
|
|
);
|
|
}
|
|
|
|
await hubspotApiRequest.call(this, 'DELETE', `/webhooks/v3/${appId}/settings`, {});
|
|
|
|
return false;
|
|
},
|
|
async create(this: IHookFunctions): Promise<boolean> {
|
|
const webhookUrl = this.getNodeWebhookUrl('default');
|
|
const { appId } = await this.getCredentials('hubspotDeveloperApi');
|
|
const events =
|
|
((this.getNodeParameter('eventsUi') as IDataObject)?.eventValues as IDataObject[]) || [];
|
|
const additionalFields = this.getNodeParameter('additionalFields') as IDataObject;
|
|
let endpoint = `/webhooks/v3/${appId}/settings`;
|
|
let body: IDataObject = {
|
|
targetUrl: webhookUrl,
|
|
maxConcurrentRequests: additionalFields.maxConcurrentRequests || 5,
|
|
};
|
|
|
|
await hubspotApiRequest.call(this, 'PUT', endpoint, body);
|
|
|
|
endpoint = `/webhooks/v3/${appId}/subscriptions`;
|
|
|
|
if (Array.isArray(events) && events.length === 0) {
|
|
throw new NodeOperationError(this.getNode(), 'You must define at least one event');
|
|
}
|
|
|
|
for (const event of events) {
|
|
body = {
|
|
eventType: event.name,
|
|
active: true,
|
|
};
|
|
if (propertyEvents.includes(event.name as string)) {
|
|
const property = event.property;
|
|
body.propertyName = property;
|
|
}
|
|
await hubspotApiRequest.call(this, 'POST', endpoint, body);
|
|
}
|
|
|
|
return true;
|
|
},
|
|
async delete(this: IHookFunctions): Promise<boolean> {
|
|
const { appId } = await this.getCredentials('hubspotDeveloperApi');
|
|
|
|
const { results: subscriptions } = await hubspotApiRequest.call(
|
|
this,
|
|
'GET',
|
|
`/webhooks/v3/${appId}/subscriptions`,
|
|
{},
|
|
);
|
|
|
|
for (const subscription of subscriptions) {
|
|
await hubspotApiRequest.call(
|
|
this,
|
|
'DELETE',
|
|
`/webhooks/v3/${appId}/subscriptions/${subscription.id}`,
|
|
{},
|
|
);
|
|
}
|
|
|
|
try {
|
|
await hubspotApiRequest.call(this, 'DELETE', `/webhooks/v3/${appId}/settings`, {});
|
|
} catch (error) {
|
|
return false;
|
|
}
|
|
return true;
|
|
},
|
|
},
|
|
};
|
|
|
|
async webhook(this: IWebhookFunctions): Promise<IWebhookResponseData> {
|
|
const credentials = await this.getCredentials('hubspotDeveloperApi');
|
|
|
|
if (credentials === undefined) {
|
|
throw new NodeOperationError(this.getNode(), 'No credentials found!');
|
|
}
|
|
|
|
const req = this.getRequestObject();
|
|
const bodyData = req.body;
|
|
const headerData = this.getHeaderData();
|
|
//@ts-ignore
|
|
if (headerData['x-hubspot-signature'] === undefined) {
|
|
return {};
|
|
}
|
|
|
|
const hash = `${credentials.clientSecret}${JSON.stringify(bodyData)}`;
|
|
const signature = createHash('sha256').update(hash).digest('hex');
|
|
//@ts-ignore
|
|
if (signature !== headerData['x-hubspot-signature']) {
|
|
return {};
|
|
}
|
|
|
|
for (let i = 0; i < bodyData.length; i++) {
|
|
const subscriptionType = bodyData[i].subscriptionType as string;
|
|
if (subscriptionType.includes('contact')) {
|
|
bodyData[i].contactId = bodyData[i].objectId;
|
|
}
|
|
if (subscriptionType.includes('company')) {
|
|
bodyData[i].companyId = bodyData[i].objectId;
|
|
}
|
|
if (subscriptionType.includes('deal')) {
|
|
bodyData[i].dealId = bodyData[i].objectId;
|
|
}
|
|
delete bodyData[i].objectId;
|
|
}
|
|
return {
|
|
workflowData: [this.helpers.returnJsonArray(bodyData as IDataObject[])],
|
|
};
|
|
}
|
|
}
|