From 87c3719203c30bbd3e7c45babfcba603048f992f Mon Sep 17 00:00:00 2001 From: rafiarrafif Date: Fri, 20 Jun 2025 10:27:37 +0700 Subject: [PATCH] edit:module:user:edit:controller | add documentation for edit user data controller --- .../user/controller/editUser.controller.ts | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/src/modules/user/controller/editUser.controller.ts b/src/modules/user/controller/editUser.controller.ts index 5baebad..28ecfc7 100644 --- a/src/modules/user/controller/editUser.controller.ts +++ b/src/modules/user/controller/editUser.controller.ts @@ -12,25 +12,73 @@ import { setCookie } from "../../../helpers/http/userHeader/cookies/setCookies"; import { COOKIE_KEYS } from "../../../constants/cookie.keys"; import { jwtEncode } from "../../../helpers/http/jwt/encode"; +/** + * @function editUserController + * @description Updates user profile information. Requires valid JWT authentication token in cookies. + * On success, returns updated user data and sets a new JWT token in cookies. + * In development environment, the new JWT token is also returned in the response. + * + * @param {Context & { body: Prisma.UserUncheckedCreateInput }} ctx - The context object containing request information. + * @param {Object} ctx.body - The updated user data. + * + * @returns {Promise} A response object indicating success or failure. + * @throws {Object} An error response if authentication fails or validation errors occur. + * + * @example + * Request route: PUT /users + * Request headers: + * { + * "Cookie": "auth_token=" + * } + * Request body: + * { + * "username": "new_username", + * "name": "Updated Name", + * "birthDate": "1990-01-01T00:00:00.000Z", + * "gender": "male", + * "phoneCC": 62, + * "phoneNumber": 81234567890, + * "bioProfile": "Updated bio", + * "profilePicture": "https://example.com/new-profile.jpg", + * "commentPicture": "https://example.com/new-comment.jpg", + * "deletedAt": null + * } + * + * Success Response: + * Status: 201 Created + * { + * "message": "User data updated", + * "token": "" // Only in development environment + * } + * + * Failure Responses: + * - 401 Unauthorized: Missing or invalid authentication token + * - 400 Bad Request: Invalid user data + * - 500 Internal Server Error: Database operation failed + */ export const editUserController = async ( ctx: Context & { body: Prisma.UserUncheckedCreateInput; } ) => { try { + // Get the user JWT token from cookies, if the token is not found, return an error response const userCookie = getCookie(ctx); const auth_token = userCookie.auth_token; if (!auth_token) return returnErrorResponse(ctx.set, 401, "User Unauthenticated"); + // Get user browser header information from the context const userHeaderInfo = getUserHeaderInformation(ctx); + // Excecute the edit user data service const newUserData = await editUserService( auth_token, userHeaderInfo, ctx.body ); + // create a new JWT token with the updated user data, and set it in the cookies const newJwtToken = await jwtEncode(newUserData); setCookie(ctx.set, COOKIE_KEYS.AUTH, newJwtToken);