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.9 kB ยท 62 lines
TypeScript
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263import { and, eq } from "@openstatus/db";import { type SlackUser, slackUser } from "@openstatus/db/src/schema";
import { emitAudit } from "../audit";import { requireScope } from "../auth";import { type ServiceContext, withTransaction } from "../context";import { ForbiddenError } from "../errors";import { getMembership } from "../member/membership";import { deleteSlackUserMappings } from "./internal";import { CreateSlackUserMappingInput } from "./schemas";
/** Link a Slack user to a workspace member; re-linking replaces the old row. */export async function createSlackUserMapping(args: { ctx: ServiceContext; input: CreateSlackUserMappingInput;}): Promise<SlackUser> { const { ctx } = args; requireScope(ctx, "write"); const input = CreateSlackUserMappingInput.parse(args.input);
return withTransaction(ctx, async (tx) => { const membership = await getMembership(tx, input.userId, ctx.workspace.id); if (!membership) { throw new ForbiddenError("User is not a member of this workspace"); }
const existing = await tx .select() .from(slackUser) .where( and( eq(slackUser.workspaceId, ctx.workspace.id), eq(slackUser.slackTeamId, input.teamId), eq(slackUser.slackUserId, input.slackUserId), ), ) .get(); if (existing?.userId === input.userId) return existing; if (existing) { await deleteSlackUserMappings({ tx, ctx, where: { id: existing.id } }); }
const record = await tx .insert(slackUser) .values({ workspaceId: ctx.workspace.id, slackTeamId: input.teamId, slackUserId: input.slackUserId, userId: input.userId, }) .returning() .get();
await emitAudit(tx, ctx, { action: "slack_user.create", entityType: "slack_user", entityId: record.id, after: record, }); return record; });}