fix: Fetch webhooks by flow id

This commit is contained in:
Faruk AYDIN
2023-07-12 14:05:46 +02:00
parent de7a35dfe9
commit 04f4693c85

View File

@@ -1,8 +1,7 @@
import path from 'node:path';
import { Response } from 'express';
import { IRequest } from '@automatisch/types';
import Step from '../../models/step';
import Flow from '../../models/flow';
import logger from '../../helpers/logger';
import handler from '../../helpers/webhook-handler';
@@ -13,20 +12,21 @@ export default async (request: IRequest, response: Response) => {
query: request.query,
params: request.params,
};
logger.debug(`Handling incoming webhook request at ${request.originalUrl}.`);
logger.debug(JSON.stringify(computedRequestPayload, null, 2));
const flowId = request.params.flowId;
const triggerStep = await Step.query()
.findOne({
webhook_path: path.join(request.baseUrl, request.path),
})
.throwIfNotFound();
const flow = await Flow.query().findById(flowId).throwIfNotFound();
const triggerStep = await flow.getTriggerStep();
if (triggerStep.appKey !== 'webhook') {
const connection = await triggerStep.$relatedQuery('connection');
if (!await connection.verifyWebhook(request)) {
if (!(await connection.verifyWebhook(request))) {
return response.sendStatus(401);
}
}
await handler(flowId, request, response);