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 ยท 86 lines
TypeScript
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687import { notification, notificationsToMonitors, selectNotificationSchema,} from "@openstatus/db/src/schema";
import { emitAudit } from "../audit";import { requireScope } from "../auth";import { type ServiceContext, withTransaction } from "../context";import { ValidationError } from "../errors";import { assertWithinLimit } from "../limits";import type { Notification } from "../types";import { DEPRECATED_PROVIDERS, assertProviderAllowed, validateMonitorIds, validateNotificationData,} from "./internal";import { CreateNotificationInput } from "./schemas";
export async function createNotification(args: { ctx: ServiceContext; input: CreateNotificationInput;}): Promise<Notification> { const { ctx } = args; requireScope(ctx, "write"); const input = CreateNotificationInput.parse(args.input);
// Static check first: must win over quota/ownership errors, needs no DB. if (DEPRECATED_PROVIDERS.has(input.provider)) { throw new ValidationError( `The provider ${input.provider} is deprecated, use whatsapp instead`, ); }
return withTransaction(ctx, async (tx) => { // Ownership before quota: a cross-workspace monitor must fail with // ForbiddenError regardless of the workspace's notification count. const validatedMonitors = await validateMonitorIds({ tx, workspaceId: ctx.workspace.id, monitorIds: input.monitors, });
// Plan gate on notification count. await assertWithinLimit({ tx, workspaceId: ctx.workspace.id, limit: "notification-channels", });
// Plan gate on provider (pagerduty / opsgenie / โฆ). assertProviderAllowed(ctx.workspace, input.provider);
validateNotificationData(input.provider, input.data);
const row = await tx .insert(notification) .values({ name: input.name, provider: input.provider, data: JSON.stringify(input.data), workspaceId: ctx.workspace.id, }) .returning() .get();
if (validatedMonitors.length > 0) { await tx.insert(notificationsToMonitors).values( validatedMonitors.map((monitorId) => ({ notificationId: row.id, monitorId, })), ); }
await emitAudit(tx, ctx, { action: "notification.create", entityType: "notification", entityId: row.id, after: row, });
return selectNotificationSchema.parse(row); });}