n8n/packages/nodes-base/nodes/Todoist/Todoist.node.ts

363 lines
8 KiB
TypeScript
Raw Normal View History

2020-08-17 13:41:05 -07:00
import {
2019-11-05 07:17:06 -08:00
IExecuteSingleFunctions,
} from 'n8n-core';
import {
IDataObject,
INodeTypeDescription,
INodeExecutionData,
INodeType,
ILoadOptionsFunctions,
INodePropertyOptions,
} from 'n8n-workflow';
import {
todoistApiRequest,
2020-08-17 13:41:05 -07:00
filterAndExecuteForEachTask,
2019-11-05 07:17:06 -08:00
} from './GenericFunctions';
interface IBodyCreateTask {
content: string;
project_id?: number;
parent?: number;
order?: number;
label_ids?: number[];
priority?: number;
due_string?: string;
due_datetime?: string;
due_date?: string;
due_lang?: string;
}
2019-11-05 07:17:06 -08:00
2019-11-05 12:56:10 -08:00
export class Todoist implements INodeType {
2019-11-05 07:17:06 -08:00
description: INodeTypeDescription = {
displayName: 'Todoist',
name: 'todoist',
icon: 'file:todoist.png',
group: ['output'],
version: 1,
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
description: 'Consume Todoist API',
defaults: {
name: 'Todoist',
color: '#c02428',
},
inputs: ['main'],
outputs: ['main'],
credentials: [
{
name: 'todoistApi',
required: true,
}
],
2019-11-05 07:17:06 -08:00
properties: [
{
2019-11-05 12:56:10 -08:00
displayName: 'Resource',
name: 'resource',
type: 'options',
options: [
{
name: 'Task',
value: 'task',
description: 'Task resource.',
},
],
default: 'task',
2019-11-05 12:56:10 -08:00
required: true,
description: 'Resource to consume.',
},
{
displayName: 'Operation',
name: 'operation',
type: 'options',
required: true,
displayOptions: {
show: {
resource: [
'task',
],
},
2019-11-05 07:17:06 -08:00
},
options: [
{
2019-11-05 12:56:10 -08:00
name: 'Create',
value: 'create',
description: 'Create a new task',
2019-11-05 07:17:06 -08:00
},
2020-08-17 13:41:05 -07:00
{
name: 'Close by ID',
value: 'close_id',
description: 'Close a task by passing an ID',
},
{
name: 'Close matching',
value: 'close_match',
description: 'Close a task by passing a regular expression',
},
{
name: 'Delete by ID',
value: 'delete_id',
description: 'Delete a task by passing an ID',
},
{
name: 'Delete matching',
value: 'delete_match',
description: 'Delete a task by passing a regular expression',
},
2019-11-05 07:17:06 -08:00
],
2019-11-05 12:56:10 -08:00
default: 'create',
description: 'The operation to perform.',
},
{
2019-11-05 12:56:10 -08:00
displayName: 'Project',
name: 'project',
type: 'options',
typeOptions: {
2019-11-05 12:56:10 -08:00
loadOptionsMethod: 'getProjects',
},
displayOptions: {
show: {
resource: [
'task',
],
operation: [
'create',
2020-08-17 13:41:05 -07:00
'close_match',
'delete_match',
]
2019-11-05 12:56:10 -08:00
},
},
2020-01-04 20:19:10 -08:00
default: '',
2020-08-17 13:41:05 -07:00
description: 'The project you want to operate on.',
2019-11-05 15:50:55 -08:00
},
{
displayName: 'Labels',
name: 'labels',
type: 'multiOptions',
typeOptions: {
loadOptionsMethod: 'getLabels',
},
displayOptions: {
show: {
resource: [
'task',
],
operation: [
'create',
]
2019-11-05 15:50:55 -08:00
},
},
default: [],
required: false,
2019-11-05 15:50:55 -08:00
description: 'Labels',
},
{
2019-11-05 12:56:10 -08:00
displayName: 'Content',
name: 'content',
type: 'string',
typeOptions: {
2019-11-05 12:56:10 -08:00
rows: 5,
},
displayOptions: {
show: {
resource: [
'task',
],
operation: [
'create',
]
2019-11-05 12:56:10 -08:00
},
},
2020-01-04 20:19:10 -08:00
default: '',
required: true,
2019-11-05 12:56:10 -08:00
description: 'Task content',
2019-11-05 15:50:55 -08:00
},
2020-08-17 13:41:05 -07:00
{
displayName: 'ID',
name: 'id',
type: 'string',
default: '',
required: true,
typeOptions: { rows: 1 },
displayOptions: {
show: { resource: ['task'], operation: ['close_id', 'delete_id'] }
},
},
{
displayName: 'Expression to match',
name: 'expression',
type: 'string',
default: '',
required: true,
typeOptions: { rows: 1 },
displayOptions: {
show: {
resource: ['task'],
operation: ['close_match', 'delete_match']
}
}
},
{
2019-11-05 12:56:10 -08:00
displayName: 'Options',
name: 'options',
type: 'collection',
placeholder: 'Add Option',
default: {},
displayOptions: {
show: {
resource: [
'task',
],
operation: [
'create',
]
2019-11-05 12:56:10 -08:00
},
},
options: [
{
displayName: 'Priority',
name: 'priority',
type: 'number',
typeOptions: {
numberStepSize: 1,
maxValue: 4,
minValue: 1,
},
2019-11-05 12:56:10 -08:00
default: 1,
description: 'Task priority from 1 (normal) to 4 (urgent).',
},
{
2019-11-05 12:56:10 -08:00
displayName: 'Due Date Time',
name: 'dueDateTime',
type: 'dateTime',
2019-11-05 12:56:10 -08:00
default: '',
description: 'Specific date and time in RFC3339 format in UTC.',
},
{
displayName: 'Due String',
name: 'dueString',
type: 'string',
2019-11-05 12:56:10 -08:00
default: '',
description: 'Human defined task due date (ex.: “next Monday”, “Tomorrow”). Value is set using local (not UTC) time.',
},
]
}
]
};
2019-11-05 07:17:06 -08:00
methods = {
2019-11-05 12:56:10 -08:00
loadOptions: {
// Get all the available projects to display them to user so that he can
// select them easily
async getProjects(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const returnData: INodePropertyOptions[] = [];
let projects;
try {
projects = await todoistApiRequest.call(this, '/projects', 'GET');
} catch (err) {
throw new Error(`Todoist Error: ${err}`);
}
for (const project of projects) {
const projectName = project.name;
const projectId = project.id;
returnData.push({
name: projectName,
value: projectId,
});
}
return returnData;
2019-11-05 15:50:55 -08:00
},
// Get all the available labels to display them to user so that he can
// select them easily
async getLabels(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const returnData: INodePropertyOptions[] = [];
let labels;
try {
labels = await todoistApiRequest.call(this, '/labels', 'GET');
} catch (err) {
throw new Error(`Todoist Error: ${err}`);
}
for (const label of labels) {
const labelName = label.name;
const labelId = label.id;
returnData.push({
name: labelName,
value: labelId,
});
}
return returnData;
2019-11-05 12:56:10 -08:00
}
2019-11-05 15:50:55 -08:00
}
};
2019-11-05 12:56:10 -08:00
async executeSingle(this: IExecuteSingleFunctions): Promise<INodeExecutionData> {
const resource = this.getNodeParameter('resource') as string;
2020-08-17 13:41:05 -07:00
const operation = this.getNodeParameter('operation') as string;
try {
return {
json: { result: await OPERATIONS[resource]?.[operation]?.bind(this)() }
};
} catch (err) {
return { json: { error: `Todoist Error: ${err.message}` } };
}
}
}
2019-11-05 12:56:10 -08:00
2020-08-17 13:41:05 -07:00
const OPERATIONS: {
[key: string]: { [key: string]: (this: IExecuteSingleFunctions) => any };
} = {
task: {
create(this: IExecuteSingleFunctions) {
//https://developer.todoist.com/rest/v1/#create-a-new-task
2019-11-05 12:56:10 -08:00
const content = this.getNodeParameter('content') as string;
const projectId = this.getNodeParameter('project') as number;
const labels = this.getNodeParameter('labels') as number[];
2019-11-05 12:56:10 -08:00
const options = this.getNodeParameter('options') as IDataObject;
const body: IBodyCreateTask = {
content,
project_id: projectId,
2019-11-05 15:50:55 -08:00
priority: (options.priority!) ? parseInt(options.priority as string, 10) : 1,
};
2019-11-05 12:56:10 -08:00
if (options.dueDateTime) {
body.due_datetime = options.dueDateTime as string;
2019-11-05 12:56:10 -08:00
}
if (options.dueString) {
2019-11-05 15:50:55 -08:00
body.due_string = options.dueString as string;
}
if (labels !== undefined && labels.length !== 0) {
body.label_ids = labels;
2019-11-05 12:56:10 -08:00
}
2020-08-17 13:41:05 -07:00
return todoistApiRequest.call(this, '/tasks', 'POST', body);
},
close_id(this: IExecuteSingleFunctions) {
const id = this.getNodeParameter('id') as string;
return todoistApiRequest.call(this, `/tasks/${id}/close`, 'POST');
},
delete_id(this: IExecuteSingleFunctions) {
const id = this.getNodeParameter('id') as string;
return todoistApiRequest.call(this, `/tasks/${id}`, 'DELETE');
},
close_match(this) {
return filterAndExecuteForEachTask.call(this, t =>
todoistApiRequest.call(this, `/tasks/${t.id}/close`, 'POST')
);
},
delete_match(this) {
return filterAndExecuteForEachTask.call(this, t =>
todoistApiRequest.call(this, `/tasks/${t.id}`, 'DELETE')
);
2019-11-05 12:56:10 -08:00
}
2019-11-05 07:17:06 -08:00
}
2020-08-17 13:41:05 -07:00
};