checkExists modified

This commit is contained in:
Rupenieks 2020-06-26 13:32:48 +02:00
parent 55ed53579f
commit d199e1e638
2 changed files with 128 additions and 117 deletions

View file

@ -37,6 +37,8 @@ export async function postmarkApiRequest(this: IExecuteFunctions | IWebhookFunct
} }
options = Object.assign({}, options, option); options = Object.assign({}, options, option);
console.log(options);
try { try {
return await this.helpers.request!(options); return await this.helpers.request!(options);
} catch (error) { } catch (error) {
@ -44,4 +46,50 @@ export async function postmarkApiRequest(this: IExecuteFunctions | IWebhookFunct
} }
} }
// tslint:disable-next-line: no-any
export function convertTriggerObjectToStringArray (webhookObject : any) : string[] {
const triggers = webhookObject.Triggers;
const webhookEvents : string[] = [];
// Translate Webhook trigger settings to string array
if (triggers.Open.Enabled) {
webhookEvents.push('open');
}
if (triggers.Open.PostFirstOpenOnly) {
webhookEvents.push('firstOpen');
}
if (triggers.Click.Enabled) {
webhookEvents.push('click');
}
if (triggers.Delivery.Enabled) {
webhookEvents.push('delivery');
}
if (triggers.Bounce.Enabled) {
webhookEvents.push('bounce');
}
if (triggers.Bounce.IncludeContent) {
webhookEvents.push('bounceContent');
}
if (triggers.SpamComplaint.Enabled) {
webhookEvents.push('spamComplaint');
}
if (triggers.SpamComplaint.IncludeContent) {
webhookEvents.push('spamComplaintContent');
}
if (triggers.SubscriptionChange.Enabled) {
webhookEvents.push('subscriptionChange');
}
return webhookEvents;
}
export function eventExists (currentEvents : string[], webhookEvents: string[]) {
for (const currentEvent of currentEvents) {
if (!webhookEvents.includes(currentEvent)) {
return false;
}
}
return true;
}

View file

@ -10,7 +10,9 @@ import {
} from 'n8n-workflow'; } from 'n8n-workflow';
import { import {
postmarkApiRequest, convertTriggerObjectToStringArray,
eventExists,
postmarkApiRequest
} from './GenericFunctions'; } from './GenericFunctions';
export class PostmarkTrigger implements INodeType { export class PostmarkTrigger implements INodeType {
@ -43,88 +45,54 @@ export class PostmarkTrigger implements INodeType {
], ],
properties: [ properties: [
{ {
displayName: 'Open', displayName: 'Events',
name: 'open', name: 'events',
type: 'boolean', type: 'multiOptions',
default: false, options: [
description: 'Listing for if the Open webhook is enabled/disabled.', {
name: 'Open',
value: 'open',
description: 'Trigger webhook on open.'
}, },
{ {
displayName: 'First Open Only', name: 'First Open',
name: 'postFirstOpenOnly', value: 'firstOpen',
type: 'boolean', description: 'Trigger on first open only.'
default: false, },
displayOptions: { {
show: { name: 'Click',
open: [ value: 'click',
true },
{
name: 'Delivery',
value: 'delivery',
},
{
name: 'Bounce',
value: 'bounce',
},
{
name: 'Bounce Content',
value: 'bounceContent',
description: 'Webhook will send full bounce content.'
},
{
name: 'Spam Complaint',
value: 'spamComplaint',
},
{
name: 'Spam Complaint Content',
value: 'spamComplaintContent',
description: 'Webhook will send full bounce content.'
},
{
name: 'Subscription Change',
value: 'subscriptionChange',
},
], ],
}, default: [],
}, required: true,
description: 'Webhook will only post on first open if enabled.', description: 'Webhook events that will be enabled for that endpoint.',
},
{
displayName: 'Click',
name: 'click',
type: 'boolean',
default: false,
description: 'Listing for if the Click webhook is enabled/disabled.',
},
{
displayName: 'Delivery',
name: 'delivery',
type: 'boolean',
default: false,
description: 'Listing for if the Delivery webhook is enabled/disabled.',
},
{
displayName: 'Bounce',
name: 'bounce',
type: 'boolean',
default: false,
description: 'Listing for if the Bounce webhook is enabled/disabled.',
},
{
displayName: 'Bounce Include Content',
name: 'bounceIncludeContent',
type: 'boolean',
default: false,
displayOptions: {
show: {
bounce: [
true
],
},
},
description: 'Webhook will send full bounce content if IncludeContent is enabled.',
},
{
displayName: 'Spam Complaint',
name: 'spamComplaint',
type: 'boolean',
default: false,
description: 'Listing for if the Spam webhook is enabled/disabled.',
},
{
displayName: 'Spam Complaint Include Content',
name: 'spamComplaintIncludeContent',
type: 'boolean',
default: false,
displayOptions: {
show: {
spamComplaint: [
true
],
},
},
description: 'Webhook will send full spam content if IncludeContent is enabled.',
},
{
displayName: 'Subscription Change',
name: 'subscriptionChange',
type: 'boolean',
default: false,
description: 'Listing for if the Subscription Change webhook is enabled/disabled.',
}, },
], ],
@ -135,23 +103,28 @@ export class PostmarkTrigger 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 events = this.getNodeParameter('events') as string[];
if (webhookData.webhookId === undefined) { // Get all webhooks
// No webhook id is set so no webhook can exist const endpoint = `/webhooks`;
return false;
}
// Webhook got created before so check if it still exists
const endpoint = `/webhooks/${webhookData.webhookId}`;
const responseData = await postmarkApiRequest.call(this, 'GET', endpoint, {}); const responseData = await postmarkApiRequest.call(this, 'GET', endpoint, {});
if (responseData.ID === undefined) { // No webhooks exist
if (responseData.Webhooks.length === 0) {
return false; return false;
} }
else if (responseData.ID === webhookData.id) {
// If webhooks exist, check if any match current settings
for (const webhook of responseData.Webhooks) {
if (webhook.Url === webhookUrl && eventExists(events, convertTriggerObjectToStringArray(webhook))) {
webhookData.webhookId = webhook.ID;
// webhook identical to current settings. re-assign webhook id to found webhook.
return true; return true;
} }
}
return false; return false;
}, },
async create(this: IHookFunctions): Promise<boolean> { async create(this: IHookFunctions): Promise<boolean> {
@ -187,44 +160,34 @@ export class PostmarkTrigger implements INodeType {
} }
}; };
const open = this.getNodeParameter('open', 0); const events = this.getNodeParameter('events') as string[];
const postFirstOpenOnly = this.getNodeParameter('postFirstOpenOnly', 0);
const click = this.getNodeParameter('click', 0);
const delivery = this.getNodeParameter('delivery', 0);
const bounce = this.getNodeParameter('bounce', 0);
const bounceIncludeContent = this.getNodeParameter('bounceIncludeContent', 0);
const spamComplaint = this.getNodeParameter('spamComplaint', 0);
const spamComplaintIncludeContent = this.getNodeParameter('spamComplaintIncludeContent', 0);
const subscriptionChange = this.getNodeParameter('subscriptionChange', 0);
if (open) { if (events.includes('open')) {
body.Triggers.Open.Enabled = true;
}
if (events.includes('firstOpen')) {
body.Triggers.Open.Enabled = true; body.Triggers.Open.Enabled = true;
if (postFirstOpenOnly) {
body.Triggers.Open.PostFirstOpenOnly = true; body.Triggers.Open.PostFirstOpenOnly = true;
} }
} if (events.includes('click')) {
if (click) {
body.Triggers.Click.Enabled = true; body.Triggers.Click.Enabled = true;
} }
if (delivery) { if (events.includes('delivery')) {
body.Triggers.Delivery.Enabled = true; body.Triggers.Delivery.Enabled = true;
} }
if (bounce) { if (events.includes('bounce')) {
body.Triggers.Bounce.Enabled = true; body.Triggers.Bounce.Enabled = true;
}
if (bounceIncludeContent) { if (events.includes('bounceContent')) {
body.Triggers.Bounce.IncludeContent = true; body.Triggers.Bounce.IncludeContent = true;
} }
} if (events.includes('spamComplaint')) {
if (spamComplaint) {
body.Triggers.SpamComplaint.Enabled = true; body.Triggers.SpamComplaint.Enabled = true;
}
if (spamComplaintIncludeContent) { if (events.includes('spamComplaintContent')) {
body.Triggers.SpamComplaint.IncludeContent = true; body.Triggers.SpamComplaint.IncludeContent = true;
} }
} if (events.includes('subscriptionChange')) {
if (subscriptionChange) {
body.Triggers.SubscriptionChange.Enabled = true; body.Triggers.SubscriptionChange.Enabled = true;
} }