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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121import { and, desc, eq, inArray, isNotNull, sql } from "@openstatus/db";import { incident } from "@openstatus/db/src/schema";
import { type ServiceContext, getReadDb } from "../context";import { IncidentIdInput, ListIncidentsInput } from "./schemas";
const userColumns = { id: true, name: true, firstName: true, lastName: true, email: true, photoUrl: true, deletedAt: true,} as const;
const statusOrder = sql`case ${incident.status} when 'open' then 0 when 'mitigated' then 1 when 'resolved' then 2 else 3 end`;
/** Open incidents first, then newest declared. */export async function listIncidents(args: { ctx: ServiceContext; input?: ListIncidentsInput;}) { const { ctx } = args; const input = ListIncidentsInput.parse(args.input ?? {});
const where = and( eq(incident.workspaceId, ctx.workspace.id), input.status?.length ? inArray(incident.status, input.status) : undefined, ); return getReadDb(ctx).query.incident.findMany({ where, orderBy: [statusOrder, desc(incident.declaredAt), desc(incident.id)], limit: input.limit, offset: input.offset, with: { commander: { columns: userColumns }, statusReport: { columns: { id: true, title: true, status: true } }, }, });}
export async function getIncident(args: { ctx: ServiceContext; input: IncidentIdInput;}) { const { ctx } = args; const input = IncidentIdInput.parse(args.input); return getReadDb(ctx).query.incident.findFirst({ where: and( eq(incident.id, input.id), eq(incident.workspaceId, ctx.workspace.id), ), with: { commander: { columns: userColumns }, declaredByUser: { columns: userColumns }, resolvedByUser: { columns: userColumns }, statusReport: { columns: { id: true, title: true, status: true, pageId: true }, }, }, });}
/** The incident a status report communicates, if any. */export async function getIncidentForStatusReport(args: { ctx: ServiceContext; input: { statusReportId: number };}) { const { ctx } = args; return getReadDb(ctx) .select({ id: incident.id, title: incident.title, status: incident.status }) .from(incident) .where( and( eq(incident.workspaceId, ctx.workspace.id), eq(incident.statusReportId, args.input.statusReportId), ), ) .get();}
/** Status reports already held by an incident; each links to at most one. */export async function listLinkedStatusReportIds(args: { ctx: ServiceContext;}): Promise<number[]> { const { ctx } = args; const rows = await getReadDb(ctx) .select({ statusReportId: incident.statusReportId }) .from(incident) .where( and( eq(incident.workspaceId, ctx.workspace.id), isNotNull(incident.statusReportId), ), ) .all(); return rows.flatMap((r) => r.statusReportId === null ? [] : [r.statusReportId], );}
/** The incident bound to a Slack channel, or `undefined`. */export async function getIncidentBySlackChannel(args: { ctx: ServiceContext; input: { teamId: string; channelId: string };}) { const { ctx } = args; return getReadDb(ctx).query.incident.findFirst({ where: and( eq(incident.workspaceId, ctx.workspace.id), eq(incident.slackTeamId, args.input.teamId), eq(incident.slackChannelId, args.input.channelId), ), with: { commander: { columns: userColumns }, statusReport: { columns: { id: true, title: true, status: true } }, }, });}