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.4 kB ยท 84 lines
TypeScript
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485import { eq } from "@openstatus/db";import { notification, notificationsToMonitors, selectNotificationSchema,} from "@openstatus/db/src/schema";
import { emitAudit } from "../audit";import { requireScope } from "../auth";import { type ServiceContext, withTransaction } from "../context";import type { Notification } from "../types";import { DEPRECATED_PROVIDERS, assertProviderAllowed, getNotificationInWorkspace, validateMonitorIds, validateNotificationData,} from "./internal";import { UpdateNotificationInput } from "./schemas";
export async function updateNotification(args: { ctx: ServiceContext; input: UpdateNotificationInput;}): Promise<Notification> { const { ctx } = args; requireScope(ctx, "write"); const input = UpdateNotificationInput.parse(args.input);
return withTransaction(ctx, async (tx) => { const existing = await getNotificationInWorkspace({ tx, id: input.id, workspaceId: ctx.workspace.id, });
// Re-check the plan gate against the stored provider. After a plan // downgrade a previously allowed channel (e.g. pagerduty) should no // longer be editable โ matches the create-time gate. if (!DEPRECATED_PROVIDERS.has(existing.provider)) { assertProviderAllowed(ctx.workspace, existing.provider); }
validateNotificationData(existing.provider, input.data);
const validatedMonitors = await validateMonitorIds({ tx, workspaceId: ctx.workspace.id, monitorIds: input.monitors, });
const updated = await tx .update(notification) .set({ name: input.name, data: JSON.stringify(input.data), updatedAt: new Date(), }) .where(eq(notification.id, existing.id)) .returning() .get();
await tx .delete(notificationsToMonitors) .where(eq(notificationsToMonitors.notificationId, existing.id)); if (validatedMonitors.length > 0) { await tx.insert(notificationsToMonitors).values( validatedMonitors.map((monitorId) => ({ notificationId: existing.id, monitorId, })), ); }
await emitAudit(tx, ctx, { action: "notification.update", entityType: "notification", entityId: existing.id, before: existing, after: updated, });
return selectNotificationSchema.parse(updated); });}