Openstatus www.openstatus.dev

🔥 (#609)

authored by

Thibault Le Ouay and committed by
GitHub
a0c5b48d 0da38c3e

+105 -3
+4
apps/docs/api-reference/incident/put-incident:id.mdx
··· 1 + --- 2 + title: Update an incident 3 + openapi: put /incident/:id 4 + ---
+24
apps/server/src/v1/incidents.test.ts
··· 15 15 monitorId: 1, 16 16 }); 17 17 }); 18 + 19 + test("Update an incident ", async () => { 20 + const res = await api.request("/incident/1", { 21 + method: "PUT", 22 + headers: { 23 + "x-openstatus-key": "1", 24 + "content-type": "application/json", 25 + }, 26 + body: JSON.stringify({ 27 + acknowledgedAt: "2023-11-08T21:03:13.000Z", 28 + }), 29 + }); 30 + expect(res.status).toBe(200); 31 + 32 + expect(await res.json()).toMatchObject({ 33 + acknowledgedAt: "2023-11-08T21:03:13.000Z", 34 + monitorId: 1, 35 + id: 1, 36 + startedAt: expect.any(String), 37 + resolvedAt: null, 38 + resolvedBy: null, 39 + acknowledgedBy: null, 40 + }); 41 + });
+77 -3
apps/server/src/v1/incidents.ts
··· 28 28 example: 1, 29 29 }), 30 30 startedAt: z 31 - .preprocess((val) => String(val), z.string()) 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 - .preprocess((val) => String(val), z.string()) 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 - .optional(), 63 + .optional() 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 + 160 + return c.jsonT(data); 161 + }); 162 + 163 + const incidentInputSchema = z 164 + .object({ 165 + acknowledgedAt: z.coerce.date().optional(), 166 + resolvedAt: z.coerce.date().optional(), 167 + }) 168 + .openapi({ description: "The incident to create" }); 169 + const putRoute = createRoute({ 170 + method: "put", 171 + tags: ["incident"], 172 + description: "Update an incident", 173 + path: "/:id", 174 + request: { 175 + params: ParamsSchema, 176 + body: { 177 + description: "The incident to update", 178 + content: { 179 + "application/json": { 180 + schema: incidentInputSchema, 181 + }, 182 + }, 183 + }, 184 + }, 185 + responses: { 186 + 200: { 187 + content: { 188 + "application/json": { 189 + schema: IncidentSchema, 190 + }, 191 + }, 192 + description: "Update a monitor", 193 + }, 194 + 400: { 195 + content: { 196 + "application/json": { 197 + schema: ErrorSchema, 198 + }, 199 + }, 200 + description: "Returns an error", 201 + }, 202 + }, 203 + }); 204 + 205 + incidentsApi.openapi(putRoute, async (c) => { 206 + const input = c.req.valid("json"); 207 + const workspaceId = Number(c.get("workspaceId")); 208 + const { id } = c.req.valid("param"); 209 + if (!id) return c.jsonT({ code: 400, message: "Bad Request" }); 210 + 211 + const _incident = await db 212 + .select() 213 + .from(incidentTable) 214 + .where(eq(incidentTable.id, Number(id))) 215 + .get(); 216 + 217 + if (!_incident) return c.jsonT({ code: 404, message: "Not Found" }); 218 + 219 + if (workspaceId !== _incident.workspaceId) 220 + return c.jsonT({ code: 401, message: "Unauthorized" }); 221 + 222 + console.log(input); 223 + const _newIncident = await db 224 + .update(incidentTable) 225 + .set({ 226 + ...input, 227 + }) 228 + .returning() 229 + .get(); 230 + 231 + const data = IncidentSchema.parse(_newIncident); 158 232 159 233 return c.jsonT(data); 160 234 });