@poli-digital/n8n-nodes-poli
Version:
Nó para interagir com a API da Poli
99 lines • 4.15 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.CreateWebhook = void 0;
const n8n_workflow_1 = require("n8n-workflow");
const transport_1 = require("./transport");
const parameterUtils_1 = require("./utils/parameterUtils");
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 UUID',
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 = (0, parameterUtils_1.getParameterSafe)(this, 'applicationId', i, '', true);
const url = (0, parameterUtils_1.getParameterSafe)(this, 'url', i, '', true);
const subscriptions = (0, parameterUtils_1.getParameterSafe)(this, 'subscriptions', i, [], true);
const validatedUrl = (0, parameterUtils_1.validateAndSanitizeUrl)(url, 'url');
(0, parameterUtils_1.validateRequiredArray)(subscriptions, 'subscriptions');
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: validatedUrl,
subscriptions: subscriptions.filter(sub => validSubscriptions.includes(sub)),
};
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