Improved Clickify-Node

This commit is contained in:
Jan Oberhauser 2020-02-21 11:41:08 +01:00
parent 78bf915e65
commit 5548e4e794
7 changed files with 49 additions and 47 deletions

View file

@ -1,3 +1,5 @@
import * as moment from 'moment-timezone';
import {IPollFunctions} from 'n8n-core'; import {IPollFunctions} from 'n8n-core';
import { import {
IDataObject, IDataObject,
@ -12,22 +14,21 @@ import {
clockifyApiRequest, clockifyApiRequest,
} from './GenericFunctions'; } from './GenericFunctions';
import {IWorkspaceDto} from "./WorkpaceInterfaces"; import { EntryTypeEnum } from './EntryTypeEnum';
import {EntryTypeEnum} from "./EntryTypeEnum"; import { ICurrentUserDto } from './UserDtos';
import {ICurrentUserDto} from "./UserDtos"; import { IWorkspaceDto } from './WorkpaceInterfaces';
import * as moment from "moment";
export class ClockifyTrigger implements INodeType { export class ClockifyTrigger implements INodeType {
description: INodeTypeDescription = { description: INodeTypeDescription = {
displayName: 'Clockify Event', displayName: 'Clockify Trigger',
icon: 'file:images/clockify-mark-blue.png', icon: 'file:clockify.png',
name: 'clockifyTrigger', name: 'clockifyTrigger',
group: ['trigger'], group: ['trigger'],
version: 1, version: 1,
description: 'Watches Clockify For Events', description: 'Watches Clockify For Events',
defaults: { defaults: {
name: 'Clockify Event', name: 'Clockify Trigger',
color: '#00FF00', color: '#00FF00',
}, },
inputs: [], inputs: [],
@ -38,6 +39,7 @@ export class ClockifyTrigger implements INodeType {
required: true, required: true,
} }
], ],
polling: true,
properties: [ properties: [
{ {
displayName: 'Workspace', displayName: 'Workspace',
@ -60,7 +62,7 @@ export class ClockifyTrigger implements INodeType {
} }
], ],
required: true, required: true,
default: '', default: EntryTypeEnum.NEW_TIME_ENTRY,
}, },
] ]
}; };
@ -89,7 +91,12 @@ export class ClockifyTrigger implements INodeType {
const triggerField = this.getNodeParameter('watchField') as EntryTypeEnum; const triggerField = this.getNodeParameter('watchField') as EntryTypeEnum;
const workspaceId = this.getNodeParameter('workspaceId'); const workspaceId = this.getNodeParameter('workspaceId');
const userInfo : ICurrentUserDto = await clockifyApiRequest.call(this,'GET', 'user'); if (!webhookData.userId) {
// Cache the user-id that we do not have to request it every time
const userInfo: ICurrentUserDto = await clockifyApiRequest.call(this, 'GET', 'user');
webhookData.userId = userInfo.id;
}
const qs : IDataObject = {}; const qs : IDataObject = {};
let resource: string; let resource: string;
let result = null; let result = null;
@ -97,21 +104,18 @@ export class ClockifyTrigger implements INodeType {
switch (triggerField) { switch (triggerField) {
case EntryTypeEnum.NEW_TIME_ENTRY : case EntryTypeEnum.NEW_TIME_ENTRY :
default: default:
resource = `workspaces/${workspaceId}/user/${userInfo.id}/time-entries`; const workflowTimezone = this.getTimezone();
resource = `workspaces/${workspaceId}/user/${webhookData.userId}/time-entries`;
qs.start = webhookData.lastTimeChecked; qs.start = webhookData.lastTimeChecked;
qs.end = moment().toISOString(); qs.end = moment().tz(workflowTimezone).format('YYYY-MM-DDTHH:mm:SS') + 'Z';
qs.hydrated = true; qs.hydrated = true;
qs['in-progress'] = false; qs['in-progress'] = false;
break; break;
} }
try { result = await clockifyApiRequest.call(this, 'GET', resource, {}, qs );
result = await clockifyApiRequest.call(this, 'GET', resource, {}, qs ); webhookData.lastTimeChecked = qs.end;
webhookData.lastTimeChecked = qs.end_date;
}
catch( e ) {
throw new Error(`Clockify Exception: ${e}`);
}
if (Array.isArray(result) && result.length !== 0) { if (Array.isArray(result) && result.length !== 0) {
result = [this.helpers.returnJsonArray(result)]; result = [this.helpers.returnJsonArray(result)];
} }

View file

@ -4,10 +4,10 @@ export interface IHourlyRateDto {
} }
enum MembershipStatusEnum { enum MembershipStatusEnum {
PENDING = "PENDING", PENDING = 'PENDING',
ACTIVE = "ACTIVE", ACTIVE = 'ACTIVE',
DECLINED = "DECLINED", DECLINED = 'DECLINED',
INACTIVE = "INACTIVE" INACTIVE = 'INACTIVE'
} }
export interface IMembershipDto { export interface IMembershipDto {

View file

@ -2,16 +2,16 @@ import { OptionsWithUri } from 'request';
import { import {
ILoadOptionsFunctions, ILoadOptionsFunctions,
IPollFunctions IPollFunctions
} from "n8n-core"; } from 'n8n-core';
import {IDataObject} from "n8n-workflow"; import { IDataObject } from 'n8n-workflow';
export async function clockifyApiRequest(this: ILoadOptionsFunctions | IPollFunctions, method: string, resource: string, body: any = {}, qs: IDataObject = {}, uri?: string, option: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any export async function clockifyApiRequest(this: ILoadOptionsFunctions | IPollFunctions, method: string, resource: string, body: any = {}, qs: IDataObject = {}, uri?: string, option: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
const credentials = this.getCredentials('clockifyApi'); const credentials = this.getCredentials('clockifyApi');
if (credentials === undefined) { if (credentials === undefined) {
throw new Error('No credentials got returned!'); throw new Error('No credentials got returned!');
} }
const BASE_URL = `https://api.clockify.me/api/v1`; const BASE_URL = 'https://api.clockify.me/api/v1';
const options: OptionsWithUri = { const options: OptionsWithUri = {
headers: { headers: {

View file

@ -1,4 +1,4 @@
import {IMembershipDto} from "./CommonDtos"; import { IMembershipDto } from './CommonDtos';
enum UserStatusEnum { enum UserStatusEnum {
ACTIVE, PENDING_EMAIL_VERIFICATION, DELETED ACTIVE, PENDING_EMAIL_VERIFICATION, DELETED

View file

@ -1,31 +1,31 @@
import {IHourlyRateDto, IMembershipDto} from "./CommonDtos"; import { IHourlyRateDto, IMembershipDto } from './CommonDtos';
enum AdminOnlyPagesEnum { enum AdminOnlyPagesEnum {
PROJECT ="PROJECT", PROJECT ='PROJECT',
TEAM = "TEAM", TEAM = 'TEAM',
REPORTS = "REPORTS" REPORTS = 'REPORTS',
} }
enum DaysOfWeekEnum { enum DaysOfWeekEnum {
MONDAY = "MONDAY", MONDAY = 'MONDAY',
TUESDAY = "TUESDAY", TUESDAY = 'TUESDAY',
WEDNESDAY = "WEDNESDAY", WEDNESDAY = 'WEDNESDAY',
THURSDAY = "THURSDAY", THURSDAY = 'THURSDAY',
FRIDAY = "FRIDAY", FRIDAY = 'FRIDAY',
SATURDAY = "SATURDAY", SATURDAY = 'SATURDAY',
SUNDAY = "SUNDAY" SUNDAY = 'SUNDAY',
} }
enum DatePeriodEnum { enum DatePeriodEnum {
DAYS="DAYS", DAYS = 'DAYS',
WEEKS = "WEEKS", WEEKS = 'WEEKS',
MONTHS = "MONTHS" MONTHS = 'MONTHS',
} }
enum AutomaticLockTypeEnum { enum AutomaticLockTypeEnum {
WEEKLY = "WEEKLY", WEEKLY = 'WEEKLY',
MONTHLY = "MONTHLY", MONTHLY = 'MONTHLY',
OLDER_THAN = "OLDER_THAN" OLDER_THAN = 'OLDER_THAN',
} }
interface IAutomaticLockDto { interface IAutomaticLockDto {
@ -74,4 +74,3 @@ export interface IWorkspaceDto {
name: string; name: string;
workspaceSettings: IWorkspaceSettingsDto; workspaceSettings: IWorkspaceSettingsDto;
} }

View file

Before

Width:  |  Height:  |  Size: 5.8 KiB

After

Width:  |  Height:  |  Size: 5.8 KiB

View file

@ -37,7 +37,7 @@
"dist/credentials/ChargebeeApi.credentials.js", "dist/credentials/ChargebeeApi.credentials.js",
"dist/credentials/ClearbitApi.credentials.js", "dist/credentials/ClearbitApi.credentials.js",
"dist/credentials/ClickUpApi.credentials.js", "dist/credentials/ClickUpApi.credentials.js",
"dist/credentials/ClockifyApi.credentials.js", "dist/credentials/ClockifyApi.credentials.js",
"dist/credentials/CodaApi.credentials.js", "dist/credentials/CodaApi.credentials.js",
"dist/credentials/CopperApi.credentials.js", "dist/credentials/CopperApi.credentials.js",
"dist/credentials/DisqusApi.credentials.js", "dist/credentials/DisqusApi.credentials.js",
@ -119,7 +119,7 @@
"dist/nodes/Clearbit/Clearbit.node.js", "dist/nodes/Clearbit/Clearbit.node.js",
"dist/nodes/ClickUp/ClickUp.node.js", "dist/nodes/ClickUp/ClickUp.node.js",
"dist/nodes/ClickUp/ClickUpTrigger.node.js", "dist/nodes/ClickUp/ClickUpTrigger.node.js",
"dist/nodes/Clockify/ClockifyTrigger.node.js", "dist/nodes/Clockify/ClockifyTrigger.node.js",
"dist/nodes/Coda/Coda.node.js", "dist/nodes/Coda/Coda.node.js",
"dist/nodes/Copper/CopperTrigger.node.js", "dist/nodes/Copper/CopperTrigger.node.js",
"dist/nodes/Cron.node.js", "dist/nodes/Cron.node.js",
@ -295,4 +295,3 @@
] ]
} }
} }