Skip to content
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

updateUser #30

Merged
merged 1 commit into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/controllers/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@ export const deleteUser = asyncHandler(async (req: Request, res: Response) => {
if (!existingUser) {
return errorResponse(res, 'User not found', 404)
}
const users = await userService.deleteUserById(userId)
await userService.deleteUserById(userId)
successResponse(res, null)
})

export const updateUser = asyncHandler(async (req: Request, res: Response) => {
const { userId } = req.params

const existingUser = await userService.getUserById(userId)
if (!existingUser) {
return errorResponse(res, 'User not found', 404)
}
const users = await userService.updateUser(userId, req.body)
successResponse(res, users)
likui628 marked this conversation as resolved.
Show resolved Hide resolved
})
6 changes: 6 additions & 0 deletions src/routes/v1/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ router
validate(userValidation.getUserSchema),
userController.getUser,
)
.patch(
verifyJwt,
verifyRoles('manageUsers'),
validate(userValidation.updateUserSchema),
userController.updateUser,
)
likui628 marked this conversation as resolved.
Show resolved Hide resolved
.delete(
verifyJwt,
verifyRoles('manageUsers'),
Expand Down
22 changes: 21 additions & 1 deletion src/services/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,24 @@ export const deleteUserById = async (id: string) => {
logger.error(`Failed to delete user by id: ${JSON.stringify(err)}`)
throw new Error('Failed to delete user by id')
}
}
}

export const updateUser = async (
id: string,
user: Omit<User, 'id' | 'createdAt' | 'updatedAt'>,
) => {
try {
const updatedUser = await prisma.user.update({
where: {
id,
},
data: {
...user,
},
})
return updatedUser
} catch (err: unknown) {
logger.error(`Failed to update user by id: ${JSON.stringify(err)}`)
throw new Error('Failed to update user by id')
}
}
9 changes: 8 additions & 1 deletion src/validations/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,11 @@ export const getUserSchema = z.object({
}),
})

export const deleteUserSchema = getUserSchema
export const deleteUserSchema = getUserSchema

export const updateUserSchema = z.object({
params: z.object({
userId: z.string().uuid(),
}),
body: createUserSchema.shape.body.partial(),
})