userController.ts58 lines, 1.5 KB
| 1 | import { asyncHandler } from "../../../shared/middleware"; |
| 2 | import { Request, Response } from "express"; |
| 3 | import { UserService } from "./userService"; |
| 4 | import { createErrorResponse, createSuccessResponse } from "../../../shared/utils"; |
| 5 | |
| 6 | const userService = new UserService(); |
| 7 | |
| 8 | export const getProfile = asyncHandler(async (req: Request, res: Response) => { |
| 9 | const userId = req.user?.userId; |
| 10 | |
| 11 | if (!userId) { |
| 12 | return res.status(401).json(createErrorResponse("User not authenticated")); |
| 13 | } |
| 14 | |
| 15 | const profile = await userService.getProfile(userId); |
| 16 | |
| 17 | res |
| 18 | .status(200) |
| 19 | .json( |
| 20 | createSuccessResponse(profile, "User profile retrieved successfully") |
| 21 | ); |
| 22 | }); |
| 23 | |
| 24 | export const updateProfile = asyncHandler( |
| 25 | async (req: Request, res: Response) => { |
| 26 | const userId = req.user?.userId; |
| 27 | |
| 28 | if (!userId) { |
| 29 | return res |
| 30 | .status(401) |
| 31 | .json(createErrorResponse("User not authenticated")); |
| 32 | } |
| 33 | |
| 34 | const profile = await userService.updateProfile(userId, req.body); |
| 35 | |
| 36 | res |
| 37 | .status(200) |
| 38 | .json( |
| 39 | createSuccessResponse(profile, "User profile updated successfully") |
| 40 | ); |
| 41 | } |
| 42 | ); |
| 43 | |
| 44 | export const deleteProfile = asyncHandler( |
| 45 | async (req: Request, res: Response) => { |
| 46 | const userId = req.user?.userId; |
| 47 | |
| 48 | if (!userId) { |
| 49 | return res |
| 50 | .status(401) |
| 51 | .json(createErrorResponse("User not authenticated")); |
| 52 | } |
| 53 | |
| 54 | await userService.deleteProfile(userId); |
| 55 | |
| 56 | res.status(204).json(createSuccessResponse(null, "Profile deleted")); // No content response |
| 57 | } |
| 58 | ); |