feat: Adjust webhook handler to work with run method

This commit is contained in:
Faruk AYDIN
2023-10-08 15:51:59 +02:00
parent 27a3edeb93
commit 4c66cc1e33
2 changed files with 56 additions and 45 deletions

View File

@@ -102,7 +102,9 @@ const globalVariable = async (
$.triggerOutput.data.push(triggerItem); $.triggerOutput.data.push(triggerItem);
if ($.execution.testRun) { const isWebhookApp = app.key === 'webhook';
if ($.execution.testRun && !isWebhookApp) {
// early exit after receiving one item as it is enough for test execution // early exit after receiving one item as it is enough for test execution
throw new EarlyExitError(); throw new EarlyExitError();
} }
@@ -145,7 +147,9 @@ const globalVariable = async (
} }
const lastInternalIds = const lastInternalIds =
testRun || (flow && step?.isAction) ? [] : await flow?.lastInternalIds(2000); testRun || (flow && step?.isAction)
? []
: await flow?.lastInternalIds(2000);
const isAlreadyProcessed = (internalId: string) => { const isAlreadyProcessed = (internalId: string) => {
return lastInternalIds?.includes(internalId); return lastInternalIds?.includes(internalId);

View File

@@ -1,6 +1,6 @@
import Crypto from 'node:crypto';
import { Response } from 'express'; import { Response } from 'express';
import { IRequest, ITriggerItem } from '@automatisch/types'; import { IRequest } from '@automatisch/types';
import isEmpty from 'lodash/isEmpty';
import Flow from '../models/flow'; import Flow from '../models/flow';
import { processTrigger } from '../services/trigger'; import { processTrigger } from '../services/trigger';
@@ -12,18 +12,12 @@ import {
REMOVE_AFTER_7_DAYS_OR_50_JOBS, REMOVE_AFTER_7_DAYS_OR_50_JOBS,
} from './remove-job-configuration'; } from './remove-job-configuration';
export default async (flowId: string, request: IRequest, response: Response) => { export default async (
// in case it's our built-in generic webhook trigger flowId: string,
let computedRequestPayload = { request: IRequest,
headers: request.headers, response: Response
body: request.body, ) => {
query: request.query, const flow = await Flow.query().findById(flowId).throwIfNotFound();
};
const flow = await Flow.query()
.findById(flowId)
.throwIfNotFound();
const user = await flow.$relatedQuery('user'); const user = await flow.$relatedQuery('user');
const testRun = !flow.active; const testRun = !flow.active;
@@ -37,22 +31,34 @@ export default async (flowId: string, request: IRequest, response: Response) =>
const app = await triggerStep.getApp(); const app = await triggerStep.getApp();
const isWebhookApp = app.key === 'webhook'; const isWebhookApp = app.key === 'webhook';
if ((testRun && !isWebhookApp)) { if (testRun && !isWebhookApp) {
return response.status(404); return response.status(404);
} }
// in case trigger type is 'webhook' const connection = await triggerStep.$relatedQuery('connection');
if (!isWebhookApp) {
computedRequestPayload = request.body; const $ = await globalVariable({
flow,
connection,
app,
step: triggerStep,
testRun,
request,
});
const triggerCommand = await triggerStep.getTriggerCommand();
await triggerCommand.run($);
const reversedTriggerItems = $.triggerOutput.data.reverse();
// This is the case when we filter out the incoming data
// in the run method of the webhook trigger.
// In this case, we don't want to process anything.
if (isEmpty(reversedTriggerItems)) {
return response.status(204);
} }
const triggerItem: ITriggerItem = { for (const triggerItem of reversedTriggerItems) {
raw: computedRequestPayload,
meta: {
internalId: Crypto.randomUUID(),
},
};
const { executionId } = await processTrigger({ const { executionId } = await processTrigger({
flowId, flowId,
stepId: triggerStep.id, stepId: triggerStep.id,
@@ -61,7 +67,7 @@ export default async (flowId: string, request: IRequest, response: Response) =>
}); });
if (testRun) { if (testRun) {
return response.status(204); continue;
} }
const nextStep = await triggerStep.getNextStep(); const nextStep = await triggerStep.getNextStep();
@@ -79,6 +85,7 @@ export default async (flowId: string, request: IRequest, response: Response) =>
}; };
await actionQueue.add(jobName, jobPayload, jobOptions); await actionQueue.add(jobName, jobPayload, jobOptions);
}
return response.status(204); return response.status(204);
}; };