4
0

toggleSuperAdmin.ts 943 B

1234567891011121314151617181920212223242526272829
  1. 'use server'
  2. import { withSuperAdmin } from '@/lib/with-super-admin'
  3. import { db } from '@/lib/db'
  4. import { demoGuard } from '@/lib/demo'
  5. import { toggleSuperAdminSchema } from '../Schema/adminSchema'
  6. export async function toggleSuperAdmin(input: { userId: string; isSuperAdmin: boolean }) {
  7. return withSuperAdmin(async (ctx) => {
  8. // Granting platform-wide admin is the one thing in here worth refusing
  9. // twice, so it refuses alongside deleteUser and deleteOrganization rather
  10. // than resting on the demo owner never being a super admin.
  11. demoGuard()
  12. const { userId, isSuperAdmin } = toggleSuperAdminSchema.parse(input)
  13. if (userId === ctx.userId) {
  14. throw new Error('Cannot modify your own super admin status')
  15. }
  16. const user = await db.user.update({
  17. where: { id: userId },
  18. data: { isSuperAdmin },
  19. select: { id: true, name: true, isSuperAdmin: true },
  20. })
  21. return user
  22. })
  23. }