Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/openstatusHQ/openstatus. ๐ซ Status page with uptime monitoring & API monitoring as code ๐ซ openstatus.dev
bun drizzle-orm monitoring monitoring-as-code nextjs observability on-call open-source shadcn-ui status-page statuspage synthetic-monitoring tinybird turso uptime uptime-checker uptime-monitor
Something went wrong. Try again.
2.9 kB ยท 90 lines
TypeScript
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091import { eq } from "@openstatus/db";import { maintenance } from "@openstatus/db/src/schema";
import { emitAudit } from "../audit";import { requireScope } from "../auth";import { type ServiceContext, withTransaction } from "../context";import { ConflictError, InternalServiceError } from "../errors";import type { Maintenance } from "../types";import { getMaintenanceInWorkspace, updatePageComponentAssociations, validatePageComponentIds,} from "./internal";import { UpdateMaintenanceInput } from "./schemas";
export async function updateMaintenance(args: { ctx: ServiceContext; input: UpdateMaintenanceInput;}): Promise<Maintenance> { const { ctx } = args; requireScope(ctx, "write"); const input = UpdateMaintenanceInput.parse(args.input);
return withTransaction(ctx, async (tx) => { const existing = await getMaintenanceInWorkspace({ tx, id: input.id, workspaceId: ctx.workspace.id, });
// Effective from/to for the range check โ either the incoming value or // whatever was persisted before. const effectiveFrom = input.from ?? existing.from; const effectiveTo = input.to ?? existing.to; if (effectiveFrom >= effectiveTo) { throw new ConflictError("End date must be after start date."); }
const updateValues: Record<string, unknown> = { updatedAt: new Date() }; if (input.title !== undefined) updateValues.title = input.title; if (input.message !== undefined) updateValues.message = input.message; if (input.from !== undefined) updateValues.from = input.from; if (input.to !== undefined) updateValues.to = input.to;
if (input.pageComponentIds !== undefined) { const validated = await validatePageComponentIds({ tx, workspaceId: ctx.workspace.id, pageComponentIds: input.pageComponentIds, });
// A non-empty set moves the maintenance to that page; an empty set // only clears associations and keeps `pageId` (same as status-report). // The dashboard edit sheet always sends the array, so nulling here // orphaned every maintenance edited without components. if (validated.pageId !== null) { updateValues.pageId = validated.pageId; }
await updatePageComponentAssociations({ tx, maintenanceId: existing.id, componentIds: validated.componentIds, }); }
const updated = await tx .update(maintenance) .set(updateValues) .where(eq(maintenance.id, existing.id)) .returning() .get();
if (!updated) { throw new InternalServiceError( `failed to update maintenance ${existing.id}`, ); }
await emitAudit(tx, ctx, { action: "maintenance.update", entityType: "maintenance", entityId: updated.id, before: existing, after: updated, });
return updated; });}