257 lines
7.8 KiB
C++
257 lines
7.8 KiB
C++
/*
|
|
File: UserManagementService.cpp
|
|
Description: Implementation file containing the method definitions of the
|
|
UserManagementService class, including user retrieval and removal logic.
|
|
Author: Trenser
|
|
Date:19-May-2026
|
|
*/
|
|
|
|
#include <stdexcept>
|
|
#include "Config.h"
|
|
#include "Enums.h"
|
|
#include "Factory.h"
|
|
#include "FileManager.h"
|
|
#include "InventoryManagementService.h"
|
|
#include "Invoice.h"
|
|
#include "PaymentManagementService.h"
|
|
#include "ServiceManagementService.h"
|
|
#include "User.h"
|
|
#include "UserManagementService.h"
|
|
#include "Utility.h"
|
|
#include "Vector.h"
|
|
|
|
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);
|
|
}
|
|
}
|
|
|
|
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();
|
|
int index = usersMap.findIf(
|
|
[&](const std::string&, User* user)
|
|
{
|
|
return user->getUserName() == username;
|
|
}
|
|
);
|
|
if (index != -1)
|
|
{
|
|
throw std::runtime_error("Username 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);
|
|
}
|
|
}
|
|
|
|
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!");
|
|
}
|
|
User* user = usersMap.getValueAt(index);
|
|
user->setEmail(email);
|
|
user->setPhone(phone);
|
|
}
|
|
|
|
/*
|
|
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();
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
/*
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
|
|
/*
|
|
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);
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|
|
|
|
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);
|
|
}
|