Improvements to Eventbrite-Trigger

This commit is contained in:
ricardo 2020-06-25 19:43:49 -04:00
parent 755079c360
commit 28befc1034
3 changed files with 35 additions and 17 deletions

View file

@ -8,7 +8,7 @@ export class EventbriteApi implements ICredentialType {
displayName = 'Eventbrite API'; displayName = 'Eventbrite API';
properties = [ properties = [
{ {
displayName: 'API Key', displayName: 'Private Key',
name: 'apiKey', name: 'apiKey',
type: 'string' as NodePropertyTypes, type: 'string' as NodePropertyTypes,
default: '', default: '',

View file

@ -38,7 +38,7 @@ export class EventbriteTrigger implements INodeType {
displayOptions: { displayOptions: {
show: { show: {
authentication: [ authentication: [
'accessToken', 'privateKey',
], ],
}, },
}, },
@ -70,15 +70,15 @@ export class EventbriteTrigger implements INodeType {
type: 'options', type: 'options',
options: [ options: [
{ {
name: 'Access Token', name: 'Private Key',
value: 'accessToken', value: 'privateKey',
}, },
{ {
name: 'OAuth2', name: 'OAuth2',
value: 'oAuth2', value: 'oAuth2',
}, },
], ],
default: 'accessToken', default: 'privateKey',
description: 'The resource to operate on.', description: 'The resource to operate on.',
}, },
{ {
@ -184,7 +184,6 @@ export class EventbriteTrigger implements INodeType {
description: 'By default does the webhook-data only contain the URL to receive<br />the object data manually. If this option gets activated it<br />will resolve the data automatically.', description: 'By default does the webhook-data only contain the URL to receive<br />the object data manually. If this option gets activated it<br />will resolve the data automatically.',
}, },
], ],
}; };
methods = { methods = {
@ -227,18 +226,31 @@ export class EventbriteTrigger implements INodeType {
default: { default: {
async checkExists(this: IHookFunctions): Promise<boolean> { async checkExists(this: IHookFunctions): Promise<boolean> {
const webhookData = this.getWorkflowStaticData('node'); const webhookData = this.getWorkflowStaticData('node');
const webhookUrl = this.getNodeWebhookUrl('default');
const organisation = this.getNodeParameter('organization') as string; const organisation = this.getNodeParameter('organization') as string;
const actions = this.getNodeParameter('actions') as string[];
if (webhookData.webhookId === undefined) {
return false;
}
const endpoint = `/organizations/${organisation}/webhooks/`; const endpoint = `/organizations/${organisation}/webhooks/`;
try {
await eventbriteApiRequest.call(this, 'GET', endpoint); const { webhooks } = await eventbriteApiRequest.call(this, 'GET', endpoint);
} catch (e) {
return false; 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;
}
} }
return true;
return false;
}, },
async create(this: IHookFunctions): Promise<boolean> { async create(this: IHookFunctions): Promise<boolean> {
const webhookUrl = this.getNodeWebhookUrl('default'); const webhookUrl = this.getNodeWebhookUrl('default');

View file

@ -1,4 +1,7 @@
import { OptionsWithUri } from 'request'; import {
OptionsWithUri,
} from 'request';
import { import {
IExecuteFunctions, IExecuteFunctions,
IExecuteSingleFunctions, IExecuteSingleFunctions,
@ -6,7 +9,10 @@ import {
ILoadOptionsFunctions, ILoadOptionsFunctions,
IWebhookFunctions, IWebhookFunctions,
} from 'n8n-core'; } from 'n8n-core';
import { IDataObject } from 'n8n-workflow';
import {
IDataObject,
} from 'n8n-workflow';
export async function eventbriteApiRequest(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions | IWebhookFunctions, method: string, resource: string, body: any = {}, qs: IDataObject = {}, uri?: string, option: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any export async function eventbriteApiRequest(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions | IWebhookFunctions, method: string, resource: string, body: any = {}, qs: IDataObject = {}, uri?: string, option: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
let options: OptionsWithUri = { let options: OptionsWithUri = {
@ -25,7 +31,7 @@ export async function eventbriteApiRequest(this: IHookFunctions | IExecuteFuncti
const authenticationMethod = this.getNodeParameter('authentication', 0); const authenticationMethod = this.getNodeParameter('authentication', 0);
try { try {
if (authenticationMethod === 'accessToken') { if (authenticationMethod === 'privateKey') {
const credentials = this.getCredentials('eventbriteApi'); const credentials = this.getCredentials('eventbriteApi');
if (credentials === undefined) { if (credentials === undefined) {
throw new Error('No credentials got returned!'); throw new Error('No credentials got returned!');