2019-06-23 03:35:23 -07:00
import {
IExecuteFunctions ,
} from 'n8n-core' ;
import {
IDataObject ,
ILoadOptionsFunctions ,
INodePropertyOptions ,
INodeTypeDescription ,
INodeExecutionData ,
INodeType ,
} from 'n8n-workflow' ;
import {
asanaApiRequest ,
} from './GenericFunctions' ;
export class Asana implements INodeType {
description : INodeTypeDescription = {
displayName : 'Asana' ,
name : 'asana' ,
icon : 'file:asana.png' ,
group : [ 'input' ] ,
version : 1 ,
2019-07-12 09:35:35 -07:00
subtitle : '={{$parameter["operation"] + ": " + $parameter["resource"]}}' ,
2019-06-23 03:35:23 -07:00
description : 'Access and edit Asana tasks' ,
defaults : {
name : 'Asana' ,
color : '#339922' ,
} ,
inputs : [ 'main' ] ,
outputs : [ 'main' ] ,
credentials : [
{
name : 'asanaApi' ,
required : true ,
}
] ,
properties : [
2019-07-12 09:35:35 -07:00
{
displayName : 'Resource' ,
name : 'resource' ,
type : 'options' ,
options : [
{
name : 'Task' ,
value : 'task' ,
} ,
{
name : 'User' ,
value : 'user' ,
} ,
] ,
default : 'task' ,
description : 'The resource to operate on.' ,
} ,
// ----------------------------------
// task
// ----------------------------------
2019-06-23 03:35:23 -07:00
{
displayName : 'Operation' ,
name : 'operation' ,
type : 'options' ,
2019-07-12 09:35:35 -07:00
displayOptions : {
show : {
resource : [
'task' ,
] ,
} ,
} ,
2019-06-23 03:35:23 -07:00
options : [
{
2019-07-12 09:35:35 -07:00
name : 'Create' ,
value : 'create' ,
description : 'Create a task' ,
2019-06-23 03:35:23 -07:00
} ,
{
2019-07-12 09:35:35 -07:00
name : 'Delete' ,
value : 'delete' ,
2019-06-23 03:35:23 -07:00
description : 'Delete a task' ,
} ,
{
2019-07-12 09:35:35 -07:00
name : 'Get' ,
value : 'get' ,
2019-06-23 03:35:23 -07:00
description : 'Get data of task' ,
} ,
{
2019-07-12 09:35:35 -07:00
name : 'Update' ,
value : 'update' ,
2019-06-23 03:35:23 -07:00
description : 'Update a task' ,
} ,
{
2019-07-12 09:35:35 -07:00
name : 'Search' ,
value : 'search' ,
description : 'Search for tasks' ,
2019-06-23 03:35:23 -07:00
} ,
] ,
2019-07-12 09:35:35 -07:00
default : 'create' ,
2019-06-23 03:35:23 -07:00
description : 'The operation to perform.' ,
} ,
// ----------------------------------
2019-07-12 09:35:35 -07:00
// task:create
2019-06-23 03:35:23 -07:00
// ----------------------------------
{
displayName : 'Workspace' ,
name : 'workspace' ,
type : 'options' ,
typeOptions : {
loadOptionsMethod : 'getWorkspaces' ,
} ,
options : [ ] ,
default : '' ,
required : true ,
displayOptions : {
show : {
operation : [
2019-07-12 09:35:35 -07:00
'create' ,
] ,
resource : [
'task' ,
2019-06-23 03:35:23 -07:00
] ,
} ,
} ,
description : 'The workspace to create the task in' ,
} ,
{
displayName : 'Name' ,
2019-07-12 09:35:35 -07:00
name : 'name' ,
2019-06-23 03:35:23 -07:00
type : 'string' ,
default : '' ,
required : true ,
displayOptions : {
show : {
operation : [
2019-07-12 09:35:35 -07:00
'create' ,
] ,
resource : [
'task' ,
2019-06-23 03:35:23 -07:00
] ,
} ,
} ,
description : 'The name of the task to create' ,
} ,
// ----------------------------------
2019-07-12 09:35:35 -07:00
// delete
2019-06-23 03:35:23 -07:00
// ----------------------------------
{
displayName : 'Task ID' ,
2019-07-12 09:35:35 -07:00
name : 'id' ,
2019-06-23 03:35:23 -07:00
type : 'string' ,
default : '' ,
required : true ,
displayOptions : {
show : {
operation : [
2019-07-12 09:35:35 -07:00
'delete' ,
] ,
resource : [
'task' ,
2019-06-23 03:35:23 -07:00
] ,
} ,
} ,
description : 'The ID of the task to delete.' ,
} ,
// ----------------------------------
2019-07-12 09:35:35 -07:00
// get
2019-06-23 03:35:23 -07:00
// ----------------------------------
{
displayName : 'Task ID' ,
2019-07-12 09:35:35 -07:00
name : 'id' ,
2019-06-23 03:35:23 -07:00
type : 'string' ,
default : '' ,
required : true ,
displayOptions : {
show : {
operation : [
2019-07-12 09:35:35 -07:00
'get' ,
] ,
resource : [
'task' ,
2019-06-23 03:35:23 -07:00
] ,
} ,
} ,
description : 'The ID of the task to get the data of.' ,
} ,
// ----------------------------------
2019-07-12 09:35:35 -07:00
// update
2019-06-23 03:35:23 -07:00
// ----------------------------------
{
displayName : 'Task ID' ,
2019-07-12 09:35:35 -07:00
name : 'id' ,
2019-06-23 03:35:23 -07:00
type : 'string' ,
default : '' ,
required : true ,
displayOptions : {
show : {
operation : [
2019-07-12 09:35:35 -07:00
'update' ,
] ,
resource : [
'task' ,
2019-06-23 03:35:23 -07:00
] ,
} ,
} ,
description : 'The ID of the task to update the data of.' ,
} ,
// ----------------------------------
2019-07-12 09:35:35 -07:00
// search
2019-06-23 03:35:23 -07:00
// ----------------------------------
{
displayName : 'Workspace' ,
name : 'workspace' ,
type : 'options' ,
typeOptions : {
loadOptionsMethod : 'getWorkspaces' ,
} ,
options : [ ] ,
default : '' ,
required : true ,
displayOptions : {
show : {
operation : [
2019-07-12 09:35:35 -07:00
'search' ,
] ,
resource : [
'task' ,
2019-06-23 03:35:23 -07:00
] ,
} ,
} ,
description : 'The workspace to create the task in' ,
} ,
{
displayName : 'Search Properties' ,
name : 'searchTaskProperties' ,
type : 'collection' ,
displayOptions : {
show : {
operation : [
2019-07-12 09:35:35 -07:00
'search' ,
] ,
resource : [
'task' ,
2019-06-23 03:35:23 -07:00
] ,
} ,
} ,
default : { } ,
description : 'Properties to search for' ,
placeholder : 'Add Search Property' ,
options : [
// TODO: Add "assignee" and "assignee_status"
{
displayName : 'Text' ,
name : 'text' ,
type : 'string' ,
typeOptions : {
alwaysOpenEditWindow : true ,
rows : 5 ,
} ,
default : '' ,
description : 'Text to search for in name or notes.' ,
} ,
{
displayName : 'Completed' ,
name : 'completed' ,
type : 'boolean' ,
default : false ,
description : 'If the task is marked completed.' ,
} ,
] ,
} ,
// ----------------------------------
2019-07-12 09:35:35 -07:00
// create/update
2019-06-23 03:35:23 -07:00
// ----------------------------------
{
displayName : 'Other Properties' ,
name : 'otherProperties' ,
type : 'collection' ,
displayOptions : {
2019-07-12 09:35:35 -07:00
show : {
resource : [
'task' ,
] ,
2019-06-23 03:35:23 -07:00
operation : [
2019-07-12 09:35:35 -07:00
'create' ,
'update' ,
2019-06-23 03:35:23 -07:00
] ,
} ,
} ,
default : { } ,
description : 'Other properties to set' ,
placeholder : 'Add Property' ,
options : [
{
displayName : 'Name' ,
name : 'name' ,
type : 'string' ,
default : '' ,
displayOptions : {
show : {
'/operation' : [
2019-07-12 09:35:35 -07:00
'update' ,
2019-06-23 03:35:23 -07:00
] ,
} ,
} ,
description : 'The new name of the task' ,
} ,
// TODO: Add "assignee" and "assignee_status"
{
displayName : 'Notes' ,
name : 'notes' ,
type : 'string' ,
typeOptions : {
alwaysOpenEditWindow : true ,
rows : 5 ,
} ,
default : '' ,
description : 'The task notes' ,
} ,
{
displayName : 'Completed' ,
name : 'completed' ,
type : 'boolean' ,
default : false ,
description : 'If the task should be marked completed.' ,
} ,
{
displayName : 'Due On' ,
name : 'due_on' ,
type : 'dateTime' ,
default : '' ,
description : 'Date on which the time is due.' ,
} ,
{
displayName : 'Liked' ,
name : 'liked' ,
type : 'boolean' ,
default : false ,
description : 'If the task is liked by the authorized user.' ,
} ,
] ,
} ,
2019-07-12 09:35:35 -07:00
// ----------------------------------
// user
// ----------------------------------
{
displayName : 'Operation' ,
name : 'operation' ,
type : 'options' ,
displayOptions : {
show : {
resource : [
'user' ,
] ,
} ,
} ,
options : [
{
name : 'Get All' ,
value : 'getAll' ,
description : 'Data of all users' ,
} ,
{
name : 'Get' ,
value : 'get' ,
description : 'Get data of user' ,
} ,
] ,
default : 'get' ,
description : 'The operation to perform.' ,
} ,
// ----------------------------------
// user:get
// ----------------------------------
{
displayName : 'Id' ,
name : 'userId' ,
type : 'string' ,
default : '' ,
required : true ,
displayOptions : {
show : {
operation : [
'get' ,
] ,
resource : [
'user' ,
] ,
} ,
} ,
description : 'An identifier for the user to get data of. Can be one of an<br />email address,the globally unique identifier for the user,<br />or the keyword me to indicate the current user making the request.' ,
} ,
// ----------------------------------
// user:getAll
// ----------------------------------
{
displayName : 'Workspace' ,
name : 'workspace' ,
type : 'options' ,
typeOptions : {
loadOptionsMethod : 'getWorkspaces' ,
} ,
options : [ ] ,
default : '' ,
required : true ,
displayOptions : {
show : {
operation : [
'getAll' ,
] ,
resource : [
'user' ,
] ,
} ,
} ,
description : 'The workspace in which to get users.' ,
} ,
2019-06-23 03:35:23 -07:00
] ,
} ;
methods = {
loadOptions : {
// Get all the available workspaces to display them to user so that he can
// select them easily
async getWorkspaces ( this : ILoadOptionsFunctions ) : Promise < INodePropertyOptions [ ] > {
const endpoint = 'workspaces' ;
const responseData = await asanaApiRequest . call ( this , 'GET' , endpoint , { } ) ;
if ( responseData . data === undefined ) {
throw new Error ( 'No data got returned' ) ;
}
const returnData : INodePropertyOptions [ ] = [ ] ;
for ( const workspaceData of responseData . data ) {
if ( workspaceData . resource_type !== 'workspace' ) {
// Not sure if for some reason also ever other resources
// get returned but just in case filter them out
continue ;
}
returnData . push ( {
name : workspaceData.name ,
value : workspaceData.gid ,
} ) ;
}
return returnData ;
}
} ,
} ;
async execute ( this : IExecuteFunctions ) : Promise < INodeExecutionData [ ] [ ] > {
const items = this . getInputData ( ) ;
const returnData : IDataObject [ ] = [ ] ;
const credentials = this . getCredentials ( 'asanaApi' ) ;
if ( credentials === undefined ) {
throw new Error ( 'No credentials got returned!' ) ;
}
2019-07-12 09:35:35 -07:00
const resource = this . getNodeParameter ( 'resource' , 0 ) as string ;
2019-06-23 03:35:23 -07:00
const operation = this . getNodeParameter ( 'operation' , 0 ) as string ;
let endpoint = '' ;
let requestMethod = '' ;
let body : IDataObject ;
let qs : IDataObject ;
for ( let i = 0 ; i < items . length ; i ++ ) {
body = { } ;
qs = { } ;
2019-07-12 09:35:35 -07:00
if ( resource === 'task' ) {
if ( operation === 'create' ) {
// ----------------------------------
// create
// ----------------------------------
2019-06-23 03:35:23 -07:00
2019-07-12 09:35:35 -07:00
requestMethod = 'POST' ;
endpoint = 'tasks' ;
2019-06-23 03:35:23 -07:00
2019-07-12 09:35:35 -07:00
body . name = this . getNodeParameter ( 'name' , i ) as string ;
// body.notes = this.getNodeParameter('taskNotes', 0) as string;
body . workspace = this . getNodeParameter ( 'workspace' , i ) as string ;
2019-06-23 03:35:23 -07:00
2019-07-12 09:35:35 -07:00
const otherProperties = this . getNodeParameter ( 'otherProperties' , i ) as IDataObject ;
Object . assign ( body , otherProperties ) ;
2019-06-23 03:35:23 -07:00
2019-07-12 09:35:35 -07:00
} else if ( operation === 'delete' ) {
// ----------------------------------
// delete
// ----------------------------------
2019-06-23 03:35:23 -07:00
2019-07-12 09:35:35 -07:00
requestMethod = 'DELETE' ;
endpoint = 'tasks/' + this . getNodeParameter ( 'id' , i ) as string ;
2019-06-23 03:35:23 -07:00
2019-07-12 09:35:35 -07:00
} else if ( operation === 'get' ) {
// ----------------------------------
// get
// ----------------------------------
2019-06-23 03:35:23 -07:00
2019-07-12 09:35:35 -07:00
requestMethod = 'GET' ;
endpoint = 'tasks/' + this . getNodeParameter ( 'id' , i ) as string ;
2019-06-23 03:35:23 -07:00
2019-07-12 09:35:35 -07:00
} else if ( operation === 'update' ) {
// ----------------------------------
// update
// ----------------------------------
2019-06-23 03:35:23 -07:00
2019-07-12 09:35:35 -07:00
requestMethod = 'PUT' ;
endpoint = 'tasks/' + this . getNodeParameter ( 'id' , i ) as string ;
2019-06-23 03:35:23 -07:00
2019-07-12 09:35:35 -07:00
const otherProperties = this . getNodeParameter ( 'otherProperties' , i ) as IDataObject ;
Object . assign ( body , otherProperties ) ;
2019-06-23 03:35:23 -07:00
2019-07-12 09:35:35 -07:00
} else if ( operation === 'search' ) {
// ----------------------------------
// search
// ----------------------------------
2019-06-23 03:35:23 -07:00
2019-07-12 09:35:35 -07:00
const workspaceId = this . getNodeParameter ( 'workspace' , i ) as string ;
2019-06-23 03:35:23 -07:00
2019-07-12 09:35:35 -07:00
requestMethod = 'GET' ;
endpoint = ` workspaces/ ${ workspaceId } /tasks/search ` ;
const searchTaskProperties = this . getNodeParameter ( 'searchTaskProperties' , i ) as IDataObject ;
Object . assign ( qs , searchTaskProperties ) ;
} else {
throw new Error ( ` The operation " ${ operation } " is not known! ` ) ;
}
} else if ( resource === 'user' ) {
if ( operation === 'get' ) {
// ----------------------------------
// get
// ----------------------------------
2019-06-23 03:35:23 -07:00
2019-07-12 09:35:35 -07:00
const userId = this . getNodeParameter ( 'userId' , i ) as string ;
2019-06-23 03:35:23 -07:00
2019-07-12 09:35:35 -07:00
requestMethod = 'GET' ;
endpoint = ` users/ ${ userId } ` ;
2019-06-23 03:35:23 -07:00
2019-07-12 09:35:35 -07:00
} else if ( operation === 'getAll' ) {
// ----------------------------------
// getAll
// ----------------------------------
const workspaceId = this . getNodeParameter ( 'workspace' , i ) as string ;
requestMethod = 'GET' ;
endpoint = ` workspaces/ ${ workspaceId } /users ` ;
} else {
throw new Error ( ` The operation " ${ operation } " is not known! ` ) ;
}
2019-06-23 03:35:23 -07:00
} else {
2019-07-12 09:35:35 -07:00
throw new Error ( ` The resource " ${ resource } " is not known! ` ) ;
2019-06-23 03:35:23 -07:00
}
const responseData = await asanaApiRequest . call ( this , requestMethod , endpoint , body ) ;
returnData . push ( responseData . data as IDataObject ) ;
}
return [ this . helpers . returnJsonArray ( returnData ) ] ;
}
}