This commit is contained in:
Ricardo Espinoza 2019-11-05 18:50:55 -05:00
parent c8b6217903
commit 9d50fb0c33

View file

@ -14,6 +14,7 @@ import {
} from './GenericFunctions'; } from './GenericFunctions';
import moment = require('moment'); import moment = require('moment');
import _ = require('lodash')
export class Todoist implements INodeType { export class Todoist implements INodeType {
@ -94,6 +95,27 @@ export class Todoist implements INodeType {
}, },
default: [], default: [],
description: 'The project you want to add the task to.', description: 'The project you want to add the task to.',
},
{
displayName: 'Labels',
name: 'labels',
type: 'multiOptions',
typeOptions: {
loadOptionsMethod: 'getLabels',
},
displayOptions: {
show: {
resource: [
'task',
],
operation: [
'create'
]
},
},
default: [],
required: false,
description: 'Labels',
}, },
{ {
displayName: 'Content', displayName: 'Content',
@ -115,7 +137,7 @@ export class Todoist implements INodeType {
default: [], default: [],
required: true, required: true,
description: 'Task content', description: 'Task content',
}, },
{ {
displayName: 'Options', displayName: 'Options',
name: 'options', name: 'options',
@ -187,12 +209,34 @@ export class Todoist implements INodeType {
}); });
} }
return returnData
},
// 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 return returnData
} }
}, }
}; };
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;
@ -205,9 +249,9 @@ export class Todoist implements INodeType {
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;
const labels = this.getNodeParameter('labels') as [number];
const options = this.getNodeParameter('options') as IDataObject; const options = this.getNodeParameter('options') as IDataObject;
interface IBodyCreateTask { interface IBodyCreateTask {
content: string; content: string;
project_id?: number; project_id?: number;
@ -224,17 +268,23 @@ export class Todoist implements INodeType {
const body: IBodyCreateTask = { const body: IBodyCreateTask = {
content, content,
project_id: projectId, project_id: projectId,
priority: (options.priority!) ? parseInt(options.priority, 10) : 1, priority: (options.priority!) ? parseInt(options.priority as string, 10) : 1,
} };
if (options.dueDateTime) { if (options.dueDateTime) {
body.due_datetime = moment(options.dueDateTime).utc().format() body.due_datetime = moment(options.dueDateTime as string).utc().format();
} }
if (options.dueString) { if (options.dueString) {
body.due_string = options.dueString body.due_string = options.dueString as string;
} }
if (!_.isEmpty(labels)) {
body.label_ids = labels
}
console.log(labels)
try { try {
response = await todoistApiRequest.call(this, '/tasks', 'POST', body); response = await todoistApiRequest.call(this, '/tasks', 'POST', body);
} catch (err) { } catch (err) {
@ -243,7 +293,7 @@ export class Todoist implements INodeType {
} }
return { return {
json: response, json: response
}; };
} }
} }