n8n/packages/nodes-base/nodes/Copper/CopperTrigger.node.ts
Iván Ovejero 63b6c9f128
refactor: Apply more eslint-plugin-n8n-nodes-base autofixable rules (#3243)
* ✏️ Alphabetize rules

* 🔖 Update version

*  Update lintfix command

*  Run baseline lintfix

* 📦 Update package-lock.json

* 👕 Apply `node-param-description-untrimmed` (#3200)

* Removing unneeded backticks (#3249)

* 👕 Apply node-param-description-wrong-for-return-all (#3253)

* 👕 Apply node-param-description-missing-limit (#3252)

Co-authored-by: Iván Ovejero <ivov.src@gmail.com>

* 👕 Apply node-param-description-excess-final-period (#3250)

Co-authored-by: Iván Ovejero <ivov.src@gmail.com>

* 👕 Apply node-param-description-unencoded-angle-brackets (#3256)

Co-authored-by: Iván Ovejero <ivov.src@gmail.com>

* 👕 Apply node-param-description-url-missing-protocol (#3258)

* 👕 Apply `node-param-description-miscased-id` (#3254)

Co-authored-by: Iván Ovejero <ivov.src@gmail.com>

* 👕 Apply node-param-description-wrong-for-limit (#3257)

Co-authored-by: Iván Ovejero <ivov.src@gmail.com>

* 👕 Apply node-param-description-wrong-for-ignore-ssl-issues (#3261)

* 👕 Apply rule

*  Restore lintfix script

*  Restore lintfix script

Co-authored-by: agobrech <45268029+agobrech@users.noreply.github.com>
Co-authored-by: Omar Ajoue <krynble@gmail.com>
Co-authored-by: Michael Kret <88898367+michael-radency@users.noreply.github.com>
2022-05-06 23:01:25 +02:00

174 lines
3.7 KiB
TypeScript

import {
IHookFunctions,
IWebhookFunctions,
} from 'n8n-core';
import {
IDataObject,
INodeType,
INodeTypeDescription,
IWebhookResponseData,
} from 'n8n-workflow';
import {
copperApiRequest,
getAutomaticSecret,
} from './GenericFunctions';
export class CopperTrigger implements INodeType {
description: INodeTypeDescription = {
displayName: 'Copper Trigger',
name: 'copperTrigger',
icon: 'file:copper.svg',
group: ['trigger'],
version: 1,
description: 'Handle Copper events via webhooks',
defaults: {
name: 'Copper Trigger',
},
inputs: [],
outputs: ['main'],
credentials: [
{
name: 'copperApi',
required: true,
},
],
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',
},
{
name: 'Opportunity',
value: 'opportunity',
},
{
name: 'Person',
value: 'person',
},
{
name: 'Project',
value: 'project',
},
{
name: 'Task',
value: 'task',
},
],
description: 'The resource which will fire the event',
},
{
displayName: 'Event',
name: 'event',
type: 'options',
required: true,
default: '',
options: [
{
name: 'Delete',
value: 'delete',
description: 'An existing record is removed',
},
{
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',
},
],
};
// @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 (error) {
return false;
}
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 = await this.getCredentials('copperApi');
body.secret = {
secret: getAutomaticSecret(credentials),
};
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) {
return false;
}
delete webhookData.webhookId;
return true;
},
},
};
async webhook(this: IWebhookFunctions): Promise<IWebhookResponseData> {
const credentials = await this.getCredentials('copperApi');
const req = this.getRequestObject();
// Check if the supplied secret matches. If not ignore request.
if (req.body.secret !== getAutomaticSecret(credentials)) {
return {};
}
return {
workflowData: [
this.helpers.returnJsonArray(req.body),
],
};
}
}