2022-03-20 01:54:31 -07:00
import {
IDataObject ,
IHookFunctions ,
INodeType ,
INodeTypeDescription ,
IWebhookFunctions ,
IWebhookResponseData ,
} from 'n8n-workflow' ;
import {
downloadAttachments ,
formatSubmission ,
koBoToolboxApiRequest ,
loadForms ,
2022-08-17 08:50:24 -07:00
parseStringList ,
2022-03-20 01:54:31 -07:00
} from './GenericFunctions' ;
2022-08-17 08:50:24 -07:00
import { options } from './Options' ;
2022-03-20 01:54:31 -07:00
export class KoBoToolboxTrigger implements INodeType {
description : INodeTypeDescription = {
displayName : 'KoBoToolbox Trigger' ,
name : 'koBoToolboxTrigger' ,
icon : 'file:koBoToolbox.svg' ,
group : [ 'trigger' ] ,
version : 1 ,
description : 'Process KoBoToolbox submissions' ,
defaults : {
name : 'KoBoToolbox Trigger' ,
} ,
inputs : [ ] ,
outputs : [ 'main' ] ,
credentials : [
{
name : 'koBoToolboxApi' ,
required : true ,
} ,
] ,
webhooks : [
{
name : 'default' ,
httpMethod : 'POST' ,
responseMode : 'onReceived' ,
path : 'webhook' ,
} ,
] ,
properties : [
{
2022-06-03 10:23:49 -07:00
displayName : 'Form Name or ID' ,
2022-03-20 01:54:31 -07:00
name : 'formId' ,
type : 'options' ,
typeOptions : {
loadOptionsMethod : 'loadForms' ,
} ,
required : true ,
default : '' ,
2022-08-17 08:50:24 -07:00
description :
'Form ID (e.g. aSAvYreNzVEkrWg5Gdcvg). Choose from the list, or specify an ID using an <a href="https://docs.n8n.io/code-examples/expressions/">expression</a>.' ,
2022-03-20 01:54:31 -07:00
} ,
{
displayName : 'Trigger On' ,
name : 'triggerOn' ,
type : 'options' ,
required : true ,
default : 'formSubmission' ,
options : [
{
name : 'On Form Submission' ,
value : 'formSubmission' ,
} ,
] ,
} ,
{ . . . options } ,
] ,
} ;
webhookMethods = {
default : {
async checkExists ( this : IHookFunctions ) : Promise < boolean > {
const webhookData = this . getWorkflowStaticData ( 'node' ) ;
const webhookUrl = this . getNodeWebhookUrl ( 'default' ) ;
const formId = this . getNodeParameter ( 'formId' ) as string ; //tslint:disable-line:variable-name
const webhooks = await koBoToolboxApiRequest . call ( this , {
url : ` /api/v2/assets/ ${ formId } /hooks/ ` ,
} ) ;
for ( const webhook of webhooks || [ ] ) {
if ( webhook . endpoint === webhookUrl && webhook . active === true ) {
webhookData . webhookId = webhook . uid ;
return true ;
}
}
return false ;
} ,
async create ( this : IHookFunctions ) : Promise < boolean > {
const webhookData = this . getWorkflowStaticData ( 'node' ) ;
const webhookUrl = this . getNodeWebhookUrl ( 'default' ) ;
2022-05-14 02:20:45 -07:00
const workflow = this . getWorkflow ( ) ;
2022-03-20 01:54:31 -07:00
const formId = this . getNodeParameter ( 'formId' ) as string ; //tslint:disable-line:variable-name
const response = await koBoToolboxApiRequest . call ( this , {
method : 'POST' ,
url : ` /api/v2/assets/ ${ formId } /hooks/ ` ,
body : {
2022-05-14 02:20:45 -07:00
name : ` n8n webhook id ${ workflow . id } : ${ workflow . name } ` ,
2022-03-20 01:54:31 -07:00
endpoint : webhookUrl ,
email_notification : true ,
} ,
} ) ;
if ( response . uid ) {
webhookData . webhookId = response . uid ;
return true ;
}
return false ;
} ,
async delete ( this : IHookFunctions ) : Promise < boolean > {
const webhookData = this . getWorkflowStaticData ( 'node' ) ;
const formId = this . getNodeParameter ( 'formId' ) as string ; //tslint:disable-line:variable-name
try {
await koBoToolboxApiRequest . call ( this , {
method : 'DELETE' ,
url : ` /api/v2/assets/ ${ formId } /hooks/ ${ webhookData . webhookId } ` ,
} ) ;
} catch ( error ) {
return false ;
}
delete webhookData . webhookId ;
return true ;
} ,
} ,
} ;
methods = {
loadOptions : {
loadForms ,
} ,
} ;
async webhook ( this : IWebhookFunctions ) : Promise < IWebhookResponseData > {
const req = this . getRequestObject ( ) ;
const formatOptions = this . getNodeParameter ( 'formatOptions' ) as IDataObject ;
2022-08-17 08:50:24 -07:00
// prettier-ignore
2022-03-20 01:54:31 -07:00
const responseData = formatOptions . reformat
? formatSubmission ( req . body , parseStringList ( formatOptions . selectMask as string ) , parseStringList ( formatOptions . numberMask as string ) )
: req . body ;
if ( formatOptions . download ) {
// Download related attachments
return {
2022-08-17 08:50:24 -07:00
workflowData : [ [ await downloadAttachments . call ( this , responseData , formatOptions ) ] ] ,
2022-03-20 01:54:31 -07:00
} ;
2022-08-17 08:50:24 -07:00
} else {
2022-03-20 01:54:31 -07:00
return {
2022-08-17 08:50:24 -07:00
workflowData : [ this . helpers . returnJsonArray ( [ responseData ] ) ] ,
2022-03-20 01:54:31 -07:00
} ;
}
}
}