n8n/packages/nodes-base/nodes/Copper/CopperTrigger.node.ts

175 lines
3.8 KiB
TypeScript
Raw Normal View History

2020-01-17 14:48:54 -08:00
import {
IHookFunctions,
IWebhookFunctions,
} from 'n8n-core';
import {
IDataObject,
INodeType,
INodeTypeDescription,
2020-01-17 14:48:54 -08:00
IWebhookResponseData,
} from 'n8n-workflow';
import {
copperApiRequest,
getAutomaticSecret,
2020-01-17 14:48:54 -08:00
} from './GenericFunctions';
export class CopperTrigger implements INodeType {
description: INodeTypeDescription = {
displayName: 'Copper Trigger',
name: 'copperTrigger',
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',
color: '#ff2564',
},
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',
required: true,
default: '',
options: [
{
name: 'Company',
value: 'company',
},
{
name: 'Lead',
value: 'lead',
},
2020-01-17 14:48:54 -08:00
{
name: 'Opportunity',
value: 'opportunity',
},
{
name: 'Person',
value: 'person',
},
2020-01-17 14:48:54 -08:00
{
name: 'Project',
value: 'project',
},
{
name: 'Task',
value: 'task',
},
2020-01-17 14:48:54 -08: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: [
{
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',
},
],
description: 'The event to listen to.',
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);
} catch (err) {
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,
event,
};
const credentials = this.getCredentials('copperApi');
body.secret = {
secret: getAutomaticSecret(credentials!),
2020-01-17 14:48:54 -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);
} catch (error) {
2020-01-17 14:48:54 -08:00
return false;
}
delete webhookData.webhookId;
return true;
},
},
};
async webhook(this: IWebhookFunctions): Promise<IWebhookResponseData> {
const credentials = this.getCredentials('copperApi');
const req = this.getRequestObject();
// Check if the supplied secret matches. If not ignore request.
if (req.body.secret !== getAutomaticSecret(credentials!)) {
return {};
2020-01-17 14:48:54 -08:00
}
2020-01-17 14:48:54 -08:00
return {
workflowData: [
this.helpers.returnJsonArray(req.body),
],
};
}
}