mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-17 01:56:46 +00:00
refactor(core): Standardize filenames in cli (no-changelog) (#10484)
Co-authored-by: कारतोफ्फेलस्क्रिप्ट™ <aditya@netroy.in>
This commit is contained in:
147
packages/cli/src/webhooks/waiting-webhooks.ts
Normal file
147
packages/cli/src/webhooks/waiting-webhooks.ts
Normal file
@@ -0,0 +1,147 @@
|
||||
import { NodeHelpers, Workflow } from 'n8n-workflow';
|
||||
import { Service } from 'typedi';
|
||||
import type express from 'express';
|
||||
|
||||
import * as WebhookHelpers from '@/webhooks/webhook-helpers';
|
||||
import { NodeTypes } from '@/node-types';
|
||||
import type {
|
||||
IWebhookResponseCallbackData,
|
||||
IWebhookManager,
|
||||
WaitingWebhookRequest,
|
||||
} from './webhook.types';
|
||||
import * as WorkflowExecuteAdditionalData from '@/workflow-execute-additional-data';
|
||||
import { ExecutionRepository } from '@db/repositories/execution.repository';
|
||||
import { Logger } from '@/logger';
|
||||
import { ConflictError } from '@/errors/response-errors/conflict.error';
|
||||
import { NotFoundError } from '@/errors/response-errors/not-found.error';
|
||||
import type { IExecutionResponse, IWorkflowDb } from '@/Interfaces';
|
||||
|
||||
/**
|
||||
* Service for handling the execution of webhooks of Wait nodes that use the
|
||||
* [Resume On Webhook Call](https://docs.n8n.io/integrations/builtin/core-nodes/n8n-nodes-base.wait/#on-webhook-call)
|
||||
* feature.
|
||||
*/
|
||||
@Service()
|
||||
export class WaitingWebhooks implements IWebhookManager {
|
||||
protected includeForms = false;
|
||||
|
||||
constructor(
|
||||
protected readonly logger: Logger,
|
||||
private readonly nodeTypes: NodeTypes,
|
||||
private readonly executionRepository: ExecutionRepository,
|
||||
) {}
|
||||
|
||||
// TODO: implement `getWebhookMethods` for CORS support
|
||||
|
||||
protected logReceivedWebhook(method: string, executionId: string) {
|
||||
this.logger.debug(`Received waiting-webhook "${method}" for execution "${executionId}"`);
|
||||
}
|
||||
|
||||
protected disableNode(execution: IExecutionResponse, _method?: string) {
|
||||
execution.data.executionData!.nodeExecutionStack[0].node.disabled = true;
|
||||
}
|
||||
|
||||
async executeWebhook(
|
||||
req: WaitingWebhookRequest,
|
||||
res: express.Response,
|
||||
): Promise<IWebhookResponseCallbackData> {
|
||||
const { path: executionId, suffix } = req.params;
|
||||
|
||||
this.logReceivedWebhook(req.method, executionId);
|
||||
|
||||
// Reset request parameters
|
||||
req.params = {} as WaitingWebhookRequest['params'];
|
||||
|
||||
const execution = await this.executionRepository.findSingleExecution(executionId, {
|
||||
includeData: true,
|
||||
unflattenData: true,
|
||||
});
|
||||
|
||||
if (!execution) {
|
||||
throw new NotFoundError(`The execution "${executionId} does not exist.`);
|
||||
}
|
||||
|
||||
if (execution.status === 'running') {
|
||||
throw new ConflictError(`The execution "${executionId} is running already.`);
|
||||
}
|
||||
|
||||
if (execution.finished || execution.data.resultData.error) {
|
||||
throw new ConflictError(`The execution "${executionId} has finished already.`);
|
||||
}
|
||||
|
||||
const lastNodeExecuted = execution.data.resultData.lastNodeExecuted as string;
|
||||
|
||||
// Set the node as disabled so that the data does not get executed again as it would result
|
||||
// in starting the wait all over again
|
||||
this.disableNode(execution, req.method);
|
||||
|
||||
// Remove waitTill information else the execution would stop
|
||||
execution.data.waitTill = undefined;
|
||||
|
||||
// Remove the data of the node execution again else it will display the node as executed twice
|
||||
execution.data.resultData.runData[lastNodeExecuted].pop();
|
||||
|
||||
const { workflowData } = execution;
|
||||
|
||||
const workflow = new Workflow({
|
||||
id: workflowData.id,
|
||||
name: workflowData.name,
|
||||
nodes: workflowData.nodes,
|
||||
connections: workflowData.connections,
|
||||
active: workflowData.active,
|
||||
nodeTypes: this.nodeTypes,
|
||||
staticData: workflowData.staticData,
|
||||
settings: workflowData.settings,
|
||||
});
|
||||
|
||||
const workflowStartNode = workflow.getNode(lastNodeExecuted);
|
||||
if (workflowStartNode === null) {
|
||||
throw new NotFoundError('Could not find node to process webhook.');
|
||||
}
|
||||
|
||||
const additionalData = await WorkflowExecuteAdditionalData.getBase();
|
||||
const webhookData = NodeHelpers.getNodeWebhooks(
|
||||
workflow,
|
||||
workflowStartNode,
|
||||
additionalData,
|
||||
).find(
|
||||
(webhook) =>
|
||||
webhook.httpMethod === req.method &&
|
||||
webhook.path === (suffix ?? '') &&
|
||||
webhook.webhookDescription.restartWebhook === true &&
|
||||
(webhook.webhookDescription.isForm || false) === this.includeForms,
|
||||
);
|
||||
|
||||
if (webhookData === undefined) {
|
||||
// If no data got found it means that the execution can not be started via a webhook.
|
||||
// Return 404 because we do not want to give any data if the execution exists or not.
|
||||
const errorMessage = `The workflow for execution "${executionId}" does not contain a waiting webhook with a matching path/method.`;
|
||||
throw new NotFoundError(errorMessage);
|
||||
}
|
||||
|
||||
const runExecutionData = execution.data;
|
||||
|
||||
return await new Promise((resolve, reject) => {
|
||||
const executionMode = 'webhook';
|
||||
void WebhookHelpers.executeWebhook(
|
||||
workflow,
|
||||
webhookData,
|
||||
workflowData as IWorkflowDb,
|
||||
workflowStartNode,
|
||||
executionMode,
|
||||
undefined,
|
||||
runExecutionData,
|
||||
execution.id,
|
||||
req,
|
||||
res,
|
||||
|
||||
(error: Error | null, data: object) => {
|
||||
if (error !== null) {
|
||||
return reject(error);
|
||||
}
|
||||
resolve(data);
|
||||
},
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user