1
17import type { PrismaClient } from "@prisma/client";
18import { ApiError } from "../../common/errors.js";
19
20export type OrgAdminRole = "OWNER" | "ADMIN";
21
22export async function assertOrgAdmin(
23 prisma: Pick<PrismaClient, "organization" | "organizationMember">,
24 orgId: string,
25 userId: string
26): Promise<OrgAdminRole> {
27 const org = await prisma.organization.findUnique({ where: { id: orgId } });
28 if (!org) throw ApiError.notFound("Organization not found.");
29 const m = await prisma.organizationMember.findUnique({
30 where: { organizationId_userId: { organizationId: orgId, userId } }
31 });
32 if (!m || (m.role !== "OWNER" && m.role !== "ADMIN")) {
33 throw ApiError.forbidden("Organization admin required.", [{ reason: "requires_org_admin" }]);
34 }
35 return m.role;
36}
37