UNPKG

n8n-nodes-base

Version:

Base nodes of n8n

158 lines 5.51 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CopperTrigger = void 0; const n8n_workflow_1 = require("n8n-workflow"); const GenericFunctions_1 = require("./GenericFunctions"); class CopperTrigger { description = { 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: [n8n_workflow_1.NodeConnectionTypes.Main], credentials: [ { name: 'copperApi', required: true, }, ], webhooks: [ { name: 'default', httpMethod: 'POST', responseMode: 'onReceived', path: 'webhook', }, ], properties: [ { displayName: 'Resource', name: 'resource', type: 'options', noDataExpression: true, 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', }, ], }; webhookMethods = { default: { async checkExists() { const webhookData = this.getWorkflowStaticData('node'); if (webhookData.webhookId === undefined) { return false; } const endpoint = `/webhooks/${webhookData.webhookId}`; try { await GenericFunctions_1.copperApiRequest.call(this, 'GET', endpoint); } catch (error) { return false; } return true; }, async create() { const webhookUrl = this.getNodeWebhookUrl('default'); const webhookData = this.getWorkflowStaticData('node'); const resource = this.getNodeParameter('resource'); const event = this.getNodeParameter('event'); const endpoint = '/webhooks'; const body = { target: webhookUrl, type: resource, event, }; const credentials = await this.getCredentials('copperApi'); body.secret = { secret: (0, GenericFunctions_1.getAutomaticSecret)(credentials), }; const { id } = await GenericFunctions_1.copperApiRequest.call(this, 'POST', endpoint, body); webhookData.webhookId = id; return true; }, async delete() { const webhookData = this.getWorkflowStaticData('node'); const endpoint = `/webhooks/${webhookData.webhookId}`; try { await GenericFunctions_1.copperApiRequest.call(this, 'DELETE', endpoint); } catch (error) { return false; } delete webhookData.webhookId; return true; }, }, }; async webhook() { const credentials = await this.getCredentials('copperApi'); const req = this.getRequestObject(); // Check if the supplied secret matches. If not ignore request. if (req.body.secret !== (0, GenericFunctions_1.getAutomaticSecret)(credentials)) { return {}; } return { workflowData: [this.helpers.returnJsonArray(req.body)], }; } } exports.CopperTrigger = CopperTrigger; //# sourceMappingURL=CopperTrigger.node.js.map