2022-04-08 14:32:08 -07:00
|
|
|
import moment from 'moment-timezone';
|
2020-02-21 02:41:08 -08:00
|
|
|
|
2023-01-27 03:22:44 -08:00
|
|
|
import type {
|
2023-03-09 09:13:15 -08:00
|
|
|
IPollFunctions,
|
2020-02-03 14:11:35 -08:00
|
|
|
IDataObject,
|
|
|
|
ILoadOptionsFunctions,
|
|
|
|
INodeExecutionData,
|
|
|
|
INodePropertyOptions,
|
|
|
|
INodeType,
|
|
|
|
INodeTypeDescription,
|
|
|
|
} from 'n8n-workflow';
|
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
import { clockifyApiRequest } from './GenericFunctions';
|
2020-02-03 14:11:35 -08:00
|
|
|
|
2020-02-21 02:41:08 -08:00
|
|
|
import { EntryTypeEnum } from './EntryTypeEnum';
|
2023-01-27 03:22:44 -08:00
|
|
|
import type { IUserDto } from './UserDtos';
|
|
|
|
import type { IWorkspaceDto } from './WorkpaceInterfaces';
|
2020-02-03 14:11:35 -08:00
|
|
|
|
|
|
|
export class ClockifyTrigger implements INodeType {
|
|
|
|
description: INodeTypeDescription = {
|
2020-02-21 02:41:08 -08:00
|
|
|
displayName: 'Clockify Trigger',
|
2021-06-12 12:00:37 -07:00
|
|
|
icon: 'file:clockify.svg',
|
2020-02-03 14:11:35 -08:00
|
|
|
name: 'clockifyTrigger',
|
2021-09-03 10:03:15 -07:00
|
|
|
group: ['trigger'],
|
2020-02-03 14:11:35 -08:00
|
|
|
version: 1,
|
2021-07-03 05:40:16 -07:00
|
|
|
description: 'Listens to Clockify events',
|
2020-02-03 14:11:35 -08:00
|
|
|
defaults: {
|
2020-02-21 02:41:08 -08:00
|
|
|
name: 'Clockify Trigger',
|
2020-02-03 14:11:35 -08:00
|
|
|
},
|
|
|
|
inputs: [],
|
2021-09-03 10:03:15 -07:00
|
|
|
outputs: ['main'],
|
2020-02-03 14:11:35 -08:00
|
|
|
credentials: [
|
|
|
|
{
|
|
|
|
name: 'clockifyApi',
|
|
|
|
required: true,
|
2020-10-22 06:46:03 -07:00
|
|
|
},
|
2020-02-03 14:11:35 -08:00
|
|
|
],
|
2020-02-21 02:41:08 -08:00
|
|
|
polling: true,
|
2020-02-03 14:11:35 -08:00
|
|
|
properties: [
|
|
|
|
{
|
2022-06-03 10:23:49 -07:00
|
|
|
displayName: 'Workspace Name or ID',
|
2020-02-03 14:11:35 -08:00
|
|
|
name: 'workspaceId',
|
|
|
|
type: 'options',
|
2022-08-01 13:47:55 -07:00
|
|
|
description:
|
|
|
|
'Choose from the list, or specify an ID using an <a href="https://docs.n8n.io/code-examples/expressions/">expression</a>',
|
2020-02-03 14:11:35 -08:00
|
|
|
typeOptions: {
|
|
|
|
loadOptionsMethod: 'listWorkspaces',
|
|
|
|
},
|
|
|
|
required: true,
|
|
|
|
default: '',
|
|
|
|
},
|
2022-04-22 09:29:51 -07:00
|
|
|
// eslint-disable-next-line n8n-nodes-base/node-param-default-missing
|
2020-02-03 14:11:35 -08:00
|
|
|
{
|
|
|
|
displayName: 'Trigger',
|
|
|
|
name: 'watchField',
|
|
|
|
type: 'options',
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'New Time Entry',
|
|
|
|
value: EntryTypeEnum.NEW_TIME_ENTRY,
|
2020-10-22 06:46:03 -07:00
|
|
|
},
|
2020-02-03 14:11:35 -08:00
|
|
|
],
|
|
|
|
required: true,
|
2020-02-21 02:41:08 -08:00
|
|
|
default: EntryTypeEnum.NEW_TIME_ENTRY,
|
2020-02-03 14:11:35 -08:00
|
|
|
},
|
2020-10-22 06:46:03 -07:00
|
|
|
],
|
2020-02-03 14:11:35 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
methods = {
|
|
|
|
loadOptions: {
|
2020-10-04 07:32:12 -07:00
|
|
|
async listWorkspaces(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
|
|
const rtv: INodePropertyOptions[] = [];
|
2022-08-01 13:47:55 -07:00
|
|
|
const workspaces: IWorkspaceDto[] = await clockifyApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
'workspaces',
|
|
|
|
);
|
2020-10-04 07:32:12 -07:00
|
|
|
if (undefined !== workspaces) {
|
2022-08-01 13:47:55 -07:00
|
|
|
workspaces.forEach((value) => {
|
|
|
|
rtv.push({
|
|
|
|
name: value.name,
|
|
|
|
value: value.id,
|
|
|
|
});
|
2020-02-03 14:11:35 -08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
return rtv;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
async poll(this: IPollFunctions): Promise<INodeExecutionData[][] | null> {
|
|
|
|
const webhookData = this.getWorkflowStaticData('node');
|
|
|
|
const triggerField = this.getNodeParameter('watchField') as EntryTypeEnum;
|
2020-10-04 07:32:12 -07:00
|
|
|
const workspaceId = this.getNodeParameter('workspaceId');
|
2020-02-03 14:11:35 -08:00
|
|
|
|
2020-02-21 02:41:08 -08:00
|
|
|
if (!webhookData.userId) {
|
|
|
|
// Cache the user-id that we do not have to request it every time
|
2020-10-04 07:28:05 -07:00
|
|
|
const userInfo: IUserDto = await clockifyApiRequest.call(this, 'GET', 'user');
|
2020-02-21 02:41:08 -08:00
|
|
|
webhookData.userId = userInfo.id;
|
|
|
|
}
|
|
|
|
|
2020-10-04 07:32:12 -07:00
|
|
|
const qs: IDataObject = {};
|
2020-02-03 14:11:35 -08:00
|
|
|
let resource: string;
|
|
|
|
let result = null;
|
|
|
|
|
|
|
|
switch (triggerField) {
|
2020-10-04 07:32:12 -07:00
|
|
|
case EntryTypeEnum.NEW_TIME_ENTRY:
|
2020-02-03 14:11:35 -08:00
|
|
|
default:
|
2020-02-21 02:41:08 -08:00
|
|
|
const workflowTimezone = this.getTimezone();
|
|
|
|
resource = `workspaces/${workspaceId}/user/${webhookData.userId}/time-entries`;
|
2020-02-03 14:11:35 -08:00
|
|
|
qs.start = webhookData.lastTimeChecked;
|
2020-04-22 11:42:58 -07:00
|
|
|
qs.end = moment().tz(workflowTimezone).format('YYYY-MM-DDTHH:mm:ss') + 'Z';
|
2020-02-03 14:11:35 -08:00
|
|
|
qs.hydrated = true;
|
2021-09-03 10:03:15 -07:00
|
|
|
qs['in-progress'] = false;
|
2020-10-04 07:32:12 -07:00
|
|
|
break;
|
2020-02-03 14:11:35 -08:00
|
|
|
}
|
2020-02-19 09:34:32 -08:00
|
|
|
|
2020-10-04 07:32:12 -07:00
|
|
|
result = await clockifyApiRequest.call(this, 'GET', resource, {}, qs);
|
2020-02-21 02:41:08 -08:00
|
|
|
webhookData.lastTimeChecked = qs.end;
|
|
|
|
|
2020-02-03 14:11:35 -08:00
|
|
|
if (Array.isArray(result) && result.length !== 0) {
|
2021-09-03 10:03:15 -07:00
|
|
|
return [this.helpers.returnJsonArray(result)];
|
2020-02-03 14:11:35 -08:00
|
|
|
}
|
2021-05-12 11:48:48 -07:00
|
|
|
return null;
|
2020-02-03 14:11:35 -08:00
|
|
|
}
|
|
|
|
}
|