From fea545f0de48a8d20d9eee28f9886d0edde7272b Mon Sep 17 00:00:00 2001 From: unknown Date: Sun, 29 Jun 2025 05:23:10 +0700 Subject: [PATCH] :necktie: (comment bg) add command background helper add command background helper and implemented on edit user service --- .../saveFile/modules/saveCommentBackgorund.ts | 21 +++++++++++++++++++ src/modules/user/schemas/editUser.schema.ts | 4 ++++ src/modules/user/services/editUser.service.ts | 20 +++++++++++------- 3 files changed, 38 insertions(+), 7 deletions(-) create mode 100644 src/helpers/files/saveFile/modules/saveCommentBackgorund.ts diff --git a/src/helpers/files/saveFile/modules/saveCommentBackgorund.ts b/src/helpers/files/saveFile/modules/saveCommentBackgorund.ts new file mode 100644 index 0000000..8f7ebb1 --- /dev/null +++ b/src/helpers/files/saveFile/modules/saveCommentBackgorund.ts @@ -0,0 +1,21 @@ +import { saveFile } from ".."; +import { AppError } from "../../../error/instances/app"; + +export const saveCommentBackground = async (file: File): Promise => { + if (Array.isArray(file)) { + throw new AppError(415, "Can't upload more than 1 file"); + } + + const allowedTypes = ["image/png", "image/jpeg", "image/webp"]; + if (!allowedTypes.includes(file.type)) { + throw new AppError( + 415, + "Unsupported Media Type. File must be in .jpg, .png, or .webp format" + ); + } + + return await saveFile(file, { + folder: "comment-backgorund", + prefix: "cmnt-bg-", + }); +}; diff --git a/src/modules/user/schemas/editUser.schema.ts b/src/modules/user/schemas/editUser.schema.ts index 0291674..9079ae1 100644 --- a/src/modules/user/schemas/editUser.schema.ts +++ b/src/modules/user/schemas/editUser.schema.ts @@ -17,4 +17,8 @@ export const editUserSchema = Joi.object({ .max(15), bioProfile: Joi.string().max(300), deletedAt: Joi.date(), + + // validate in helper + avatar: Joi.any(), + commentBackground: Joi.any(), }); diff --git a/src/modules/user/services/editUser.service.ts b/src/modules/user/services/editUser.service.ts index 3e5f3ae..4e7245d 100644 --- a/src/modules/user/services/editUser.service.ts +++ b/src/modules/user/services/editUser.service.ts @@ -8,6 +8,7 @@ import { logoutService } from "../../auth/services/logout.service"; import { loginFromSystemService } from "../../auth/services/loginFromSystem.service"; import { UserHeaderInformation } from "../../../helpers/http/userHeader/getUserHeaderInformation/types"; import { saveAvatar } from "../../../helpers/files/saveFile/modules/saveAvatar"; +import { saveCommentBackground } from "../../../helpers/files/saveFile/modules/saveCommentBackgorund"; export const editUserService = async ( cookie: string, @@ -19,11 +20,10 @@ export const editUserService = async ( const jwtSession = jwtDecode(cookie); // Check if the username or email is being taken by another user, if so, throw an error - const isUsernameOrEmailIsBeingTaken = - await checkUserEmailAndUsernameAvailabillityService( - payload, - jwtSession.userId - ); + const isUsernameOrEmailIsBeingTaken = await checkUserEmailAndUsernameAvailabillityService( + payload, + jwtSession.userId + ); if (isUsernameOrEmailIsBeingTaken) throw new AppError( 409, @@ -34,6 +34,12 @@ export const editUserService = async ( let storeAvatar: string | undefined = undefined; if (payload.avatar) storeAvatar = await saveAvatar(payload.avatar as File); + let storeCommentBackground: string | undefined = undefined; + if (payload.commentBackground) + storeCommentBackground = await saveCommentBackground( + payload.commentBackground as File + ); + // Prepare the fields to update, only include fields that are provided in the payload const fieldsToUpdate: Partial = { ...(payload.username && payload.username !== jwtSession.user.username @@ -53,8 +59,8 @@ export const editUserService = async ( ? { bioProfile: payload.bioProfile } : {}), ...(storeAvatar !== undefined ? { avatar: storeAvatar } : {}), - ...(payload.commentBackground !== undefined - ? { commentPicture: payload.commentBackground } + ...(storeCommentBackground !== undefined + ? { commentBackground: storeCommentBackground } : {}), ...(payload.deletedAt !== undefined ? { deletedAt: payload.deletedAt }