-
Notifications
You must be signed in to change notification settings - Fork 263
Expand file tree
/
Copy pathroute.ts
More file actions
157 lines (134 loc) · 4.97 KB
/
route.ts
File metadata and controls
157 lines (134 loc) · 4.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
'use server';
import { getAuditService } from "@/ee/features/audit/factory";
import { apiHandler } from "@/lib/apiHandler";
import { ErrorCode } from "@/lib/errorCodes";
import { serviceErrorResponse, missingQueryParam, notFound } from "@/lib/serviceError";
import { isServiceError } from "@/lib/utils";
import { withAuthV2, withMinimumOrgRole } from "@/withAuthV2";
import { OrgRole } from "@sourcebot/db";
import { createLogger, hasEntitlement } from "@sourcebot/shared";
import { StatusCodes } from "http-status-codes";
import { NextRequest } from "next/server";
const logger = createLogger('ee-user-api');
const auditService = getAuditService();
export const GET = apiHandler(async (request: NextRequest) => {
if (!hasEntitlement('org-management')) {
return serviceErrorResponse({
statusCode: StatusCodes.FORBIDDEN,
errorCode: ErrorCode.INSUFFICIENT_PERMISSIONS,
message: "Organization management is not enabled for your license",
});
}
const url = new URL(request.url);
const userId = url.searchParams.get('userId');
if (!userId) {
return serviceErrorResponse(missingQueryParam('userId'));
}
const result = await withAuthV2(async ({ org, role, user, prisma }) => {
return withMinimumOrgRole(role, OrgRole.OWNER, async () => {
try {
const userData = await prisma.user.findUnique({
where: {
id: userId,
},
select: {
name: true,
email: true,
createdAt: true,
updatedAt: true,
},
});
if (!userData) {
return notFound('User not found');
}
await auditService.createAudit({
action: "user.read",
actor: {
id: user.id,
type: "user"
},
target: {
id: userId,
type: "user"
},
orgId: org.id,
});
return userData;
} catch (error) {
logger.error('Error fetching user info', { error, userId });
throw error;
}
});
});
if (isServiceError(result)) {
return serviceErrorResponse(result);
}
return Response.json(result, { status: StatusCodes.OK });
});
export const DELETE = apiHandler(async (request: NextRequest) => {
const url = new URL(request.url);
const userId = url.searchParams.get('userId');
if (!userId) {
return serviceErrorResponse(missingQueryParam('userId'));
}
const result = await withAuthV2(async ({ org, role, user: currentUser, prisma }) => {
return withMinimumOrgRole(role, OrgRole.OWNER, async () => {
try {
if (currentUser.id === userId) {
return {
statusCode: StatusCodes.BAD_REQUEST,
errorCode: ErrorCode.INVALID_REQUEST_BODY,
message: 'Cannot delete your own user account',
};
}
const targetUser = await prisma.user.findUnique({
where: {
id: userId,
},
select: {
id: true,
email: true,
name: true,
},
});
if (!targetUser) {
return notFound('User not found');
}
await auditService.createAudit({
action: "user.delete",
actor: {
id: currentUser.id,
type: "user"
},
target: {
id: userId,
type: "user"
},
orgId: org.id,
});
// Delete the user (cascade will handle all related records)
await prisma.user.delete({
where: {
id: userId,
},
});
logger.info('User deleted successfully', {
deletedUserId: userId,
deletedByUserId: currentUser.id,
orgId: org.id
});
return {
success: true,
message: 'User deleted successfully'
};
} catch (error) {
logger.error('Error deleting user', { error, userId });
throw error;
}
});
});
if (isServiceError(result)) {
return serviceErrorResponse(result);
}
return Response.json(result, { status: StatusCodes.OK });
});