n8n/packages/nodes-base/nodes/Toggl/TogglTrigger.node.ts
Iván Ovejero 027dfb2f0a
Enable esModuleInterop compiler option and upgrade to TypeScript 4.6 (#3106)
*  Enable `esModuleInterop` for /core

*  Adjust imports in /core

*  Enable `esModuleInterop` for /cli

*  Adjust imports in /cli

*  Enable `esModuleInterop` for /nodes-base

*  Adjust imports in /nodes-base

*  Make imports consistent

* ⬆️ Upgrade TypeScript to 4.6 (#3109)

* ⬆️ Upgrade TypeScript to 4.6

* 📦 Update package-lock.json

* 🔧 Avoid erroring on untyped errors

* 📘 Fix type error

Co-authored-by: Jan Oberhauser <jan.oberhauser@gmail.com>
2022-04-08 23:32:08 +02:00

81 lines
1.8 KiB
TypeScript

import { IPollFunctions } from 'n8n-core';
import {
IDataObject,
INodeExecutionData,
INodeType,
INodeTypeDescription,
NodeApiError,
NodeOperationError,
} from 'n8n-workflow';
import moment from 'moment';
import { togglApiRequest } from './GenericFunctions';
export class TogglTrigger implements INodeType {
description: INodeTypeDescription = {
displayName: 'Toggl Trigger',
name: 'togglTrigger',
icon: 'file:toggl.png',
group: ['trigger'],
version: 1,
description: 'Starts the workflow when Toggl events occur',
defaults: {
name: 'Toggl',
},
credentials: [
{
name: 'togglApi',
required: true,
},
],
polling: true,
inputs: [],
outputs: ['main'],
properties: [
{
displayName: 'Event',
name: 'event',
type: 'options',
options: [
{
name: 'New Time Entry',
value: 'newTimeEntry',
},
],
required: true,
default: 'newTimeEntry',
},
],
};
async poll(this: IPollFunctions): Promise<INodeExecutionData[][] | null> {
const webhookData = this.getWorkflowStaticData('node');
const event = this.getNodeParameter('event') as string;
let endpoint: string;
if (event === 'newTimeEntry') {
endpoint = '/time_entries';
} else {
throw new NodeOperationError(this.getNode(), `The defined event "${event}" is not supported`);
}
const qs: IDataObject = {};
let timeEntries = [];
qs.start_date = webhookData.lastTimeChecked;
qs.end_date = moment().format();
try {
timeEntries = await togglApiRequest.call(this, 'GET', endpoint, {}, qs);
webhookData.lastTimeChecked = qs.end_date;
} catch (error) {
throw new NodeApiError(this.getNode(), error);
}
if (Array.isArray(timeEntries) && timeEntries.length !== 0) {
return [this.helpers.returnJsonArray(timeEntries)];
}
return null;
}
}