2023-01-27 03:22:44 -08:00
|
|
|
import type { IHookFunctions, IWebhookFunctions } from 'n8n-core';
|
2020-01-17 14:48:54 -08:00
|
|
|
|
2023-01-27 03:22:44 -08:00
|
|
|
import type {
|
|
|
|
IDataObject,
|
|
|
|
INodeType,
|
|
|
|
INodeTypeDescription,
|
|
|
|
IWebhookResponseData,
|
|
|
|
} from 'n8n-workflow';
|
2020-01-17 14:48:54 -08:00
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
import { copperApiRequest, getAutomaticSecret } from './GenericFunctions';
|
2020-01-17 14:48:54 -08:00
|
|
|
|
|
|
|
export class CopperTrigger implements INodeType {
|
|
|
|
description: INodeTypeDescription = {
|
|
|
|
displayName: 'Copper Trigger',
|
2020-01-18 07:28:19 -08:00
|
|
|
name: 'copperTrigger',
|
2021-04-02 10:12:19 -07:00
|
|
|
icon: 'file:copper.svg',
|
2020-01-17 14:48:54 -08:00
|
|
|
group: ['trigger'],
|
|
|
|
version: 1,
|
|
|
|
description: 'Handle Copper events via webhooks',
|
|
|
|
defaults: {
|
|
|
|
name: 'Copper Trigger',
|
|
|
|
},
|
|
|
|
inputs: [],
|
|
|
|
outputs: ['main'],
|
|
|
|
credentials: [
|
|
|
|
{
|
|
|
|
name: 'copperApi',
|
|
|
|
required: true,
|
2020-10-22 06:46:03 -07:00
|
|
|
},
|
2020-01-17 14:48:54 -08:00
|
|
|
],
|
|
|
|
webhooks: [
|
|
|
|
{
|
|
|
|
name: 'default',
|
|
|
|
httpMethod: 'POST',
|
|
|
|
responseMode: 'onReceived',
|
|
|
|
path: 'webhook',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
properties: [
|
|
|
|
{
|
|
|
|
displayName: 'Resource',
|
|
|
|
name: 'resource',
|
|
|
|
type: 'options',
|
2022-05-20 14:47:24 -07:00
|
|
|
noDataExpression: true,
|
2020-01-17 14:48:54 -08:00
|
|
|
required: true,
|
|
|
|
default: '',
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'Company',
|
|
|
|
value: 'company',
|
|
|
|
},
|
2020-01-18 07:28:19 -08:00
|
|
|
{
|
|
|
|
name: 'Lead',
|
|
|
|
value: 'lead',
|
|
|
|
},
|
2020-01-17 14:48:54 -08:00
|
|
|
{
|
|
|
|
name: 'Opportunity',
|
|
|
|
value: 'opportunity',
|
|
|
|
},
|
2020-01-18 07:28:19 -08:00
|
|
|
{
|
|
|
|
name: 'Person',
|
|
|
|
value: 'person',
|
|
|
|
},
|
2020-01-17 14:48:54 -08:00
|
|
|
{
|
|
|
|
name: 'Project',
|
|
|
|
value: 'project',
|
|
|
|
},
|
2020-01-18 07:28:19 -08:00
|
|
|
{
|
|
|
|
name: 'Task',
|
|
|
|
value: 'task',
|
|
|
|
},
|
2020-01-17 14:48:54 -08:00
|
|
|
],
|
2022-05-06 14:01:25 -07:00
|
|
|
description: 'The resource which will fire the event',
|
2020-01-17 14:48:54 -08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Event',
|
|
|
|
name: 'event',
|
|
|
|
type: 'options',
|
|
|
|
required: true,
|
|
|
|
default: '',
|
|
|
|
options: [
|
2020-01-18 07:28:19 -08:00
|
|
|
{
|
|
|
|
name: 'Delete',
|
|
|
|
value: 'delete',
|
|
|
|
description: 'An existing record is removed',
|
|
|
|
},
|
2020-01-17 14:48:54 -08:00
|
|
|
{
|
|
|
|
name: 'New',
|
|
|
|
value: 'new',
|
|
|
|
description: 'A new record is created',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Update',
|
|
|
|
value: 'update',
|
|
|
|
description: 'Any field in the existing entity record is changed',
|
|
|
|
},
|
|
|
|
],
|
2022-05-06 14:01:25 -07:00
|
|
|
description: 'The event to listen to',
|
2020-01-17 14:48:54 -08:00
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
2022-12-02 12:54:28 -08:00
|
|
|
|
2020-01-17 14:48:54 -08:00
|
|
|
// @ts-ignore
|
|
|
|
webhookMethods = {
|
|
|
|
default: {
|
|
|
|
async checkExists(this: IHookFunctions): Promise<boolean> {
|
|
|
|
const webhookData = this.getWorkflowStaticData('node');
|
|
|
|
if (webhookData.webhookId === undefined) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const endpoint = `/webhooks/${webhookData.webhookId}`;
|
|
|
|
try {
|
|
|
|
await copperApiRequest.call(this, 'GET', endpoint);
|
2021-04-16 09:33:36 -07:00
|
|
|
} catch (error) {
|
2020-01-18 07:28:19 -08:00
|
|
|
return false;
|
2020-01-17 14:48:54 -08:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
async create(this: IHookFunctions): Promise<boolean> {
|
|
|
|
const webhookUrl = this.getNodeWebhookUrl('default');
|
|
|
|
const webhookData = this.getWorkflowStaticData('node');
|
|
|
|
const resource = this.getNodeParameter('resource') as string;
|
|
|
|
const event = this.getNodeParameter('event') as string;
|
|
|
|
const endpoint = '/webhooks';
|
|
|
|
const body: IDataObject = {
|
|
|
|
target: webhookUrl,
|
|
|
|
type: resource,
|
2020-01-18 07:28:19 -08:00
|
|
|
event,
|
|
|
|
};
|
|
|
|
|
2021-08-20 09:57:30 -07:00
|
|
|
const credentials = await this.getCredentials('copperApi');
|
2020-01-18 07:28:19 -08:00
|
|
|
body.secret = {
|
2022-04-14 23:00:47 -07:00
|
|
|
secret: getAutomaticSecret(credentials),
|
2020-01-17 14:48:54 -08:00
|
|
|
};
|
2020-01-18 07:28:19 -08:00
|
|
|
|
2020-01-17 14:48:54 -08:00
|
|
|
const { id } = await copperApiRequest.call(this, 'POST', endpoint, body);
|
|
|
|
webhookData.webhookId = id;
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
async delete(this: IHookFunctions): Promise<boolean> {
|
|
|
|
const webhookData = this.getWorkflowStaticData('node');
|
|
|
|
const endpoint = `/webhooks/${webhookData.webhookId}`;
|
|
|
|
try {
|
|
|
|
await copperApiRequest.call(this, 'DELETE', endpoint);
|
2021-04-02 10:12:19 -07:00
|
|
|
} catch (error) {
|
2020-01-17 14:48:54 -08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
delete webhookData.webhookId;
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
async webhook(this: IWebhookFunctions): Promise<IWebhookResponseData> {
|
2021-08-20 09:57:30 -07:00
|
|
|
const credentials = await this.getCredentials('copperApi');
|
2020-01-17 14:48:54 -08:00
|
|
|
const req = this.getRequestObject();
|
2020-01-18 07:28:19 -08:00
|
|
|
|
|
|
|
// Check if the supplied secret matches. If not ignore request.
|
2022-04-14 23:00:47 -07:00
|
|
|
if (req.body.secret !== getAutomaticSecret(credentials)) {
|
2020-01-18 07:28:19 -08:00
|
|
|
return {};
|
2020-01-17 14:48:54 -08:00
|
|
|
}
|
2020-01-18 07:28:19 -08:00
|
|
|
|
2020-01-17 14:48:54 -08:00
|
|
|
return {
|
2023-02-27 19:39:43 -08:00
|
|
|
workflowData: [this.helpers.returnJsonArray(req.body as IDataObject)],
|
2020-01-17 14:48:54 -08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|