Skip to content

Open-source status page, incident response and uptime monitoring

🔥 (#609)

Thibault Le Ouaya0c5b48d2522
diff --git a/apps/docs/api-reference/incident/put-incident:id.mdx b/apps/docs/api-reference/incident/put-incident:id.mdx
new file mode 100644
index 000000000..99da29ec8
--- /dev/null
+++ b/apps/docs/api-reference/incident/put-incident:id.mdx
@@ -0,0 +1,4 @@
+---
+title: Update an incident
+openapi: put /incident/:id
+---
diff --git a/apps/server/src/v1/incidents.test.ts b/apps/server/src/v1/incidents.test.ts
index 32a8ceb46..4ddf480e7 100644
--- a/apps/server/src/v1/incidents.test.ts
+++ b/apps/server/src/v1/incidents.test.ts
@@ -15,3 +15,27 @@ test("GET one Incident", async () => {
monitorId: 1,
});
});
+
+test("Update an incident ", async () => {
+ const res = await api.request("/incident/1", {
+ method: "PUT",
+ headers: {
+ "x-openstatus-key": "1",
+ "content-type": "application/json",
+ },
+ body: JSON.stringify({
+ acknowledgedAt: "2023-11-08T21:03:13.000Z",
+ }),
+ });
+ expect(res.status).toBe(200);
+
+ expect(await res.json()).toMatchObject({
+ acknowledgedAt: "2023-11-08T21:03:13.000Z",
+ monitorId: 1,
+ id: 1,
+ startedAt: expect.any(String),
+ resolvedAt: null,
+ resolvedBy: null,
+ acknowledgedBy: null,
+ });
+});
diff --git a/apps/server/src/v1/incidents.ts b/apps/server/src/v1/incidents.ts
index 5e06bbe74..67469ce60 100644
--- a/apps/server/src/v1/incidents.ts
+++ b/apps/server/src/v1/incidents.ts
@@ -28,7 +28,7 @@ const IncidentSchema = z.object({
example: 1,
}),
startedAt: z
- .preprocess((val) => String(val), z.string())
+ .preprocess((val) => new Date(String(val)).toISOString(), z.string())
.openapi({
description: "The date the incident started",
}),
@@ -42,7 +42,7 @@ const IncidentSchema = z.object({
.nullable(),
acknowledgedAt: z
- .preprocess((val) => String(val), z.string())
+ .preprocess((val) => new Date(String(val)).toISOString(), z.string())
.openapi({
description: "The date the incident was acknowledged",
})
@@ -60,7 +60,8 @@ const IncidentSchema = z.object({
.openapi({
description: "The date the incident was resolved",
})
- .optional(),
+ .optional()
+ .nullable(),
resolvedBy: z
.number()
.openapi({
@@ -159,4 +160,77 @@ incidentsApi.openapi(getRoute, async (c) => {
return c.jsonT(data);
});
+const incidentInputSchema = z
+ .object({
+ acknowledgedAt: z.coerce.date().optional(),
+ resolvedAt: z.coerce.date().optional(),
+ })
+ .openapi({ description: "The incident to create" });
+const putRoute = createRoute({
+ method: "put",
+ tags: ["incident"],
+ description: "Update an incident",
+ path: "/:id",
+ request: {
+ params: ParamsSchema,
+ body: {
+ description: "The incident to update",
+ content: {
+ "application/json": {
+ schema: incidentInputSchema,
+ },
+ },
+ },
+ },
+ responses: {
+ 200: {
+ content: {
+ "application/json": {
+ schema: IncidentSchema,
+ },
+ },
+ description: "Update a monitor",
+ },
+ 400: {
+ content: {
+ "application/json": {
+ schema: ErrorSchema,
+ },
+ },
+ description: "Returns an error",
+ },
+ },
+});
+
+incidentsApi.openapi(putRoute, async (c) => {
+ const input = c.req.valid("json");
+ const workspaceId = Number(c.get("workspaceId"));
+ const { id } = c.req.valid("param");
+ if (!id) return c.jsonT({ code: 400, message: "Bad Request" });
+
+ const _incident = await db
+ .select()
+ .from(incidentTable)
+ .where(eq(incidentTable.id, Number(id)))
+ .get();
+
+ if (!_incident) return c.jsonT({ code: 404, message: "Not Found" });
+
+ if (workspaceId !== _incident.workspaceId)
+ return c.jsonT({ code: 401, message: "Unauthorized" });
+
+ console.log(input);
+ const _newIncident = await db
+ .update(incidentTable)
+ .set({
+ ...input,
+ })
+ .returning()
+ .get();
+
+ const data = IncidentSchema.parse(_newIncident);
+
+ return c.jsonT(data);
+});
+
export { incidentsApi };