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.
TypeScript
at incident-api
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182import { and, db, desc, eq, sql } from "@openstatus/db";import { integration, selectWorkspaceSchema, workspace,} from "@openstatus/db/src/schema";import type { Workspace } from "@openstatus/db/src/schema/workspaces/validation";import { z } from "zod";
const integrationDataSchema = z.object({ scopes: z.string().optional() });
export interface SlackWorkspace { workspace: Workspace; botToken: string; botUserId: string; /** Comma list Slack granted at install; missing scopes mean "reconnect". */ scopes?: string;}
function parseScopes(raw: string | null): string { try { return ( integrationDataSchema.safeParse(JSON.parse(raw ?? "{}")).data?.scopes ?? "" ); } catch { return ""; }}
interface IntegrationCredential { botToken: string; botUserId: string;}
export async function resolveWorkspace( teamId: string,): Promise<SlackWorkspace | null> { const row = await db .select({ workspaceId: integration.workspaceId, credential: integration.credential, // Raw text: the JSON-mode column throws on a malformed legacy row. rawData: sql<string | null>`${integration.data}`, }) .from(integration) .where( and( eq(integration.name, "slack-agent"), eq(integration.externalId, teamId), ), ) // Slack keeps one live token per (app, team); a reinstall invalidates all // earlier ones. If the team is linked to several workspaces, the newest row // is the only one holding a valid token. .orderBy(desc(integration.updatedAt)) .get();
if (!row?.workspaceId) return null;
const credential = row.credential as IntegrationCredential | null; if (!credential?.botToken) return null;
const ws = await db .select() .from(workspace) .where(eq(workspace.id, row.workspaceId)) .get();
if (!ws) return null;
const parsed = selectWorkspaceSchema.safeParse(ws); if (!parsed.success) return null;
return { workspace: parsed.data, botToken: credential.botToken, botUserId: credential.botUserId ?? "", scopes: parseScopes(row.rawData), };}