👔 (comment bg) add command background helper
add command background helper and implemented on edit user service
This commit is contained in:
21
src/helpers/files/saveFile/modules/saveCommentBackgorund.ts
Normal file
21
src/helpers/files/saveFile/modules/saveCommentBackgorund.ts
Normal file
@ -0,0 +1,21 @@
|
||||
import { saveFile } from "..";
|
||||
import { AppError } from "../../../error/instances/app";
|
||||
|
||||
export const saveCommentBackground = async (file: File): Promise<string> => {
|
||||
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-",
|
||||
});
|
||||
};
|
||||
@ -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(),
|
||||
});
|
||||
|
||||
@ -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<Prisma.UserUpdateInput> = {
|
||||
...(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 }
|
||||
|
||||
Reference in New Issue
Block a user