2022-08-17 08:50:24 -07:00
|
|
|
import { IExecuteFunctions } from 'n8n-core';
|
2020-06-15 15:47:44 -07:00
|
|
|
|
|
|
|
import {
|
|
|
|
IDataObject,
|
2020-06-17 14:15:54 -07:00
|
|
|
ILoadOptionsFunctions,
|
2020-06-15 15:47:44 -07:00
|
|
|
INodeExecutionData,
|
2020-06-17 14:15:54 -07:00
|
|
|
INodePropertyOptions,
|
2020-06-15 15:47:44 -07:00
|
|
|
INodeType,
|
2020-06-17 14:15:54 -07:00
|
|
|
INodeTypeDescription,
|
2020-06-15 15:47:44 -07:00
|
|
|
} from 'n8n-workflow';
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { googleApiRequest, googleApiRequestAllItems } from './GenericFunctions';
|
2020-06-17 14:15:54 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { taskFields, taskOperations } from './TaskDescription';
|
2020-06-17 12:49:37 -07:00
|
|
|
|
2020-06-15 15:47:44 -07:00
|
|
|
export class GoogleTasks implements INodeType {
|
|
|
|
description: INodeTypeDescription = {
|
|
|
|
displayName: 'Google Tasks',
|
|
|
|
name: 'googleTasks',
|
2022-06-20 07:54:01 -07:00
|
|
|
// eslint-disable-next-line n8n-nodes-base/node-class-description-icon-not-svg
|
2020-06-15 15:47:44 -07:00
|
|
|
icon: 'file:googleTasks.png',
|
|
|
|
group: ['input'],
|
|
|
|
version: 1,
|
|
|
|
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
|
2021-07-03 05:40:16 -07:00
|
|
|
description: 'Consume Google Tasks API',
|
2020-06-15 15:47:44 -07:00
|
|
|
defaults: {
|
|
|
|
name: 'Google Tasks',
|
|
|
|
},
|
|
|
|
inputs: ['main'],
|
|
|
|
outputs: ['main'],
|
|
|
|
credentials: [
|
|
|
|
{
|
|
|
|
name: 'googleTasksOAuth2Api',
|
2020-10-22 06:46:03 -07:00
|
|
|
required: true,
|
|
|
|
},
|
2020-06-15 15:47:44 -07:00
|
|
|
],
|
|
|
|
properties: [
|
|
|
|
{
|
|
|
|
displayName: 'Resource',
|
|
|
|
name: 'resource',
|
|
|
|
type: 'options',
|
2022-05-20 14:47:24 -07:00
|
|
|
noDataExpression: true,
|
2020-06-15 15:47:44 -07:00
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'Task',
|
2020-10-22 06:46:03 -07:00
|
|
|
value: 'task',
|
|
|
|
},
|
2020-06-15 15:47:44 -07:00
|
|
|
],
|
|
|
|
default: 'task',
|
|
|
|
},
|
|
|
|
...taskOperations,
|
2020-10-22 06:46:03 -07:00
|
|
|
...taskFields,
|
|
|
|
],
|
2020-06-15 15:47:44 -07:00
|
|
|
};
|
|
|
|
methods = {
|
|
|
|
loadOptions: {
|
|
|
|
// Get all the tasklists to display them to user so that he can select them easily
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
async getTasks(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
2020-06-15 15:47:44 -07:00
|
|
|
const returnData: INodePropertyOptions[] = [];
|
|
|
|
const tasks = await googleApiRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
'items',
|
|
|
|
'GET',
|
2020-10-22 09:00:28 -07:00
|
|
|
'/tasks/v1/users/@me/lists',
|
2020-06-15 15:47:44 -07:00
|
|
|
);
|
|
|
|
for (const task of tasks) {
|
|
|
|
const taskName = task.title;
|
|
|
|
const taskId = task.id;
|
|
|
|
returnData.push({
|
|
|
|
name: taskName,
|
2020-10-22 06:46:03 -07:00
|
|
|
value: taskId,
|
2020-06-15 15:47:44 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
return returnData;
|
2020-10-22 06:46:03 -07:00
|
|
|
},
|
|
|
|
},
|
2020-06-15 15:47:44 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
|
|
|
const items = this.getInputData();
|
2022-08-30 08:55:33 -07:00
|
|
|
const returnData: INodeExecutionData[] = [];
|
2022-04-22 09:29:51 -07:00
|
|
|
const length = items.length;
|
2020-06-15 15:47:44 -07:00
|
|
|
const qs: IDataObject = {};
|
|
|
|
let responseData;
|
|
|
|
const resource = this.getNodeParameter('resource', 0) as string;
|
|
|
|
const operation = this.getNodeParameter('operation', 0) as string;
|
|
|
|
let body: IDataObject = {};
|
|
|
|
for (let i = 0; i < length; i++) {
|
2021-07-19 23:58:54 -07:00
|
|
|
try {
|
|
|
|
if (resource === 'task') {
|
|
|
|
if (operation === 'create') {
|
|
|
|
body = {};
|
|
|
|
//https://developers.google.com/tasks/v1/reference/tasks/insert
|
|
|
|
const taskId = this.getNodeParameter('task', i) as string;
|
|
|
|
body.title = this.getNodeParameter('title', i) as string;
|
2022-08-17 08:50:24 -07:00
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
|
2020-06-15 15:47:44 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (additionalFields.parent) {
|
|
|
|
qs.parent = additionalFields.parent as string;
|
|
|
|
}
|
|
|
|
if (additionalFields.previous) {
|
|
|
|
qs.previous = additionalFields.previous as string;
|
|
|
|
}
|
2020-06-17 12:49:37 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (additionalFields.status) {
|
|
|
|
body.status = additionalFields.status as string;
|
|
|
|
}
|
2020-06-15 15:47:44 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (additionalFields.notes) {
|
|
|
|
body.notes = additionalFields.notes as string;
|
|
|
|
}
|
|
|
|
if (additionalFields.dueDate) {
|
2021-11-13 01:16:56 -08:00
|
|
|
body.due = additionalFields.dueDate as string;
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
2020-06-15 15:47:44 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (additionalFields.completed) {
|
|
|
|
body.completed = additionalFields.completed as string;
|
|
|
|
}
|
2020-06-15 15:47:44 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (additionalFields.deleted) {
|
|
|
|
body.deleted = additionalFields.deleted as boolean;
|
|
|
|
}
|
2020-06-15 15:47:44 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = await googleApiRequest.call(
|
|
|
|
this,
|
|
|
|
'POST',
|
|
|
|
`/tasks/v1/lists/${taskId}/tasks`,
|
|
|
|
body,
|
|
|
|
qs,
|
|
|
|
);
|
2020-06-15 15:47:44 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
if (operation === 'delete') {
|
|
|
|
//https://developers.google.com/tasks/v1/reference/tasks/delete
|
|
|
|
const taskListId = this.getNodeParameter('task', i) as string;
|
|
|
|
const taskId = this.getNodeParameter('taskId', i) as string;
|
2020-06-17 14:15:54 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = await googleApiRequest.call(
|
2020-06-15 15:47:44 -07:00
|
|
|
this,
|
2021-07-19 23:58:54 -07:00
|
|
|
'DELETE',
|
|
|
|
`/tasks/v1/lists/${taskListId}/tasks/${taskId}`,
|
2020-06-15 15:47:44 -07:00
|
|
|
{},
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = { success: true };
|
|
|
|
}
|
|
|
|
if (operation === 'get') {
|
|
|
|
//https://developers.google.com/tasks/v1/reference/tasks/get
|
|
|
|
const taskListId = this.getNodeParameter('task', i) as string;
|
|
|
|
const taskId = this.getNodeParameter('taskId', i) as string;
|
2020-06-15 15:47:44 -07:00
|
|
|
responseData = await googleApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
2021-07-19 23:58:54 -07:00
|
|
|
`/tasks/v1/lists/${taskListId}/tasks/${taskId}`,
|
2020-06-15 15:47:44 -07:00
|
|
|
{},
|
2020-10-22 09:00:28 -07:00
|
|
|
qs,
|
2020-06-15 15:47:44 -07:00
|
|
|
);
|
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
if (operation === 'getAll') {
|
|
|
|
//https://developers.google.com/tasks/v1/reference/tasks/list
|
|
|
|
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
|
|
|
|
const taskListId = this.getNodeParameter('task', i) as string;
|
2022-08-17 08:50:24 -07:00
|
|
|
const {
|
|
|
|
showCompleted = true,
|
|
|
|
showDeleted = false,
|
|
|
|
showHidden = false,
|
|
|
|
...options
|
|
|
|
} = this.getNodeParameter('additionalFields', i) as IDataObject;
|
2021-07-19 23:58:54 -07:00
|
|
|
if (options.completedMax) {
|
|
|
|
qs.completedMax = options.completedMax as string;
|
|
|
|
}
|
|
|
|
if (options.completedMin) {
|
|
|
|
qs.completedMin = options.completedMin as string;
|
|
|
|
}
|
|
|
|
if (options.dueMin) {
|
|
|
|
qs.dueMin = options.dueMin as string;
|
|
|
|
}
|
|
|
|
if (options.dueMax) {
|
|
|
|
qs.dueMax = options.dueMax as string;
|
|
|
|
}
|
2022-03-31 23:32:41 -07:00
|
|
|
|
|
|
|
qs.showCompleted = showCompleted;
|
|
|
|
qs.showDeleted = showDeleted;
|
|
|
|
qs.showHidden = showHidden;
|
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (options.updatedMin) {
|
|
|
|
qs.updatedMin = options.updatedMin as string;
|
|
|
|
}
|
2020-06-15 15:47:44 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (returnAll) {
|
|
|
|
responseData = await googleApiRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
'items',
|
|
|
|
'GET',
|
|
|
|
`/tasks/v1/lists/${taskListId}/tasks`,
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
qs.maxResults = this.getNodeParameter('limit', i) as number;
|
|
|
|
responseData = await googleApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
`/tasks/v1/lists/${taskListId}/tasks`,
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
|
|
|
responseData = responseData.items;
|
|
|
|
}
|
2020-06-17 12:49:37 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
if (operation === 'update') {
|
|
|
|
body = {};
|
|
|
|
//https://developers.google.com/tasks/v1/reference/tasks/patch
|
|
|
|
const taskListId = this.getNodeParameter('task', i) as string;
|
|
|
|
const taskId = this.getNodeParameter('taskId', i) as string;
|
2022-08-17 08:50:24 -07:00
|
|
|
const updateFields = this.getNodeParameter('updateFields', i) as IDataObject;
|
2020-06-15 15:47:44 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (updateFields.previous) {
|
|
|
|
qs.previous = updateFields.previous as string;
|
|
|
|
}
|
2020-06-15 15:47:44 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (updateFields.status) {
|
|
|
|
body.status = updateFields.status as string;
|
|
|
|
}
|
2020-06-15 15:47:44 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (updateFields.notes) {
|
|
|
|
body.notes = updateFields.notes as string;
|
|
|
|
}
|
2020-06-15 15:47:44 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (updateFields.title) {
|
|
|
|
body.title = updateFields.title as string;
|
|
|
|
}
|
2020-06-15 15:47:44 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (updateFields.dueDate) {
|
2021-11-13 01:16:56 -08:00
|
|
|
body.due = updateFields.dueDate as string;
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
2020-06-15 15:47:44 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (updateFields.completed) {
|
|
|
|
body.completed = updateFields.completed as string;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (updateFields.deleted) {
|
|
|
|
body.deleted = updateFields.deleted as boolean;
|
|
|
|
}
|
2020-06-15 15:47:44 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = await googleApiRequest.call(
|
|
|
|
this,
|
|
|
|
'PATCH',
|
|
|
|
`/tasks/v1/lists/${taskListId}/tasks/${taskId}`,
|
|
|
|
body,
|
|
|
|
qs,
|
|
|
|
);
|
|
|
|
}
|
2020-06-15 15:47:44 -07:00
|
|
|
}
|
2022-08-30 08:55:33 -07:00
|
|
|
|
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
|
|
|
this.helpers.returnJsonArray(responseData),
|
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
|
|
|
|
returnData.push(...executionData);
|
2021-07-19 23:58:54 -07:00
|
|
|
} catch (error) {
|
|
|
|
if (this.continueOnFail()) {
|
2022-08-30 08:55:33 -07:00
|
|
|
const executionErrorData = this.helpers.constructExecutionMetaData(
|
|
|
|
this.helpers.returnJsonArray({ error: error.message }),
|
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
returnData.push(...executionErrorData);
|
2021-07-19 23:58:54 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
throw error;
|
2020-06-17 14:15:54 -07:00
|
|
|
}
|
2020-06-15 15:47:44 -07:00
|
|
|
}
|
2022-08-30 08:55:33 -07:00
|
|
|
|
|
|
|
return this.prepareOutputData(returnData);
|
2020-06-15 15:47:44 -07:00
|
|
|
}
|
|
|
|
}
|