👔 (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),
|
.max(15),
|
||||||
bioProfile: Joi.string().max(300),
|
bioProfile: Joi.string().max(300),
|
||||||
deletedAt: Joi.date(),
|
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 { loginFromSystemService } from "../../auth/services/loginFromSystem.service";
|
||||||
import { UserHeaderInformation } from "../../../helpers/http/userHeader/getUserHeaderInformation/types";
|
import { UserHeaderInformation } from "../../../helpers/http/userHeader/getUserHeaderInformation/types";
|
||||||
import { saveAvatar } from "../../../helpers/files/saveFile/modules/saveAvatar";
|
import { saveAvatar } from "../../../helpers/files/saveFile/modules/saveAvatar";
|
||||||
|
import { saveCommentBackground } from "../../../helpers/files/saveFile/modules/saveCommentBackgorund";
|
||||||
|
|
||||||
export const editUserService = async (
|
export const editUserService = async (
|
||||||
cookie: string,
|
cookie: string,
|
||||||
@ -19,11 +20,10 @@ export const editUserService = async (
|
|||||||
const jwtSession = jwtDecode(cookie);
|
const jwtSession = jwtDecode(cookie);
|
||||||
|
|
||||||
// Check if the username or email is being taken by another user, if so, throw an error
|
// Check if the username or email is being taken by another user, if so, throw an error
|
||||||
const isUsernameOrEmailIsBeingTaken =
|
const isUsernameOrEmailIsBeingTaken = await checkUserEmailAndUsernameAvailabillityService(
|
||||||
await checkUserEmailAndUsernameAvailabillityService(
|
payload,
|
||||||
payload,
|
jwtSession.userId
|
||||||
jwtSession.userId
|
);
|
||||||
);
|
|
||||||
if (isUsernameOrEmailIsBeingTaken)
|
if (isUsernameOrEmailIsBeingTaken)
|
||||||
throw new AppError(
|
throw new AppError(
|
||||||
409,
|
409,
|
||||||
@ -34,6 +34,12 @@ export const editUserService = async (
|
|||||||
let storeAvatar: string | undefined = undefined;
|
let storeAvatar: string | undefined = undefined;
|
||||||
if (payload.avatar) storeAvatar = await saveAvatar(payload.avatar as File);
|
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
|
// Prepare the fields to update, only include fields that are provided in the payload
|
||||||
const fieldsToUpdate: Partial<Prisma.UserUpdateInput> = {
|
const fieldsToUpdate: Partial<Prisma.UserUpdateInput> = {
|
||||||
...(payload.username && payload.username !== jwtSession.user.username
|
...(payload.username && payload.username !== jwtSession.user.username
|
||||||
@ -53,8 +59,8 @@ export const editUserService = async (
|
|||||||
? { bioProfile: payload.bioProfile }
|
? { bioProfile: payload.bioProfile }
|
||||||
: {}),
|
: {}),
|
||||||
...(storeAvatar !== undefined ? { avatar: storeAvatar } : {}),
|
...(storeAvatar !== undefined ? { avatar: storeAvatar } : {}),
|
||||||
...(payload.commentBackground !== undefined
|
...(storeCommentBackground !== undefined
|
||||||
? { commentPicture: payload.commentBackground }
|
? { commentBackground: storeCommentBackground }
|
||||||
: {}),
|
: {}),
|
||||||
...(payload.deletedAt !== undefined
|
...(payload.deletedAt !== undefined
|
||||||
? { deletedAt: payload.deletedAt }
|
? { deletedAt: payload.deletedAt }
|
||||||
|
|||||||
Reference in New Issue
Block a user