|
| 1 | +'use server'; |
| 2 | + |
| 3 | +import { withAuthV2, withMinimumOrgRole } from "@/withAuthV2"; |
| 4 | +import { OrgRole } from "@sourcebot/db"; |
| 5 | +import { isServiceError } from "@/lib/utils"; |
| 6 | +import { serviceErrorResponse, missingQueryParam, notFound } from "@/lib/serviceError"; |
| 7 | +import { createLogger } from "@sourcebot/logger"; |
| 8 | +import { NextRequest } from "next/server"; |
| 9 | +import { StatusCodes } from "http-status-codes"; |
| 10 | +import { ErrorCode } from "@/lib/errorCodes"; |
| 11 | +import { getAuditService } from "@/ee/features/audit/factory"; |
| 12 | + |
| 13 | +const logger = createLogger('ee-user-api'); |
| 14 | +const auditService = getAuditService(); |
| 15 | + |
| 16 | +export const DELETE = async (request: NextRequest) => { |
| 17 | + const url = new URL(request.url); |
| 18 | + const userId = url.searchParams.get('userId'); |
| 19 | + |
| 20 | + if (!userId) { |
| 21 | + return serviceErrorResponse(missingQueryParam('userId')); |
| 22 | + } |
| 23 | + |
| 24 | + const result = await withAuthV2(async ({ org, role, user: currentUser, prisma }) => { |
| 25 | + return withMinimumOrgRole(role, OrgRole.OWNER, async () => { |
| 26 | + try { |
| 27 | + if (currentUser.id === userId) { |
| 28 | + return { |
| 29 | + statusCode: StatusCodes.BAD_REQUEST, |
| 30 | + errorCode: ErrorCode.INVALID_REQUEST_BODY, |
| 31 | + message: 'Cannot delete your own user account', |
| 32 | + }; |
| 33 | + } |
| 34 | + |
| 35 | + const targetUser = await prisma.user.findUnique({ |
| 36 | + where: { |
| 37 | + id: userId, |
| 38 | + }, |
| 39 | + select: { |
| 40 | + id: true, |
| 41 | + email: true, |
| 42 | + name: true, |
| 43 | + }, |
| 44 | + }); |
| 45 | + |
| 46 | + if (!targetUser) { |
| 47 | + return notFound('User not found'); |
| 48 | + } |
| 49 | + |
| 50 | + await auditService.createAudit({ |
| 51 | + action: "user.delete", |
| 52 | + actor: { |
| 53 | + id: currentUser.id, |
| 54 | + type: "user" |
| 55 | + }, |
| 56 | + target: { |
| 57 | + id: userId, |
| 58 | + type: "user" |
| 59 | + }, |
| 60 | + orgId: org.id, |
| 61 | + }); |
| 62 | + |
| 63 | + // Delete the user (cascade will handle all related records) |
| 64 | + await prisma.user.delete({ |
| 65 | + where: { |
| 66 | + id: userId, |
| 67 | + }, |
| 68 | + }); |
| 69 | + |
| 70 | + logger.info('User deleted successfully', { |
| 71 | + deletedUserId: userId, |
| 72 | + deletedByUserId: currentUser.id, |
| 73 | + orgId: org.id |
| 74 | + }); |
| 75 | + |
| 76 | + return { |
| 77 | + success: true, |
| 78 | + message: 'User deleted successfully' |
| 79 | + }; |
| 80 | + } catch (error) { |
| 81 | + logger.error('Error deleting user', { error, userId }); |
| 82 | + throw error; |
| 83 | + } |
| 84 | + }); |
| 85 | + }); |
| 86 | + |
| 87 | + if (isServiceError(result)) { |
| 88 | + return serviceErrorResponse(result); |
| 89 | + } |
| 90 | + |
| 91 | + return Response.json(result, { status: StatusCodes.OK }); |
| 92 | +}; |
| 93 | + |
0 commit comments