UNPKG

n8n-nodes-cativa

Version:

This is an n8n community node to communicate with Cativa.

198 lines 8.04 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.CativaTrigger = void 0; const descriptions_1 = require("./descriptions"); const methods_1 = require("./methods"); const GenericFunctions_1 = require("./GenericFunctions"); const moment_1 = __importDefault(require("moment")); class CativaTrigger { constructor() { this.description = { displayName: 'Cativa Trigger', name: 'cativaTrigger', icon: 'file:cativa.png', group: ['trigger'], polling: true, version: 1, subtitle: '={{$parameter["event"]}}', description: 'Start workflow when an event occurs', defaults: { name: 'Cativa Trigger', }, inputs: [], outputs: ['main'], credentials: [ { name: 'cativaApi', required: true, }, ], properties: [ { displayName: 'Resource', name: 'resource', type: 'options', noDataExpression: true, options: [ { name: 'User', value: 'user', }, { name: 'Group', value: 'group', }, ], default: 'user', required: true, }, { displayName: 'Initial Data', name: 'fetchType', type: 'options', noDataExpression: true, default: 'fromNow', options: [ { name: 'From Now', value: 'fromNow', }, { name: 'From ID', value: 'fromId', }, { name: 'All', value: 'all', }, ], required: true, }, { displayName: 'From ID', name: 'fromId', type: 'string', noDataExpression: true, default: '', displayOptions: { show: { fetchType: ['fromId'], }, }, required: true, }, ...descriptions_1.userTriggerDescription, ...descriptions_1.groupTriggerDescription, ], }; this.methods = { credentialTest: { async cativaCredentialTest(credential) { const credentials = credential.data; const options = (0, GenericFunctions_1.requestAccountOptions)(credentials); try { await this.helpers.request(options); return { status: 'OK', message: 'Authentication successful', }; } catch (error) { return { status: 'Error', message: error.message, }; } }, }, }; } async poll() { var _a; const webhookData = this.getWorkflowStaticData('node'); const resource = this.getNodeParameter('resource', 0); const operation = this.getNodeParameter('operation', 0); const fetchType = this.getNodeParameter('fetchType'); let { lastIdChecked } = webhookData; const { lastCreatedAtChecked: _lastCreatedAtChecked } = webhookData; console.log({ _lastCreatedAtChecked, lastIdChecked }); let lastCreatedAtChecked = (0, moment_1.default)(); if (!_lastCreatedAtChecked) { if (fetchType === 'fromNow') { lastCreatedAtChecked = (0, moment_1.default)(); } else if (fetchType === 'all' || fetchType === 'fromId') { lastCreatedAtChecked = (0, moment_1.default)().subtract(1, 'y'); } } else { lastCreatedAtChecked = (0, moment_1.default)(_lastCreatedAtChecked); } if (!lastIdChecked && fetchType === 'fromId' && this.getNodeParameter('fromId')) { lastIdChecked = this.getNodeParameter('fromId'); } console.log({ lastCreatedAtChecked }); const records = []; let page = 1; let hasNextPage = true; let finished = false; let response; if (resource === 'user') { response = await methods_1.resourceTriggerUser.call(this, operation, page); } else if (resource === 'group') { response = await methods_1.resourceTriggerGroup.call(this, operation, page); } const { items: data } = response.body; if (this.getMode() === 'manual') { if (Array.isArray(data) && data.length) { return [this.helpers.returnJsonArray(data.slice(0, 1))]; } } if (Array.isArray(data) && data.length && Object.keys(data[0]).length !== 0) { do { console.log('do work', page); let response; if (resource === 'user') { response = await methods_1.resourceTriggerUser.call(this, operation, page); } else if (resource === 'group') { response = await methods_1.resourceTriggerGroup.call(this, operation, page); } const { items, hasNextPage: hasNext } = response .body; items.every((item) => { var _a, _b, _c; if (((_a = item.id) === null || _a === void 0 ? void 0 : _a.toLowerCase()) === lastIdChecked || ((_c = (_b = item.shortId) === null || _b === void 0 ? void 0 : _b.guid) === null || _c === void 0 ? void 0 : _c.toLowerCase()) === lastIdChecked || (lastCreatedAtChecked && (0, moment_1.default)(item.createdAt) < lastCreatedAtChecked)) { console.log('finished'); finished = true; return false; } console.log('item', { item, hasNextPage }); records.push(item); return true; }); hasNextPage = hasNext; page += 1; } while (!finished && hasNextPage); if (records[0]) { webhookData.lastIdChecked = records[0].id; webhookData.lastCreatedAtChecked = (_a = records[0].createdAt) === null || _a === void 0 ? void 0 : _a.toLowerCase(); } else { webhookData.lastCreatedAtChecked = lastCreatedAtChecked.toDate(); } if (Array.isArray(records) && records.length) { return [this.helpers.returnJsonArray(records.reverse())]; } } return null; } } exports.CativaTrigger = CativaTrigger; //# sourceMappingURL=CativaTrigger.node.js.map