UNPKG

@skriptfabrik/n8n-nodes-clockify-enhanced

Version:
298 lines 13.7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ClockifyEnhancedTrigger = exports.WebhookEvent = void 0; const tslib_1 = require("tslib"); const GenericFunctions_1 = require("n8n-nodes-base/dist/nodes/Clockify/GenericFunctions"); const n8n_workflow_1 = require("n8n-workflow"); var WebhookEvent; (function (WebhookEvent) { WebhookEvent["APPROVAL_REQUEST_STATUS_UPDATED"] = "APPROVAL_REQUEST_STATUS_UPDATED"; WebhookEvent["BALANCE_UPDATED"] = "BALANCE_UPDATED"; WebhookEvent["INVOICE_UPDATED"] = "INVOICE_UPDATED"; WebhookEvent["NEW_APPROVAL_REQUEST"] = "NEW_APPROVAL_REQUEST"; WebhookEvent["NEW_CLIENT"] = "NEW_CLIENT"; WebhookEvent["NEW_INVOICE"] = "NEW_INVOICE"; WebhookEvent["NEW_PROJECT"] = "NEW_PROJECT"; WebhookEvent["NEW_TAG"] = "NEW_TAG"; WebhookEvent["NEW_TASK"] = "NEW_TASK"; WebhookEvent["NEW_TIME_ENTRY"] = "NEW_TIME_ENTRY"; WebhookEvent["NEW_TIMER_STARTED"] = "NEW_TIMER_STARTED"; WebhookEvent["TIME_ENTRY_DELETED"] = "TIME_ENTRY_DELETED"; WebhookEvent["TIME_ENTRY_UPDATED"] = "TIME_ENTRY_UPDATED"; WebhookEvent["TIME_OFF_REQUEST_APPROVED"] = "TIME_OFF_REQUEST_APPROVED"; WebhookEvent["TIME_OFF_REQUEST_REJECTED"] = "TIME_OFF_REQUEST_REJECTED"; WebhookEvent["TIME_OFF_REQUEST_WITHDRAWN"] = "TIME_OFF_REQUEST_WITHDRAWN"; WebhookEvent["TIME_OFF_REQUESTED"] = "TIME_OFF_REQUESTED"; WebhookEvent["TIMER_STOPPED"] = "TIMER_STOPPED"; WebhookEvent["USER_ACTIVATED_ON_WORKSPACE"] = "USER_ACTIVATED_ON_WORKSPACE"; WebhookEvent["USER_DEACTIVATED_ON_WORKSPACE"] = "USER_DEACTIVATED_ON_WORKSPACE"; WebhookEvent["USER_DELETED_FROM_WORKSPACE"] = "USER_DELETED_FROM_WORKSPACE"; WebhookEvent["USER_EMAIL_CHANGED"] = "USER_EMAIL_CHANGED"; WebhookEvent["USER_JOINED_WORKSPACE"] = "USER_JOINED_WORKSPACE"; WebhookEvent["USER_UPDATED"] = "USER_UPDATED"; })(WebhookEvent || (exports.WebhookEvent = WebhookEvent = {})); class ClockifyEnhancedTrigger { constructor() { this.description = { displayName: 'Clockify Enhanced Trigger', name: 'clockifyEnhancedTrigger', icon: 'file:icons/clockify-enhanced.svg', group: ['trigger'], version: 1, subtitle: '={{$parameter["name"]}}', description: 'Listen to Clockify events', defaults: { name: 'Clockify Enhanced Trigger', }, inputs: [], outputs: [n8n_workflow_1.NodeConnectionTypes.Main], credentials: [ { name: 'clockifyApi', required: true, }, ], webhooks: [ { name: 'default', httpMethod: 'POST', responseMode: 'onReceived', path: 'webhook', }, ], properties: [ { displayName: 'Workspace Name or ID', name: 'workspaceId', type: 'options', description: 'Choose from the list, or specify an ID using an <a href="https://docs.n8n.io/code/expressions/">expression</a>', typeOptions: { loadOptionsMethod: 'listWorkspaces', }, required: true, default: '', }, { displayName: 'Webhook Name', name: 'name', type: 'string', required: true, default: '', }, { displayName: 'Webhook Event', name: 'webhookEvent', type: 'options', options: [ { name: 'Approval Request Status Updated', value: WebhookEvent.APPROVAL_REQUEST_STATUS_UPDATED, }, { name: 'Balance Updated', value: WebhookEvent.BALANCE_UPDATED, }, { name: 'Client Created', value: WebhookEvent.NEW_CLIENT, }, { name: 'Invoice Created', value: WebhookEvent.NEW_INVOICE, }, { name: 'Invoice Updated', value: WebhookEvent.INVOICE_UPDATED, }, { name: 'New Approval Request', value: WebhookEvent.NEW_APPROVAL_REQUEST, }, { name: 'Project Created', value: WebhookEvent.NEW_PROJECT, }, { name: 'Tag Created', value: WebhookEvent.NEW_TAG, }, { name: 'Task Created', value: WebhookEvent.NEW_TASK, }, { name: 'Time Entry Created', value: WebhookEvent.NEW_TIME_ENTRY, }, { name: 'Time Entry Deleted', value: WebhookEvent.TIME_ENTRY_DELETED, }, { name: 'Time Entry Updated', value: WebhookEvent.TIME_ENTRY_UPDATED, }, { name: 'Time Off Request Approved', value: WebhookEvent.TIME_OFF_REQUEST_APPROVED, }, { name: 'Time Off Request Rejected', value: WebhookEvent.TIME_OFF_REQUEST_REJECTED, }, { name: 'Time Off Request Withdrawn', value: WebhookEvent.TIME_OFF_REQUEST_WITHDRAWN, }, { name: 'Time Off Requested', value: WebhookEvent.TIME_OFF_REQUESTED, }, { name: 'Timer Started', value: WebhookEvent.NEW_TIMER_STARTED, }, { name: 'Timer Stopped', value: WebhookEvent.TIMER_STOPPED, }, { name: 'User Activated On Workspace', value: WebhookEvent.USER_ACTIVATED_ON_WORKSPACE, }, { name: 'User Deactivated On Workspace', value: WebhookEvent.USER_DEACTIVATED_ON_WORKSPACE, }, { name: 'User Deleted From Workspace', value: WebhookEvent.USER_DELETED_FROM_WORKSPACE, }, { name: 'User Email Changed', value: WebhookEvent.USER_EMAIL_CHANGED, }, { name: 'User Joined Workspace', value: WebhookEvent.USER_JOINED_WORKSPACE, }, { name: 'User Updated', value: WebhookEvent.USER_UPDATED, }, ], required: true, default: `${WebhookEvent.APPROVAL_REQUEST_STATUS_UPDATED}`, }, ], }; this.methods = { loadOptions: { listWorkspaces() { return tslib_1.__awaiter(this, void 0, void 0, function* () { const returnData = []; const workspaces = yield GenericFunctions_1.clockifyApiRequest.call(this, 'GET', 'workspaces'); for (const workspace of workspaces) { if (workspace.name === undefined || workspace.id === undefined) { continue; } returnData.push({ name: workspace.name, value: workspace.id, }); } return returnData; }); }, }, }; this.webhookMethods = { default: { checkExists() { return tslib_1.__awaiter(this, void 0, void 0, function* () { const workspaceId = this.getNodeParameter('workspaceId'); const responseData = yield GenericFunctions_1.clockifyApiRequest.call(this, 'GET', `workspaces/${workspaceId}/webhooks`); if (responseData.webhooks === undefined) { return false; } const webhookUrl = this.getNodeWebhookUrl('default'); const webhookData = this.getWorkflowStaticData('node'); for (const webhook of responseData.webhooks) { if (webhook.url === webhookUrl) { webhookData.webhookAuthToken = webhook.authToken; webhookData.webhookId = webhook.id; return true; } } return false; }); }, create() { return tslib_1.__awaiter(this, void 0, void 0, function* () { const workspaceId = this.getNodeParameter('workspaceId'); const name = this.getNodeParameter('name'); const webhookUrl = this.getNodeWebhookUrl('default'); const webhookEvent = this.getNodeParameter('webhookEvent'); const body = { name, triggerSource: [], triggerSourceType: 'WORKSPACE_ID', url: webhookUrl, webhookEvent, }; const responseData = yield GenericFunctions_1.clockifyApiRequest.call(this, 'POST', `workspaces/${workspaceId}/webhooks`, body); if (responseData.authToken === undefined || responseData.id === undefined) { return false; } const webhookData = this.getWorkflowStaticData('node'); webhookData.webhookAuthToken = responseData.authToken; webhookData.webhookId = responseData.id; return true; }); }, delete() { return tslib_1.__awaiter(this, void 0, void 0, function* () { const webhookData = this.getWorkflowStaticData('node'); if (webhookData.webhookId === undefined) { return true; } const workspaceId = this.getNodeParameter('workspaceId'); try { yield GenericFunctions_1.clockifyApiRequest.call(this, 'DELETE', `workspaces/${workspaceId}/webhooks/${webhookData.webhookId}`); } catch (_a) { return false; } delete webhookData.webhookAuthToken; delete webhookData.webhookId; return true; }); }, }, }; } webhook() { return tslib_1.__awaiter(this, void 0, void 0, function* () { const headerData = this.getHeaderData(); if (headerData['clockify-signature'] === undefined || headerData['clockify-webhook-event-type'] === undefined) { return {}; } const webhookData = this.getWorkflowStaticData('node'); if (headerData['clockify-signature'] !== webhookData.webhookAuthToken) { return {}; } const webhookEvent = this.getNodeParameter('webhookEvent'); if (headerData['clockify-webhook-event-type'] !== webhookEvent) { return {}; } const req = this.getRequestObject(); return { workflowData: [this.helpers.returnJsonArray(req.body)], }; }); } } exports.ClockifyEnhancedTrigger = ClockifyEnhancedTrigger; //# sourceMappingURL=ClockifyEnhancedTrigger.node.js.map