diff --git a/src/app/api/servers/[id]/admins/[userId]/route.ts b/src/app/api/servers/[id]/admins/[userId]/route.ts index 550366b..7134666 100644 --- a/src/app/api/servers/[id]/admins/[userId]/route.ts +++ b/src/app/api/servers/[id]/admins/[userId]/route.ts @@ -4,6 +4,7 @@ import connectToDatabase from '@/lib/mongodb' import { Server, User } from '@/lib/models' import { isValidObjectId } from '@/lib/input-validation' import { createAuditLog, getClientIP } from '@/lib/audit' +import mongoose from 'mongoose' // DELETE /api/servers/[id]/admins/[userId] — Remove a server admin export async function DELETE( @@ -42,11 +43,13 @@ export async function DELETE( return NextResponse.json({ error: 'Cannot remove the last admin from a server' }, { status: 400 }) } - server.admins = server.admins.filter((a) => a.toString() !== userId) - await server.save() - const removedUser = await User.findById(userId, { username: 1 }).lean() + await Server.updateOne( + { _id: id }, + { $pull: { admins: new mongoose.Types.ObjectId(userId) } } + ) + await createAuditLog({ action: 'server_admin_removed', entityType: 'server',