Fix: add duplicate user validation and update register customer UI

- add username duplicate validation
- add email duplicate validation
- add phone duplicate validation
- move duplicate checks to Validator utility
- add Register Customer header
- update registration success message

Fixes #1737
This commit is contained in:
2026-05-26 16:47:11 +05:30
parent 5fd0a47459
commit 31e660bc9e
4 changed files with 51 additions and 10 deletions
@@ -19,7 +19,7 @@ Date:19-May-2026
#include "User.h"
#include "UserManagementService.h"
#include "Vector.h"
#include "Validator.h"
/*
Function: ensureAdminExists
@@ -74,16 +74,18 @@ void UserManagementService::createUser(const std::string& username, const std::s
PaymentManagementService paymentManagementService;
ServiceManagementService serviceManagementService;
auto& usersMap = m_dataStore.getUsers();
int index = usersMap.findIf(
[&](const std::string&, User* user)
{
return user->getUserName() == username;
}
);
if (index != -1)
if (util::isUsernameDuplicate(username, usersMap))
{
throw std::runtime_error("Username already exists");
}
if (util::isEmailDuplicate(email, usersMap))
{
throw std::runtime_error("Email already exists");
}
if (util::isPhoneDuplicate(phone, usersMap))
{
throw std::runtime_error("Phone already exists");
}
User* newUser = Factory::getObject<User>(username, password, name, phone, email, type);
usersMap.insert(newUser->getId(), newUser);
paymentManagementService.attach(newUser);
@@ -106,4 +106,37 @@ bool util::isPasswordValid(const std::string& password)
}
return hasUpper && hasLower && hasDigit && hasSpecial;
}
}
bool util::isUsernameDuplicate(const std::string& username, const util::Map<std::string, User*>& usersMap)
{
int index = usersMap.findIf(
[&](const std::string&, User* user)
{
return (user->getUserName() == username && user->getState() == util::State::ACTIVE);
}
);
return index != -1;
}
bool util::isPhoneDuplicate(const std::string& phone, const util::Map<std::string, User*>& usersMap)
{
int index = usersMap.findIf(
[&](const std::string&, User* user)
{
return (user->getPhone() == phone && user->getState() == util::State::ACTIVE);
}
);
return index != -1;
}
bool util::isEmailDuplicate(const std::string& email, const util::Map<std::string, User*>& usersMap)
{
int index = usersMap.findIf(
[&](const std::string&, User* user)
{
return (user->getEmail() == email && user->getState() == util::State::ACTIVE);
}
);
return index != -1;
}
@@ -9,10 +9,15 @@
#include<string>
#include<algorithm>
#include<cctype>
#include "Map.h"
#include "User.h"
namespace util
{
bool isPhoneNumberValid(const std::string&);
bool isEmailValid(const std::string&);
bool isPasswordValid(const std::string&);
bool isUsernameDuplicate(const std::string&, const util::Map<std::string, User*>&);
bool isPhoneDuplicate(const std::string&, const util::Map<std::string, User*>&);
bool isEmailDuplicate(const std::string&, const util::Map<std::string, User*>&);
}
@@ -136,6 +136,7 @@ void UserInterface::registerCustomer()
{
std::string username, name, email, phone, password;
util::clear();
std::cout << "Register Customer\n";
std::cout << "Enter username: ";
util::read(username);
std::cout << "Enter name: ";
@@ -165,6 +166,6 @@ void UserInterface::registerCustomer()
return;
}
m_controller.createCustomer(username, name, password, email, phone);
std::cout << "Registration is successful";
std::cout << "Registration is successfull";
util::pressEnter();
}