1
9import { Body, Controller, Delete, Get, HttpCode, Inject, Param, Patch, Post, Put, Query, Req } from "@nestjs/common";
10import type { Request } from "express";
11import { AutomationsService } from "./automations.service.js";
12import { ZodValidate } from "../../common/pipes/zod-validation.pipe.js";
13import { requireActor } from "../../common/types.js";
14import {
15 CreateAutomationDto,
16 type CreateAutomationInput,
17 ListRunsQuery,
18 type ListRunsQueryInput,
19 PutActionsDto,
20 type PutActionsInput,
21 PutConditionsDto,
22 type PutConditionsInput,
23 PutTriggersDto,
24 type PutTriggersInput,
25 RunInputDto,
26 type RunInputInput,
27 SetStatusDto,
28 type SetStatusInput,
29 UpdateAutomationDto,
30 type UpdateAutomationInput
31} from "./dto.js";
32
33@Controller()
34export class AutomationsController {
35 constructor(@Inject(AutomationsService) private readonly automations: AutomationsService) {}
36
37
38
39 @Get("workspaces/:workspaceId/automations")
40 list(
41 @Param("workspaceId") workspaceId: string,
42 @Query() query: { status?: string; cursor?: string; limit?: number },
43 @Req() req: Request
44 ) {
45 return this.automations.list(workspaceId, requireActor(req), {
46 status: query.status,
47 cursor: query.cursor,
48 limit: query.limit ? Number(query.limit) : undefined
49 });
50 }
51
52 @Post("workspaces/:workspaceId/automations")
53 create(
54 @Param("workspaceId") workspaceId: string,
55 @Body(ZodValidate(CreateAutomationDto)) body: CreateAutomationInput,
56 @Req() req: Request
57 ) {
58 return this.automations.create(workspaceId, requireActor(req), body, req);
59 }
60
61
62
63 @Get("automations/:id")
64 get(@Param("id") id: string, @Req() req: Request) {
65 return this.automations.get(id, requireActor(req));
66 }
67
68 @Patch("automations/:id")
69 update(
70 @Param("id") id: string,
71 @Body(ZodValidate(UpdateAutomationDto)) body: UpdateAutomationInput,
72 @Req() req: Request
73 ) {
74 return this.automations.update(id, requireActor(req), body, req);
75 }
76
77
78 @Delete("automations/:id")
79 remove(@Param("id") id: string, @Req() req: Request) {
80 return this.automations.remove(id, requireActor(req), req).then(() => ({ archived: true }));
81 }
82
83
84 @Post("automations/:id/status")
85 @HttpCode(200)
86 status(
87 @Param("id") id: string,
88 @Body(ZodValidate(SetStatusDto)) body: SetStatusInput,
89 @Req() req: Request
90 ) {
91 return this.automations.setStatus(id, requireActor(req), body.status, req);
92 }
93
94
95
96
97 @Put("automations/:id/triggers")
98 putTriggers(
99 @Param("id") id: string,
100 @Body(ZodValidate(PutTriggersDto)) body: PutTriggersInput,
101 @Req() req: Request
102 ) {
103 return this.automations.putTriggers(id, requireActor(req), body, req);
104 }
105
106
107 @Put("automations/:id/conditions")
108 putConditions(
109 @Param("id") id: string,
110 @Body(ZodValidate(PutConditionsDto)) body: PutConditionsInput,
111 @Req() req: Request
112 ) {
113 return this.automations.putConditions(id, requireActor(req), body, req);
114 }
115
116
117 @Put("automations/:id/actions")
118 putActions(
119 @Param("id") id: string,
120 @Body(ZodValidate(PutActionsDto)) body: PutActionsInput,
121 @Req() req: Request
122 ) {
123 return this.automations.putActions(id, requireActor(req), body, req);
124 }
125
126
127 @Post("automations/:id/webhook/regenerate")
128 @HttpCode(200)
129 regenerateWebhook(@Param("id") id: string, @Req() req: Request) {
130 return this.automations.regenerateWebhookSecret(id, requireActor(req), req);
131 }
132
133
134
135
136 @Post("automations/:id/run")
137 @HttpCode(202)
138 run(
139 @Param("id") id: string,
140 @Body(ZodValidate(RunInputDto)) body: RunInputInput,
141 @Req() req: Request
142 ) {
143 return this.automations.manualRun(id, requireActor(req), body, req);
144 }
145
146 @Get("automations/:id/runs")
147 listRuns(
148 @Param("id") id: string,
149 @Query(ZodValidate(ListRunsQuery)) query: ListRunsQueryInput,
150 @Req() req: Request
151 ) {
152 return this.automations.listRuns(id, requireActor(req), query);
153 }
154
155 @Get("automations/:id/runs/:runId")
156 getRun(@Param("id") id: string, @Param("runId") runId: string, @Req() req: Request) {
157 return this.automations.getRun(id, runId, requireActor(req));
158 }
159
160
161 @Post("automations/:id/runs/:runId/retry")
162 @HttpCode(200)
163 retryRun(@Param("id") id: string, @Param("runId") runId: string, @Req() req: Request) {
164 return this.automations.retryRun(id, runId, requireActor(req), req);
165 }
166}
167