Todoist node enhancement (#763)

This commit is contained in:
Łukasz 2020-08-17 22:41:05 +02:00 committed by GitHub
parent 815e663717
commit 226dbce5c6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 123 additions and 18 deletions

View file

@ -9,7 +9,43 @@ import {
import * as _ from 'lodash'; import * as _ from 'lodash';
export async function todoistApiRequest(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, resource: string, method: string, body: any = {}, headers?: object): Promise<any> { // tslint:disable-line:no-any export const filterAndExecuteForEachTask = async function(
this: IExecuteSingleFunctions,
taskCallback: (t: any) => any
) {
const expression = this.getNodeParameter('expression') as string;
const projectId = this.getNodeParameter('project') as number;
// Enable regular expressions
const reg = new RegExp(expression);
const tasks = await todoistApiRequest.call(this, '/tasks', 'GET');
const filteredTasks = tasks.filter(
// Make sure that project will match no matter what the type is. If project was not selected match all projects
(el: any) => (!projectId || el.project_id) && el.content.match(reg)
);
return {
affectedTasks: (
await Promise.all(filteredTasks.map((t: any) => taskCallback(t)))
)
// This makes it more clear and informative. We pass the ID as a convention and content to give the user confirmation that his/her expression works as expected
.map(
(el, i) =>
el || { id: filteredTasks[i].id, content: filteredTasks[i].content }
)
};
};
export async function todoistApiRequest(
this:
| IHookFunctions
| IExecuteFunctions
| IExecuteSingleFunctions
| ILoadOptionsFunctions,
resource: string,
method: string,
body: any = {},
headers?: object
): Promise<any> {
// tslint:disable-line:no-any
const credentials = this.getCredentials('todoistApi'); const credentials = this.getCredentials('todoistApi');
if (credentials === undefined) { if (credentials === undefined) {
@ -32,7 +68,7 @@ export async function todoistApiRequest(this: IHookFunctions | IExecuteFunctions
} }
try { try {
return await this.helpers.request!(options); return this.helpers.request!(options);
} catch (error) { } catch (error) {
const errorMessage = error.response.body.message || error.response.body.Message; const errorMessage = error.response.body.message || error.response.body.Message;

View file

@ -1,4 +1,4 @@
import { import {
IExecuteSingleFunctions, IExecuteSingleFunctions,
} from 'n8n-core'; } from 'n8n-core';
import { import {
@ -11,6 +11,7 @@ import {
} from 'n8n-workflow'; } from 'n8n-workflow';
import { import {
todoistApiRequest, todoistApiRequest,
filterAndExecuteForEachTask,
} from './GenericFunctions'; } from './GenericFunctions';
@ -83,6 +84,26 @@ export class Todoist implements INodeType {
value: 'create', value: 'create',
description: 'Create a new task', description: 'Create a new task',
}, },
{
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',
},
], ],
default: 'create', default: 'create',
description: 'The operation to perform.', description: 'The operation to perform.',
@ -101,11 +122,13 @@ export class Todoist implements INodeType {
], ],
operation: [ operation: [
'create', 'create',
'close_match',
'delete_match',
] ]
}, },
}, },
default: '', default: '',
description: 'The project you want to add the task to.', description: 'The project you want to operate on.',
}, },
{ {
displayName: 'Labels', displayName: 'Labels',
@ -149,6 +172,31 @@ export class Todoist implements INodeType {
required: true, required: true,
description: 'Task content', description: 'Task content',
}, },
{
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']
}
}
},
{ {
displayName: 'Options', displayName: 'Options',
name: 'options', name: 'options',
@ -249,12 +297,23 @@ export class Todoist implements INodeType {
}; };
async executeSingle(this: IExecuteSingleFunctions): Promise<INodeExecutionData> { async executeSingle(this: IExecuteSingleFunctions): Promise<INodeExecutionData> {
const resource = this.getNodeParameter('resource') as string; const resource = this.getNodeParameter('resource') as string;
const opeation = this.getNodeParameter('operation') as string; const operation = this.getNodeParameter('operation') as string;
let response; try {
return {
json: { result: await OPERATIONS[resource]?.[operation]?.bind(this)() }
};
} catch (err) {
return { json: { error: `Todoist Error: ${err.message}` } };
}
}
}
if (resource === 'task' && opeation === 'create') { const OPERATIONS: {
[key: string]: { [key: string]: (this: IExecuteSingleFunctions) => any };
} = {
task: {
create(this: IExecuteSingleFunctions) {
//https://developer.todoist.com/rest/v1/#create-a-new-task //https://developer.todoist.com/rest/v1/#create-a-new-task
const content = this.getNodeParameter('content') as string; const content = this.getNodeParameter('content') as string;
const projectId = this.getNodeParameter('project') as number; const projectId = this.getNodeParameter('project') as number;
@ -279,15 +338,25 @@ export class Todoist implements INodeType {
body.label_ids = labels; body.label_ids = labels;
} }
try { return todoistApiRequest.call(this, '/tasks', 'POST', body);
response = await todoistApiRequest.call(this, '/tasks', 'POST', body); },
} catch (err) { close_id(this: IExecuteSingleFunctions) {
throw new Error(`Todoist Error: ${err}`); 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')
);
} }
return {
json: response
};
} }
} };