2019-12-31 12:19:37 -08:00
|
|
|
import { IPollFunctions } from 'n8n-core';
|
2019-12-21 18:44:56 -08:00
|
|
|
import {
|
2020-10-01 05:01:39 -07:00
|
|
|
IDataObject,
|
2019-12-31 12:19:37 -08:00
|
|
|
INodeExecutionData,
|
2019-12-21 18:44:56 -08:00
|
|
|
INodeType,
|
|
|
|
INodeTypeDescription,
|
2021-04-16 09:33:36 -07:00
|
|
|
NodeApiError,
|
|
|
|
NodeOperationError,
|
2019-12-21 18:44:56 -08:00
|
|
|
} from 'n8n-workflow';
|
|
|
|
|
2022-04-08 14:32:08 -07:00
|
|
|
import moment from 'moment';
|
2019-12-21 18:44:56 -08:00
|
|
|
import { togglApiRequest } from './GenericFunctions';
|
|
|
|
|
|
|
|
export class TogglTrigger implements INodeType {
|
|
|
|
description: INodeTypeDescription = {
|
2020-01-05 07:52:00 -08:00
|
|
|
displayName: 'Toggl Trigger',
|
2020-05-12 06:08:19 -07:00
|
|
|
name: 'togglTrigger',
|
2022-06-20 07:54:01 -07:00
|
|
|
// eslint-disable-next-line n8n-nodes-base/node-class-description-icon-not-svg
|
2019-12-21 18:44:56 -08:00
|
|
|
icon: 'file:toggl.png',
|
|
|
|
group: ['trigger'],
|
|
|
|
version: 1,
|
2021-05-15 09:02:07 -07:00
|
|
|
description: 'Starts the workflow when Toggl events occur',
|
2019-12-21 18:44:56 -08:00
|
|
|
defaults: {
|
|
|
|
name: 'Toggl',
|
|
|
|
},
|
|
|
|
credentials: [
|
|
|
|
{
|
|
|
|
name: 'togglApi',
|
|
|
|
required: true,
|
2020-10-22 06:46:03 -07:00
|
|
|
},
|
2019-12-21 18:44:56 -08:00
|
|
|
],
|
2019-12-31 12:19:37 -08:00
|
|
|
polling: true,
|
2019-12-21 18:44:56 -08:00
|
|
|
inputs: [],
|
|
|
|
outputs: ['main'],
|
|
|
|
properties: [
|
|
|
|
{
|
|
|
|
displayName: 'Event',
|
|
|
|
name: 'event',
|
|
|
|
type: 'options',
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'New Time Entry',
|
|
|
|
value: 'newTimeEntry',
|
2020-10-22 06:46:03 -07:00
|
|
|
},
|
2019-12-21 18:44:56 -08:00
|
|
|
],
|
|
|
|
required: true,
|
|
|
|
default: 'newTimeEntry',
|
|
|
|
},
|
2020-10-22 06:46:03 -07:00
|
|
|
],
|
2019-12-21 18:44:56 -08:00
|
|
|
};
|
|
|
|
|
2019-12-31 12:19:37 -08:00
|
|
|
async poll(this: IPollFunctions): Promise<INodeExecutionData[][] | null> {
|
2019-12-21 18:44:56 -08:00
|
|
|
const webhookData = this.getWorkflowStaticData('node');
|
|
|
|
const event = this.getNodeParameter('event') as string;
|
|
|
|
let endpoint: string;
|
2019-12-31 12:19:37 -08:00
|
|
|
|
2019-12-21 18:44:56 -08:00
|
|
|
if (event === 'newTimeEntry') {
|
|
|
|
endpoint = '/time_entries';
|
2019-12-31 12:19:37 -08:00
|
|
|
} else {
|
2021-04-16 09:33:36 -07:00
|
|
|
throw new NodeOperationError(this.getNode(), `The defined event "${event}" is not supported`);
|
2019-12-21 18:44:56 -08:00
|
|
|
}
|
|
|
|
|
2019-12-31 12:19:37 -08:00
|
|
|
const qs: IDataObject = {};
|
|
|
|
let timeEntries = [];
|
|
|
|
qs.start_date = webhookData.lastTimeChecked;
|
|
|
|
qs.end_date = moment().format();
|
2019-12-21 18:44:56 -08:00
|
|
|
|
2019-12-31 12:19:37 -08:00
|
|
|
try {
|
|
|
|
timeEntries = await togglApiRequest.call(this, 'GET', endpoint, {}, qs);
|
|
|
|
webhookData.lastTimeChecked = qs.end_date;
|
2021-04-16 09:33:36 -07:00
|
|
|
} catch (error) {
|
|
|
|
throw new NodeApiError(this.getNode(), error);
|
2019-12-21 18:44:56 -08:00
|
|
|
}
|
2019-12-31 12:19:37 -08:00
|
|
|
if (Array.isArray(timeEntries) && timeEntries.length !== 0) {
|
|
|
|
return [this.helpers.returnJsonArray(timeEntries)];
|
2019-12-21 18:44:56 -08:00
|
|
|
}
|
2019-12-31 12:19:37 -08:00
|
|
|
|
|
|
|
return null;
|
2019-12-21 18:44:56 -08:00
|
|
|
}
|
|
|
|
}
|