n8n/packages/nodes-base/nodes/Clockify/ClockifyTrigger.node.ts

125 lines
3 KiB
TypeScript
Raw Normal View History

2020-02-21 02:41:08 -08:00
import * as moment from 'moment-timezone';
import { IPollFunctions } from 'n8n-core';
import {
IDataObject,
ILoadOptionsFunctions,
INodeExecutionData,
INodePropertyOptions,
INodeType,
INodeTypeDescription,
} from 'n8n-workflow';
import {
clockifyApiRequest,
} from './GenericFunctions';
2020-02-21 02:41:08 -08:00
import { EntryTypeEnum } from './EntryTypeEnum';
import { IUserDto } from './UserDtos';
2020-02-21 02:41:08 -08:00
import { IWorkspaceDto } from './WorkpaceInterfaces';
export class ClockifyTrigger implements INodeType {
description: INodeTypeDescription = {
2020-02-21 02:41:08 -08:00
displayName: 'Clockify Trigger',
icon: 'file:clockify.svg',
name: 'clockifyTrigger',
group: ['trigger'],
version: 1,
description: 'Listens to Clockify events',
defaults: {
2020-02-21 02:41:08 -08:00
name: 'Clockify Trigger',
color: '#000000',
},
inputs: [],
outputs: ['main'],
credentials: [
{
name: 'clockifyApi',
required: true,
2020-10-22 06:46:03 -07:00
},
],
2020-02-21 02:41:08 -08:00
polling: true,
properties: [
{
displayName: 'Workspace',
name: 'workspaceId',
type: 'options',
typeOptions: {
loadOptionsMethod: 'listWorkspaces',
},
required: true,
default: '',
},
{
displayName: 'Trigger',
name: 'watchField',
type: 'options',
options: [
{
name: 'New Time Entry',
value: EntryTypeEnum.NEW_TIME_ENTRY,
2020-10-22 06:46:03 -07:00
},
],
required: true,
2020-02-21 02:41:08 -08:00
default: EntryTypeEnum.NEW_TIME_ENTRY,
},
2020-10-22 06:46:03 -07:00
],
};
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 poll(this: IPollFunctions): Promise<INodeExecutionData[][] | null> {
const webhookData = this.getWorkflowStaticData('node');
const triggerField = this.getNodeParameter('watchField') as EntryTypeEnum;
const workspaceId = this.getNodeParameter('workspaceId');
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
const userInfo: IUserDto = await clockifyApiRequest.call(this, 'GET', 'user');
2020-02-21 02:41:08 -08:00
webhookData.userId = userInfo.id;
}
const qs: IDataObject = {};
let resource: string;
let result = null;
switch (triggerField) {
case EntryTypeEnum.NEW_TIME_ENTRY:
default:
2020-02-21 02:41:08 -08:00
const workflowTimezone = this.getTimezone();
resource = `workspaces/${workspaceId}/user/${webhookData.userId}/time-entries`;
qs.start = webhookData.lastTimeChecked;
2020-04-22 11:42:58 -07:00
qs.end = moment().tz(workflowTimezone).format('YYYY-MM-DDTHH:mm:ss') + 'Z';
qs.hydrated = true;
qs['in-progress'] = false;
break;
}
2020-02-19 09:34:32 -08:00
result = await clockifyApiRequest.call(this, 'GET', resource, {}, qs);
2020-02-21 02:41:08 -08:00
webhookData.lastTimeChecked = qs.end;
if (Array.isArray(result) && result.length !== 0) {
return [this.helpers.returnJsonArray(result)];
}
return null;
}
}