mirror of
https://github.com/n8n-io/n8n.git
synced 2025-03-05 20:50:17 -08:00
⚡ Todoist node enhancement (#763)
This commit is contained in:
parent
815e663717
commit
226dbce5c6
|
@ -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) {
|
||||
|
@ -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;
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import {
|
||||
import {
|
||||
IExecuteSingleFunctions,
|
||||
} from 'n8n-core';
|
||||
import {
|
||||
|
@ -11,6 +11,7 @@ import {
|
|||
} from 'n8n-workflow';
|
||||
import {
|
||||
todoistApiRequest,
|
||||
filterAndExecuteForEachTask,
|
||||
} from './GenericFunctions';
|
||||
|
||||
|
||||
|
@ -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.',
|
||||
|
@ -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',
|
||||
|
@ -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',
|
||||
|
@ -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;
|
||||
|
@ -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
|
||||
};
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue