UNPKG

@skriptfabrik/n8n-nodes-clockify-enhanced

Version:
233 lines 11.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ClockifyEnhanced = void 0; const tslib_1 = require("tslib"); const n8n_workflow_1 = require("n8n-workflow"); const GenericFunctions_1 = require("n8n-nodes-base/dist/nodes/Clockify/GenericFunctions"); const ProjectDescription_1 = require("./descriptions/ProjectDescription"); const ProjectMembershipsDescription_1 = require("./descriptions/ProjectMembershipsDescription"); class ClockifyEnhanced { constructor() { this.description = { displayName: 'Clockify Enhanced', name: 'clockifyEnhanced', icon: 'file:icons/clockify-enhanced.svg', group: ['transform'], version: 1, subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}', description: 'Consume Clockify REST API', defaults: { name: 'Clockify Enhanced', }, inputs: [n8n_workflow_1.NodeConnectionTypes.Main], outputs: [n8n_workflow_1.NodeConnectionTypes.Main], credentials: [ { name: 'clockifyApi', required: true, }, ], properties: [ { displayName: 'Resource', name: 'resource', type: 'options', noDataExpression: true, options: [ { name: 'Project', value: 'project', }, { name: 'Project Membership', value: 'project membership', }, ], default: 'project', }, ...ProjectDescription_1.projectOperations, ...ProjectMembershipsDescription_1.projectMembershipOperations, { displayName: 'Workspace Name or ID', name: 'workspaceId', type: 'options', typeOptions: { loadOptionsMethod: 'loadWorkspaces', }, default: '', description: 'Choose from the list, or specify an ID using an <a href="https://docs.n8n.io/code/expressions/">expression</a>', required: true, requiresDataPath: 'single', validateType: 'string', }, ...ProjectDescription_1.projectFields, ...ProjectMembershipsDescription_1.projectMembershipFields, ], }; this.methods = { loadOptions: { loadWorkspaces() { return tslib_1.__awaiter(this, void 0, void 0, function* () { const workspaces = yield GenericFunctions_1.clockifyApiRequest.call(this, 'GET', 'workspaces'); if (!workspaces) { return []; } return workspaces.map((value) => ({ name: value.name, value: value.id, })); }); }, loadClients() { return tslib_1.__awaiter(this, void 0, void 0, function* () { const workspaceId = this.getCurrentNodeParameter('workspaceId'); if (!workspaceId) { return []; } const clients = yield GenericFunctions_1.clockifyApiRequestAllItems.call(this, 'GET', `workspaces/${workspaceId}/clients`); if (!clients) { return []; } return clients.map((client) => ({ name: client.name, value: client.id, })); }); }, loadProjects() { return tslib_1.__awaiter(this, void 0, void 0, function* () { const workspaceId = this.getCurrentNodeParameter('workspaceId'); if (!workspaceId) { return []; } const projects = yield GenericFunctions_1.clockifyApiRequestAllItems.call(this, 'GET', `workspaces/${workspaceId}/projects`); if (!projects) { return []; } return projects.map((project) => ({ name: project.name, value: project.id, })); }); }, loadUsers() { return tslib_1.__awaiter(this, void 0, void 0, function* () { const workspaceId = this.getCurrentNodeParameter('workspaceId'); if (!workspaceId) { return []; } const users = yield GenericFunctions_1.clockifyApiRequestAllItems.call(this, 'GET', `workspaces/${workspaceId}/users`); if (!users) { return []; } return users.map((value) => ({ name: value.name, value: value.id, })); }); }, loadNonProjectUsers() { return tslib_1.__awaiter(this, void 0, void 0, function* () { const workspaceId = this.getCurrentNodeParameter('workspaceId'); const projectId = this.getCurrentNodeParameter('projectId'); if (!workspaceId || !projectId) { return []; } const users = yield GenericFunctions_1.clockifyApiRequestAllItems.call(this, 'GET', `workspaces/${workspaceId}/users`, undefined, { memberships: 'PROJECT', }); if (!users) { return []; } return users .filter((user) => { const memberships = user.memberships.filter((membership) => membership.targetId === projectId); return memberships.length <= 0; }) .map((user) => ({ name: user.name, value: user.id, })); }); }, loadProjectUsers() { return tslib_1.__awaiter(this, void 0, void 0, function* () { const workspaceId = this.getCurrentNodeParameter('workspaceId'); const projectId = this.getCurrentNodeParameter('projectId'); if (!workspaceId || !projectId) { return []; } const users = yield GenericFunctions_1.clockifyApiRequestAllItems.call(this, 'GET', `workspaces/${workspaceId}/users`, undefined, { projectId, }); if (!users) { return []; } return users.map((user) => ({ name: user.name, value: user.id, })); }); }, }, }; } execute() { return tslib_1.__awaiter(this, void 0, void 0, function* () { const items = this.getInputData(); const returnData = []; let responseData = {}; const resource = this.getNodeParameter('resource', 0); const operation = this.getNodeParameter('operation', 0); for (let item = 0; item < items.length; item++) { try { if (resource === 'project') { if (operation === 'update') { const workspaceId = this.getNodeParameter('workspaceId', item); const projectId = this.getNodeParameter('projectId', item); const updateFields = this.getNodeParameter('updateFields', item); responseData = yield GenericFunctions_1.clockifyApiRequest.call(this, 'PUT', `/workspaces/${workspaceId}/projects/${projectId}`, updateFields); } } if (resource === 'project membership') { const workspaceId = this.getNodeParameter('workspaceId', item); const projectId = this.getNodeParameter('projectId', item); if (operation === 'patch') { const memberships = this.getNodeParameter('memberships', item).map((membership) => ({ userId: membership })); responseData = yield GenericFunctions_1.clockifyApiRequest.call(this, 'PATCH', `/workspaces/${workspaceId}/projects/${projectId}/memberships`, { memberships, }); } if (operation === 'assign') { responseData = yield GenericFunctions_1.clockifyApiRequest.call(this, 'POST', `/workspaces/${workspaceId}/projects/${projectId}/memberships`, { remove: false, userIds: this.getNodeParameter('userIdsToAssign', item), }); } if (operation === 'remove') { responseData = yield GenericFunctions_1.clockifyApiRequest.call(this, 'POST', `/workspaces/${workspaceId}/projects/${projectId}/memberships`, { remove: true, userIds: this.getNodeParameter('userIdsToRemove', item), }); } } const executionData = this.helpers.constructExecutionMetaData(this.helpers.returnJsonArray(responseData), { itemData: { item } }); returnData.push(...executionData); } catch (error) { if (this.continueOnFail()) { returnData.push({ json: { error: error.message }, pairedItem: { item }, }); continue; } throw error; } } return [returnData]; }); } } exports.ClockifyEnhanced = ClockifyEnhanced; //# sourceMappingURL=ClockifyEnhanced.node.js.map