Added delete image profile functionality to update user
This commit is contained in:
1 parent
7deb796fa1
commit
48ca591006
4 files changed
+37
-3
No files matched your search
@@ -108,9 +108,13 @@ const loginController = async (req, res, next) => {
|
|||||||
// Remove password from user object. Should this be abstracted to DB layer?
|
// Remove password from user object. Should this be abstracted to DB layer?
|
||||||
const userWithoutPassword = { ...user._doc };
|
const userWithoutPassword = { ...user._doc };
|
||||||
delete userWithoutPassword.password;
|
delete userWithoutPassword.password;
|
||||||
|
delete userWithoutPassword.avatarImage;
|
||||||
|
|
||||||
// Happy path, return token
|
// Happy path, return token
|
||||||
const token = issueToken(userWithoutPassword);
|
const token = issueToken(userWithoutPassword);
|
||||||
|
// reset avatar image
|
||||||
|
userWithoutPassword.avatarImage = user.avatarImage;
|
||||||
|
|
||||||
return res.status(200).json({
|
return res.status(200).json({
|
||||||
success: true,
|
success: true,
|
||||||
msg: successMessages.AUTH_LOGIN_USER,
|
msg: successMessages.AUTH_LOGIN_USER,
|
||||||
@@ -135,6 +139,7 @@ const userEditController = async (req, res, next) => {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO is this neccessary any longer? Verify ownership middleware should handle this
|
||||||
if (req.params.userId !== req.user._id.toString()) {
|
if (req.params.userId !== req.user._id.toString()) {
|
||||||
const error = new Error(errorMessages.AUTH_UNAUTHORIZED);
|
const error = new Error(errorMessages.AUTH_UNAUTHORIZED);
|
||||||
error.status = 401;
|
error.status = 401;
|
||||||
|
|||||||
+13
-2
@@ -7,8 +7,8 @@ const RecoveryToken = require("../models/RecoveryToken");
|
|||||||
const crypto = require("crypto");
|
const crypto = require("crypto");
|
||||||
const DUPLICATE_KEY_CODE = 11000; // MongoDB error code for duplicate key
|
const DUPLICATE_KEY_CODE = 11000; // MongoDB error code for duplicate key
|
||||||
const { errorMessages, successMessages } = require("../utils/messages");
|
const { errorMessages, successMessages } = require("../utils/messages");
|
||||||
const { error } = require("console");
|
|
||||||
const { GenerateAvatarImage } = require("../utils/imageProcessing");
|
const { GenerateAvatarImage } = require("../utils/imageProcessing");
|
||||||
|
const { ParseBoolean } = require("../utils/utils");
|
||||||
const connect = async () => {
|
const connect = async () => {
|
||||||
try {
|
try {
|
||||||
await mongoose.connect(process.env.DB_CONNECTION_STRING);
|
await mongoose.connect(process.env.DB_CONNECTION_STRING);
|
||||||
@@ -100,7 +100,14 @@ const updateUser = async (req, res) => {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
const candidateUser = { ...req.body };
|
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
|
// 1. Save the full size image
|
||||||
candidateUser.profileImage = {
|
candidateUser.profileImage = {
|
||||||
data: req.file.buffer,
|
data: req.file.buffer,
|
||||||
@@ -112,6 +119,10 @@ const updateUser = async (req, res) => {
|
|||||||
candidateUser.avatarImage = avatar;
|
candidateUser.avatarImage = avatar;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ******************************************
|
||||||
|
// End handling profile image
|
||||||
|
// ******************************************
|
||||||
|
|
||||||
const updatedUser = await UserModel.findByIdAndUpdate(
|
const updatedUser = await UserModel.findByIdAndUpdate(
|
||||||
candidateUserId,
|
candidateUserId,
|
||||||
candidateUser,
|
candidateUser,
|
||||||
|
|||||||
@@ -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,
|
||||||
|
};
|
||||||
@@ -26,6 +26,7 @@ const editUserBodyValidation = joi.object({
|
|||||||
firstname: joi.string(),
|
firstname: joi.string(),
|
||||||
lastname: joi.string(),
|
lastname: joi.string(),
|
||||||
profileImage: joi.any(),
|
profileImage: joi.any(),
|
||||||
|
deleteProfileImage: joi.boolean(),
|
||||||
role: joi.string(),
|
role: joi.string(),
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -165,5 +166,5 @@ module.exports = {
|
|||||||
createCheckBodyValidation,
|
createCheckBodyValidation,
|
||||||
getChecksParamValidation,
|
getChecksParamValidation,
|
||||||
deleteChecksParamValidation,
|
deleteChecksParamValidation,
|
||||||
deleteUserParamValidation
|
deleteUserParamValidation,
|
||||||
};
|
};
|
||||||
Reference in new issue
Block a user