106 lines
2.6 KiB
C++
106 lines
2.6 KiB
C++
#include <stdexcept>
|
|
#include "InventoryManagementService.h"
|
|
#include "Vector.h"
|
|
#include "Enums.h"
|
|
#include "InventoryItem.h"
|
|
#include "Config.h"
|
|
#include "User.h"
|
|
#include "Factory.h"
|
|
#include "Timestamp.h"
|
|
|
|
util::Map<std::string, User*> InventoryManagementService::m_observers{};
|
|
|
|
void InventoryManagementService::attach(User* user)
|
|
{
|
|
if (user)
|
|
{
|
|
const std::string& userID = user->getId();
|
|
if (m_observers.find(userID) == -1)
|
|
{
|
|
m_observers[userID] = user;
|
|
}
|
|
}
|
|
}
|
|
|
|
void InventoryManagementService::detach(User* user)
|
|
{
|
|
if (user)
|
|
{
|
|
const std::string& userID = user->getId();
|
|
if (m_observers.find(userID) != -1)
|
|
{
|
|
m_observers.remove(userID);
|
|
}
|
|
}
|
|
}
|
|
|
|
void InventoryManagementService::sendNotification(User* user, const std::string& title, const std::string& message)
|
|
{
|
|
if (user)
|
|
{
|
|
if (m_observers.find(user->getId()) != -1)
|
|
{
|
|
Notification* notification =
|
|
Factory::getObject<Notification>(
|
|
user->getId(),
|
|
user,
|
|
"InventoryManagementService: " + title,
|
|
message,
|
|
util::Timestamp()
|
|
);
|
|
if (notification)
|
|
{
|
|
user->addNotification(notification);
|
|
}
|
|
else
|
|
{
|
|
throw std::runtime_error("Failed to create notification");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
static void sendLowStockAlertsToAdmins(InventoryManagementService& inventoryManagementService, const InventoryItem* inventoryItem, const util::Vector<User*>& adminUsers)
|
|
{
|
|
int adminUsersSize = adminUsers.getSize();
|
|
for (int index = 0; index < adminUsersSize; index++)
|
|
{
|
|
inventoryManagementService.sendNotification(
|
|
adminUsers[index],
|
|
"Low Stock Alert",
|
|
"The inventory item with ID " + inventoryItem->getId() +
|
|
" has very low quantity in the inventory"
|
|
);
|
|
}
|
|
}
|
|
|
|
void InventoryManagementService::sendLowStockAlerts()
|
|
{
|
|
auto& inventoryItems = m_dataStore.getInventoryItems();
|
|
int inventoryItemsSize = inventoryItems.getSize();
|
|
auto& usersMap = m_dataStore.getUsers();
|
|
int usersMapSize = usersMap.getSize();
|
|
util::Vector<User*> adminUsers;
|
|
for (int index = 0; index < usersMapSize; index++)
|
|
{
|
|
User* user = usersMap.getValueAt(index);
|
|
if (user->getUserType() == util::UserType::ADMIN)
|
|
{
|
|
adminUsers.push_back(user);
|
|
}
|
|
}
|
|
int adminUsersSize = adminUsers.getSize();
|
|
if (adminUsersSize < 1)
|
|
{
|
|
throw std::runtime_error("The system has no admins present!");
|
|
}
|
|
for (int index = 0; index <= inventoryItemsSize; index++)
|
|
{
|
|
InventoryItem* inventoryItem = inventoryItems.getValueAt(index);
|
|
if (inventoryItem && inventoryItem->getQuantity() < config::threshold::INVENTORY_LOW_STOCK_THRESHOLD)
|
|
{
|
|
sendLowStockAlertsToAdmins(*this, inventoryItem, adminUsers);
|
|
}
|
|
}
|
|
}
|