701 workflow improve webhook triggers (#11455)
as title Nota bene: I did not filter execution by http method. A POST webhook trigger can be triggered by a GET request for more flexibility. Tell me if you think it is a mistake https://github.com/user-attachments/assets/1833cbea-51a8-4772-bcd8-088d6a087e79
This commit is contained in:
@ -1,6 +1,7 @@
|
||||
import { Controller, Get, Param, UseFilters } from '@nestjs/common';
|
||||
import { Controller, Get, Param, Post, Req, UseFilters } from '@nestjs/common';
|
||||
|
||||
import { isDefined } from 'twenty-shared/utils';
|
||||
import { Request } from 'express';
|
||||
|
||||
import { WorkflowTriggerWorkspaceService } from 'src/modules/workflow/workflow-trigger/workspace-services/workflow-trigger.workspace-service';
|
||||
import { TwentyORMManager } from 'src/engine/twenty-orm/twenty-orm.manager';
|
||||
@ -22,11 +23,26 @@ export class WorkflowTriggerController {
|
||||
private readonly workflowTriggerWorkspaceService: WorkflowTriggerWorkspaceService,
|
||||
) {}
|
||||
|
||||
@Get('workflows/:workspaceId/:workflowId')
|
||||
async runWorkflow(
|
||||
@Param('workspaceId') workspaceId: string,
|
||||
@Post('workflows/:workspaceId/:workflowId')
|
||||
async runWorkflowByPostRequest(
|
||||
@Param('workflowId') workflowId: string,
|
||||
@Req() request: Request,
|
||||
) {
|
||||
return await this.runWorkflow({ workflowId, payload: request.body || {} });
|
||||
}
|
||||
|
||||
@Get('workflows/:workspaceId/:workflowId')
|
||||
async runWorkflowByGetRequest(@Param('workflowId') workflowId: string) {
|
||||
return await this.runWorkflow({ workflowId });
|
||||
}
|
||||
|
||||
private async runWorkflow({
|
||||
workflowId,
|
||||
payload,
|
||||
}: {
|
||||
workflowId: string;
|
||||
payload?: object;
|
||||
}) {
|
||||
const workflowRepository =
|
||||
await this.twentyORMManager.getRepository<WorkflowWorkspaceEntity>(
|
||||
'workflow',
|
||||
@ -78,7 +94,7 @@ export class WorkflowTriggerController {
|
||||
const { workflowRunId } =
|
||||
await this.workflowTriggerWorkspaceService.runWorkflowVersion({
|
||||
workflowVersionId: workflow.lastPublishedVersionId,
|
||||
payload: {},
|
||||
payload: payload || {},
|
||||
createdBy: {
|
||||
source: FieldActorSource.WEBHOOK,
|
||||
workspaceMemberId: null,
|
||||
|
||||
@ -61,6 +61,16 @@ export type WorkflowCronTrigger = BaseTrigger & {
|
||||
|
||||
export type WorkflowWebhookTrigger = BaseTrigger & {
|
||||
type: WorkflowTriggerType.WEBHOOK;
|
||||
settings:
|
||||
| {
|
||||
httpMethod: 'GET';
|
||||
authentication: 'API_KEY' | null;
|
||||
}
|
||||
| ({
|
||||
httpMethod: 'POST';
|
||||
authentication: 'API_KEY' | null;
|
||||
expectedBody: object;
|
||||
} & { outputSchema: object });
|
||||
};
|
||||
|
||||
export type WorkflowManualTriggerSettings = WorkflowManualTrigger['settings'];
|
||||
|
||||
Reference in New Issue
Block a user