diff --git a/packages/nodes-base/nodes/WooCommerce/GenericFunctions.ts b/packages/nodes-base/nodes/WooCommerce/GenericFunctions.ts index d96bbff10c..227f369c92 100644 --- a/packages/nodes-base/nodes/WooCommerce/GenericFunctions.ts +++ b/packages/nodes-base/nodes/WooCommerce/GenericFunctions.ts @@ -1,6 +1,6 @@ -import { OptionsWithUri } from 'request'; -import { createHash } from 'crypto'; -import { snakeCase } from 'change-case'; +import { + OptionsWithUri, +} from 'request'; import { IExecuteFunctions, @@ -13,6 +13,7 @@ import { ICredentialDataDecryptedObject, IDataObject, } from 'n8n-workflow'; + import { ICouponLine, IFeeLine, @@ -20,6 +21,14 @@ import { IShoppingLine, } 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 { // tslint:disable-line:no-any const credentials = this.getCredentials('wooCommerceApi'); if (credentials === undefined) { @@ -40,6 +49,7 @@ export async function woocommerceApiRequest(this: IHookFunctions | IExecuteFunct delete options.form; } options = Object.assign({}, options, option); + try { return await this.helpers.request!(options); } 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 { // tslint:disable-line:no-any +export async function woocommerceApiRequestAllItems(this: IExecuteFunctions | ILoadOptionsFunctions | IHookFunctions, method: string, endpoint: string, body: any = {}, query: IDataObject = {}): Promise { // tslint:disable-line:no-any const returnData: IDataObject[] = []; @@ -135,4 +145,4 @@ export function toSnakeCase(data: } } } -} +} \ No newline at end of file diff --git a/packages/nodes-base/nodes/WooCommerce/WooCommerceTrigger.node.ts b/packages/nodes-base/nodes/WooCommerce/WooCommerceTrigger.node.ts index e947792f5d..1bea61ff6c 100644 --- a/packages/nodes-base/nodes/WooCommerce/WooCommerceTrigger.node.ts +++ b/packages/nodes-base/nodes/WooCommerce/WooCommerceTrigger.node.ts @@ -15,7 +15,9 @@ import { woocommerceApiRequest, } from './GenericFunctions'; -import { createHmac } from 'crypto'; +import { + createHmac, +} from 'crypto'; export class WooCommerceTrigger implements INodeType { description: INodeTypeDescription = { @@ -112,17 +114,22 @@ export class WooCommerceTrigger implements INodeType { webhookMethods = { default: { async checkExists(this: IHookFunctions): Promise { + const webhookUrl = this.getNodeWebhookUrl('default'); const webhookData = this.getWorkflowStaticData('node'); - if (webhookData.webhookId === undefined) { - return false; + const currentEvent = this.getNodeParameter('event') as string; + 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}`; - try { - await woocommerceApiRequest.call(this, 'GET', endpoint); - } catch (e) { - return false; - } - return true; + return false; }, async create(this: IHookFunctions): Promise { const credentials = this.getCredentials('wooCommerceApi'); @@ -178,4 +185,4 @@ export class WooCommerceTrigger implements INodeType { ], }; } -} +} \ No newline at end of file