2022-08-01 13:47:55 -07:00
import { IHookFunctions , IWebhookFunctions } from 'n8n-core' ;
2020-01-04 08:09:25 -08:00
import {
2020-01-04 11:45:14 -08:00
IDataObject ,
ILoadOptionsFunctions ,
INodePropertyOptions ,
2020-01-04 08:09:25 -08:00
INodeType ,
2020-10-01 05:01:39 -07:00
INodeTypeDescription ,
2020-01-04 08:09:25 -08:00
IWebhookResponseData ,
2021-04-16 09:33:36 -07:00
NodeApiError ,
2020-01-04 08:09:25 -08:00
} from 'n8n-workflow' ;
2022-08-01 13:47:55 -07:00
import { eventbriteApiRequest , eventbriteApiRequestAllItems } from './GenericFunctions' ;
2020-01-04 08:09:25 -08:00
export class EventbriteTrigger implements INodeType {
description : INodeTypeDescription = {
displayName : 'Eventbrite Trigger' ,
2020-05-12 06:08:19 -07:00
name : 'eventbriteTrigger' ,
2022-06-20 07:54:01 -07:00
// eslint-disable-next-line n8n-nodes-base/node-class-description-icon-not-svg
2020-01-04 08:09:25 -08:00
icon : 'file:eventbrite.png' ,
group : [ 'trigger' ] ,
version : 1 ,
description : 'Handle Eventbrite events via webhooks' ,
2022-04-14 00:36:46 -07:00
subtitle : '={{$parameter["event"]}}' ,
2020-01-04 08:09:25 -08:00
defaults : {
name : 'Eventbrite Trigger' ,
} ,
inputs : [ ] ,
outputs : [ 'main' ] ,
credentials : [
{
name : 'eventbriteApi' ,
required : true ,
2020-06-16 05:16:03 -07:00
displayOptions : {
show : {
2022-08-01 13:47:55 -07:00
authentication : [ 'privateKey' ] ,
2020-06-16 05:16:03 -07:00
} ,
} ,
} ,
{
name : 'eventbriteOAuth2Api' ,
required : true ,
displayOptions : {
show : {
2022-08-01 13:47:55 -07:00
authentication : [ 'oAuth2' ] ,
2020-06-16 05:16:03 -07:00
} ,
} ,
} ,
2020-01-04 08:09:25 -08:00
] ,
webhooks : [
{
name : 'default' ,
httpMethod : 'POST' ,
responseMode : 'onReceived' ,
path : 'webhook' ,
} ,
] ,
properties : [
2020-06-16 05:16:03 -07:00
{
displayName : 'Authentication' ,
name : 'authentication' ,
type : 'options' ,
options : [
{
2020-06-25 16:43:49 -07:00
name : 'Private Key' ,
value : 'privateKey' ,
2020-06-16 05:16:03 -07:00
} ,
{
name : 'OAuth2' ,
value : 'oAuth2' ,
} ,
] ,
2020-06-25 16:43:49 -07:00
default : 'privateKey' ,
2020-06-16 05:16:03 -07:00
} ,
2020-01-04 08:09:25 -08:00
{
2022-06-03 10:23:49 -07:00
displayName : 'Organization Name or ID' ,
2020-01-04 08:09:25 -08:00
name : 'organization' ,
type : 'options' ,
required : true ,
typeOptions : {
2020-10-22 06:46:03 -07:00
loadOptionsMethod : 'getOrganizations' ,
2020-01-04 08:09:25 -08:00
} ,
2020-01-04 20:19:10 -08:00
default : '' ,
2022-08-01 13:47:55 -07:00
description :
'The Eventbrite Organization to work on. Choose from the list, or specify an ID using an <a href="https://docs.n8n.io/code-examples/expressions/">expression</a>.' ,
2020-01-04 08:09:25 -08:00
} ,
{
2022-06-03 10:23:49 -07:00
displayName : 'Event Name or ID' ,
2020-01-04 08:09:25 -08:00
name : 'event' ,
type : 'options' ,
required : true ,
typeOptions : {
2022-08-01 13:47:55 -07:00
loadOptionsDependsOn : [ 'organization' ] ,
2020-10-22 06:46:03 -07:00
loadOptionsMethod : 'getEvents' ,
2020-01-04 08:09:25 -08:00
} ,
2020-01-04 20:19:10 -08:00
default : '' ,
2022-08-01 13:47:55 -07:00
description :
'Limit the triggers to this event. Choose from the list, or specify an ID using an <a href="https://docs.n8n.io/code-examples/expressions/">expression</a>.' ,
2020-01-04 08:09:25 -08:00
} ,
{
displayName : 'Actions' ,
name : 'actions' ,
type : 'multiOptions' ,
options : [
{
name : 'attendee.checked_in' ,
2020-10-22 06:46:03 -07:00
value : 'attendee.checked_in' ,
2020-01-04 08:09:25 -08:00
} ,
{
name : 'attendee.checked_out' ,
2020-10-22 06:46:03 -07:00
value : 'attendee.checked_out' ,
2020-01-04 08:09:25 -08:00
} ,
2022-06-20 07:54:01 -07:00
{
name : 'attendee.updated' ,
value : 'attendee.updated' ,
} ,
2020-01-04 08:09:25 -08:00
{
name : 'event.created' ,
2020-10-22 06:46:03 -07:00
value : 'event.created' ,
2020-01-04 08:09:25 -08:00
} ,
{
name : 'event.published' ,
2020-10-22 06:46:03 -07:00
value : 'event.published' ,
2020-01-04 08:09:25 -08:00
} ,
{
name : 'event.unpublished' ,
2020-10-22 06:46:03 -07:00
value : 'event.unpublished' ,
2020-01-04 08:09:25 -08:00
} ,
{
name : 'event.updated' ,
2020-10-22 06:46:03 -07:00
value : 'event.updated' ,
2020-01-04 08:09:25 -08:00
} ,
{
name : 'order.placed' ,
2020-10-22 06:46:03 -07:00
value : 'order.placed' ,
2020-01-04 08:09:25 -08:00
} ,
{
name : 'order.refunded' ,
2020-10-22 06:46:03 -07:00
value : 'order.refunded' ,
2020-01-04 08:09:25 -08:00
} ,
{
name : 'order.updated' ,
2020-10-22 06:46:03 -07:00
value : 'order.updated' ,
2020-01-04 08:09:25 -08:00
} ,
{
name : 'organizer.updated' ,
2020-10-22 06:46:03 -07:00
value : 'organizer.updated' ,
2020-01-04 08:09:25 -08:00
} ,
{
name : 'ticket_class.created' ,
2020-10-22 06:46:03 -07:00
value : 'ticket_class.created' ,
2020-01-04 08:09:25 -08:00
} ,
{
name : 'ticket_class.deleted' ,
2020-10-22 06:46:03 -07:00
value : 'ticket_class.deleted' ,
2020-01-04 08:09:25 -08:00
} ,
{
name : 'ticket_class.updated' ,
2020-10-22 06:46:03 -07:00
value : 'ticket_class.updated' ,
2020-01-04 08:09:25 -08:00
} ,
{
name : 'venue.updated' ,
2020-10-22 06:46:03 -07:00
value : 'venue.updated' ,
2020-01-04 08:09:25 -08:00
} ,
] ,
required : true ,
default : [ ] ,
2022-05-06 14:01:25 -07:00
description : 'One or more action to subscribe to' ,
2020-01-04 08:09:25 -08:00
} ,
2020-01-04 11:45:14 -08:00
{
displayName : 'Resolve Data' ,
name : 'resolveData' ,
type : 'boolean' ,
default : true ,
2022-06-20 07:54:01 -07:00
// eslint-disable-next-line n8n-nodes-base/node-param-description-boolean-without-whether
2022-08-01 13:47:55 -07:00
description :
'By default does the webhook-data only contain the URL to receive the object data manually. If this option gets activated, it will resolve the data automatically.' ,
2020-01-04 11:45:14 -08:00
} ,
2020-01-04 08:09:25 -08:00
] ,
} ;
methods = {
loadOptions : {
// Get all the available organizations to display them to user so that he can
// select them easily
async getOrganizations ( this : ILoadOptionsFunctions ) : Promise < INodePropertyOptions [ ] > {
const returnData : INodePropertyOptions [ ] = [ ] ;
2022-08-01 13:47:55 -07:00
const organizations = await eventbriteApiRequestAllItems . call (
this ,
'organizations' ,
'GET' ,
'/users/me/organizations' ,
) ;
2020-01-04 08:09:25 -08:00
for ( const organization of organizations ) {
const organizationName = organization . name ;
const organizationId = organization . id ;
returnData . push ( {
name : organizationName ,
value : organizationId ,
} ) ;
}
return returnData ;
} ,
// Get all the available events to display them to user so that he can
// select them easily
async getEvents ( this : ILoadOptionsFunctions ) : Promise < INodePropertyOptions [ ] > {
2022-04-14 00:36:46 -07:00
const returnData : INodePropertyOptions [ ] = [ { name : 'All' , value : 'all' } ] ;
2020-01-04 08:09:25 -08:00
const organization = this . getCurrentNodeParameter ( 'organization' ) ;
2022-08-01 13:47:55 -07:00
const events = await eventbriteApiRequestAllItems . call (
this ,
'events' ,
'GET' ,
` /organizations/ ${ organization } /events ` ,
) ;
2020-01-04 08:09:25 -08:00
for ( const event of events ) {
const eventName = event . name . text ;
const eventId = event . id ;
returnData . push ( {
name : eventName ,
value : eventId ,
} ) ;
}
return returnData ;
} ,
} ,
} ;
2022-12-02 12:54:28 -08:00
2020-01-04 08:09:25 -08:00
// @ts-ignore
webhookMethods = {
default : {
async checkExists ( this : IHookFunctions ) : Promise < boolean > {
const webhookData = this . getWorkflowStaticData ( 'node' ) ;
2020-06-25 16:43:49 -07:00
const webhookUrl = this . getNodeWebhookUrl ( 'default' ) ;
2020-06-16 05:16:03 -07:00
const organisation = this . getNodeParameter ( 'organization' ) as string ;
2020-06-25 16:43:49 -07:00
const actions = this . getNodeParameter ( 'actions' ) as string [ ] ;
2020-06-16 05:16:03 -07:00
const endpoint = ` /organizations/ ${ organisation } /webhooks/ ` ;
2020-06-25 16:43:49 -07:00
const { webhooks } = await eventbriteApiRequest . call ( this , 'GET' , endpoint ) ;
const check = ( currentActions : string [ ] , webhookActions : string [ ] ) = > {
for ( const currentAction of currentActions ) {
if ( ! webhookActions . includes ( currentAction ) ) {
return false ;
}
}
return true ;
} ;
for ( const webhook of webhooks ) {
if ( webhook . endpoint_url === webhookUrl && check ( actions , webhook . actions ) ) {
webhookData . webhookId = webhook . id ;
return true ;
}
2020-01-04 08:09:25 -08:00
}
2020-06-25 16:43:49 -07:00
return false ;
2020-01-04 08:09:25 -08:00
} ,
async create ( this : IHookFunctions ) : Promise < boolean > {
const webhookUrl = this . getNodeWebhookUrl ( 'default' ) ;
const webhookData = this . getWorkflowStaticData ( 'node' ) ;
2020-06-16 05:16:03 -07:00
const organisation = this . getNodeParameter ( 'organization' ) as string ;
2020-01-04 08:09:25 -08:00
const event = this . getNodeParameter ( 'event' ) as string ;
const actions = this . getNodeParameter ( 'actions' ) as string [ ] ;
2020-06-16 05:16:03 -07:00
const endpoint = ` /organizations/ ${ organisation } /webhooks/ ` ;
2020-01-04 11:45:14 -08:00
const body : IDataObject = {
2020-01-04 08:09:25 -08:00
endpoint_url : webhookUrl ,
actions : actions.join ( ',' ) ,
event_id : event ,
} ;
2022-04-14 00:36:46 -07:00
if ( event === 'all' || event === '' ) {
delete body . event_id ;
}
2020-01-04 11:45:14 -08:00
const responseData = await eventbriteApiRequest . call ( this , 'POST' , endpoint , body ) ;
2020-01-04 08:09:25 -08:00
webhookData . webhookId = responseData . id ;
return true ;
} ,
async delete ( this : IHookFunctions ) : Promise < boolean > {
let responseData ;
const webhookData = this . getWorkflowStaticData ( 'node' ) ;
const endpoint = ` /webhooks/ ${ webhookData . webhookId } / ` ;
try {
responseData = await eventbriteApiRequest . call ( this , 'DELETE' , endpoint ) ;
2022-08-01 13:47:55 -07:00
} catch ( error ) {
2020-01-04 08:09:25 -08:00
return false ;
}
if ( ! responseData . success ) {
return false ;
}
delete webhookData . webhookId ;
return true ;
} ,
} ,
} ;
async webhook ( this : IWebhookFunctions ) : Promise < IWebhookResponseData > {
const req = this . getRequestObject ( ) ;
2020-01-04 11:45:14 -08:00
if ( req . body . api_url === undefined ) {
2022-08-01 13:47:55 -07:00
throw new NodeApiError ( this . getNode ( ) , req . body , {
message : 'The received data does not contain required "api_url" property!' ,
} ) ;
2020-01-04 11:45:14 -08:00
}
const resolveData = this . getNodeParameter ( 'resolveData' , false ) as boolean ;
2022-12-02 12:54:28 -08:00
if ( ! resolveData ) {
2020-01-04 11:45:14 -08:00
// Return the data as it got received
return {
2022-08-01 13:47:55 -07:00
workflowData : [ this . helpers . returnJsonArray ( req . body ) ] ,
2020-01-04 11:45:14 -08:00
} ;
}
if ( req . body . api_url . includes ( 'api-endpoint-to-fetch-object-details' ) ) {
return {
workflowData : [
this . helpers . returnJsonArray ( {
2022-08-01 13:47:55 -07:00
placeholder :
'Test received. To display actual data of object get the webhook triggered by performing the action which triggers it.' ,
2020-10-22 06:46:03 -07:00
} ) ,
2020-01-04 11:45:14 -08:00
] ,
} ;
}
2022-08-01 13:47:55 -07:00
const responseData = await eventbriteApiRequest . call (
this ,
'GET' ,
'' ,
{ } ,
undefined ,
req . body . api_url ,
) ;
2020-01-04 11:45:14 -08:00
2020-01-04 08:09:25 -08:00
return {
2022-08-01 13:47:55 -07:00
workflowData : [ this . helpers . returnJsonArray ( responseData ) ] ,
2020-01-04 08:09:25 -08:00
} ;
}
}