2022-10-21 11:52:43 -07:00
|
|
|
import { IDataObject, jsonParse } from 'n8n-workflow';
|
2022-08-17 08:50:24 -07:00
|
|
|
import {
|
|
|
|
Context,
|
|
|
|
FormatDueDatetime,
|
|
|
|
todoistApiRequest,
|
|
|
|
todoistSyncRequest,
|
2022-11-29 03:37:37 -08:00
|
|
|
} from '../GenericFunctions';
|
2022-12-21 01:46:26 -08:00
|
|
|
import type { Section, TodoistResponse } from './Service';
|
2022-08-17 08:50:24 -07:00
|
|
|
import { v4 as uuid } from 'uuid';
|
2022-06-20 16:42:08 -07:00
|
|
|
|
|
|
|
export interface OperationHandler {
|
|
|
|
handleOperation(ctx: Context, itemIndex: number): Promise<TodoistResponse>;
|
|
|
|
}
|
|
|
|
|
2023-01-13 09:11:56 -08:00
|
|
|
export interface CreateTaskRequest {
|
|
|
|
content?: string;
|
|
|
|
description?: string;
|
|
|
|
project_id?: number;
|
|
|
|
section_id?: number;
|
|
|
|
parent_id?: string;
|
|
|
|
order?: number;
|
|
|
|
labels?: string[];
|
|
|
|
priority?: number;
|
|
|
|
due_string?: string;
|
|
|
|
due_datetime?: string;
|
|
|
|
due_date?: string;
|
|
|
|
due_lang?: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface SyncRequest {
|
|
|
|
commands: Command[];
|
|
|
|
temp_id_mapping?: IDataObject;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface Command {
|
|
|
|
type: CommandType;
|
|
|
|
uuid: string;
|
|
|
|
temp_id?: string;
|
|
|
|
args: {
|
|
|
|
id?: number;
|
|
|
|
section_id?: number;
|
|
|
|
project_id?: number | string;
|
|
|
|
section?: string;
|
|
|
|
content?: string;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export enum CommandType {
|
|
|
|
ITEM_MOVE = 'item_move',
|
|
|
|
ITEM_ADD = 'item_add',
|
|
|
|
ITEM_UPDATE = 'item_update',
|
|
|
|
ITEM_REORDER = 'item_reorder',
|
|
|
|
ITEM_DELETE = 'item_delete',
|
|
|
|
ITEM_COMPLETE = 'item_complete',
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getLabelNameFromId(ctx: Context, labelIds: number[]): Promise<string[]> {
|
|
|
|
const labelList = [];
|
|
|
|
for (const label of labelIds) {
|
|
|
|
const thisLabel = await todoistApiRequest.call(ctx, 'GET', `/labels/${label}`);
|
|
|
|
labelList.push(thisLabel.name);
|
|
|
|
}
|
|
|
|
return labelList;
|
|
|
|
}
|
|
|
|
|
2022-06-20 16:42:08 -07:00
|
|
|
export class CreateHandler implements OperationHandler {
|
|
|
|
async handleOperation(ctx: Context, itemIndex: number): Promise<TodoistResponse> {
|
2022-11-29 03:37:37 -08:00
|
|
|
//https://developer.todoist.com/rest/v2/#create-a-new-task
|
2022-06-20 16:42:08 -07:00
|
|
|
const content = ctx.getNodeParameter('content', itemIndex) as string;
|
2022-11-29 03:37:37 -08:00
|
|
|
const projectId = ctx.getNodeParameter('project', itemIndex, undefined, {
|
|
|
|
extractValue: true,
|
|
|
|
}) as number;
|
2022-06-20 16:42:08 -07:00
|
|
|
const labels = ctx.getNodeParameter('labels', itemIndex) as number[];
|
|
|
|
const options = ctx.getNodeParameter('options', itemIndex) as IDataObject;
|
|
|
|
|
|
|
|
const body: CreateTaskRequest = {
|
|
|
|
content,
|
|
|
|
project_id: projectId,
|
2022-08-17 08:50:24 -07:00
|
|
|
priority: options.priority! ? parseInt(options.priority as string, 10) : 1,
|
2022-06-20 16:42:08 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
if (options.description) {
|
|
|
|
body.description = options.description as string;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.dueDateTime) {
|
|
|
|
body.due_datetime = FormatDueDatetime(options.dueDateTime as string);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.dueString) {
|
|
|
|
body.due_string = options.dueString as string;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (labels !== undefined && labels.length !== 0) {
|
2022-11-29 03:37:37 -08:00
|
|
|
body.labels = await getLabelNameFromId(ctx, labels);
|
2022-06-20 16:42:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (options.section) {
|
|
|
|
body.section_id = options.section as number;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.dueLang) {
|
|
|
|
body.due_lang = options.dueLang as string;
|
|
|
|
}
|
|
|
|
|
2022-11-29 03:37:37 -08:00
|
|
|
if (options.parentId) {
|
|
|
|
body.parent_id = options.parentId as string;
|
|
|
|
}
|
|
|
|
|
2022-06-20 16:42:08 -07:00
|
|
|
const data = await todoistApiRequest.call(ctx, 'POST', '/tasks', body);
|
|
|
|
|
|
|
|
return {
|
|
|
|
data,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class CloseHandler implements OperationHandler {
|
|
|
|
async handleOperation(ctx: Context, itemIndex: number): Promise<TodoistResponse> {
|
|
|
|
const id = ctx.getNodeParameter('taskId', itemIndex) as string;
|
|
|
|
|
|
|
|
await todoistApiRequest.call(ctx, 'POST', `/tasks/${id}/close`);
|
|
|
|
|
|
|
|
return {
|
|
|
|
success: true,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class DeleteHandler implements OperationHandler {
|
|
|
|
async handleOperation(ctx: Context, itemIndex: number): Promise<TodoistResponse> {
|
|
|
|
const id = ctx.getNodeParameter('taskId', itemIndex) as string;
|
|
|
|
|
2022-11-08 06:28:21 -08:00
|
|
|
await todoistApiRequest.call(ctx, 'DELETE', `/tasks/${id}`);
|
2022-06-20 16:42:08 -07:00
|
|
|
|
|
|
|
return {
|
|
|
|
success: true,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class GetHandler implements OperationHandler {
|
|
|
|
async handleOperation(ctx: Context, itemIndex: number): Promise<TodoistResponse> {
|
|
|
|
const id = ctx.getNodeParameter('taskId', itemIndex) as string;
|
|
|
|
|
|
|
|
const responseData = await todoistApiRequest.call(ctx, 'GET', `/tasks/${id}`);
|
|
|
|
return {
|
|
|
|
data: responseData,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class GetAllHandler implements OperationHandler {
|
|
|
|
async handleOperation(ctx: Context, itemIndex: number): Promise<TodoistResponse> {
|
2022-11-29 03:37:37 -08:00
|
|
|
//https://developer.todoist.com/rest/v2/#get-active-tasks
|
2022-06-20 16:42:08 -07:00
|
|
|
const returnAll = ctx.getNodeParameter('returnAll', itemIndex) as boolean;
|
|
|
|
const filters = ctx.getNodeParameter('filters', itemIndex) as IDataObject;
|
|
|
|
const qs: IDataObject = {};
|
|
|
|
|
|
|
|
if (filters.projectId) {
|
|
|
|
qs.project_id = filters.projectId as string;
|
|
|
|
}
|
|
|
|
if (filters.labelId) {
|
2022-11-29 03:37:37 -08:00
|
|
|
qs.label = filters.labelId as string;
|
2022-06-20 16:42:08 -07:00
|
|
|
}
|
|
|
|
if (filters.filter) {
|
|
|
|
qs.filter = filters.filter as string;
|
|
|
|
}
|
|
|
|
if (filters.lang) {
|
|
|
|
qs.lang = filters.lang as string;
|
|
|
|
}
|
|
|
|
if (filters.ids) {
|
|
|
|
qs.ids = filters.ids as string;
|
|
|
|
}
|
|
|
|
|
|
|
|
let responseData = await todoistApiRequest.call(ctx, 'GET', '/tasks', {}, qs);
|
|
|
|
|
|
|
|
if (!returnAll) {
|
|
|
|
const limit = ctx.getNodeParameter('limit', itemIndex) as number;
|
|
|
|
responseData = responseData.splice(0, limit);
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
data: responseData,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getSectionIds(ctx: Context, projectId: number): Promise<Map<string, number>> {
|
2022-08-17 08:50:24 -07:00
|
|
|
const sections: Section[] = await todoistApiRequest.call(
|
|
|
|
ctx,
|
|
|
|
'GET',
|
|
|
|
'/sections',
|
|
|
|
{},
|
|
|
|
{ project_id: projectId },
|
|
|
|
);
|
|
|
|
return new Map(sections.map((s) => [s.name, s.id as unknown as number]));
|
2022-06-20 16:42:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
export class ReopenHandler implements OperationHandler {
|
|
|
|
async handleOperation(ctx: Context, itemIndex: number): Promise<TodoistResponse> {
|
2022-11-29 03:37:37 -08:00
|
|
|
//https://developer.todoist.com/rest/v2/#get-an-active-task
|
2022-06-20 16:42:08 -07:00
|
|
|
const id = ctx.getNodeParameter('taskId', itemIndex) as string;
|
|
|
|
|
2022-11-29 03:37:37 -08:00
|
|
|
await todoistApiRequest.call(ctx, 'POST', `/tasks/${id}/reopen`);
|
|
|
|
|
2022-06-20 16:42:08 -07:00
|
|
|
return {
|
2022-11-29 03:37:37 -08:00
|
|
|
success: true,
|
2022-06-20 16:42:08 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class UpdateHandler implements OperationHandler {
|
|
|
|
async handleOperation(ctx: Context, itemIndex: number): Promise<TodoistResponse> {
|
2022-11-29 03:37:37 -08:00
|
|
|
//https://developer.todoist.com/rest/v2/#update-a-task
|
2022-06-20 16:42:08 -07:00
|
|
|
const id = ctx.getNodeParameter('taskId', itemIndex) as string;
|
|
|
|
const updateFields = ctx.getNodeParameter('updateFields', itemIndex) as IDataObject;
|
|
|
|
|
|
|
|
const body: CreateTaskRequest = {};
|
|
|
|
|
|
|
|
if (updateFields.content) {
|
|
|
|
body.content = updateFields.content as string;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (updateFields.priority) {
|
|
|
|
body.priority = parseInt(updateFields.priority as string, 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (updateFields.description) {
|
|
|
|
body.description = updateFields.description as string;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (updateFields.dueDateTime) {
|
|
|
|
body.due_datetime = FormatDueDatetime(updateFields.dueDateTime as string);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (updateFields.dueString) {
|
|
|
|
body.due_string = updateFields.dueString as string;
|
|
|
|
}
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
if (
|
|
|
|
updateFields.labels !== undefined &&
|
2022-06-20 16:42:08 -07:00
|
|
|
Array.isArray(updateFields.labels) &&
|
2022-08-17 08:50:24 -07:00
|
|
|
updateFields.labels.length !== 0
|
|
|
|
) {
|
2022-11-29 03:37:37 -08:00
|
|
|
body.labels = await getLabelNameFromId(ctx, updateFields.labels as number[]);
|
2022-06-20 16:42:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (updateFields.dueLang) {
|
|
|
|
body.due_lang = updateFields.dueLang as string;
|
|
|
|
}
|
|
|
|
|
|
|
|
await todoistApiRequest.call(ctx, 'POST', `/tasks/${id}`, body);
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
return { success: true };
|
2022-06-20 16:42:08 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class MoveHandler implements OperationHandler {
|
|
|
|
async handleOperation(ctx: Context, itemIndex: number): Promise<TodoistResponse> {
|
2022-11-29 03:37:37 -08:00
|
|
|
//https://api.todoist.com/sync/v9/sync
|
2022-06-20 16:42:08 -07:00
|
|
|
const taskId = ctx.getNodeParameter('taskId', itemIndex) as number;
|
|
|
|
const section = ctx.getNodeParameter('section', itemIndex) as number;
|
|
|
|
|
|
|
|
const body: SyncRequest = {
|
|
|
|
commands: [
|
|
|
|
{
|
|
|
|
type: CommandType.ITEM_MOVE,
|
|
|
|
uuid: uuid(),
|
|
|
|
args: {
|
|
|
|
id: taskId,
|
|
|
|
section_id: section,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
await todoistSyncRequest.call(ctx, body);
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
return { success: true };
|
2022-06-20 16:42:08 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class SyncHandler implements OperationHandler {
|
|
|
|
async handleOperation(ctx: Context, itemIndex: number): Promise<TodoistResponse> {
|
|
|
|
const commandsJson = ctx.getNodeParameter('commands', itemIndex) as string;
|
2022-11-29 03:37:37 -08:00
|
|
|
const projectId = ctx.getNodeParameter('project', itemIndex, undefined, {
|
|
|
|
extractValue: true,
|
|
|
|
}) as number;
|
2022-06-20 16:42:08 -07:00
|
|
|
const sections = await getSectionIds(ctx, projectId);
|
2022-10-21 11:52:43 -07:00
|
|
|
const commands: Command[] = jsonParse(commandsJson);
|
2022-08-17 08:50:24 -07:00
|
|
|
const tempIdMapping = new Map<string, string>();
|
2022-06-20 16:42:08 -07:00
|
|
|
|
|
|
|
for (let i = 0; i < commands.length; i++) {
|
|
|
|
const command = commands[i];
|
|
|
|
this.enrichUUID(command);
|
|
|
|
this.enrichSection(command, sections);
|
|
|
|
this.enrichProjectId(command, projectId);
|
2022-08-17 08:50:24 -07:00
|
|
|
this.enrichTempId(command, tempIdMapping, projectId);
|
2022-06-20 16:42:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
const body: SyncRequest = {
|
|
|
|
commands,
|
|
|
|
temp_id_mapping: this.convertToObject(tempIdMapping),
|
|
|
|
};
|
|
|
|
|
|
|
|
await todoistSyncRequest.call(ctx, body);
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
return { success: true };
|
2022-06-20 16:42:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
private convertToObject(map: Map<string, string>) {
|
|
|
|
return Array.from(map.entries()).reduce((o, [key, value]) => {
|
2022-12-02 12:54:28 -08:00
|
|
|
o[key] = value;
|
2022-06-20 16:42:08 -07:00
|
|
|
return o;
|
2022-11-29 03:37:37 -08:00
|
|
|
}, {} as IDataObject);
|
2022-06-20 16:42:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
private enrichUUID(command: Command) {
|
|
|
|
command.uuid = uuid();
|
|
|
|
}
|
|
|
|
|
|
|
|
private enrichSection(command: Command, sections: Map<string, number>) {
|
2022-12-02 12:54:28 -08:00
|
|
|
if (command.args?.section !== undefined) {
|
2022-06-20 16:42:08 -07:00
|
|
|
const sectionId = sections.get(command.args.section);
|
|
|
|
if (sectionId) {
|
|
|
|
command.args.section_id = sectionId;
|
|
|
|
} else {
|
2022-08-17 08:50:24 -07:00
|
|
|
throw new Error('Section ' + command.args.section + " doesn't exist on Todoist");
|
2022-06-20 16:42:08 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private enrichProjectId(command: Command, projectId: number) {
|
|
|
|
if (this.requiresProjectId(command)) {
|
|
|
|
command.args.project_id = projectId;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private requiresProjectId(command: Command) {
|
|
|
|
return command.type === CommandType.ITEM_ADD;
|
|
|
|
}
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
private enrichTempId(command: Command, tempIdMapping: Map<string, string>, projectId: number) {
|
2022-06-20 16:42:08 -07:00
|
|
|
if (this.requiresTempId(command)) {
|
2022-12-02 12:54:28 -08:00
|
|
|
command.temp_id = uuid();
|
2022-06-20 16:42:08 -07:00
|
|
|
tempIdMapping.set(command.temp_id, projectId as unknown as string);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private requiresTempId(command: Command) {
|
|
|
|
return command.type === CommandType.ITEM_ADD;
|
|
|
|
}
|
|
|
|
}
|