mirror of
https://github.com/n8n-io/n8n.git
synced 2025-01-12 13:27:31 -08:00
⚡ Reuse webhook if one exists - Woocommerce Trigger (#1196)
This commit is contained in:
parent
8214a7f22a
commit
07ed360f8f
|
@ -1,6 +1,6 @@
|
||||||
import { OptionsWithUri } from 'request';
|
import {
|
||||||
import { createHash } from 'crypto';
|
OptionsWithUri,
|
||||||
import { snakeCase } from 'change-case';
|
} from 'request';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
IExecuteFunctions,
|
IExecuteFunctions,
|
||||||
|
@ -13,6 +13,7 @@ import {
|
||||||
ICredentialDataDecryptedObject,
|
ICredentialDataDecryptedObject,
|
||||||
IDataObject,
|
IDataObject,
|
||||||
} from 'n8n-workflow';
|
} from 'n8n-workflow';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
ICouponLine,
|
ICouponLine,
|
||||||
IFeeLine,
|
IFeeLine,
|
||||||
|
@ -20,6 +21,14 @@ import {
|
||||||
IShoppingLine,
|
IShoppingLine,
|
||||||
} from './OrderInterface';
|
} from './OrderInterface';
|
||||||
|
|
||||||
|
import {
|
||||||
|
createHash,
|
||||||
|
} from 'crypto';
|
||||||
|
|
||||||
|
import {
|
||||||
|
snakeCase,
|
||||||
|
} from 'change-case';
|
||||||
|
|
||||||
export async function woocommerceApiRequest(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 woocommerceApiRequest(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
|
||||||
const credentials = this.getCredentials('wooCommerceApi');
|
const credentials = this.getCredentials('wooCommerceApi');
|
||||||
if (credentials === undefined) {
|
if (credentials === undefined) {
|
||||||
|
@ -40,6 +49,7 @@ export async function woocommerceApiRequest(this: IHookFunctions | IExecuteFunct
|
||||||
delete options.form;
|
delete options.form;
|
||||||
}
|
}
|
||||||
options = Object.assign({}, options, option);
|
options = Object.assign({}, options, option);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
return await this.helpers.request!(options);
|
return await this.helpers.request!(options);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
@ -58,7 +68,7 @@ export async function woocommerceApiRequest(this: IHookFunctions | IExecuteFunct
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function woocommerceApiRequestAllItems(this: IExecuteFunctions | ILoadOptionsFunctions, method: string, endpoint: string, body: any = {}, query: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
|
export async function woocommerceApiRequestAllItems(this: IExecuteFunctions | ILoadOptionsFunctions | IHookFunctions, method: string, endpoint: string, body: any = {}, query: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
|
||||||
|
|
||||||
const returnData: IDataObject[] = [];
|
const returnData: IDataObject[] = [];
|
||||||
|
|
||||||
|
|
|
@ -15,7 +15,9 @@ import {
|
||||||
woocommerceApiRequest,
|
woocommerceApiRequest,
|
||||||
} from './GenericFunctions';
|
} from './GenericFunctions';
|
||||||
|
|
||||||
import { createHmac } from 'crypto';
|
import {
|
||||||
|
createHmac,
|
||||||
|
} from 'crypto';
|
||||||
|
|
||||||
export class WooCommerceTrigger implements INodeType {
|
export class WooCommerceTrigger implements INodeType {
|
||||||
description: INodeTypeDescription = {
|
description: INodeTypeDescription = {
|
||||||
|
@ -112,17 +114,22 @@ export class WooCommerceTrigger implements INodeType {
|
||||||
webhookMethods = {
|
webhookMethods = {
|
||||||
default: {
|
default: {
|
||||||
async checkExists(this: IHookFunctions): Promise<boolean> {
|
async checkExists(this: IHookFunctions): Promise<boolean> {
|
||||||
|
const webhookUrl = this.getNodeWebhookUrl('default');
|
||||||
const webhookData = this.getWorkflowStaticData('node');
|
const webhookData = this.getWorkflowStaticData('node');
|
||||||
if (webhookData.webhookId === undefined) {
|
const currentEvent = this.getNodeParameter('event') as string;
|
||||||
return false;
|
const endpoint = `/webhooks`;
|
||||||
|
|
||||||
|
const webhooks = await woocommerceApiRequest.call(this, 'GET', endpoint, {}, { status: 'active', per_page: 100 });
|
||||||
|
|
||||||
|
for (const webhook of webhooks) {
|
||||||
|
if (webhook.status === 'active'
|
||||||
|
&& webhook.delivery_url === webhookUrl
|
||||||
|
&& webhook.topic === currentEvent) {
|
||||||
|
webhookData.webhookId = webhook.id;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
const endpoint = `/webhooks/${webhookData.webhookId}`;
|
return false;
|
||||||
try {
|
|
||||||
await woocommerceApiRequest.call(this, 'GET', endpoint);
|
|
||||||
} catch (e) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
},
|
},
|
||||||
async create(this: IHookFunctions): Promise<boolean> {
|
async create(this: IHookFunctions): Promise<boolean> {
|
||||||
const credentials = this.getCredentials('wooCommerceApi');
|
const credentials = this.getCredentials('wooCommerceApi');
|
||||||
|
|
Loading…
Reference in a new issue