2023-01-27 03:22:44 -08:00
import type { IExecuteFunctions } from 'n8n-core' ;
import type {
2019-12-06 09:04:50 -08:00
IDataObject ,
INodeExecutionData ,
INodeType ,
2020-10-01 05:01:39 -07:00
INodeTypeDescription ,
2023-02-27 19:39:43 -08:00
JsonObject ,
2019-12-06 09:04:50 -08:00
} from 'n8n-workflow' ;
2023-01-27 03:22:44 -08:00
import { NodeApiError } from 'n8n-workflow' ;
2022-08-01 13:47:55 -07:00
import { flowApiRequest , FlowApiRequestAllItems } from './GenericFunctions' ;
2022-12-02 12:54:28 -08:00
import { taskFields , taskOperations } from './TaskDescription' ;
2023-01-27 03:22:44 -08:00
import type { ITask , TaskInfo } from './TaskInterface' ;
2019-12-06 09:04:50 -08:00
export class Flow implements INodeType {
description : INodeTypeDescription = {
displayName : 'Flow' ,
2020-05-12 06:45:41 -07:00
name : 'flow' ,
2022-06-20 07:54:01 -07:00
// eslint-disable-next-line n8n-nodes-base/node-class-description-icon-not-svg
2019-12-06 09:04:50 -08:00
icon : 'file:flow.png' ,
group : [ 'output' ] ,
version : 1 ,
subtitle : '={{$parameter["operation"] + ": " + $parameter["resource"]}}' ,
description : 'Consume Flow API' ,
defaults : {
name : 'Flow' ,
} ,
inputs : [ 'main' ] ,
outputs : [ 'main' ] ,
credentials : [
{
name : 'flowApi' ,
required : true ,
2020-10-22 06:46:03 -07:00
} ,
2019-12-06 09:04:50 -08:00
] ,
properties : [
{
displayName : 'Resource' ,
name : 'resource' ,
type : 'options' ,
2022-05-20 14:47:24 -07:00
noDataExpression : true ,
2019-12-06 09:04:50 -08:00
options : [
{
name : 'Task' ,
value : 'task' ,
2022-08-01 13:47:55 -07:00
description :
'Tasks are units of work that can be private or assigned to a list. Through this endpoint, you can manipulate your tasks in Flow, including creating new ones.' ,
2019-12-06 09:04:50 -08:00
} ,
] ,
default : 'task' ,
} ,
2022-12-02 12:54:28 -08:00
. . . taskOperations ,
2019-12-06 09:04:50 -08:00
. . . taskFields ,
] ,
} ;
async execute ( this : IExecuteFunctions ) : Promise < INodeExecutionData [ ] [ ] > {
2021-08-20 09:57:30 -07:00
const credentials = await this . getCredentials ( 'flowApi' ) ;
2019-12-09 03:07:48 -08:00
2019-12-06 09:04:50 -08:00
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 ;
2019-12-06 09:04:50 -08:00
let responseData ;
const qs : IDataObject = { } ;
2022-12-02 03:53:59 -08:00
const resource = this . getNodeParameter ( 'resource' , 0 ) ;
const operation = this . getNodeParameter ( 'operation' , 0 ) ;
2019-12-06 09:04:50 -08:00
for ( let i = 0 ; i < length ; i ++ ) {
if ( resource === 'task' ) {
//https://developer.getflow.com/api/#tasks_create-task
if ( operation === 'create' ) {
const workspaceId = this . getNodeParameter ( 'workspaceId' , i ) as string ;
const name = this . getNodeParameter ( 'name' , i ) as string ;
2022-11-18 07:29:44 -08:00
const additionalFields = this . getNodeParameter ( 'additionalFields' , i ) ;
2019-12-06 09:04:50 -08:00
const body : ITask = {
2019-12-09 03:07:48 -08:00
organization_id : credentials.organizationId as number ,
2019-12-06 09:04:50 -08:00
} ;
const task : TaskInfo = {
name ,
2020-10-22 06:46:03 -07:00
workspace_id : parseInt ( workspaceId , 10 ) ,
2019-12-06 09:04:50 -08:00
} ;
if ( additionalFields . ownerId ) {
task . owner_id = parseInt ( additionalFields . ownerId as string , 10 ) ;
}
if ( additionalFields . listId ) {
task . list_id = parseInt ( additionalFields . listId as string , 10 ) ;
}
if ( additionalFields . startsOn ) {
task . starts_on = additionalFields . startsOn as string ;
}
if ( additionalFields . dueOn ) {
task . due_on = additionalFields . dueOn as string ;
}
if ( additionalFields . mirrorParentSubscribers ) {
task . mirror_parent_subscribers = additionalFields . mirrorParentSubscribers as boolean ;
}
if ( additionalFields . mirrorParentTags ) {
task . mirror_parent_tags = additionalFields . mirrorParentTags as boolean ;
}
if ( additionalFields . noteContent ) {
task . note_content = additionalFields . noteContent as string ;
}
if ( additionalFields . noteMimeType ) {
task . note_mime_type = additionalFields . noteMimeType as string ;
}
if ( additionalFields . parentId ) {
task . parent_id = parseInt ( additionalFields . parentId as string , 10 ) ;
}
if ( additionalFields . positionList ) {
task . position_list = additionalFields . positionList as number ;
}
if ( additionalFields . positionUpcoming ) {
task . position_upcoming = additionalFields . positionUpcoming as number ;
}
if ( additionalFields . position ) {
task . position = additionalFields . position as number ;
}
if ( additionalFields . sectionId ) {
task . section_id = additionalFields . sectionId as number ;
}
if ( additionalFields . tags ) {
task . tags = ( additionalFields . tags as string ) . split ( ',' ) ;
}
body . task = task ;
try {
responseData = await flowApiRequest . call ( this , 'POST' , '/tasks' , body ) ;
responseData = responseData . task ;
2021-04-16 09:33:36 -07:00
} catch ( error ) {
2023-02-27 19:39:43 -08:00
throw new NodeApiError ( this . getNode ( ) , error as JsonObject ) ;
2019-12-06 09:04:50 -08:00
}
}
//https://developer.getflow.com/api/#tasks_update-a-task
if ( operation === 'update' ) {
const workspaceId = this . getNodeParameter ( 'workspaceId' , i ) as string ;
const taskId = this . getNodeParameter ( 'taskId' , i ) as string ;
2022-11-18 07:29:44 -08:00
const updateFields = this . getNodeParameter ( 'updateFields' , i ) ;
2019-12-06 09:04:50 -08:00
const body : ITask = {
2019-12-09 03:07:48 -08:00
organization_id : credentials.organizationId as number ,
2019-12-06 09:04:50 -08:00
} ;
const task : TaskInfo = {
workspace_id : parseInt ( workspaceId , 10 ) ,
id : parseInt ( taskId , 10 ) ,
} ;
if ( updateFields . name ) {
task . name = updateFields . name as string ;
}
if ( updateFields . ownerId ) {
task . owner_id = parseInt ( updateFields . ownerId as string , 10 ) ;
}
if ( updateFields . listId ) {
task . list_id = parseInt ( updateFields . listId as string , 10 ) ;
}
if ( updateFields . startsOn ) {
task . starts_on = updateFields . startsOn as string ;
}
if ( updateFields . dueOn ) {
task . due_on = updateFields . dueOn as string ;
}
if ( updateFields . mirrorParentSubscribers ) {
task . mirror_parent_subscribers = updateFields . mirrorParentSubscribers as boolean ;
}
if ( updateFields . mirrorParentTags ) {
task . mirror_parent_tags = updateFields . mirrorParentTags as boolean ;
}
if ( updateFields . noteContent ) {
task . note_content = updateFields . noteContent as string ;
}
if ( updateFields . noteMimeType ) {
task . note_mime_type = updateFields . noteMimeType as string ;
}
if ( updateFields . parentId ) {
task . parent_id = parseInt ( updateFields . parentId as string , 10 ) ;
}
if ( updateFields . positionList ) {
task . position_list = updateFields . positionList as number ;
}
if ( updateFields . positionUpcoming ) {
task . position_upcoming = updateFields . positionUpcoming as number ;
}
if ( updateFields . position ) {
task . position = updateFields . position as number ;
}
if ( updateFields . sectionId ) {
task . section_id = updateFields . sectionId as number ;
}
if ( updateFields . tags ) {
task . tags = ( updateFields . tags as string ) . split ( ',' ) ;
}
if ( updateFields . completed ) {
task . completed = updateFields . completed as boolean ;
}
body . task = task ;
try {
responseData = await flowApiRequest . call ( this , 'PUT' , ` /tasks/ ${ taskId } ` , body ) ;
responseData = responseData . task ;
2021-04-16 09:33:36 -07:00
} catch ( error ) {
2023-02-27 19:39:43 -08:00
throw new NodeApiError ( this . getNode ( ) , error as JsonObject ) ;
2019-12-06 09:04:50 -08:00
}
}
//https://developer.getflow.com/api/#tasks_get-task
if ( operation === 'get' ) {
const taskId = this . getNodeParameter ( 'taskId' , i ) as string ;
2022-11-18 07:29:44 -08:00
const filters = this . getNodeParameter ( 'filters' , i ) ;
2019-12-09 03:07:48 -08:00
qs . organization_id = credentials . organizationId as number ;
2019-12-06 09:04:50 -08:00
if ( filters . include ) {
qs . include = ( filters . include as string [ ] ) . join ( ',' ) ;
}
try {
2022-08-01 13:47:55 -07:00
responseData = await flowApiRequest . call ( this , 'GET' , ` /tasks/ ${ taskId } ` , { } , qs ) ;
2021-04-16 09:33:36 -07:00
} catch ( error ) {
2023-02-27 19:39:43 -08:00
throw new NodeApiError ( this . getNode ( ) , error as JsonObject ) ;
2019-12-06 09:04:50 -08:00
}
}
//https://developer.getflow.com/api/#tasks_get-tasks
if ( operation === 'getAll' ) {
2022-11-18 05:31:38 -08:00
const returnAll = this . getNodeParameter ( 'returnAll' , i ) ;
2022-11-18 07:29:44 -08:00
const filters = this . getNodeParameter ( 'filters' , i ) ;
2019-12-09 03:07:48 -08:00
qs . organization_id = credentials . organizationId as number ;
2019-12-06 09:04:50 -08:00
if ( filters . include ) {
qs . include = ( filters . include as string [ ] ) . join ( ',' ) ;
}
if ( filters . order ) {
qs . order = filters . order as string ;
}
if ( filters . workspaceId ) {
qs . workspace_id = filters . workspaceId as string ;
}
if ( filters . createdBefore ) {
qs . created_before = filters . createdBefore as string ;
}
if ( filters . createdAfter ) {
qs . created_after = filters . createdAfter as string ;
}
if ( filters . updateBefore ) {
qs . updated_before = filters . updateBefore as string ;
}
if ( filters . updateAfter ) {
qs . updated_after = filters . updateAfter as string ;
}
if ( filters . deleted ) {
qs . deleted = filters . deleted as boolean ;
}
if ( filters . cleared ) {
qs . cleared = filters . cleared as boolean ;
}
try {
2022-12-02 12:54:28 -08:00
if ( returnAll ) {
2022-08-01 13:47:55 -07:00
responseData = await FlowApiRequestAllItems . call (
this ,
'tasks' ,
'GET' ,
'/tasks' ,
{ } ,
qs ,
) ;
2019-12-06 09:04:50 -08:00
} else {
2022-11-18 06:26:22 -08:00
qs . limit = this . getNodeParameter ( 'limit' , i ) ;
2019-12-06 09:04:50 -08:00
responseData = await flowApiRequest . call ( this , 'GET' , '/tasks' , { } , qs ) ;
responseData = responseData . tasks ;
}
2021-04-16 09:33:36 -07:00
} catch ( error ) {
2023-02-27 19:39:43 -08:00
throw new NodeApiError ( this . getNode ( ) , error as JsonObject , { itemIndex : i } ) ;
2019-12-06 09:04:50 -08:00
}
}
}
2022-08-30 08:55:33 -07:00
const executionData = this . helpers . constructExecutionMetaData (
2023-02-27 19:39:43 -08:00
this . helpers . returnJsonArray ( responseData as IDataObject [ ] ) ,
2022-08-30 08:55:33 -07:00
{ itemData : { item : i } } ,
) ;
returnData . push ( . . . executionData ) ;
2019-12-06 09:04:50 -08:00
}
2022-08-30 08:55:33 -07:00
return this . prepareOutputData ( returnData ) ;
2019-12-06 09:04:50 -08:00
}
}