From d57da557a03d88ba9ee554b869dcacb47ff0b1be Mon Sep 17 00:00:00 2001
From: Michael Kret <88898367+michael-radency@users.noreply.github.com>
Date: Fri, 25 Oct 2024 13:49:36 +0300
Subject: [PATCH] fix(n8n Form Node): Form Trigger does not wait in multi-form
mode (#11404)
---
packages/cli/src/webhooks/waiting-forms.ts | 44 +++++++++++--------
packages/cli/src/webhooks/waiting-webhooks.ts | 16 ++++---
.../cli/templates/form-trigger.handlebars | 3 ++
3 files changed, 38 insertions(+), 25 deletions(-)
diff --git a/packages/cli/src/webhooks/waiting-forms.ts b/packages/cli/src/webhooks/waiting-forms.ts
index 72963f21dadf9..5a491c1fb3bf8 100644
--- a/packages/cli/src/webhooks/waiting-forms.ts
+++ b/packages/cli/src/webhooks/waiting-forms.ts
@@ -38,6 +38,25 @@ export class WaitingForms extends WaitingWebhooks {
});
}
+ 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(`
+
+ `);
+ }
+ } catch (error) {}
+ }
+
async executeWebhook(
req: WaitingWebhookRequest,
res: express.Response,
@@ -56,30 +75,17 @@ export class WaitingForms extends WaitingWebhooks {
}
if (execution.data.resultData.error) {
- throw new ConflictError(`The execution "${executionId}" has finished with 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 sleep(1000);
-
- const url = `${req.protocol}://${req.get('host')}${req.originalUrl}`;
- const page = await axios({ url });
-
- if (page) {
- res.send(`
-
- `);
- }
-
- return {
- noWebhookResponse: true,
- };
+ await this.reloadForm(req, res);
+ return { noWebhookResponse: true };
}
+
throw new ConflictError(`The execution "${executionId}" is running already.`);
}
diff --git a/packages/cli/src/webhooks/waiting-webhooks.ts b/packages/cli/src/webhooks/waiting-webhooks.ts
index a14563eea2e58..9529d04c04a97 100644
--- a/packages/cli/src/webhooks/waiting-webhooks.ts
+++ b/packages/cli/src/webhooks/waiting-webhooks.ts
@@ -102,7 +102,9 @@ export class WaitingWebhooks implements IWebhookManager {
}
if (execution.data?.resultData?.error) {
- throw new ConflictError(`The execution "${executionId} has finished already.`);
+ const message = `The execution "${executionId}" has finished with error.`;
+ this.logger.debug(message, { error: execution.data.resultData.error });
+ throw new ConflictError(message);
}
if (execution.finished) {
@@ -182,23 +184,25 @@ export class WaitingWebhooks implements IWebhookManager {
if (this.isSendAndWaitRequest(workflow.nodes, suffix)) {
res.render('send-and-wait-no-action-required', { isTestWebhook: false });
return { noWebhookResponse: true };
- } else if (!execution.data.resultData.error && execution.status === 'waiting') {
+ }
+
+ 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 };
- } else {
- throw new NotFoundError(errorMessage);
}
- } else {
- throw new NotFoundError(errorMessage);
}
+
+ throw new NotFoundError(errorMessage);
}
const runExecutionData = execution.data;
diff --git a/packages/cli/templates/form-trigger.handlebars b/packages/cli/templates/form-trigger.handlebars
index 39627c0204f29..08f6d2e290cf5 100644
--- a/packages/cli/templates/form-trigger.handlebars
+++ b/packages/cli/templates/form-trigger.handlebars
@@ -752,6 +752,8 @@
console.error('Error:', error);
});
+ const isWaitingForm = window.location.href.includes('form-waiting');
+ if(isWaitingForm) {
const interval = setInterval(function() {
const isSubmited = document.querySelector('#submitted-form').style.display;
if(isSubmited === 'block') {
@@ -760,6 +762,7 @@
}
window.location.reload();
}, 2000);
+ }
}
});