mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-17 10:02:05 +00:00
⚡ Prepare webhooks for multitenancy
This commit is contained in:
@@ -1,11 +1,18 @@
|
||||
import * as express from 'express';
|
||||
import {
|
||||
In as findIn,
|
||||
FindManyOptions,
|
||||
} from 'typeorm';
|
||||
|
||||
import {
|
||||
Db,
|
||||
IResponseCallbackData,
|
||||
IWorkflowDb,
|
||||
NodeTypes,
|
||||
Push,
|
||||
ResponseHelper,
|
||||
WebhookHelpers,
|
||||
IWorkflowDb,
|
||||
WorkflowHelpers,
|
||||
} from './';
|
||||
|
||||
import {
|
||||
@@ -60,9 +67,17 @@ export class TestWebhooks {
|
||||
throw new ResponseHelper.ResponseError('The requested webhook is not registred.', 404, 404);
|
||||
}
|
||||
|
||||
const workflowData = await Db.collections.Workflow!.findOne(webhookData.workflowId);
|
||||
if (workflowData === undefined) {
|
||||
throw new ResponseHelper.ResponseError(`Could not find workflow with id "${webhookData.workflowId}"`, 404, 404);
|
||||
}
|
||||
|
||||
const nodeTypes = NodeTypes();
|
||||
const workflow = new Workflow(webhookData.workflowId, workflowData.nodes, workflowData.connections, workflowData.active, nodeTypes, workflowData.staticData, workflowData.settings);
|
||||
|
||||
// Get the node which has the webhook defined to know where to start from and to
|
||||
// get additional data
|
||||
const workflowStartNode = webhookData.workflow.getNode(webhookData.node);
|
||||
const workflowStartNode = workflow.getNode(webhookData.node);
|
||||
if (workflowStartNode === null) {
|
||||
throw new ResponseHelper.ResponseError('Could not find node to process webhook.', 404, 404);
|
||||
}
|
||||
@@ -72,8 +87,7 @@ export class TestWebhooks {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
try {
|
||||
const executionMode = 'manual';
|
||||
|
||||
const executionId = await WebhookHelpers.executeWebhook(webhookData, this.testWebhookData[webhookKey].workflowData, workflowStartNode, executionMode, this.testWebhookData[webhookKey].sessionId, request, response, (error: Error | null, data: IResponseCallbackData) => {
|
||||
const executionId = await WebhookHelpers.executeWebhook(workflow, webhookData, this.testWebhookData[webhookKey].workflowData, workflowStartNode, executionMode, this.testWebhookData[webhookKey].sessionId, request, response, (error: Error | null, data: IResponseCallbackData) => {
|
||||
if (error !== null) {
|
||||
return reject(error);
|
||||
}
|
||||
@@ -90,7 +104,7 @@ export class TestWebhooks {
|
||||
// Inform editor-ui that webhook got received
|
||||
if (this.testWebhookData[webhookKey].sessionId !== undefined) {
|
||||
const pushInstance = Push.getInstance();
|
||||
pushInstance.send('testWebhookReceived', { workflowId: webhookData.workflow.id, executionId }, this.testWebhookData[webhookKey].sessionId!);
|
||||
pushInstance.send('testWebhookReceived', { workflowId: webhookData.workflowId, executionId }, this.testWebhookData[webhookKey].sessionId!);
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
@@ -100,7 +114,7 @@ export class TestWebhooks {
|
||||
// Remove the webhook
|
||||
clearTimeout(this.testWebhookData[webhookKey].timeout);
|
||||
delete this.testWebhookData[webhookKey];
|
||||
this.activeWebhooks!.removeByWorkflowId(webhookData.workflow.id!.toString());
|
||||
this.activeWebhooks!.removeWorkflow(workflow);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -125,7 +139,7 @@ export class TestWebhooks {
|
||||
|
||||
// Remove test-webhooks automatically if they do not get called (after 120 seconds)
|
||||
const timeout = setTimeout(() => {
|
||||
this.cancelTestWebhook(workflowData.id.toString());
|
||||
this.cancelTestWebhook(workflowData.id.toString(), workflow);
|
||||
}, 120000);
|
||||
|
||||
let key: string;
|
||||
@@ -136,9 +150,12 @@ export class TestWebhooks {
|
||||
timeout,
|
||||
workflowData,
|
||||
};
|
||||
await this.activeWebhooks!.add(webhookData, mode);
|
||||
await this.activeWebhooks!.add(workflow, webhookData, mode);
|
||||
}
|
||||
|
||||
// Save static data!
|
||||
await WorkflowHelpers.saveStaticData(workflow);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -150,7 +167,7 @@ export class TestWebhooks {
|
||||
* @returns {boolean}
|
||||
* @memberof TestWebhooks
|
||||
*/
|
||||
cancelTestWebhook(workflowId: string): boolean {
|
||||
cancelTestWebhook(workflowId: string, workflow: Workflow): boolean {
|
||||
let foundWebhook = false;
|
||||
for (const webhookKey of Object.keys(this.testWebhookData)) {
|
||||
const webhookData = this.testWebhookData[webhookKey];
|
||||
@@ -175,7 +192,7 @@ export class TestWebhooks {
|
||||
|
||||
// Remove the webhook
|
||||
delete this.testWebhookData[webhookKey];
|
||||
this.activeWebhooks!.removeByWorkflowId(workflowId);
|
||||
this.activeWebhooks!.removeWorkflow(workflow);
|
||||
}
|
||||
|
||||
return foundWebhook;
|
||||
@@ -189,8 +206,22 @@ export class TestWebhooks {
|
||||
if (this.activeWebhooks === null) {
|
||||
return;
|
||||
}
|
||||
const nodeTypes = NodeTypes();
|
||||
|
||||
return this.activeWebhooks.removeAll();
|
||||
const findQuery = {
|
||||
where: {
|
||||
id: findIn(this.activeWebhooks.getWorkflowIds())
|
||||
},
|
||||
} as FindManyOptions;
|
||||
|
||||
const workflowsDb = await Db.collections.Workflow!.find(findQuery);
|
||||
const workflows: Workflow[] = [];
|
||||
for (const workflowData of workflowsDb) {
|
||||
const workflow = new Workflow(workflowData.id.toString(), workflowData.nodes, workflowData.connections, workflowData.active, nodeTypes, workflowData.staticData, workflowData.settings);
|
||||
workflows.push(workflow);
|
||||
}
|
||||
|
||||
return this.activeWebhooks.removeAll(workflows);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user