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';
import moment = require('moment');
import _ = require('lodash')
export class Todoist implements INodeType {
@ -95,6 +96,27 @@ export class Todoist implements INodeType {
default: [],
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',
name: 'content',
@ -188,10 +210,32 @@ 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
}
}
};
async executeSingle(this: IExecuteSingleFunctions): Promise<INodeExecutionData> {
@ -205,9 +249,9 @@ export class Todoist implements INodeType {
const content = this.getNodeParameter('content') as string;
const projectId = this.getNodeParameter('project') as number;
const labels = this.getNodeParameter('labels') as [number];
const options = this.getNodeParameter('options') as IDataObject;
interface IBodyCreateTask {
content: string;
project_id?: number;
@ -224,17 +268,23 @@ export class Todoist implements INodeType {
const body: IBodyCreateTask = {
content,
project_id: projectId,
priority: (options.priority!) ? parseInt(options.priority, 10) : 1,
}
priority: (options.priority!) ? parseInt(options.priority as string, 10) : 1,
};
if (options.dueDateTime) {
body.due_datetime = moment(options.dueDateTime).utc().format()
body.due_datetime = moment(options.dueDateTime as string).utc().format();
}
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 {
response = await todoistApiRequest.call(this, '/tasks', 'POST', body);
} catch (err) {
@ -243,7 +293,7 @@ export class Todoist implements INodeType {
}
return {
json: response,
json: response
};
}
}