febfa45e4a
Changes: - Strengthened UserManagementService::updateUserDetails by checking duplicates only when email/phone are changed, preventing false errors - Updated AdminMenu::viewStockLevels header text from "View Stock Level" to "View Stock Levels" for consistency - Cleaned up CustomerMenu::updateDetails by removing unused user list retrieval and improving header/message formatting
324 lines
10 KiB
C++
324 lines
10 KiB
C++
/*
|
||
File: UserManagementService.cpp
|
||
Description: Implementation file containing the method definitions of the
|
||
UserManagementService class, including user creation, updates,
|
||
and ensuring an admin account exists.
|
||
Author: Trenser
|
||
Date:19-May-2026
|
||
*/
|
||
|
||
#include <stdexcept>
|
||
#include "Config.h"
|
||
#include "Enums.h"
|
||
#include "Factory.h"
|
||
#include "FileManager.h"
|
||
#include "InventoryManagementService.h"
|
||
#include "Notification.h"
|
||
#include "PaymentManagementService.h"
|
||
#include "ServiceManagementService.h"
|
||
#include "User.h"
|
||
#include "UserManagementService.h"
|
||
#include "Vector.h"
|
||
#include "Validator.h"
|
||
|
||
/*
|
||
Function: ensureAdminExists
|
||
Description: Ensures that at least one admin user exists in the system.
|
||
If no admin is found, creates a default admin user using
|
||
configuration constants.
|
||
Parameter: None
|
||
Return type: void
|
||
*/
|
||
void UserManagementService::ensureAdminExists()
|
||
{
|
||
auto& usersMap = m_dataStore.getUsers();
|
||
int usersMapSize = usersMap.getSize();
|
||
bool isAdminFound = false;
|
||
for (int index = 0; index < usersMapSize; index++)
|
||
{
|
||
User* user = usersMap.getValueAt(index);
|
||
if (user && user->getUserType() == util::UserType::ADMIN)
|
||
{
|
||
isAdminFound = true;
|
||
break;
|
||
}
|
||
}
|
||
if (!isAdminFound)
|
||
{
|
||
createUser(
|
||
config::admin::DEFAULT_ADMIN_USERNAME,
|
||
config::admin::DEFAULT_ADMIN_NAME,
|
||
config::admin::DEFAULT_ADMIN_PASSWORD,
|
||
config::admin::DEFAULT_ADMIN_EMAIL,
|
||
config::admin::DEFAULT_ADMIN_PHONE,
|
||
util::UserType::ADMIN);
|
||
}
|
||
}
|
||
|
||
/*
|
||
Function: createUser
|
||
Description: Creates a new user with the provided details. Validates that
|
||
the username is unique, then attaches the user to relevant
|
||
management services (payment, service, inventory).
|
||
Parameter: const std::string& username - user’s username
|
||
const std::string& name - user’s name
|
||
const std::string& password - user’s password
|
||
const std::string& email - user’s email address
|
||
const std::string& phone - user’s phone number
|
||
util::UserType type - type of user (ADMIN, CUSTOMER, TECHNICIAN)
|
||
Return type: void
|
||
*/
|
||
void UserManagementService::createUser(const std::string& username, const std::string& name, const std::string& password, const std::string& email, const std::string& phone, util::UserType type)
|
||
{
|
||
InventoryManagementService inventoryManagementService;
|
||
PaymentManagementService paymentManagementService;
|
||
ServiceManagementService serviceManagementService;
|
||
auto& usersMap = m_dataStore.getUsers();
|
||
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);
|
||
serviceManagementService.attach(newUser);
|
||
if (newUser->getUserType() == util::UserType::ADMIN)
|
||
{
|
||
inventoryManagementService.attach(newUser);
|
||
}
|
||
}
|
||
|
||
/*
|
||
Function: updateUserDetails
|
||
Description: Updates the email and phone details of an existing user.
|
||
Throws an exception if the user does not exist.
|
||
Parameter: const std::string& userID - ID of the user to update
|
||
const std::string& email - new email address
|
||
const std::string& phone - new phone number
|
||
Return type: void
|
||
*/
|
||
void UserManagementService::updateUserDetails(const std::string& userID, const std::string& email, const std::string& phone)
|
||
{
|
||
auto& usersMap = m_dataStore.getUsers();
|
||
int index = usersMap.find(userID);
|
||
if (index == -1)
|
||
{
|
||
throw std::runtime_error("User does not exist!\n");
|
||
}
|
||
User* user = usersMap.getValueAt(index);
|
||
if (email != user->getEmail())
|
||
{
|
||
if (util::isEmailDuplicate(email, usersMap))
|
||
{
|
||
throw std::runtime_error("Email already exists!\n");
|
||
}
|
||
}
|
||
if (phone != user->getPhone())
|
||
{
|
||
if (util::isPhoneDuplicate(phone, usersMap))
|
||
{
|
||
throw std::runtime_error("Phone number already exists!\n");
|
||
}
|
||
}
|
||
user->setEmail(email);
|
||
user->setPhone(phone);
|
||
}
|
||
|
||
/*
|
||
Function: getUserNotifications
|
||
Description: Retrieves all notifications associated with a given user ID.
|
||
Parameters:
|
||
- userID: The unique ID of the user whose notifications are to be retrieved.
|
||
Returns:
|
||
- util::Vector<Notification*> containing all notifications for the user.
|
||
Throws:
|
||
- std::runtime_error if no user is found with the given UserID or if the User object is invalid.
|
||
*/
|
||
util::Vector<Notification*> UserManagementService::getUserNotifications(const std::string& userID)
|
||
{
|
||
auto& usersMap = m_dataStore.getUsers();
|
||
if (usersMap.find(userID) == -1)
|
||
{
|
||
throw std::runtime_error("No user found with given UserID");
|
||
}
|
||
User* user = usersMap[userID];
|
||
if (user)
|
||
{
|
||
auto& notifications = user->getNotifications();
|
||
int numberOfNotifications = notifications.getSize();
|
||
util::Vector<Notification*> notificationsVector;
|
||
for (int index = 0; index < numberOfNotifications; index++)
|
||
{
|
||
notificationsVector.push_back(notifications.getValueAt(index));
|
||
}
|
||
return notificationsVector;
|
||
}
|
||
else
|
||
{
|
||
throw std::runtime_error("Invalid User object");
|
||
}
|
||
}
|
||
|
||
/*
|
||
Function: deleteNotification
|
||
Description: Deletes a specific notification associated with a given user ID.
|
||
Parameters:
|
||
- notificationID: The unique ID of the notification to be deleted.
|
||
- userID: The unique ID of the user whose notification is to be deleted.
|
||
Returns:
|
||
- void
|
||
Throws:
|
||
- std::runtime_error if no user is found with the given UserID or if no notification is found with the given NotificationID.
|
||
*/
|
||
void UserManagementService::deleteNotification(const std::string& notificationID, const std::string& userID)
|
||
{
|
||
auto& usersMap = m_dataStore.getUsers();
|
||
if (usersMap.find(userID) == -1)
|
||
{
|
||
throw std::runtime_error("No user found with given UserID");
|
||
}
|
||
User* user = usersMap[userID];
|
||
auto& notifications = user->getNotifications();
|
||
if (notifications.find(notificationID) == -1)
|
||
{
|
||
throw std::runtime_error("No notification found with given NotificationID");
|
||
}
|
||
notifications.remove(notificationID);
|
||
}
|
||
|
||
/*
|
||
Function: loadUsers
|
||
Description: Loads users and notifications from persistent storage into the datastore.
|
||
Validates that each notification’s recipient exists and attaches the
|
||
notification to the corresponding user.
|
||
Parameters:
|
||
- None
|
||
Returns:
|
||
- void
|
||
Throws:
|
||
- std::runtime_error if a notification recipient user ID is invalid
|
||
*/
|
||
void UserManagementService::loadUsers()
|
||
{
|
||
util::FileManager<User> userFileManager(config::file::USER_FILE);
|
||
util::FileManager<Notification> notificationFileManager(config::file::NOTIFICATION_FILE);
|
||
auto& users = m_dataStore.getUsers();
|
||
auto usersMap = userFileManager.load();
|
||
auto notificationsMap = notificationFileManager.load();
|
||
int numberOfUsers = usersMap.getSize();
|
||
int numberOfNotifications = notificationsMap.getSize();
|
||
for (int index = 0; index < numberOfUsers; index++)
|
||
{
|
||
users[usersMap.getKeyAt(index)] = usersMap.getValueAt(index);
|
||
}
|
||
for (int index = 0; index < numberOfNotifications; index++)
|
||
{
|
||
Notification* notification = notificationsMap.getValueAt(index);
|
||
const std::string& recipientUserId = notification->getRecipientUserId();
|
||
int userIndex = users.find(recipientUserId);
|
||
if (userIndex == -1)
|
||
{
|
||
throw std::runtime_error("Invalid recipient user ID");
|
||
}
|
||
User* user = users.getValueAt(userIndex);
|
||
user->addNotification(notification);
|
||
}
|
||
}
|
||
|
||
/*
|
||
Function: saveUsers
|
||
Description: Saves users and their notifications from the datastore to persistent storage.
|
||
Collects notifications from all users into a single map before saving.
|
||
Parameters:
|
||
- None
|
||
Returns:
|
||
- void
|
||
*/
|
||
void UserManagementService::saveUsers()
|
||
{
|
||
util::FileManager<User> userFileManager(config::file::USER_FILE);
|
||
util::FileManager<Notification> notificationFileManager(config::file::NOTIFICATION_FILE);
|
||
auto& users = m_dataStore.getUsers();
|
||
util::Map<std::string, Notification*> notifications;
|
||
for (int userIndex = 0; userIndex < users.getSize(); userIndex++)
|
||
{
|
||
User* user = users.getValueAt(userIndex);
|
||
auto& userNotifications = user->getNotifications();
|
||
for (int notificationIndex = 0; notificationIndex < userNotifications.getSize(); notificationIndex++)
|
||
{
|
||
notifications[userNotifications.getKeyAt(notificationIndex)] =
|
||
userNotifications.getValueAt(notificationIndex);
|
||
}
|
||
}
|
||
userFileManager.save(users);
|
||
notificationFileManager.save(notifications);
|
||
}
|
||
|
||
/*
|
||
Function: getUsers
|
||
Description: Retrieves all users stored in the DataStore.
|
||
Parameter: None
|
||
Return type: util::Map<std::string, User*>
|
||
*/
|
||
util::Map<std::string, User*> UserManagementService::getUsers()
|
||
{
|
||
return m_dataStore.getUsers();
|
||
}
|
||
|
||
/*
|
||
Function: getUser
|
||
Description: Retrieves a specific user by ID from the DataStore.
|
||
Parameter: const std::string& userID - ID of the user
|
||
Return type: User*
|
||
*/
|
||
User* UserManagementService::getUser(const std::string& userID)
|
||
{
|
||
int index = m_dataStore.getUsers().find(userID);
|
||
if (index != -1)
|
||
{
|
||
return m_dataStore.getUsers().getValueAt(index);
|
||
}
|
||
return nullptr;
|
||
}
|
||
|
||
/*
|
||
Function: removeUser
|
||
Description: Marks a user as inactive in the DataStore instead of deleting them.
|
||
Parameter: const std::string& userID - ID of the user to remove
|
||
Return type: void
|
||
*/
|
||
void UserManagementService::removeUser(const std::string& userID)
|
||
{
|
||
int index = m_dataStore.getUsers().find(userID);
|
||
if (index != -1)
|
||
{
|
||
User* user = m_dataStore.getUsers().getValueAt(index);
|
||
if (user != nullptr)
|
||
{
|
||
user->setState(util::State::INACTIVE);
|
||
}
|
||
}
|
||
}
|
||
|
||
util::Map<std::string, User*> UserManagementService::getUsers(util::UserType type)
|
||
{
|
||
util::Map<std::string, User*>& currentUsers = m_dataStore.getUsers();
|
||
util::Map<std::string, User*> filteredUsersMap;
|
||
for (int iterator = 0; iterator < currentUsers.getSize(); iterator++)
|
||
{
|
||
User* currentUser = currentUsers.getValueAt(iterator);
|
||
if (currentUser->getUserType() == type)
|
||
{
|
||
filteredUsersMap.insert(currentUser->getId(), currentUser);
|
||
}
|
||
}
|
||
return filteredUsersMap;
|
||
} |