03be8f81d2
<SRS> SRS02 : Employee Management </SRS> <Changes> - Removed duplicate employee creation helper functions from AdminMenu and HRManagerMenu - Centralized employee creation flow into MenuHelper.cpp - Updated createEmployee to use current employee type for access control - Fixed parameter order bug in createEmployee (email, name, phone) - Improved menu titles for all roles (removed generic system header) - Added util::pressEnter() for better UX on invalid input across menus - Added newline before pause in InputHelper::pressEnter - Cleaned up minor formatting issues </Changes>
173 lines
6.0 KiB
C++
173 lines
6.0 KiB
C++
#include <stdexcept>
|
|
#include "EmployeeManagementService.h"
|
|
#include "Factory.h"
|
|
#include "Validator.h"
|
|
#include "AuthorizationHelper.h"
|
|
#include "Enums.h"
|
|
#include "HRManager.h"
|
|
#include "ITExecutive.h"
|
|
#include "TalentExecutive.h"
|
|
#include "TeamExecutive.h"
|
|
#include "FinanceExecutive.h"
|
|
#include "GeneralEmployee.h"
|
|
#include "FileManager.h"
|
|
#include "ApplicationConfig.h"
|
|
|
|
void EmployeeManagementService::createEmployee(Enums::EmployeeType employeeType, Enums::EmployeeDesignation employeeDesignation, const std::string& email, const std::string& name, const std::string& phone)
|
|
{
|
|
std::shared_ptr<Employee> authenticatedEmployee = m_dataStore.getAuthenticatedEmployee();
|
|
if (!authenticatedEmployee)
|
|
{
|
|
throw std::runtime_error("No authenticated user");
|
|
}
|
|
Enums::EmployeeType authenticatedEmployeeType = authenticatedEmployee->getEmployeeType();
|
|
std::shared_ptr<Employee> employee;
|
|
std::shared_ptr<Payroll> payroll;
|
|
if (!util::isEmailValid(email))
|
|
{
|
|
throw std::runtime_error("Invalid Email");
|
|
}
|
|
if (!util::isPhoneNumberValid(phone))
|
|
{
|
|
throw std::runtime_error("Invalid Phone");
|
|
}
|
|
switch (employeeType)
|
|
{
|
|
case Enums::EmployeeType::HR:
|
|
util::enforceAuthorization(authenticatedEmployeeType, Enums::EmployeeType::ADMIN);
|
|
payroll = Factory::getObject<Payroll>(Config::Payroll::HR_MANAGER_BASIC_SALARY,
|
|
Config::Payroll::HR_MANAGER_HOUSE_RENT_ALLOWANCE,
|
|
Config::Payroll::HR_MANAGER_FOOD_ALLOWANCE,
|
|
Config::Payroll::HR_MANAGER_EMPLOYEE_PF_CONTRIBUTION,
|
|
Config::Payroll::HR_MANAGER_EMPLOYER_PF_CONTRIBUTION);
|
|
employee = Factory::getObject<HRManager>(name, phone, email, payroll);
|
|
break;
|
|
|
|
case Enums::EmployeeType::IT:
|
|
case Enums::EmployeeType::FINANCE:
|
|
case Enums::EmployeeType::TEAM:
|
|
case Enums::EmployeeType::TAG:
|
|
util::enforceAuthorization(authenticatedEmployeeType, Enums::EmployeeType::ADMIN, Enums::EmployeeType::HR);
|
|
payroll = Factory::getObject<Payroll>(Config::Payroll::EXECUTIVE_BASIC_SALARY,
|
|
Config::Payroll::EXECUTIVE_HOUSE_RENT_ALLOWANCE,
|
|
Config::Payroll::EXECUTIVE_FOOD_ALLOWANCE,
|
|
Config::Payroll::EXECUTIVE_EMPLOYEE_PF_CONTRIBUTION,
|
|
Config::Payroll::EXECUTIVE_EMPLOYER_PF_CONTRIBUTION);
|
|
switch (employeeType)
|
|
{
|
|
case Enums::EmployeeType::IT:
|
|
employee = Factory::getObject<ITExecutive>(name, phone, email, payroll);
|
|
break;
|
|
case Enums::EmployeeType::FINANCE:
|
|
employee = Factory::getObject<FinanceExecutive>(name, phone, email, payroll);
|
|
break;
|
|
case Enums::EmployeeType::TEAM:
|
|
employee = Factory::getObject<TeamExecutive>(name, phone, email, payroll);
|
|
break;
|
|
case Enums::EmployeeType::TAG:
|
|
employee = Factory::getObject <TalentExecutive> (name, phone, email, payroll);
|
|
break;
|
|
}
|
|
break;
|
|
case Enums::EmployeeType::GENERAL:
|
|
util::enforceAuthorization(authenticatedEmployeeType, Enums::EmployeeType::ADMIN, Enums::EmployeeType::HR);
|
|
switch (employeeDesignation)
|
|
{
|
|
case Enums::EmployeeDesignation::JUNIOR:
|
|
payroll = Factory::getObject<Payroll>(Config::Payroll::JUNIOR_BASIC_SALARY,
|
|
Config::Payroll::JUNIOR_HOUSE_RENT_ALLOWANCE,
|
|
Config::Payroll::JUNIOR_FOOD_ALLOWANCE,
|
|
Config::Payroll::JUNIOR_EMPLOYEE_PF_CONTRIBUTION,
|
|
Config::Payroll::JUNIOR_EMPLOYER_PF_CONTRIBUTION);
|
|
break;
|
|
case Enums::EmployeeDesignation::SENIOR:
|
|
payroll = Factory::getObject<Payroll>(Config::Payroll::SENIOR_BASIC_SALARY,
|
|
Config::Payroll::SENIOR_HOUSE_RENT_ALLOWANCE,
|
|
Config::Payroll::SENIOR_FOOD_ALLOWANCE,
|
|
Config::Payroll::SENIOR_EMPLOYEE_PF_CONTRIBUTION,
|
|
Config::Payroll::SENIOR_EMPLOYER_PF_CONTRIBUTION);
|
|
break;
|
|
default:
|
|
throw std::runtime_error("Invalid General Employee Designation");
|
|
}
|
|
employee = Factory::getObject<GeneralEmployee>(name, phone, email, payroll, employeeDesignation);
|
|
break;
|
|
default:
|
|
throw std::runtime_error("Invalid Employee Type");
|
|
}
|
|
m_dataStore.getEmployees().emplace(std::make_pair(employee->getId(), employee));
|
|
}
|
|
|
|
bool EmployeeManagementService::deactivateEmployee(const std::string& id)
|
|
{
|
|
auto& authenticatedEmployee = m_dataStore.getAuthenticatedEmployee();
|
|
util::enforceAuthorization(authenticatedEmployee->getEmployeeType(), Enums::EmployeeType::ADMIN, Enums::EmployeeType::HR);
|
|
auto& employee = m_dataStore.getEmployees();
|
|
auto iterator = employee.find(id);
|
|
if (iterator == employee.end())
|
|
{
|
|
return false;
|
|
}
|
|
if (iterator->second->getEmployeeType() == Enums::EmployeeType::ADMIN)
|
|
{
|
|
throw std::runtime_error("Cannot deactivate Admin Account");
|
|
}
|
|
iterator->second->setEmployeeAccountStatus(Enums::AccountStatus::INACTIVE);
|
|
return true;
|
|
}
|
|
|
|
Employees EmployeeManagementService::getEmployees()
|
|
{
|
|
Employees result;
|
|
auto& employees = m_dataStore.getEmployees();
|
|
if (employees.size() <= 0)
|
|
{
|
|
return result;
|
|
}
|
|
for (const auto& iterator : employees)
|
|
{
|
|
if (iterator.second->getEmployeeAccountStatus() == Enums::AccountStatus::ACTIVE)
|
|
{
|
|
result.push_back(iterator.second);
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
std::shared_ptr<const Employee> EmployeeManagementService::getCurrentEmployee()
|
|
{
|
|
return m_dataStore.getAuthenticatedEmployee();
|
|
}
|
|
|
|
void EmployeeManagementService::updateProfile(const std::string& name,const std::string& phone)
|
|
{
|
|
std::shared_ptr<Employee> employee = m_dataStore.getAuthenticatedEmployee();
|
|
employee->setEmployeeName(name);
|
|
employee->setEmployeePhone(phone);
|
|
}
|
|
|
|
void EmployeeManagementService::loadEmployees()
|
|
{
|
|
FileManager<Employee> employeeFileManager(Config::File::EMPLOYEES_FILE);
|
|
FileManager<GeneralEmployee> generalEmployeeFileManager(Config::File::GENERAL_EMPLOYEES_FILE);
|
|
bool isAdminFound = false;
|
|
auto& employees = m_dataStore.getEmployees();
|
|
auto employeesMap = employeeFileManager.load();
|
|
auto generalEmployeesMap = generalEmployeeFileManager.load();
|
|
employees.insert(employeesMap.begin(), employeesMap.end());
|
|
employees.insert(generalEmployeesMap.begin(), generalEmployeesMap.end());
|
|
for (auto& employeePair : employees)
|
|
{
|
|
if (employeePair.second->getEmployeeType() == Enums::EmployeeType::ADMIN)
|
|
{
|
|
isAdminFound = true;
|
|
break;
|
|
}
|
|
}
|
|
if (!isAdminFound)
|
|
{
|
|
auto admin = Factory::getObject<Admin>("Admin", "", "admin@trenser.com", nullptr);
|
|
employees.emplace(std::make_pair(admin->getId(), admin));
|
|
}
|
|
}
|