2020-10-04 07:28:05 -07:00
|
|
|
import {
|
|
|
|
IExecuteFunctions,
|
|
|
|
} from 'n8n-core';
|
|
|
|
|
|
|
|
import {
|
2020-10-04 07:32:12 -07:00
|
|
|
IDataObject,
|
2020-10-04 07:28:05 -07:00
|
|
|
ILoadOptionsFunctions,
|
|
|
|
INodeExecutionData,
|
|
|
|
INodePropertyOptions,
|
|
|
|
INodeType,
|
|
|
|
INodeTypeDescription,
|
|
|
|
} from 'n8n-workflow';
|
|
|
|
|
|
|
|
import {
|
|
|
|
clockifyApiRequest,
|
|
|
|
clockifyApiRequestAllItems,
|
|
|
|
} from './GenericFunctions';
|
|
|
|
|
|
|
|
import {
|
|
|
|
IClientDto,
|
|
|
|
IWorkspaceDto,
|
|
|
|
} from './WorkpaceInterfaces';
|
|
|
|
|
|
|
|
import {
|
|
|
|
IUserDto,
|
|
|
|
} from './UserDtos';
|
|
|
|
|
|
|
|
import {
|
|
|
|
IProjectDto,
|
|
|
|
} from './ProjectInterfaces';
|
|
|
|
|
|
|
|
import {
|
|
|
|
projectFields,
|
2020-10-04 07:32:12 -07:00
|
|
|
projectOperations,
|
2020-10-04 07:28:05 -07:00
|
|
|
} from './ProjectDescription';
|
|
|
|
|
|
|
|
import {
|
|
|
|
tagFields,
|
2020-10-04 07:32:12 -07:00
|
|
|
tagOperations,
|
2020-10-04 07:28:05 -07:00
|
|
|
} from './TagDescription';
|
|
|
|
|
2021-09-03 10:03:15 -07:00
|
|
|
import {
|
|
|
|
taskFields,
|
|
|
|
taskOperations,
|
|
|
|
} from './TaskDescription';
|
|
|
|
|
2020-10-04 07:28:05 -07:00
|
|
|
import {
|
|
|
|
timeEntryFields,
|
|
|
|
timeEntryOperations,
|
|
|
|
} from './TimeEntryDescription';
|
|
|
|
|
|
|
|
import * as moment from 'moment-timezone';
|
|
|
|
|
|
|
|
export class Clockify implements INodeType {
|
|
|
|
description: INodeTypeDescription = {
|
|
|
|
displayName: 'Clockify',
|
|
|
|
name: 'clockify',
|
2021-06-12 12:00:37 -07:00
|
|
|
icon: 'file:clockify.svg',
|
2020-10-04 07:28:05 -07:00
|
|
|
group: ['transform'],
|
|
|
|
version: 1,
|
|
|
|
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
|
|
|
|
description: 'Consume Clockify REST API',
|
|
|
|
defaults: {
|
|
|
|
name: 'Clockify',
|
|
|
|
color: '#000000',
|
|
|
|
},
|
|
|
|
inputs: ['main'],
|
|
|
|
outputs: ['main'],
|
|
|
|
credentials: [
|
|
|
|
{
|
|
|
|
name: 'clockifyApi',
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
properties: [
|
|
|
|
{
|
|
|
|
displayName: 'Resource',
|
|
|
|
name: 'resource',
|
|
|
|
type: 'options',
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'Project',
|
2020-10-22 06:46:03 -07:00
|
|
|
value: 'project',
|
2020-10-04 07:28:05 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Tag',
|
2020-10-22 06:46:03 -07:00
|
|
|
value: 'tag',
|
2020-10-04 07:28:05 -07:00
|
|
|
},
|
2021-09-03 10:03:15 -07:00
|
|
|
{
|
|
|
|
name: 'Task',
|
|
|
|
value: 'task',
|
|
|
|
},
|
2020-10-04 07:28:05 -07:00
|
|
|
{
|
|
|
|
name: 'Time Entry',
|
2020-10-22 06:46:03 -07:00
|
|
|
value: 'timeEntry',
|
2020-10-04 07:28:05 -07:00
|
|
|
},
|
|
|
|
],
|
|
|
|
default: 'project',
|
|
|
|
description: 'The resource to operate on.',
|
|
|
|
},
|
|
|
|
...projectOperations,
|
|
|
|
...tagOperations,
|
2021-09-03 10:03:15 -07:00
|
|
|
...taskOperations,
|
2020-10-04 07:28:05 -07:00
|
|
|
...timeEntryOperations,
|
|
|
|
{
|
|
|
|
displayName: 'Workspace ID',
|
|
|
|
name: 'workspaceId',
|
|
|
|
type: 'options',
|
|
|
|
typeOptions: {
|
|
|
|
loadOptionsMethod: 'listWorkspaces',
|
|
|
|
},
|
|
|
|
required: true,
|
|
|
|
default: [],
|
|
|
|
},
|
|
|
|
...projectFields,
|
|
|
|
...tagFields,
|
2021-09-03 10:03:15 -07:00
|
|
|
...taskFields,
|
2020-10-04 07:28:05 -07:00
|
|
|
...timeEntryFields,
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
methods = {
|
|
|
|
loadOptions: {
|
|
|
|
async listWorkspaces(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
|
|
const rtv: INodePropertyOptions[] = [];
|
|
|
|
const workspaces: IWorkspaceDto[] = await clockifyApiRequest.call(this, 'GET', 'workspaces');
|
|
|
|
if (undefined !== workspaces) {
|
|
|
|
workspaces.forEach(value => {
|
|
|
|
rtv.push(
|
|
|
|
{
|
|
|
|
name: value.name,
|
|
|
|
value: value.id,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return rtv;
|
|
|
|
},
|
|
|
|
async loadUsersForWorkspace(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
|
|
const rtv: INodePropertyOptions[] = [];
|
|
|
|
const workspaceId = this.getCurrentNodeParameter('workspaceId');
|
|
|
|
if (undefined !== workspaceId) {
|
|
|
|
const resource = `workspaces/${workspaceId}/users`;
|
|
|
|
const users: IUserDto[] = await clockifyApiRequest.call(this, 'GET', resource);
|
|
|
|
if (undefined !== users) {
|
|
|
|
users.forEach(value => {
|
|
|
|
rtv.push(
|
|
|
|
{
|
|
|
|
name: value.name,
|
|
|
|
value: value.id,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rtv;
|
|
|
|
},
|
|
|
|
async loadClientsForWorkspace(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
|
|
const rtv: INodePropertyOptions[] = [];
|
|
|
|
const workspaceId = this.getCurrentNodeParameter('workspaceId');
|
|
|
|
if (undefined !== workspaceId) {
|
|
|
|
const resource = `workspaces/${workspaceId}/clients`;
|
|
|
|
const clients: IClientDto[] = await clockifyApiRequest.call(this, 'GET', resource);
|
|
|
|
if (undefined !== clients) {
|
|
|
|
clients.forEach(value => {
|
|
|
|
rtv.push(
|
|
|
|
{
|
|
|
|
name: value.name,
|
|
|
|
value: value.id,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rtv;
|
|
|
|
},
|
|
|
|
async loadProjectsForWorkspace(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
|
|
const rtv: INodePropertyOptions[] = [];
|
|
|
|
const workspaceId = this.getCurrentNodeParameter('workspaceId');
|
|
|
|
if (undefined !== workspaceId) {
|
|
|
|
const resource = `workspaces/${workspaceId}/projects`;
|
|
|
|
const users: IProjectDto[] = await clockifyApiRequest.call(this, 'GET', resource);
|
|
|
|
if (undefined !== users) {
|
|
|
|
users.forEach(value => {
|
|
|
|
rtv.push(
|
|
|
|
{
|
|
|
|
name: value.name,
|
|
|
|
value: value.id,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rtv;
|
|
|
|
},
|
|
|
|
async loadTagsForWorkspace(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
|
|
const rtv: INodePropertyOptions[] = [];
|
|
|
|
const workspaceId = this.getCurrentNodeParameter('workspaceId');
|
|
|
|
if (undefined !== workspaceId) {
|
|
|
|
const resource = `workspaces/${workspaceId}/tags`;
|
|
|
|
const users: IProjectDto[] = await clockifyApiRequest.call(this, 'GET', resource);
|
|
|
|
if (undefined !== users) {
|
|
|
|
users.forEach(value => {
|
|
|
|
rtv.push(
|
|
|
|
{
|
|
|
|
name: value.name,
|
|
|
|
value: value.id,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rtv;
|
|
|
|
},
|
|
|
|
async loadCustomFieldsForWorkspace(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
|
|
const rtv: INodePropertyOptions[] = [];
|
|
|
|
const workspaceId = this.getCurrentNodeParameter('workspaceId');
|
|
|
|
if (undefined !== workspaceId) {
|
|
|
|
const resource = `workspaces/${workspaceId}/custom-fields`;
|
|
|
|
const customFields = await clockifyApiRequest.call(this, 'GET', resource);
|
|
|
|
for (const customField of customFields) {
|
|
|
|
rtv.push(
|
|
|
|
{
|
|
|
|
name: customField.name,
|
|
|
|
value: customField.id,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rtv;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
|
|
|
|
|
|
|
const items = this.getInputData();
|
|
|
|
|
|
|
|
const returnData: IDataObject[] = [];
|
|
|
|
|
|
|
|
const length = (items.length as unknown) as number;
|
|
|
|
|
|
|
|
const qs: IDataObject = {};
|
|
|
|
|
|
|
|
let responseData;
|
|
|
|
|
|
|
|
const resource = this.getNodeParameter('resource', 0) as string;
|
|
|
|
|
|
|
|
const operation = this.getNodeParameter('operation', 0) as string;
|
|
|
|
|
|
|
|
for (let i = 0; i < length; i++) {
|
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
try {
|
|
|
|
if (resource === 'project') {
|
2020-10-04 07:28:05 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (operation === 'create') {
|
2020-10-04 07:28:05 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const workspaceId = this.getNodeParameter('workspaceId', i) as string;
|
2020-10-04 07:28:05 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const name = this.getNodeParameter('name', i) as string;
|
2020-10-04 07:28:05 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
|
2020-10-04 07:28:05 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const body: IDataObject = {
|
|
|
|
name,
|
|
|
|
};
|
2020-10-04 07:28:05 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
Object.assign(body, additionalFields);
|
2020-10-04 07:28:05 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (body.estimateUi) {
|
2020-10-04 07:28:05 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
body.estimate = (body.estimateUi as IDataObject).estimateValues;
|
2020-10-04 07:28:05 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
delete body.estimateUi;
|
|
|
|
}
|
2020-10-04 07:28:05 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = await clockifyApiRequest.call(
|
|
|
|
this,
|
|
|
|
'POST',
|
|
|
|
`/workspaces/${workspaceId}/projects`,
|
|
|
|
body,
|
|
|
|
qs,
|
|
|
|
);
|
|
|
|
}
|
2020-10-04 07:28:05 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (operation === 'delete') {
|
2020-10-04 07:28:05 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const workspaceId = this.getNodeParameter('workspaceId', i) as string;
|
2020-10-04 07:28:05 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const projectId = this.getNodeParameter('projectId', i) as string;
|
2020-10-04 07:28:05 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = await clockifyApiRequest.call(
|
2020-10-04 07:28:05 -07:00
|
|
|
this,
|
2021-07-19 23:58:54 -07:00
|
|
|
'DELETE',
|
|
|
|
`/workspaces/${workspaceId}/projects/${projectId}`,
|
2020-10-04 07:28:05 -07:00
|
|
|
{},
|
2020-10-22 09:00:28 -07:00
|
|
|
qs,
|
2020-10-04 07:28:05 -07:00
|
|
|
);
|
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = { success: true };
|
|
|
|
}
|
2020-10-04 07:28:05 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (operation === 'get') {
|
2020-10-04 07:28:05 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const workspaceId = this.getNodeParameter('workspaceId', i) as string;
|
|
|
|
|
|
|
|
const projectId = this.getNodeParameter('projectId', i) as string;
|
|
|
|
|
|
|
|
responseData = await clockifyApiRequest.call(
|
2020-10-04 07:28:05 -07:00
|
|
|
this,
|
|
|
|
'GET',
|
2021-07-19 23:58:54 -07:00
|
|
|
`/workspaces/${workspaceId}/projects/${projectId}`,
|
2020-10-04 07:28:05 -07:00
|
|
|
{},
|
2020-10-22 09:00:28 -07:00
|
|
|
qs,
|
2020-10-04 07:28:05 -07:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (operation === 'getAll') {
|
2020-10-04 07:28:05 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
|
2020-10-04 07:28:05 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const workspaceId = this.getNodeParameter('workspaceId', i) as string;
|
2020-10-04 07:28:05 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
|
2020-10-04 07:28:05 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
Object.assign(qs, additionalFields);
|
2020-10-04 07:28:05 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (returnAll) {
|
|
|
|
responseData = await clockifyApiRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
`/workspaces/${workspaceId}/projects`,
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2020-10-04 07:28:05 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
} else {
|
2020-10-04 07:28:05 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
qs.limit = this.getNodeParameter('limit', i) as number;
|
2020-10-04 07:28:05 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = await clockifyApiRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
`/workspaces/${workspaceId}/projects`,
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2020-10-04 07:28:05 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = responseData.splice(0, qs.limit);
|
|
|
|
}
|
|
|
|
}
|
2020-10-04 07:28:05 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (operation === 'update') {
|
2020-10-04 07:28:05 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const workspaceId = this.getNodeParameter('workspaceId', i) as string;
|
2020-10-04 07:28:05 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const projectId = this.getNodeParameter('projectId', i) as string;
|
2020-10-04 07:28:05 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const updateFields = this.getNodeParameter('updateFields', i) as IDataObject;
|
2020-10-04 07:28:05 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const body: IDataObject = {};
|
2020-10-04 07:28:05 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
Object.assign(body, updateFields);
|
2020-10-04 07:28:05 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (body.estimateUi) {
|
2020-10-04 07:28:05 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
body.estimate = (body.estimateUi as IDataObject).estimateValues;
|
2020-10-04 07:28:05 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
delete body.estimateUi;
|
|
|
|
}
|
2020-10-04 07:28:05 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = await clockifyApiRequest.call(
|
|
|
|
this,
|
|
|
|
'PUT',
|
|
|
|
`/workspaces/${workspaceId}/projects/${projectId}`,
|
|
|
|
body,
|
|
|
|
qs,
|
|
|
|
);
|
|
|
|
}
|
2020-10-04 07:28:05 -07:00
|
|
|
}
|
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (resource === 'tag') {
|
2020-10-04 07:28:05 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (operation === 'create') {
|
2020-10-04 07:28:05 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const workspaceId = this.getNodeParameter('workspaceId', i) as string;
|
2020-10-04 07:28:05 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const name = this.getNodeParameter('name', i) as string;
|
2020-10-04 07:28:05 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const body: IDataObject = {
|
|
|
|
name,
|
|
|
|
};
|
2020-10-04 07:28:05 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = await clockifyApiRequest.call(
|
2020-10-04 07:28:05 -07:00
|
|
|
this,
|
2021-07-19 23:58:54 -07:00
|
|
|
'POST',
|
2020-10-04 07:28:05 -07:00
|
|
|
`/workspaces/${workspaceId}/tags`,
|
2021-07-19 23:58:54 -07:00
|
|
|
body,
|
2020-10-22 09:00:28 -07:00
|
|
|
qs,
|
2020-10-04 07:28:05 -07:00
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (operation === 'delete') {
|
2020-10-04 07:28:05 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const workspaceId = this.getNodeParameter('workspaceId', i) as string;
|
2020-10-04 07:28:05 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const tagId = this.getNodeParameter('tagId', i) as string;
|
2020-10-04 07:28:05 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = await clockifyApiRequest.call(
|
2020-10-04 07:28:05 -07:00
|
|
|
this,
|
2021-07-19 23:58:54 -07:00
|
|
|
'DELETE',
|
|
|
|
`/workspaces/${workspaceId}/tags/${tagId}`,
|
2020-10-04 07:28:05 -07:00
|
|
|
{},
|
2020-10-22 09:00:28 -07:00
|
|
|
qs,
|
2020-10-04 07:28:05 -07:00
|
|
|
);
|
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = { success: true };
|
2020-10-04 07:28:05 -07:00
|
|
|
}
|
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (operation === 'getAll') {
|
2020-10-04 07:28:05 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
|
2020-10-04 07:28:05 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const workspaceId = this.getNodeParameter('workspaceId', i) as string;
|
2020-10-04 07:28:05 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
|
2020-10-04 07:28:05 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
Object.assign(qs, additionalFields);
|
2020-10-04 07:28:05 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (returnAll) {
|
|
|
|
responseData = await clockifyApiRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
`/workspaces/${workspaceId}/tags`,
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2020-10-04 07:28:05 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
} else {
|
|
|
|
|
|
|
|
qs.limit = this.getNodeParameter('limit', i) as number;
|
2020-10-04 07:28:05 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = await clockifyApiRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
`/workspaces/${workspaceId}/tags`,
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2020-10-04 07:28:05 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = responseData.splice(0, qs.limit);
|
|
|
|
}
|
|
|
|
}
|
2020-10-04 07:28:05 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (operation === 'update') {
|
2020-10-04 07:28:05 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const workspaceId = this.getNodeParameter('workspaceId', i) as string;
|
2020-10-04 07:28:05 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const tagId = this.getNodeParameter('tagId', i) as string;
|
2020-10-04 07:28:05 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const updateFields = this.getNodeParameter('updateFields', i) as IDataObject;
|
2020-10-04 07:28:05 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const body: IDataObject = {};
|
2020-10-04 07:28:05 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
Object.assign(body, updateFields);
|
2020-10-04 07:28:05 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = await clockifyApiRequest.call(
|
|
|
|
this,
|
|
|
|
'PUT',
|
|
|
|
`/workspaces/${workspaceId}/tags/${tagId}`,
|
|
|
|
body,
|
|
|
|
qs,
|
|
|
|
);
|
2020-10-04 07:28:05 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
2020-10-04 07:28:05 -07:00
|
|
|
|
2021-09-03 10:03:15 -07:00
|
|
|
if (resource === 'task') {
|
|
|
|
if (operation === 'create') {
|
|
|
|
const workspaceId = this.getNodeParameter(
|
|
|
|
'workspaceId',
|
|
|
|
i,
|
|
|
|
) as string;
|
|
|
|
|
|
|
|
const projectId = this.getNodeParameter('projectId', i) as string;
|
|
|
|
|
|
|
|
const name = this.getNodeParameter('name', i) as string;
|
|
|
|
|
|
|
|
const additionalFields = this.getNodeParameter(
|
|
|
|
'additionalFields',
|
|
|
|
i,
|
|
|
|
) as IDataObject;
|
|
|
|
|
|
|
|
const body: IDataObject = {
|
|
|
|
name,
|
|
|
|
};
|
|
|
|
|
|
|
|
Object.assign(body, additionalFields);
|
|
|
|
|
|
|
|
if (body.estimate) {
|
|
|
|
const [hour, minute] = (body.estimate as string).split(':');
|
|
|
|
body.estimate = `PT${hour}H${minute}M`;
|
|
|
|
}
|
|
|
|
|
|
|
|
responseData = await clockifyApiRequest.call(
|
|
|
|
this,
|
|
|
|
'POST',
|
|
|
|
`/workspaces/${workspaceId}/projects/${projectId}/tasks`,
|
|
|
|
body,
|
|
|
|
qs,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (operation === 'delete') {
|
|
|
|
const workspaceId = this.getNodeParameter(
|
|
|
|
'workspaceId',
|
|
|
|
i,
|
|
|
|
) as string;
|
|
|
|
|
|
|
|
const projectId = this.getNodeParameter('projectId', i) as string;
|
|
|
|
|
|
|
|
const taskId = this.getNodeParameter('taskId', i) as string;
|
|
|
|
|
|
|
|
responseData = await clockifyApiRequest.call(
|
|
|
|
this,
|
|
|
|
'DELETE',
|
|
|
|
`/workspaces/${workspaceId}/projects/${projectId}/tasks/${taskId}`,
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (operation === 'get') {
|
|
|
|
const workspaceId = this.getNodeParameter(
|
|
|
|
'workspaceId',
|
|
|
|
i,
|
|
|
|
) as string;
|
|
|
|
|
|
|
|
const projectId = this.getNodeParameter('projectId', i) as string;
|
|
|
|
|
|
|
|
const taskId = this.getNodeParameter('taskId', i) as string;
|
|
|
|
|
|
|
|
responseData = await clockifyApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
`/workspaces/${workspaceId}/projects/${projectId}/tasks/${taskId}`,
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (operation === 'getAll') {
|
|
|
|
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
|
|
|
|
|
|
|
|
const workspaceId = this.getNodeParameter(
|
|
|
|
'workspaceId',
|
|
|
|
i,
|
|
|
|
) as string;
|
|
|
|
|
|
|
|
const projectId = this.getNodeParameter('projectId', i) as string;
|
|
|
|
|
|
|
|
const filters = this.getNodeParameter(
|
|
|
|
'filters',
|
|
|
|
i,
|
|
|
|
) as IDataObject;
|
|
|
|
|
|
|
|
Object.assign(qs, filters);
|
|
|
|
|
|
|
|
if (returnAll) {
|
|
|
|
responseData = await clockifyApiRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
`/workspaces/${workspaceId}/projects/${projectId}/tasks`,
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
qs['page-size'] = this.getNodeParameter('limit', i) as number;
|
|
|
|
|
|
|
|
responseData = await clockifyApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
`/workspaces/${workspaceId}/projects/${projectId}/tasks`,
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (operation === 'update') {
|
|
|
|
const workspaceId = this.getNodeParameter(
|
|
|
|
'workspaceId',
|
|
|
|
i,
|
|
|
|
) as string;
|
|
|
|
|
|
|
|
const projectId = this.getNodeParameter('projectId', i) as string;
|
|
|
|
|
|
|
|
const taskId = this.getNodeParameter('taskId', i) as string;
|
|
|
|
|
|
|
|
const updateFields = this.getNodeParameter(
|
|
|
|
'updateFields',
|
|
|
|
i,
|
|
|
|
) as IDataObject;
|
|
|
|
|
|
|
|
const body: IDataObject = {};
|
|
|
|
|
|
|
|
Object.assign(body, updateFields);
|
|
|
|
|
|
|
|
if (body.estimate) {
|
|
|
|
const [hour, minute] = (body.estimate as string).split(':');
|
|
|
|
body.estimate = `PT${hour}H${minute}M`;
|
|
|
|
}
|
|
|
|
|
|
|
|
responseData = await clockifyApiRequest.call(
|
|
|
|
this,
|
|
|
|
'PUT',
|
|
|
|
`/workspaces/${workspaceId}/projects/${projectId}/tasks/${taskId}`,
|
|
|
|
body,
|
|
|
|
qs,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (resource === 'timeEntry') {
|
2020-10-04 07:28:05 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (operation === 'create') {
|
2020-10-04 07:28:05 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const timezone = this.getTimezone();
|
2020-10-04 07:28:05 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const workspaceId = this.getNodeParameter('workspaceId', i) as string;
|
2020-10-04 07:28:05 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const start = this.getNodeParameter('start', i) as string;
|
2020-10-04 07:28:05 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
|
2020-10-04 07:28:05 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const body: IDataObject = {
|
|
|
|
start: moment.tz(start, timezone).utc().format(),
|
|
|
|
};
|
2020-10-04 07:28:05 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
Object.assign(body, additionalFields);
|
2020-10-04 07:28:05 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (body.end) {
|
|
|
|
body.end = moment.tz(body.end, timezone).utc().format();
|
|
|
|
}
|
2020-10-04 07:28:05 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (body.customFieldsUi) {
|
2020-10-04 07:28:05 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const customFields = (body.customFieldsUi as IDataObject).customFieldsValues as IDataObject[];
|
2020-10-04 07:28:05 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
body.customFields = customFields;
|
|
|
|
}
|
2020-10-04 07:28:05 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = await clockifyApiRequest.call(
|
|
|
|
this,
|
|
|
|
'POST',
|
|
|
|
`/workspaces/${workspaceId}/time-entries`,
|
|
|
|
body,
|
|
|
|
qs,
|
|
|
|
);
|
|
|
|
}
|
2020-10-04 07:28:05 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (operation === 'delete') {
|
2020-10-04 07:28:05 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const workspaceId = this.getNodeParameter('workspaceId', i) as string;
|
2020-10-04 07:28:05 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const timeEntryId = this.getNodeParameter('timeEntryId', i) as string;
|
2020-10-04 07:28:05 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = await clockifyApiRequest.call(
|
|
|
|
this,
|
|
|
|
'DELETE',
|
|
|
|
`/workspaces/${workspaceId}/time-entries/${timeEntryId}`,
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2020-10-04 07:28:05 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = { success: true };
|
|
|
|
}
|
2020-10-04 07:28:05 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (operation === 'get') {
|
2020-10-04 07:28:05 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const workspaceId = this.getNodeParameter('workspaceId', i) as string;
|
2020-10-04 07:28:05 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const timeEntryId = this.getNodeParameter('timeEntryId', i) as string;
|
2020-10-04 07:28:05 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = await clockifyApiRequest.call(
|
2020-10-04 07:28:05 -07:00
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
`/workspaces/${workspaceId}/time-entries/${timeEntryId}`,
|
|
|
|
{},
|
2020-10-22 09:00:28 -07:00
|
|
|
qs,
|
2020-10-04 07:28:05 -07:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (operation === 'update') {
|
|
|
|
|
|
|
|
const timezone = this.getTimezone();
|
|
|
|
|
|
|
|
const workspaceId = this.getNodeParameter('workspaceId', i) as string;
|
|
|
|
|
|
|
|
const timeEntryId = this.getNodeParameter('timeEntryId', i) as string;
|
|
|
|
|
|
|
|
const updateFields = this.getNodeParameter('updateFields', i) as IDataObject;
|
|
|
|
|
|
|
|
const body: IDataObject = {};
|
|
|
|
|
|
|
|
Object.assign(body, updateFields);
|
|
|
|
|
|
|
|
if (body.end) {
|
|
|
|
body.end = moment.tz(body.end, timezone).utc().format();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (body.start) {
|
|
|
|
body.start = moment.tz(body.start, timezone).utc().format();
|
|
|
|
} else {
|
|
|
|
// even if you do not want to update the start time, it always has to be set
|
|
|
|
// to make it more simple to the user, if he did not set a start time look for the current start time
|
|
|
|
// and set it
|
|
|
|
const { timeInterval: { start } } = await clockifyApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
`/workspaces/${workspaceId}/time-entries/${timeEntryId}`,
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
|
|
|
|
|
|
|
body.start = start;
|
|
|
|
}
|
|
|
|
|
|
|
|
responseData = await clockifyApiRequest.call(
|
|
|
|
this,
|
|
|
|
'PUT',
|
|
|
|
`/workspaces/${workspaceId}/time-entries/${timeEntryId}`,
|
|
|
|
body,
|
|
|
|
qs,
|
|
|
|
);
|
|
|
|
}
|
2020-10-04 07:28:05 -07:00
|
|
|
}
|
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (Array.isArray(responseData)) {
|
2020-10-04 07:28:05 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
returnData.push.apply(returnData, responseData as IDataObject[]);
|
2020-10-04 07:28:05 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
} else if (responseData !== undefined) {
|
2020-10-04 07:28:05 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
returnData.push(responseData as IDataObject);
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
if (this.continueOnFail()) {
|
|
|
|
returnData.push({ error: error.message });
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
throw error;
|
|
|
|
}
|
2020-10-04 07:28:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return [this.helpers.returnJsonArray(returnData)];
|
|
|
|
}
|
|
|
|
}
|