From e1b08cec631cb2c7755e9c621a022bbdc6ddf549 Mon Sep 17 00:00:00 2001 From: ricardo Date: Thu, 28 Jan 2021 22:44:50 -0500 Subject: [PATCH] :zap: Add project field when creating a task --- packages/nodes-base/nodes/Asana/Asana.node.ts | 211 ++++++++++++++++++ .../nodes/Asana/GenericFunctions.ts | 8 +- 2 files changed, 215 insertions(+), 4 deletions(-) diff --git a/packages/nodes-base/nodes/Asana/Asana.node.ts b/packages/nodes-base/nodes/Asana/Asana.node.ts index 92e6f75fa138d..e0fed24e1b625 100644 --- a/packages/nodes-base/nodes/Asana/Asana.node.ts +++ b/packages/nodes-base/nodes/Asana/Asana.node.ts @@ -106,6 +106,10 @@ export class Asana implements INodeType { name: 'Task Tag', value: 'taskTag', }, + { + name: 'Task Project', + value: 'taskProject', + }, { name: 'User', value: 'user', @@ -921,6 +925,16 @@ export class Asana implements INodeType { default: '', description: 'The task notes', }, + { + displayName: 'Project IDs', + name: 'projects', + type: 'multiOptions', + typeOptions: { + loadOptionsMethod: 'getProjects', + }, + default: [], + description: 'The project to filter tasks on.', + }, ], }, @@ -1093,6 +1107,161 @@ export class Asana implements INodeType { description: 'The ID of the comment to be removed', }, + // ---------------------------------- + // taskProject + // ---------------------------------- + { + displayName: 'Operation', + name: 'operation', + type: 'options', + displayOptions: { + show: { + resource: [ + 'taskProject', + ], + }, + }, + options: [ + { + name: 'Add', + value: 'add', + description: 'Add a task to a project', + }, + { + name: 'Remove', + value: 'remove', + description: 'Remove a task from a project', + }, + ], + default: 'add', + description: 'The operation to perform.', + }, + // ---------------------------------- + // taskProject:add + // ---------------------------------- + { + displayName: 'Task ID', + name: 'id', + type: 'string', + default: '', + required: true, + displayOptions: { + show: { + operation: [ + 'add', + ], + resource: [ + 'taskProject', + ], + }, + }, + description: 'The ID of the task to add the project to', + }, + { + displayName: 'Project ID', + name: 'project', + type: 'options', + typeOptions: { + loadOptionsMethod: 'getProjects', + }, + default: '', + required: true, + displayOptions: { + show: { + operation: [ + 'add', + ], + resource: [ + 'taskProject', + ], + }, + }, + description: 'The project where the task will be added', + }, + { + displayName: 'Additional Fields', + name: 'additionalFields', + type: 'collection', + displayOptions: { + show: { + resource: [ + 'taskProject', + ], + operation: [ + 'add', + ], + }, + }, + default: {}, + description: 'Other properties to set', + placeholder: 'Add Field', + options: [ + { + displayName: 'Insert After', + name: 'insert_after', + type: 'string', + default: '', + description: 'A task in the project to insert the task after, or null to insert at the beginning of the list.', + }, + { + displayName: 'Insert Before', + name: 'insert_before', + type: 'string', + default: '', + description: 'A task in the project to insert the task before, or null to insert at the end of the list.', + }, + { + displayName: 'Section', + name: 'section', + type: 'string', + default: '', + description: 'A section in the project to insert the task into. The task will be inserted at the bottom of the section.', + }, + ], + }, + + // ---------------------------------- + // taskProject:remove + // ---------------------------------- + { + displayName: 'Task ID', + name: 'id', + type: 'string', + default: '', + required: true, + displayOptions: { + show: { + operation: [ + 'remove', + ], + resource: [ + 'taskProject', + ], + }, + }, + description: 'The ID of the task to add the project to', + }, + { + displayName: 'Project ID', + name: 'project', + type: 'options', + typeOptions: { + loadOptionsMethod: 'getProjects', + }, + default: '', + required: true, + displayOptions: { + show: { + operation: [ + 'remove', + ], + resource: [ + 'taskProject', + ], + }, + }, + description: 'The project where the task will be removed from', + }, // ---------------------------------- // taskTag // ---------------------------------- @@ -1952,7 +2121,49 @@ export class Asana implements INodeType { responseData = { success: true }; } } + if (resource === 'taskProject') { + if (operation === 'add') { + + // ---------------------------------- + // taskProject:add + // ---------------------------------- + + const taskId = this.getNodeParameter('id', i) as string; + + const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject; + + requestMethod = 'POST'; + + endpoint = `/tasks/${taskId}/addProject`; + + body.project = this.getNodeParameter('project', i) as string; + + Object.assign(body, additionalFields); + responseData = await asanaApiRequest.call(this, requestMethod, endpoint, body, qs); + + responseData = { success: true }; + } + + if (operation === 'remove') { + + // ---------------------------------- + // taskProject:remove + // ---------------------------------- + + const taskId = this.getNodeParameter('id', i) as string; + + requestMethod = 'POST'; + + endpoint = `/tasks/${taskId}/removeProject`; + + body.project = this.getNodeParameter('project', i) as string; + + responseData = await asanaApiRequest.call(this, requestMethod, endpoint, body, qs); + + responseData = { success: true }; + } + } if (resource === 'user') { if (operation === 'get') { // ---------------------------------- diff --git a/packages/nodes-base/nodes/Asana/GenericFunctions.ts b/packages/nodes-base/nodes/Asana/GenericFunctions.ts index db0b978a04cc1..c480beb078a3e 100644 --- a/packages/nodes-base/nodes/Asana/GenericFunctions.ts +++ b/packages/nodes-base/nodes/Asana/GenericFunctions.ts @@ -76,7 +76,7 @@ export async function asanaApiRequest(this: IHookFunctions | IExecuteFunctions | } } -export async function asanaApiRequestAllItems(this: IExecuteFunctions | ILoadOptionsFunctions ,method: string, endpoint: string, body: any = {}, query: IDataObject = {}): Promise { // tslint:disable-line:no-any +export async function asanaApiRequestAllItems(this: IExecuteFunctions | ILoadOptionsFunctions, method: string, endpoint: string, body: any = {}, query: IDataObject = {}): Promise { // tslint:disable-line:no-any const returnData: IDataObject[] = []; @@ -95,12 +95,12 @@ export async function asanaApiRequestAllItems(this: IExecuteFunctions | ILoadOpt return returnData; } -export async function getWorkspaces(this: ILoadOptionsFunctions): Promise < INodePropertyOptions[] > { +export async function getWorkspaces(this: ILoadOptionsFunctions): Promise { const endpoint = '/workspaces'; const responseData = await asanaApiRequestAllItems.call(this, 'GET', endpoint, {}); const returnData: INodePropertyOptions[] = []; - for(const workspaceData of responseData) { + for (const workspaceData of responseData) { if (workspaceData.resource_type !== 'workspace') { // Not sure if for some reason also ever other resources // get returned but just in case filter them out @@ -113,7 +113,7 @@ export async function getWorkspaces(this: ILoadOptionsFunctions): Promise < INod }); } - returnData.sort((a, b) => { + returnData.sort((a, b) => { if (a.name < b.name) { return -1; } if (a.name > b.name) { return 1; } return 0;