From 48ca5910060213f980c8bbfdd8240040a2da0fa0 Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Tue, 9 Jul 2024 10:23:22 -0700 Subject: [PATCH] Added delete image profile functionality to update user --- Server/controllers/authController.js | 5 +++++ Server/db/MongoDB.js | 15 +++++++++++++-- Server/utils/utils.js | 17 +++++++++++++++++ Server/validation/joi.js | 3 ++- 4 files changed, 37 insertions(+), 3 deletions(-) create mode 100644 Server/utils/utils.js diff --git a/Server/controllers/authController.js b/Server/controllers/authController.js index b64b7f764..e06c58eb9 100644 --- a/Server/controllers/authController.js +++ b/Server/controllers/authController.js @@ -108,9 +108,13 @@ const loginController = async (req, res, next) => { // Remove password from user object. Should this be abstracted to DB layer? const userWithoutPassword = { ...user._doc }; delete userWithoutPassword.password; + delete userWithoutPassword.avatarImage; // Happy path, return token const token = issueToken(userWithoutPassword); + // reset avatar image + userWithoutPassword.avatarImage = user.avatarImage; + return res.status(200).json({ success: true, msg: successMessages.AUTH_LOGIN_USER, @@ -135,6 +139,7 @@ const userEditController = async (req, res, next) => { return; } + // TODO is this neccessary any longer? Verify ownership middleware should handle this if (req.params.userId !== req.user._id.toString()) { const error = new Error(errorMessages.AUTH_UNAUTHORIZED); error.status = 401; diff --git a/Server/db/MongoDB.js b/Server/db/MongoDB.js index 1f46b63a6..4c9e19095 100644 --- a/Server/db/MongoDB.js +++ b/Server/db/MongoDB.js @@ -7,8 +7,8 @@ const RecoveryToken = require("../models/RecoveryToken"); const crypto = require("crypto"); const DUPLICATE_KEY_CODE = 11000; // MongoDB error code for duplicate key const { errorMessages, successMessages } = require("../utils/messages"); -const { error } = require("console"); const { GenerateAvatarImage } = require("../utils/imageProcessing"); +const { ParseBoolean } = require("../utils/utils"); const connect = async () => { try { await mongoose.connect(process.env.DB_CONNECTION_STRING); @@ -100,7 +100,14 @@ const updateUser = async (req, res) => { try { const candidateUser = { ...req.body }; - if (req.file) { + // ****************************************** + // Handle profile image + // ****************************************** + + if (ParseBoolean(candidateUser.deleteProfileImage) === true) { + candidateUser.profileImage = null; + candidateUser.avatarImage = null; + } else if (req.file) { // 1. Save the full size image candidateUser.profileImage = { data: req.file.buffer, @@ -112,6 +119,10 @@ const updateUser = async (req, res) => { candidateUser.avatarImage = avatar; } + // ****************************************** + // End handling profile image + // ****************************************** + const updatedUser = await UserModel.findByIdAndUpdate( candidateUserId, candidateUser, diff --git a/Server/utils/utils.js b/Server/utils/utils.js new file mode 100644 index 000000000..8893623b6 --- /dev/null +++ b/Server/utils/utils.js @@ -0,0 +1,17 @@ +/** + * Converts a request body parameter to a boolean. + * @param {string | boolean} value + * @returns {boolean} + */ +const ParseBoolean = (value) => { + if (value === true || value === "true") { + return true; + } else if (value === false || value === "false") { + return false; + } + throw new Error("Invalid boolean value"); +}; + +module.exports = { + ParseBoolean, +}; diff --git a/Server/validation/joi.js b/Server/validation/joi.js index 8864fb8bd..28973f2cf 100644 --- a/Server/validation/joi.js +++ b/Server/validation/joi.js @@ -26,6 +26,7 @@ const editUserBodyValidation = joi.object({ firstname: joi.string(), lastname: joi.string(), profileImage: joi.any(), + deleteProfileImage: joi.boolean(), role: joi.string(), }); @@ -165,5 +166,5 @@ module.exports = { createCheckBodyValidation, getChecksParamValidation, deleteChecksParamValidation, - deleteUserParamValidation + deleteUserParamValidation, };