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.
1.8 kB ยท 61 lines
TypeScript
at main
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162import { and, desc, eq, sql } from "@openstatus/db";import { integration } from "@openstatus/db/src/schema";import { z } from "zod";
import { type ServiceContext, getReadDb } from "../context";import { missingSlackScopes } from "./slack-scopes";
const credentialSchema = z.object({ botToken: z.string().min(1), botUserId: z.string().optional(),});const dataSchema = z.object({ scopes: z.string().optional() });
export type SlackConnection = { teamId: string; botToken: string; botUserId: string | null; missingScopes: string[];};
/** JSON-mode columns throw on malformed JSON when Drizzle maps the row. */function parseJson(value: string | null): unknown { if (!value) return undefined; try { return JSON.parse(value); } catch { return undefined; }}
/** The workspace's Slack agent install, bot token included. Server-side only. */export async function getSlackConnection(args: { ctx: ServiceContext;}): Promise<SlackConnection | null> { const row = await getReadDb(args.ctx) .select({ externalId: integration.externalId, rawCredential: sql<string | null>`${integration.credential}`, rawData: sql<string | null>`${integration.data}`, }) .from(integration) .where( and( eq(integration.name, "slack-agent"), eq(integration.workspaceId, args.ctx.workspace.id), ), ) .orderBy(desc(integration.updatedAt), desc(integration.id)) .get(); if (!row) return null; const credential = credentialSchema.safeParse(parseJson(row.rawCredential)); if (!credential.success) return null; return { teamId: row.externalId, botToken: credential.data.botToken, botUserId: credential.data.botUserId ?? null, missingScopes: missingSlackScopes( dataSchema.safeParse(parseJson(row.rawData)).data?.scopes, ), };}