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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

127 lines
3.2 KiB
TypeScript
Raw Normal View History

import moment from 'moment-timezone';
2020-02-21 02:41:08 -08:00
import type {
IPollFunctions,
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 type { IUserDto } from './UserDtos';
import type { IWorkspaceDto } from './WorkpaceInterfaces';
export class ClockifyTrigger implements INodeType {
description: INodeTypeDescription = {
2020-02-21 02:41:08 -08:00
displayName: 'Clockify Trigger',
2024-06-26 00:35:59 -07:00
icon: { light: 'file:clockify.svg', dark: 'file:clockify.dark.svg' },
name: 'clockifyTrigger',
group: ['trigger'],
version: 1,
description: 'Listens to Clockify events',
defaults: {
2020-02-21 02:41:08 -08:00
name: 'Clockify Trigger',
},
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: [
{
refactor: Apply more `eslint-plugin-n8n-nodes-base` autofixable rules (#3432) * :zap: Update `lintfix` script * :shirt: Remove unneeded lint exceptions * :shirt: Run baseline `lintfix` * :shirt: Apply `node-param-description-miscased-url` (#3441) * :shirt: Apply `rule node-param-placeholder-miscased-id` (#3443) Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * :shirt: Apply `node-param-option-name-wrong-for-upsert` (#3446) * :shirt: Apply `node-param-min-value-wrong-for-limit` (#3442) Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * Apply `node-param-display-name-wrong-for-dynamic-options` (#3454) * :hammer: fix * :zap: Fix `Assigned To` fields Co-authored-by: Michael Kret <michael.k@radency.com> * :shirt: Apply `rule node-param-default-wrong-for-number` (#3453) * :shirt: Apply `node-param-default-wrong-for-string` (#3452) Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * Apply `node-param-display-name-miscased` (#3449) * :hammer: fix * :hammer: exceptions * :zap: review fixes * :shirt: Apply `node-param-description-lowercase-first-char` (#3451) * :zap: fix * :zap: review fixes * :zap: fix Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * :shirt: Apply `node-param-description-wrong-for-dynamic-options` (#3456) * Rule working as intended * Add rule * :fire: Remove repetitions * :shirt: Add exceptions Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * :shirt: Small fix for `node-param-description-wrong-for-dynamic-options` * :shirt: Apply `node-param-default-wrong-for-fixed-collection` (#3460) * :shirt: Apply `node-param-description-line-break-html-tag` (#3462) * :shirt: Run baseline `lintfix` * :shirt: Apply `node-param-options-type-unsorted-items` (#3459) * :zap: fix * :hammer: exceptions * Add exception for Salesmate and Zoom Co-authored-by: Michael Kret <michael.k@radency.com> Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * :zap: Restore `lintfix` command Co-authored-by: Omar Ajoue <krynble@gmail.com> Co-authored-by: Michael Kret <88898367+michael-radency@users.noreply.github.com> Co-authored-by: agobrech <45268029+agobrech@users.noreply.github.com> Co-authored-by: Michael Kret <michael.k@radency.com> Co-authored-by: brianinoa <54530642+brianinoa@users.noreply.github.com>
2022-06-03 10:23:49 -07:00
displayName: 'Workspace Name or ID',
name: 'workspaceId',
type: 'options',
description:
'Choose from the list, or specify an ID using an <a href="https://docs.n8n.io/code-examples/expressions/">expression</a>',
typeOptions: {
loadOptionsMethod: 'listWorkspaces',
},
required: true,
default: '',
},
refactor: Apply `eslint-plugin-n8n-nodes-base` autofixable rules (#3174) * :zap: Initial setup * :shirt: Update `.eslintignore` * :shirt: Autofix node-param-default-missing (#3173) * :fire: Remove duplicate key * :shirt: Add exceptions * :package: Update package-lock.json * :shirt: Apply `node-class-description-inputs-wrong-trigger-node` (#3176) * :shirt: Apply `node-class-description-inputs-wrong-regular-node` (#3177) * :shirt: Apply `node-class-description-outputs-wrong` (#3178) * :shirt: Apply `node-execute-block-double-assertion-for-items` (#3179) * :shirt: Apply `node-param-default-wrong-for-collection` (#3180) * :shirt: Apply node-param-default-wrong-for-boolean (#3181) * Autofixed default missing * Autofixed booleans, worked well * :zap: Fix params * :rewind: Undo exempted autofixes * :package: Update package-lock.json * :shirt: Apply node-class-description-missing-subtitle (#3182) * :zap: Fix missing comma * :shirt: Apply `node-param-default-wrong-for-fixed-collection` (#3184) * :shirt: Add exception for `node-class-description-missing-subtitle` * :shirt: Apply `node-param-default-wrong-for-multi-options` (#3185) * :shirt: Apply `node-param-collection-type-unsorted-items` (#3186) * Missing coma * :shirt: Apply `node-param-default-wrong-for-simplify` (#3187) * :shirt: Apply `node-param-description-comma-separated-hyphen` (#3190) * :shirt: Apply `node-param-description-empty-string` (#3189) * :shirt: Apply `node-param-description-excess-inner-whitespace` (#3191) * Rule looks good * Add whitespace rule in eslint config * :zao: fix * :shirt: Apply `node-param-description-identical-to-display-name` (#3193) * :shirt: Apply `node-param-description-missing-for-ignore-ssl-issues` (#3195) * :rewind: Revert ":zao: fix" This reverts commit ef8a76f3dfedffd1bdccf3178af8a8d90cf5a55c. * :shirt: Apply `node-param-description-missing-for-simplify` (#3196) * :shirt: Apply `node-param-description-missing-final-period` (#3194) * Rule working as intended * Add rule to eslint * :shirt: Apply node-param-description-missing-for-return-all (#3197) * :zap: Restore `lintfix` command Co-authored-by: agobrech <45268029+agobrech@users.noreply.github.com> Co-authored-by: Omar Ajoue <krynble@gmail.com> Co-authored-by: agobrech <ael.gobrecht@gmail.com> Co-authored-by: Michael Kret <michael.k@radency.com>
2022-04-22 09:29:51 -07:00
// eslint-disable-next-line n8n-nodes-base/node-param-default-missing
{
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;
}
}