UNPKG

n8n-nodes-base

Version:

Base nodes of n8n

164 lines 6.52 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.WiseTrigger = void 0; const crypto_1 = require("crypto"); const n8n_workflow_1 = require("n8n-workflow"); const GenericFunctions_1 = require("./GenericFunctions"); class WiseTrigger { description = { displayName: 'Wise Trigger', name: 'wiseTrigger', icon: 'file:wise.svg', group: ['trigger'], version: 1, subtitle: '={{$parameter["event"]}}', description: 'Handle Wise events via webhooks', defaults: { name: 'Wise Trigger', }, inputs: [], outputs: [n8n_workflow_1.NodeConnectionTypes.Main], credentials: [ { name: 'wiseApi', required: true, }, ], webhooks: [ { name: 'default', httpMethod: 'POST', responseMode: 'onReceived', path: 'webhook', }, ], properties: [ { displayName: 'Profile Name or ID', name: 'profileId', type: 'options', description: 'Choose from the list, or specify an ID using an <a href="https://docs.n8n.io/code/expressions/">expression</a>', required: true, typeOptions: { loadOptionsMethod: 'getProfiles', }, default: '', }, { displayName: 'Event', name: 'event', type: 'options', required: true, default: '', options: [ { name: 'Balance Credit', value: 'balanceCredit', description: 'Triggered every time a balance account is credited', }, { name: 'Balance Update', value: 'balanceUpdate', description: 'Triggered every time a balance account is credited or debited', }, { name: 'Transfer Active Case', value: 'transferActiveCases', description: "Triggered every time a transfer's list of active cases is updated", }, { name: 'Transfer State Changed', value: 'tranferStateChange', description: "Triggered every time a transfer's status is updated", }, ], }, ], }; methods = { loadOptions: { async getProfiles() { const profiles = await GenericFunctions_1.wiseApiRequest.call(this, 'GET', 'v1/profiles'); return profiles.map(({ id, type }) => ({ name: type.charAt(0).toUpperCase() + type.slice(1), value: id, })); }, }, }; webhookMethods = { default: { async checkExists() { const webhookData = this.getWorkflowStaticData('node'); const webhookUrl = this.getNodeWebhookUrl('default'); const profileId = this.getNodeParameter('profileId'); const event = this.getNodeParameter('event'); const webhooks = await GenericFunctions_1.wiseApiRequest.call(this, 'GET', `v3/profiles/${profileId}/subscriptions`); const trigger = (0, GenericFunctions_1.getTriggerName)(event); for (const webhook of webhooks) { if (webhook.delivery.url === webhookUrl && webhook.scope.id === profileId && webhook.trigger_on === trigger) { webhookData.webhookId = webhook.id; return true; } } return false; }, async create() { const webhookUrl = this.getNodeWebhookUrl('default'); const webhookData = this.getWorkflowStaticData('node'); const profileId = this.getNodeParameter('profileId'); const event = this.getNodeParameter('event'); const trigger = (0, GenericFunctions_1.getTriggerName)(event); const body = { name: 'n8n Webhook', trigger_on: trigger, delivery: { version: '2.0.0', url: webhookUrl, }, }; const webhook = await GenericFunctions_1.wiseApiRequest.call(this, 'POST', `v3/profiles/${profileId}/subscriptions`, body); webhookData.webhookId = webhook.id; return true; }, async delete() { const webhookData = this.getWorkflowStaticData('node'); const profileId = this.getNodeParameter('profileId'); try { await GenericFunctions_1.wiseApiRequest.call(this, 'DELETE', `v3/profiles/${profileId}/subscriptions/${webhookData.webhookId}`); } catch (error) { return false; } delete webhookData.webhookId; return true; }, }, }; async webhook() { const req = this.getRequestObject(); const headers = this.getHeaderData(); const credentials = await this.getCredentials('wiseApi'); if (headers['x-test-notification'] === 'true') { const res = this.getResponseObject(); res.status(200).end(); return { noWebhookResponse: true, }; } const signature = headers['x-signature']; const publicKey = credentials.environment === 'test' ? GenericFunctions_1.testPublicKey : GenericFunctions_1.livePublicKey; const sig = (0, crypto_1.createVerify)('RSA-SHA1').update(req.rawBody); const verified = sig.verify(publicKey, signature, 'base64'); if (!verified) { return {}; } return { workflowData: [this.helpers.returnJsonArray(req.body)], }; } } exports.WiseTrigger = WiseTrigger; //# sourceMappingURL=WiseTrigger.node.js.map