fix unset

This commit is contained in:
Alex Holliday committed 2026-02-07 20:33:11 +00:00
1 parent f2b740a8f9
commit af4c9d8f91
4 files changed
+78 -82

No files matched your search

-1
View File
@@ -74,7 +74,6 @@ class AuthController {
editUser = async (req: Request, res: Response, next: NextFunction) => {
try {
await editUserBodyValidation.validateAsync(req.body);
const updatedUser = await this.userService.editUser(req.body, req.file, req.user);
res.status(200).json({
@@ -98,10 +98,11 @@ class MongoUsersRepository implements IUsersRepository {
updateById = async (id: string, patch: Partial<User & { deleteProfileImage?: boolean }>, file?: Express.Multer.File | null): Promise<User> => {
const candidateUser = { ...patch };
let unsetFields: Record<string, 1> | undefined;
if (ParseBoolean(candidateUser.deleteProfileImage) === true) {
candidateUser.profileImage = undefined;
candidateUser.avatarImage = undefined;
unsetFields = { profileImage: 1, avatarImage: 1 };
delete candidateUser.deleteProfileImage;
} else if (file) {
// 1. Save the full size image
candidateUser.profileImage = {
@@ -114,13 +115,14 @@ class MongoUsersRepository implements IUsersRepository {
candidateUser.avatarImage = avatar;
}
const updatedUser = await UserModel.findOneAndUpdate(
{ _id: id },
candidateUser,
{ new: true } // Returns updated user instead of pre-update user
)
.select("-password")
.select("-profileImage");
delete candidateUser.deleteProfileImage;
const updateQuery: Record<string, any> = { $set: candidateUser };
if (unsetFields) {
updateQuery.$unset = unsetFields;
}
const updatedUser = await UserModel.findOneAndUpdate({ _id: id }, updateQuery, { new: true }).select("-password").select("-profileImage");
if (!updatedUser) {
throw new AppError({ message: "User not found", service: SERVICE_NAME, status: 404 });
}
+1 -1
View File
@@ -60,7 +60,7 @@ const editUserBodyValidation = joi.object({
profileImage: joi.any(),
newPassword: joi.string().min(8).pattern(passwordPattern),
password: joi.string().min(8).pattern(passwordPattern),
deleteProfileImage: joi.boolean(),
deleteProfileImage: joi.alternatives().try(joi.boolean(), joi.string().valid("true", "false")),
});
const recoveryValidation = joi.object({