n8n/packages/nodes-base/nodes/Google/Task/GoogleTasks.node.ts

280 lines
7.1 KiB
TypeScript
Raw Normal View History

import {
IExecuteFunctions,
} from 'n8n-core';
2020-06-15 15:47:44 -07:00
import {
IDataObject,
ILoadOptionsFunctions,
2020-06-15 15:47:44 -07:00
INodeExecutionData,
INodePropertyOptions,
2020-06-15 15:47:44 -07:00
INodeType,
INodeTypeDescription,
2020-06-15 15:47:44 -07:00
} from 'n8n-workflow';
import {
googleApiRequest,
googleApiRequestAllItems,
} from './GenericFunctions';
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',
icon: 'file:googleTasks.png',
group: ['input'],
version: 1,
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
description: 'Consume Google Tasks API.',
defaults: {
name: 'Google Tasks',
2020-10-22 06:46:03 -07:00
color: '#3E87E4',
2020-06-15 15:47:44 -07:00
},
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',
options: [
{
name: 'Task',
2020-10-22 06:46:03 -07:00
value: 'task',
},
2020-06-15 15:47:44 -07:00
],
default: 'task',
2020-10-22 06:46:03 -07:00
description: 'The resource to operate on.',
2020-06-15 15:47:44 -07:00
},
...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
async getTasks(
2020-10-22 09:00:28 -07:00
this: ILoadOptionsFunctions,
2020-06-15 15:47:44 -07:00
): Promise<INodePropertyOptions[]> {
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();
const returnData: IDataObject[] = [];
const length = (items.length as unknown) as number;
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++) {
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;
2020-06-15 15:47:44 -07:00
const additionalFields = this.getNodeParameter(
'additionalFields',
2020-10-22 09:00:28 -07:00
i,
2020-06-15 15:47:44 -07:00
) as IDataObject;
2020-06-17 12:49:37 -07:00
if (additionalFields.parent) {
2020-06-15 15:47:44 -07:00
qs.parent = additionalFields.parent as string;
2020-06-17 12:49:37 -07:00
}
if (additionalFields.previous) {
2020-06-15 15:47:44 -07:00
qs.previous = additionalFields.previous as string;
}
2020-06-17 12:49:37 -07:00
if (additionalFields.status) {
2020-06-15 15:47:44 -07:00
body.status = additionalFields.status as string;
2020-06-17 12:49:37 -07:00
}
2020-06-15 15:47:44 -07:00
2020-06-17 12:49:37 -07:00
if (additionalFields.notes) {
2020-06-15 15:47:44 -07:00
body.notes = additionalFields.notes as string;
2020-06-17 12:49:37 -07:00
}
if (additionalFields.dueDate) {
2020-06-15 15:47:44 -07:00
body.dueDate = additionalFields.dueDate as string;
2020-06-17 12:49:37 -07:00
}
2020-06-15 15:47:44 -07:00
2020-06-17 12:49:37 -07:00
if (additionalFields.completed) {
2020-06-15 15:47:44 -07:00
body.completed = additionalFields.completed as string;
2020-06-17 12:49:37 -07:00
}
2020-06-15 15:47:44 -07:00
2020-06-17 12:49:37 -07:00
if (additionalFields.deleted) {
2020-06-15 15:47:44 -07:00
body.deleted = additionalFields.deleted as boolean;
2020-06-17 12:49:37 -07:00
}
2020-06-15 15:47:44 -07:00
responseData = await googleApiRequest.call(
this,
'POST',
`/tasks/v1/lists/${taskId}/tasks`,
body,
2020-10-22 09:00:28 -07:00
qs,
2020-06-15 15:47:44 -07:00
);
}
2020-06-16 12:13:34 -07:00
if (operation === 'delete') {
2020-06-15 15:47:44 -07:00
//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;
responseData = await googleApiRequest.call(
this,
'DELETE',
`/tasks/v1/lists/${taskListId}/tasks/${taskId}`,
2020-10-22 09:00:28 -07:00
{},
2020-06-15 15:47:44 -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;
responseData = await googleApiRequest.call(
this,
'GET',
`/tasks/v1/lists/${taskListId}/tasks/${taskId}`,
{},
2020-10-22 09:00:28 -07:00
qs,
2020-06-15 15:47:44 -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;
const options = this.getNodeParameter(
'additionalFields',
2020-10-22 09:00:28 -07:00
i,
2020-06-15 15:47:44 -07:00
) as IDataObject;
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;
}
if (options.showCompleted) {
qs.showCompleted = options.showCompleted as boolean;
}
if (options.showDeleted) {
qs.showDeleted = options.showDeleted as boolean;
}
if (options.showHidden) {
qs.showHidden = options.showHidden as boolean;
}
if (options.updatedMin) {
qs.updatedMin = options.updatedMin as string;
}
2020-06-15 15:47:44 -07:00
if (returnAll) {
responseData = await googleApiRequestAllItems.call(
this,
'items',
'GET',
`/tasks/v1/lists/${taskListId}/tasks`,
{},
2020-10-22 09:00:28 -07:00
qs,
2020-06-15 15:47:44 -07:00
);
} else {
qs.maxResults = this.getNodeParameter('limit', i) as number;
responseData = await googleApiRequest.call(
this,
'GET',
`/tasks/v1/lists/${taskListId}/tasks`,
{},
2020-10-22 09:00:28 -07:00
qs,
2020-06-15 15:47:44 -07:00
);
responseData = responseData.items;
}
}
2020-06-16 12:13:34 -07:00
if (operation === 'update') {
2020-06-15 15:47:44 -07:00
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;
const updateFields = this.getNodeParameter(
'updateFields',
2020-10-22 09:00:28 -07:00
i,
2020-06-15 15:47:44 -07:00
) as IDataObject;
2020-06-17 12:49:37 -07:00
if (updateFields.previous) {
2020-06-15 15:47:44 -07:00
qs.previous = updateFields.previous as string;
2020-06-17 12:49:37 -07:00
}
2020-06-15 15:47:44 -07:00
2020-06-17 12:49:37 -07:00
if (updateFields.status) {
body.status = updateFields.status as string;
2020-06-15 15:47:44 -07:00
}
2020-06-17 12:49:37 -07:00
if (updateFields.notes) {
body.notes = updateFields.notes as string;
}
2020-06-15 15:47:44 -07:00
2020-06-17 12:49:37 -07:00
if (updateFields.title) {
body.title = updateFields.title as string;
}
2020-06-15 15:47:44 -07:00
2020-06-17 12:49:37 -07:00
if (updateFields.dueDate) {
2020-06-15 15:47:44 -07:00
body.dueDate = updateFields.dueDate as string;
2020-06-17 12:49:37 -07:00
}
2020-06-15 15:47:44 -07:00
2020-06-17 12:49:37 -07:00
if (updateFields.completed) {
2020-06-15 15:47:44 -07:00
body.completed = updateFields.completed as string;
2020-06-17 12:49:37 -07:00
}
2020-06-15 15:47:44 -07:00
2020-06-17 12:49:37 -07:00
if (updateFields.deleted) {
2020-06-15 15:47:44 -07:00
body.deleted = updateFields.deleted as boolean;
2020-06-17 12:49:37 -07:00
}
2020-06-15 15:47:44 -07:00
responseData = await googleApiRequest.call(
this,
'PATCH',
`/tasks/v1/lists/${taskListId}/tasks/${taskId}`,
body,
2020-10-22 09:00:28 -07:00
qs,
2020-06-15 15:47:44 -07:00
);
}
}
if (Array.isArray(responseData)) {
returnData.push.apply(returnData, responseData as IDataObject[]);
} else if (responseData !== undefined) {
returnData.push(responseData as IDataObject);
}
2020-06-15 15:47:44 -07:00
}
return [this.helpers.returnJsonArray(returnData)];
}
}