tangled
alpha
login
or
join now
openstatus.dev
/
openstatus
6
fork
atom
Openstatus
www.openstatus.dev
6
fork
atom
overview
issues
pulls
pipelines
🔥 (#609)
authored by
Thibault Le Ouay
and committed by
GitHub
2 years ago
a0c5b48d
0da38c3e
+105
-3
3 changed files
expand all
collapse all
unified
split
apps
docs
api-reference
incident
put-incident:id.mdx
server
src
v1
incidents.test.ts
incidents.ts
+4
apps/docs/api-reference/incident/put-incident:id.mdx
reviewed
···
1
1
+
---
2
2
+
title: Update an incident
3
3
+
openapi: put /incident/:id
4
4
+
---
+24
apps/server/src/v1/incidents.test.ts
reviewed
···
15
15
monitorId: 1,
16
16
});
17
17
});
18
18
+
19
19
+
test("Update an incident ", async () => {
20
20
+
const res = await api.request("/incident/1", {
21
21
+
method: "PUT",
22
22
+
headers: {
23
23
+
"x-openstatus-key": "1",
24
24
+
"content-type": "application/json",
25
25
+
},
26
26
+
body: JSON.stringify({
27
27
+
acknowledgedAt: "2023-11-08T21:03:13.000Z",
28
28
+
}),
29
29
+
});
30
30
+
expect(res.status).toBe(200);
31
31
+
32
32
+
expect(await res.json()).toMatchObject({
33
33
+
acknowledgedAt: "2023-11-08T21:03:13.000Z",
34
34
+
monitorId: 1,
35
35
+
id: 1,
36
36
+
startedAt: expect.any(String),
37
37
+
resolvedAt: null,
38
38
+
resolvedBy: null,
39
39
+
acknowledgedBy: null,
40
40
+
});
41
41
+
});
+77
-3
apps/server/src/v1/incidents.ts
reviewed
···
28
28
example: 1,
29
29
}),
30
30
startedAt: z
31
31
-
.preprocess((val) => String(val), z.string())
31
31
+
.preprocess((val) => new Date(String(val)).toISOString(), z.string())
32
32
.openapi({
33
33
description: "The date the incident started",
34
34
}),
···
42
42
.nullable(),
43
43
44
44
acknowledgedAt: z
45
45
-
.preprocess((val) => String(val), z.string())
45
45
+
.preprocess((val) => new Date(String(val)).toISOString(), z.string())
46
46
.openapi({
47
47
description: "The date the incident was acknowledged",
48
48
})
···
60
60
.openapi({
61
61
description: "The date the incident was resolved",
62
62
})
63
63
-
.optional(),
63
63
+
.optional()
64
64
+
.nullable(),
64
65
resolvedBy: z
65
66
.number()
66
67
.openapi({
···
155
156
156
157
if (!result) return c.jsonT({ code: 404, message: "Not Found" });
157
158
const data = IncidentSchema.parse(result);
159
159
+
160
160
+
return c.jsonT(data);
161
161
+
});
162
162
+
163
163
+
const incidentInputSchema = z
164
164
+
.object({
165
165
+
acknowledgedAt: z.coerce.date().optional(),
166
166
+
resolvedAt: z.coerce.date().optional(),
167
167
+
})
168
168
+
.openapi({ description: "The incident to create" });
169
169
+
const putRoute = createRoute({
170
170
+
method: "put",
171
171
+
tags: ["incident"],
172
172
+
description: "Update an incident",
173
173
+
path: "/:id",
174
174
+
request: {
175
175
+
params: ParamsSchema,
176
176
+
body: {
177
177
+
description: "The incident to update",
178
178
+
content: {
179
179
+
"application/json": {
180
180
+
schema: incidentInputSchema,
181
181
+
},
182
182
+
},
183
183
+
},
184
184
+
},
185
185
+
responses: {
186
186
+
200: {
187
187
+
content: {
188
188
+
"application/json": {
189
189
+
schema: IncidentSchema,
190
190
+
},
191
191
+
},
192
192
+
description: "Update a monitor",
193
193
+
},
194
194
+
400: {
195
195
+
content: {
196
196
+
"application/json": {
197
197
+
schema: ErrorSchema,
198
198
+
},
199
199
+
},
200
200
+
description: "Returns an error",
201
201
+
},
202
202
+
},
203
203
+
});
204
204
+
205
205
+
incidentsApi.openapi(putRoute, async (c) => {
206
206
+
const input = c.req.valid("json");
207
207
+
const workspaceId = Number(c.get("workspaceId"));
208
208
+
const { id } = c.req.valid("param");
209
209
+
if (!id) return c.jsonT({ code: 400, message: "Bad Request" });
210
210
+
211
211
+
const _incident = await db
212
212
+
.select()
213
213
+
.from(incidentTable)
214
214
+
.where(eq(incidentTable.id, Number(id)))
215
215
+
.get();
216
216
+
217
217
+
if (!_incident) return c.jsonT({ code: 404, message: "Not Found" });
218
218
+
219
219
+
if (workspaceId !== _incident.workspaceId)
220
220
+
return c.jsonT({ code: 401, message: "Unauthorized" });
221
221
+
222
222
+
console.log(input);
223
223
+
const _newIncident = await db
224
224
+
.update(incidentTable)
225
225
+
.set({
226
226
+
...input,
227
227
+
})
228
228
+
.returning()
229
229
+
.get();
230
230
+
231
231
+
const data = IncidentSchema.parse(_newIncident);
158
232
159
233
return c.jsonT(data);
160
234
});