Fix Update Profile and User Validation Issues
- Included Validator.h in UserManagementService.cpp for duplicate checks. - Enhanced updateUserDetails in UserManagementService to validate: - Throw error if user does not exist. - Throw error if email already exists among active users. - Throw error if phone number already exists among active users. - Implemented new duplicate validation functions in Validator.cpp: - isUsernameDuplicate - isPhoneDuplicate - isEmailDuplicate - Declared new duplicate validation functions in Validator.h. - Updated CustomerMenu::updateDetails: - Added "Update Details" header for clarity. - Improved error messages with newline formatting. - Added success message with newline formatting. Fixes #1746
This commit is contained in:
+9
-1
@@ -111,7 +111,15 @@ void UserManagementService::updateUserDetails(const std::string& userID, const s
|
|||||||
int index = usersMap.find(userID);
|
int index = usersMap.find(userID);
|
||||||
if (index == -1)
|
if (index == -1)
|
||||||
{
|
{
|
||||||
throw std::runtime_error("User does not exist!");
|
throw std::runtime_error("User does not exist!\n");
|
||||||
|
}
|
||||||
|
if (util::isEmailDuplicate(email, usersMap))
|
||||||
|
{
|
||||||
|
throw std::runtime_error("Email already exists!\n");
|
||||||
|
}
|
||||||
|
if (util::isPhoneDuplicate(phone, usersMap))
|
||||||
|
{
|
||||||
|
throw std::runtime_error("Phone number already exists!\n");
|
||||||
}
|
}
|
||||||
User* user = usersMap.getValueAt(index);
|
User* user = usersMap.getValueAt(index);
|
||||||
user->setEmail(email);
|
user->setEmail(email);
|
||||||
|
|||||||
@@ -108,6 +108,17 @@ bool util::isPasswordValid(const std::string& password)
|
|||||||
return hasUpper && hasLower && hasDigit && hasSpecial;
|
return hasUpper && hasLower && hasDigit && hasSpecial;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Function: isUsernameDuplicate
|
||||||
|
* Description: Checks if the given username already exists among active users.
|
||||||
|
* Parameters:
|
||||||
|
* username - string containing the username to validate
|
||||||
|
* usersMap - map of user objects keyed by identifier
|
||||||
|
* Returns:
|
||||||
|
* bool - true if the username is already in use by an active user, false otherwise
|
||||||
|
* Notes:
|
||||||
|
* - Only considers users with state util::State::ACTIVE
|
||||||
|
*/
|
||||||
bool util::isUsernameDuplicate(const std::string& username, const util::Map<std::string, User*>& usersMap)
|
bool util::isUsernameDuplicate(const std::string& username, const util::Map<std::string, User*>& usersMap)
|
||||||
{
|
{
|
||||||
int index = usersMap.findIf(
|
int index = usersMap.findIf(
|
||||||
@@ -119,6 +130,17 @@ bool util::isUsernameDuplicate(const std::string& username, const util::Map<std:
|
|||||||
return index != -1;
|
return index != -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Function: isPhoneDuplicate
|
||||||
|
* Description: Checks if the given phone number already exists among active users.
|
||||||
|
* Parameters:
|
||||||
|
* phone - string containing the phone number to validate
|
||||||
|
* usersMap - map of user objects keyed by identifier
|
||||||
|
* Returns:
|
||||||
|
* bool - true if the phone number is already in use by an active user, false otherwise
|
||||||
|
* Notes:
|
||||||
|
* - Only considers users with state util::State::ACTIVE
|
||||||
|
*/
|
||||||
bool util::isPhoneDuplicate(const std::string& phone, const util::Map<std::string, User*>& usersMap)
|
bool util::isPhoneDuplicate(const std::string& phone, const util::Map<std::string, User*>& usersMap)
|
||||||
{
|
{
|
||||||
int index = usersMap.findIf(
|
int index = usersMap.findIf(
|
||||||
@@ -130,6 +152,17 @@ bool util::isPhoneDuplicate(const std::string& phone, const util::Map<std::strin
|
|||||||
return index != -1;
|
return index != -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Function: isEmailDuplicate
|
||||||
|
* Description: Checks if the given email address already exists among active users.
|
||||||
|
* Parameters:
|
||||||
|
* email - string containing the email address to validate
|
||||||
|
* usersMap - map of user objects keyed by identifier
|
||||||
|
* Returns:
|
||||||
|
* bool - true if the email address is already in use by an active user, false otherwise
|
||||||
|
* Notes:
|
||||||
|
* - Only considers users with state util::State::ACTIVE
|
||||||
|
*/
|
||||||
bool util::isEmailDuplicate(const std::string& email, const util::Map<std::string, User*>& usersMap)
|
bool util::isEmailDuplicate(const std::string& email, const util::Map<std::string, User*>& usersMap)
|
||||||
{
|
{
|
||||||
int index = usersMap.findIf(
|
int index = usersMap.findIf(
|
||||||
@@ -139,4 +172,4 @@ bool util::isEmailDuplicate(const std::string& email, const util::Map<std::strin
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
return index != -1;
|
return index != -1;
|
||||||
}
|
}
|
||||||
@@ -143,12 +143,14 @@ Return type: void
|
|||||||
void CustomerMenu::updateDetails()
|
void CustomerMenu::updateDetails()
|
||||||
{
|
{
|
||||||
std::string email, phone;
|
std::string email, phone;
|
||||||
|
util::Map<std::string, const User*> userList = m_controller.getUsers();
|
||||||
util::clear();
|
util::clear();
|
||||||
|
std::cout << "Update Details\n";
|
||||||
std::cout << "Enter new email: ";
|
std::cout << "Enter new email: ";
|
||||||
util::read(email);
|
util::read(email);
|
||||||
if (!util::isEmailValid(email))
|
if (!util::isEmailValid(email))
|
||||||
{
|
{
|
||||||
std::cout << "Error: Email is invalid!";
|
std::cout << "Error: Email is invalid!\n";
|
||||||
util::pressEnter();
|
util::pressEnter();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -156,12 +158,12 @@ void CustomerMenu::updateDetails()
|
|||||||
util::read(phone);
|
util::read(phone);
|
||||||
if (!util::isPhoneNumberValid(phone))
|
if (!util::isPhoneNumberValid(phone))
|
||||||
{
|
{
|
||||||
std::cout << "Error: Phone number is invalid!";
|
std::cout << "Error: Phone number is invalid!\n";
|
||||||
util::pressEnter();
|
util::pressEnter();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
m_controller.updateUserDetails(email, phone);
|
m_controller.updateUserDetails(email, phone);
|
||||||
std::cout << "Profile details updated successfully";
|
std::cout << "Profile details updated successfully\n";
|
||||||
util::pressEnter();
|
util::pressEnter();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user