-
Notifications
You must be signed in to change notification settings - Fork 261
feat(ee): Add REST API to get users and delete a user #578
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| 'use server'; | ||
|
|
||
| import { withAuthV2, withMinimumOrgRole } from "@/withAuthV2"; | ||
| import { OrgRole } from "@sourcebot/db"; | ||
| import { isServiceError } from "@/lib/utils"; | ||
| import { serviceErrorResponse, missingQueryParam, notFound } from "@/lib/serviceError"; | ||
| import { createLogger } from "@sourcebot/logger"; | ||
| import { NextRequest } from "next/server"; | ||
| import { StatusCodes } from "http-status-codes"; | ||
| import { ErrorCode } from "@/lib/errorCodes"; | ||
| import { getAuditService } from "@/ee/features/audit/factory"; | ||
|
|
||
| const logger = createLogger('ee-user-api'); | ||
| const auditService = getAuditService(); | ||
|
|
||
| export const DELETE = 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 }); | ||
| }; | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| 'use server'; | ||
|
|
||
| import { withAuthV2, withMinimumOrgRole } from "@/withAuthV2"; | ||
| import { OrgRole } from "@sourcebot/db"; | ||
| import { isServiceError } from "@/lib/utils"; | ||
| import { serviceErrorResponse } from "@/lib/serviceError"; | ||
| import { createLogger } from "@sourcebot/logger"; | ||
| import { getAuditService } from "@/ee/features/audit/factory"; | ||
|
|
||
| const logger = createLogger('ee-users-api'); | ||
| const auditService = getAuditService(); | ||
|
|
||
| export const GET = async () => { | ||
| const result = await withAuthV2(async ({ prisma, org, role, user }) => { | ||
| return withMinimumOrgRole(role, OrgRole.OWNER, async () => { | ||
| try { | ||
| const memberships = await prisma.userToOrg.findMany({ | ||
| where: { | ||
| orgId: org.id, | ||
| }, | ||
| include: { | ||
| user: true, | ||
| }, | ||
| }); | ||
|
|
||
| const usersWithActivity = await Promise.all( | ||
| memberships.map(async (membership) => { | ||
| const lastActivity = await prisma.audit.findFirst({ | ||
| where: { | ||
| actorId: membership.user.id, | ||
| actorType: 'user', | ||
| orgId: org.id, | ||
| }, | ||
| orderBy: { | ||
| timestamp: 'desc', | ||
| }, | ||
| select: { | ||
| timestamp: true, | ||
| }, | ||
| }); | ||
|
|
||
| return { | ||
| id: membership.user.id, | ||
| name: membership.user.name, | ||
| email: membership.user.email, | ||
| role: membership.role, | ||
| createdAt: membership.user.createdAt, | ||
| lastActivityAt: lastActivity?.timestamp ?? null, | ||
| }; | ||
| }) | ||
| ); | ||
|
|
||
| await auditService.createAudit({ | ||
| action: "user.list", | ||
| actor: { | ||
| id: user.id, | ||
| type: "user" | ||
| }, | ||
| target: { | ||
| id: org.id.toString(), | ||
| type: "org" | ||
| }, | ||
| orgId: org.id | ||
| }); | ||
|
|
||
| logger.info('Fetched users list', { count: usersWithActivity.length }); | ||
| return usersWithActivity; | ||
| } catch (error) { | ||
| logger.error('Error fetching users', { error }); | ||
| throw error; | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| if (isServiceError(result)) { | ||
| return serviceErrorResponse(result); | ||
| } | ||
|
|
||
| return Response.json(result); | ||
| }; | ||
|
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.