@poli-digital/n8n-nodes-poli
Version:
Nó para interagir com a API da Poli
102 lines • 4.05 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.CreateWebhook = void 0;
const n8n_workflow_1 = require("n8n-workflow");
const transport_1 = require("./transport");
class CreateWebhook {
constructor() {
this.description = {
displayName: 'Create Webhook',
name: 'createWebhook',
icon: 'file:poli.svg',
group: ['output'],
version: 1,
description: 'Create a new webhook',
defaults: {
name: 'Create Webhook',
},
inputs: ['main'],
outputs: ['main'],
credentials: [
{
name: 'poliApi',
required: true,
},
],
properties: [
{
displayName: 'Application ID',
name: 'applicationId',
type: 'string',
default: '',
required: true,
},
{
displayName: 'Webhook URL',
name: 'url',
type: 'string',
default: '',
required: true,
},
{
displayName: 'Subscriptions',
name: 'subscriptions',
type: 'multiOptions',
options: [
{ name: 'Organizations', value: 'organizations' },
{ name: 'Chats', value: 'chats' },
{ name: 'Contacts', value: 'contacts' },
{ name: 'Users', value: 'users' },
{ name: 'Applications', value: 'applications' },
],
default: [],
required: true,
},
],
};
}
async execute() {
var _a;
const items = this.getInputData();
const returnData = [];
for (let i = 0; i < items.length; i++) {
try {
const applicationId = this.getNodeParameter('applicationId', i);
const url = this.getNodeParameter('url', i);
const subscriptions = this.getNodeParameter('subscriptions', i);
try {
new URL(url);
}
catch (error) {
throw new Error('Invalid webhook URL. Please provide a valid URL.');
}
if (!subscriptions || subscriptions.length === 0) {
throw new Error('At least one subscription must be selected.');
}
const validSubscriptions = ['organizations', 'chats', 'contacts', 'users', 'applications'];
const invalidSubscriptions = subscriptions.filter(s => !validSubscriptions.includes(s));
if (invalidSubscriptions.length > 0) {
throw new Error(`Invalid subscriptions: ${invalidSubscriptions.join(', ')}`);
}
const body = { url, subscriptions };
const endpoint = `/applications/${applicationId}/webhooks?include=url,subscriptions`;
try {
const responseData = await transport_1.apiRequest.call(this, 'POST', endpoint, body);
returnData.push({ json: responseData });
}
catch (error) {
if ((_a = error.message) === null || _a === void 0 ? void 0 : _a.includes('already exists')) {
throw new Error('A webhook with this URL already exists for this application.');
}
throw error;
}
}
catch (error) {
throw new n8n_workflow_1.NodeApiError(this.getNode(), error);
}
}
return [returnData];
}
}
exports.CreateWebhook = CreateWebhook;
//# sourceMappingURL=CreateWebhook.operation.js.map