Skip to content

Commit

Permalink
⚡ Todoist node enhancement (#763)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukigarazus authored Aug 17, 2020
1 parent 815e663 commit 226dbce
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 18 deletions.
40 changes: 38 additions & 2 deletions packages/nodes-base/nodes/Todoist/GenericFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,43 @@ import {

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');

if (credentials === undefined) {
Expand All @@ -32,7 +68,7 @@ export async function todoistApiRequest(this: IHookFunctions | IExecuteFunctions
}

try {
return await this.helpers.request!(options);
return this.helpers.request!(options);
} catch (error) {
const errorMessage = error.response.body.message || error.response.body.Message;

Expand Down
101 changes: 85 additions & 16 deletions packages/nodes-base/nodes/Todoist/Todoist.node.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {
import {
IExecuteSingleFunctions,
} from 'n8n-core';
import {
Expand All @@ -11,6 +11,7 @@ import {
} from 'n8n-workflow';
import {
todoistApiRequest,
filterAndExecuteForEachTask,
} from './GenericFunctions';


Expand Down Expand Up @@ -83,6 +84,26 @@ export class Todoist implements INodeType {
value: 'create',
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',
description: 'The operation to perform.',
Expand All @@ -101,11 +122,13 @@ export class Todoist implements INodeType {
],
operation: [
'create',
'close_match',
'delete_match',
]
},
},
default: '',
description: 'The project you want to add the task to.',
description: 'The project you want to operate on.',
},
{
displayName: 'Labels',
Expand Down Expand Up @@ -149,6 +172,31 @@ export class Todoist implements INodeType {
required: true,
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',
name: 'options',
Expand Down Expand Up @@ -249,12 +297,23 @@ export class Todoist implements INodeType {
};

async executeSingle(this: IExecuteSingleFunctions): Promise<INodeExecutionData> {

const resource = this.getNodeParameter('resource') as string;
const opeation = this.getNodeParameter('operation') as string;
let response;
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}` } };
}
}
}

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
const content = this.getNodeParameter('content') as string;
const projectId = this.getNodeParameter('project') as number;
Expand All @@ -279,15 +338,25 @@ export class Todoist implements INodeType {
body.label_ids = labels;
}

try {
response = await todoistApiRequest.call(this, '/tasks', 'POST', body);
} catch (err) {
throw new Error(`Todoist Error: ${err}`);
}
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')
);
}

return {
json: response
};
}
}
};

0 comments on commit 226dbce

Please sign in to comment.