mirror of
https://github.com/n8n-io/n8n.git
synced 2024-11-14 08:34:07 -08:00
removed changes for form completion and sendAndWait from waiting webhooks
This commit is contained in:
parent
13dc90ded4
commit
58a7391b7c
|
@ -1,15 +1,8 @@
|
|||
import axios from 'axios';
|
||||
import type express from 'express';
|
||||
import { FORM_NODE_TYPE, sleep, Workflow } from 'n8n-workflow';
|
||||
import { Service } from 'typedi';
|
||||
|
||||
import { ConflictError } from '@/errors/response-errors/conflict.error';
|
||||
import { NotFoundError } from '@/errors/response-errors/not-found.error';
|
||||
import type { IExecutionResponse } from '@/interfaces';
|
||||
import { WaitingWebhooks } from '@/webhooks/waiting-webhooks';
|
||||
|
||||
import type { IWebhookResponseCallbackData, WaitingWebhookRequest } from './webhook.types';
|
||||
|
||||
@Service()
|
||||
export class WaitingForms extends WaitingWebhooks {
|
||||
protected override includeForms = true;
|
||||
|
@ -23,123 +16,4 @@ export class WaitingForms extends WaitingWebhooks {
|
|||
execution.data.executionData!.nodeExecutionStack[0].node.disabled = true;
|
||||
}
|
||||
}
|
||||
|
||||
private getWorkflow(execution: IExecutionResponse) {
|
||||
const { workflowData } = execution;
|
||||
return 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,
|
||||
});
|
||||
}
|
||||
|
||||
private async reloadForm(req: WaitingWebhookRequest, res: express.Response) {
|
||||
try {
|
||||
await sleep(1000);
|
||||
|
||||
const url = `${req.protocol}://${req.get('host')}${req.originalUrl}`;
|
||||
const page = await axios({ url });
|
||||
|
||||
if (page) {
|
||||
res.send(`
|
||||
<script>
|
||||
setTimeout(function() {
|
||||
window.location.reload();
|
||||
}, 1);
|
||||
</script>
|
||||
`);
|
||||
}
|
||||
} catch (error) {}
|
||||
}
|
||||
|
||||
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.getExecution(executionId);
|
||||
|
||||
if (!execution) {
|
||||
throw new NotFoundError(`The execution "${executionId}" does not exist.`);
|
||||
}
|
||||
|
||||
if (execution.data.resultData.error) {
|
||||
const message = `The execution "${executionId}" has finished with error.`;
|
||||
this.logger.debug(message, { error: execution.data.resultData.error });
|
||||
throw new ConflictError(message);
|
||||
}
|
||||
|
||||
if (execution.status === 'running') {
|
||||
if (this.includeForms && req.method === 'GET') {
|
||||
await this.reloadForm(req, res);
|
||||
return { noWebhookResponse: true };
|
||||
}
|
||||
|
||||
throw new ConflictError(`The execution "${executionId}" is running already.`);
|
||||
}
|
||||
|
||||
let completionPage;
|
||||
if (execution.finished) {
|
||||
const workflow = this.getWorkflow(execution);
|
||||
|
||||
const parentNodes = workflow.getParentNodes(
|
||||
execution.data.resultData.lastNodeExecuted as string,
|
||||
);
|
||||
|
||||
const lastNodeExecuted = execution.data.resultData.lastNodeExecuted as string;
|
||||
const lastNode = workflow.nodes[lastNodeExecuted];
|
||||
|
||||
if (
|
||||
!lastNode.disabled &&
|
||||
lastNode.type === FORM_NODE_TYPE &&
|
||||
lastNode.parameters.operation === 'completion'
|
||||
) {
|
||||
completionPage = lastNodeExecuted;
|
||||
} else {
|
||||
completionPage = Object.keys(workflow.nodes).find((nodeName) => {
|
||||
const node = workflow.nodes[nodeName];
|
||||
return (
|
||||
parentNodes.includes(nodeName) &&
|
||||
!node.disabled &&
|
||||
node.type === FORM_NODE_TYPE &&
|
||||
node.parameters.operation === 'completion'
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
if (!completionPage) {
|
||||
res.render('form-trigger-completion', {
|
||||
title: 'Form Submitted',
|
||||
message: 'Your response has been recorded',
|
||||
formTitle: 'Form Submitted',
|
||||
});
|
||||
|
||||
return {
|
||||
noWebhookResponse: true,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
const targetNode = completionPage || (execution.data.resultData.lastNodeExecuted as string);
|
||||
|
||||
return await this.getWebhookExecutionData({
|
||||
execution,
|
||||
req,
|
||||
res,
|
||||
lastNodeExecuted: targetNode,
|
||||
executionId,
|
||||
suffix,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,13 +1,6 @@
|
|||
import axios from 'axios';
|
||||
import type express from 'express';
|
||||
import {
|
||||
FORM_NODE_TYPE,
|
||||
type INodes,
|
||||
type IWorkflowBase,
|
||||
NodeHelpers,
|
||||
SEND_AND_WAIT_OPERATION,
|
||||
WAIT_NODE_TYPE,
|
||||
Workflow,
|
||||
} from 'n8n-workflow';
|
||||
import { FORM_NODE_TYPE, NodeHelpers, sleep, Workflow } from 'n8n-workflow';
|
||||
import { Service } from 'typedi';
|
||||
|
||||
import { ExecutionRepository } from '@/databases/repositories/execution.repository';
|
||||
|
@ -36,7 +29,7 @@ export class WaitingWebhooks implements IWebhookManager {
|
|||
|
||||
constructor(
|
||||
protected readonly logger: Logger,
|
||||
protected readonly nodeTypes: NodeTypes,
|
||||
private readonly nodeTypes: NodeTypes,
|
||||
private readonly executionRepository: ExecutionRepository,
|
||||
) {}
|
||||
|
||||
|
@ -50,34 +43,23 @@ export class WaitingWebhooks implements IWebhookManager {
|
|||
execution.data.executionData!.nodeExecutionStack[0].node.disabled = true;
|
||||
}
|
||||
|
||||
private isSendAndWaitRequest(nodes: INodes, suffix: string | undefined) {
|
||||
return (
|
||||
suffix &&
|
||||
Object.keys(nodes).some(
|
||||
(node) =>
|
||||
nodes[node].id === suffix && nodes[node].parameters.operation === SEND_AND_WAIT_OPERATION,
|
||||
)
|
||||
);
|
||||
}
|
||||
private async reloadForm(req: WaitingWebhookRequest, res: express.Response) {
|
||||
try {
|
||||
await sleep(1000);
|
||||
|
||||
private createWorkflow(workflowData: IWorkflowBase) {
|
||||
return 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 url = `${req.protocol}://${req.get('host')}${req.originalUrl}`;
|
||||
const page = await axios({ url });
|
||||
|
||||
protected async getExecution(executionId: string) {
|
||||
return await this.executionRepository.findSingleExecution(executionId, {
|
||||
includeData: true,
|
||||
unflattenData: true,
|
||||
});
|
||||
if (page) {
|
||||
res.send(`
|
||||
<script>
|
||||
setTimeout(function() {
|
||||
window.location.reload();
|
||||
}, 1);
|
||||
</script>
|
||||
`);
|
||||
}
|
||||
} catch (error) {}
|
||||
}
|
||||
|
||||
async executeWebhook(
|
||||
|
@ -91,60 +73,96 @@ export class WaitingWebhooks implements IWebhookManager {
|
|||
// Reset request parameters
|
||||
req.params = {} as WaitingWebhookRequest['params'];
|
||||
|
||||
const execution = await this.getExecution(executionId);
|
||||
const execution = await this.executionRepository.findSingleExecution(executionId, {
|
||||
includeData: true,
|
||||
unflattenData: true,
|
||||
});
|
||||
|
||||
if (!execution) {
|
||||
throw new NotFoundError(`The execution "${executionId}" does not exist.`);
|
||||
throw new NotFoundError(`The execution "${executionId} does not exist.`);
|
||||
}
|
||||
|
||||
if (execution.status === 'running') {
|
||||
throw new ConflictError(`The execution "${executionId}" is running already.`);
|
||||
if (this.includeForms && req.method === 'GET') {
|
||||
await this.reloadForm(req, res);
|
||||
return { noWebhookResponse: true };
|
||||
}
|
||||
|
||||
throw new ConflictError(`The execution "${executionId} is running already.`);
|
||||
}
|
||||
|
||||
if (execution.data?.resultData?.error) {
|
||||
const message = `The execution "${executionId}" has finished with error.`;
|
||||
this.logger.debug(message, { error: execution.data.resultData.error });
|
||||
throw new ConflictError(message);
|
||||
}
|
||||
const lastNodeExecuted = execution.data.resultData.lastNodeExecuted as string;
|
||||
|
||||
if (execution.finished) {
|
||||
const { workflowData } = execution;
|
||||
const { nodes } = this.createWorkflow(workflowData);
|
||||
if (this.isSendAndWaitRequest(nodes, suffix)) {
|
||||
res.render('send-and-wait-no-action-required', { isTestWebhook: false });
|
||||
return { noWebhookResponse: true };
|
||||
if (this.includeForms && req.method === 'GET') {
|
||||
const executionWorkflowData = execution.workflowData;
|
||||
|
||||
const hasCompletionPage = executionWorkflowData.nodes.some((node) => {
|
||||
return (
|
||||
!node.disabled &&
|
||||
node.type === FORM_NODE_TYPE &&
|
||||
node.parameters.operation === 'completion'
|
||||
);
|
||||
});
|
||||
|
||||
if (!hasCompletionPage) {
|
||||
res.render('form-trigger-completion', {
|
||||
title: 'Form Submitted',
|
||||
message: 'Your response has been recorded',
|
||||
formTitle: 'Form Submitted',
|
||||
});
|
||||
return {
|
||||
noWebhookResponse: true,
|
||||
};
|
||||
}
|
||||
|
||||
// const workflow = this.getWorkflow(execution);
|
||||
|
||||
// const parentNodes = workflow.getParentNodes(
|
||||
// execution.data.resultData.lastNodeExecuted as string,
|
||||
// );
|
||||
|
||||
// const lastNodeExecuted = execution.data.resultData.lastNodeExecuted as string;
|
||||
// const lastNode = workflow.nodes[lastNodeExecuted];
|
||||
|
||||
// if (
|
||||
// !lastNode.disabled &&
|
||||
// lastNode.type === FORM_NODE_TYPE &&
|
||||
// lastNode.parameters.operation === 'completion'
|
||||
// ) {
|
||||
// completionPage = lastNodeExecuted;
|
||||
// } else {
|
||||
// completionPage = Object.keys(workflow.nodes).find((nodeName) => {
|
||||
// const node = workflow.nodes[nodeName];
|
||||
// return (
|
||||
// parentNodes.includes(nodeName) &&
|
||||
// !node.disabled &&
|
||||
// node.type === FORM_NODE_TYPE &&
|
||||
// node.parameters.operation === 'completion'
|
||||
// );
|
||||
// });
|
||||
// }
|
||||
|
||||
// if (!completionPage) {
|
||||
// res.render('form-trigger-completion', {
|
||||
// title: 'Form Submitted',
|
||||
// message: 'Your response has been recorded',
|
||||
// formTitle: 'Form Submitted',
|
||||
// });
|
||||
|
||||
// return {
|
||||
// noWebhookResponse: true,
|
||||
// };
|
||||
// }
|
||||
} else {
|
||||
throw new ConflictError(`The execution "${executionId} has finished already.`);
|
||||
}
|
||||
}
|
||||
|
||||
const lastNodeExecuted = execution.data.resultData.lastNodeExecuted as string;
|
||||
if (execution.data.resultData.error) {
|
||||
throw new ConflictError(`The execution "${executionId} has finished already.`);
|
||||
}
|
||||
|
||||
return await this.getWebhookExecutionData({
|
||||
execution,
|
||||
req,
|
||||
res,
|
||||
lastNodeExecuted,
|
||||
executionId,
|
||||
suffix,
|
||||
});
|
||||
}
|
||||
|
||||
protected async getWebhookExecutionData({
|
||||
execution,
|
||||
req,
|
||||
res,
|
||||
lastNodeExecuted,
|
||||
executionId,
|
||||
suffix,
|
||||
}: {
|
||||
execution: IExecutionResponse;
|
||||
req: WaitingWebhookRequest;
|
||||
res: express.Response;
|
||||
lastNodeExecuted: string;
|
||||
executionId: string;
|
||||
suffix?: string;
|
||||
}): Promise<IWebhookResponseCallbackData> {
|
||||
// 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);
|
||||
|
@ -156,7 +174,17 @@ export class WaitingWebhooks implements IWebhookManager {
|
|||
execution.data.resultData.runData[lastNodeExecuted].pop();
|
||||
|
||||
const { workflowData } = execution;
|
||||
const workflow = this.createWorkflow(workflowData);
|
||||
|
||||
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) {
|
||||
|
@ -180,28 +208,6 @@ export class WaitingWebhooks implements IWebhookManager {
|
|||
// 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.`;
|
||||
|
||||
if (this.isSendAndWaitRequest(workflow.nodes, suffix)) {
|
||||
res.render('send-and-wait-no-action-required', { isTestWebhook: false });
|
||||
return { noWebhookResponse: true };
|
||||
}
|
||||
|
||||
if (!execution.data.resultData.error && execution.status === 'waiting') {
|
||||
const childNodes = workflow.getChildNodes(
|
||||
execution.data.resultData.lastNodeExecuted as string,
|
||||
);
|
||||
|
||||
const hasChildForms = childNodes.some(
|
||||
(node) =>
|
||||
workflow.nodes[node].type === FORM_NODE_TYPE ||
|
||||
workflow.nodes[node].type === WAIT_NODE_TYPE,
|
||||
);
|
||||
|
||||
if (hasChildForms) {
|
||||
return { noWebhookResponse: true };
|
||||
}
|
||||
}
|
||||
|
||||
throw new NotFoundError(errorMessage);
|
||||
}
|
||||
|
||||
|
|
|
@ -34,11 +34,8 @@ import {
|
|||
BINARY_ENCODING,
|
||||
createDeferredPromise,
|
||||
ErrorReporterProxy as ErrorReporter,
|
||||
ErrorReporterProxy,
|
||||
ExecutionCancelledError,
|
||||
FORM_NODE_TYPE,
|
||||
NodeHelpers,
|
||||
NodeOperationError,
|
||||
} from 'n8n-workflow';
|
||||
import { finished } from 'stream/promises';
|
||||
import { Container } from 'typedi';
|
||||
|
@ -124,7 +121,7 @@ export async function executeWebhook(
|
|||
);
|
||||
if (nodeType === undefined) {
|
||||
const errorMessage = `The type of the webhook node "${workflowStartNode.name}" is not known`;
|
||||
responseCallback(new ApplicationError(errorMessage), {});
|
||||
responseCallback(new Error(errorMessage), {});
|
||||
throw new InternalServerError(errorMessage);
|
||||
}
|
||||
|
||||
|
@ -201,7 +198,7 @@ export async function executeWebhook(
|
|||
// the default that people know as early as possible (probably already testing phase)
|
||||
// that something does not resolve properly.
|
||||
const errorMessage = `The response mode '${responseMode}' is not valid!`;
|
||||
responseCallback(new ApplicationError(errorMessage), {});
|
||||
responseCallback(new Error(errorMessage), {});
|
||||
throw new InternalServerError(errorMessage);
|
||||
}
|
||||
|
||||
|
@ -269,26 +266,8 @@ export async function executeWebhook(
|
|||
});
|
||||
} catch (err) {
|
||||
// Send error response to webhook caller
|
||||
const webhookType = ['formTrigger', 'form'].includes(nodeType.description.name)
|
||||
? 'Form'
|
||||
: 'Webhook';
|
||||
let errorMessage = `Workflow ${webhookType} Error: Workflow could not be started!`;
|
||||
|
||||
// if workflow started manually, show an actual error message
|
||||
if (err instanceof NodeOperationError && err.type === 'manual-form-test') {
|
||||
errorMessage = err.message;
|
||||
}
|
||||
|
||||
ErrorReporterProxy.error(err, {
|
||||
extra: {
|
||||
nodeName: workflowStartNode.name,
|
||||
nodeType: workflowStartNode.type,
|
||||
nodeVersion: workflowStartNode.typeVersion,
|
||||
workflowId: workflow.id,
|
||||
},
|
||||
});
|
||||
|
||||
responseCallback(new ApplicationError(errorMessage), {});
|
||||
const errorMessage = 'Workflow Webhook Error: Workflow could not be started!';
|
||||
responseCallback(new Error(errorMessage), {});
|
||||
didSendResponse = true;
|
||||
|
||||
// Add error to execution data that it can be logged and send to Editor-UI
|
||||
|
@ -613,7 +592,7 @@ export async function executeWebhook(
|
|||
// Return the JSON data of the first entry
|
||||
|
||||
if (returnData.data!.main[0]![0] === undefined) {
|
||||
responseCallback(new ApplicationError('No item to return got found'), {});
|
||||
responseCallback(new Error('No item to return got found'), {});
|
||||
didSendResponse = true;
|
||||
return undefined;
|
||||
}
|
||||
|
@ -667,13 +646,13 @@ export async function executeWebhook(
|
|||
data = returnData.data!.main[0]![0];
|
||||
|
||||
if (data === undefined) {
|
||||
responseCallback(new ApplicationError('No item was found to return'), {});
|
||||
responseCallback(new Error('No item was found to return'), {});
|
||||
didSendResponse = true;
|
||||
return undefined;
|
||||
}
|
||||
|
||||
if (data.binary === undefined) {
|
||||
responseCallback(new ApplicationError('No binary data was found to return'), {});
|
||||
responseCallback(new Error('No binary data was found to return'), {});
|
||||
didSendResponse = true;
|
||||
return undefined;
|
||||
}
|
||||
|
@ -688,10 +667,7 @@ export async function executeWebhook(
|
|||
);
|
||||
|
||||
if (responseBinaryPropertyName === undefined && !didSendResponse) {
|
||||
responseCallback(
|
||||
new ApplicationError("No 'responseBinaryPropertyName' is set"),
|
||||
{},
|
||||
);
|
||||
responseCallback(new Error("No 'responseBinaryPropertyName' is set"), {});
|
||||
didSendResponse = true;
|
||||
}
|
||||
|
||||
|
@ -700,7 +676,7 @@ export async function executeWebhook(
|
|||
];
|
||||
if (binaryData === undefined && !didSendResponse) {
|
||||
responseCallback(
|
||||
new ApplicationError(
|
||||
new Error(
|
||||
`The binary property '${responseBinaryPropertyName}' which should be returned does not exist`,
|
||||
),
|
||||
{},
|
||||
|
@ -757,9 +733,7 @@ export async function executeWebhook(
|
|||
);
|
||||
}
|
||||
|
||||
const internalServerError = new InternalServerError(e.message);
|
||||
if (e instanceof ExecutionCancelledError) internalServerError.level = 'warning';
|
||||
throw internalServerError;
|
||||
throw new InternalServerError(e.message);
|
||||
});
|
||||
}
|
||||
return executionId;
|
||||
|
|
Loading…
Reference in a new issue