Added validation checks for employee creation

<SRS> SRS02 : Employee Management </SRS>

<Changes>
 - Added helper function to check for existing active employee of a given type
 - Enforced business rule to allow only one active employee per non-general type
 - Added duplicate email validation before employee creation
 - Added duplicate phone number validation before employee creation
</Changes>

<Review>
Smitha Mohan
</Review>
This commit is contained in:
2026-04-10 16:43:28 +05:30
parent 03be8f81d2
commit eac6fa72df
@@ -13,8 +13,48 @@
#include "FileManager.h" #include "FileManager.h"
#include "ApplicationConfig.h" #include "ApplicationConfig.h"
static bool hasActiveEmployeeOfType(Enums::EmployeeType employeeType, const employeeMap& employees)
{
for (const auto& employeePair : employees)
{
const auto& employee = employeePair.second;
if (employee->getEmployeeType() == employeeType && employee->getEmployeeAccountStatus() == Enums::AccountStatus::ACTIVE)
{
return true;
}
}
return false;
}
static bool isEmailDuplicate(const std::string& email, const employeeMap& employees)
{
for (const auto& employeePair : employees)
{
const auto& employee = employeePair.second;
if (employee->getEmployeeEmail() == email)
{
return true;
}
}
return false;
}
static bool isPhoneDuplicate(const std::string& phone, const employeeMap& employees)
{
for (const auto& employeePair : employees)
{
const auto& employee = employeePair.second;
if (employee->getEmployeePhone() == phone)
{
return true;
}
}
return false;
}
void EmployeeManagementService::createEmployee(Enums::EmployeeType employeeType, Enums::EmployeeDesignation employeeDesignation, const std::string& email, const std::string& name, const std::string& phone) void EmployeeManagementService::createEmployee(Enums::EmployeeType employeeType, Enums::EmployeeDesignation employeeDesignation, const std::string& email, const std::string& name, const std::string& phone)
{ {
auto& employees = m_dataStore.getEmployees();
std::shared_ptr<Employee> authenticatedEmployee = m_dataStore.getAuthenticatedEmployee(); std::shared_ptr<Employee> authenticatedEmployee = m_dataStore.getAuthenticatedEmployee();
if (!authenticatedEmployee) if (!authenticatedEmployee)
{ {
@@ -23,6 +63,10 @@ void EmployeeManagementService::createEmployee(Enums::EmployeeType employeeType,
Enums::EmployeeType authenticatedEmployeeType = authenticatedEmployee->getEmployeeType(); Enums::EmployeeType authenticatedEmployeeType = authenticatedEmployee->getEmployeeType();
std::shared_ptr<Employee> employee; std::shared_ptr<Employee> employee;
std::shared_ptr<Payroll> payroll; std::shared_ptr<Payroll> payroll;
if (employeeType != Enums::EmployeeType::GENERAL && hasActiveEmployeeOfType(employeeType, employees))
{
throw std::runtime_error("Cannot create more than one employee of type " + Enums::getEmployeeTypeString(employeeType));
}
if (!util::isEmailValid(email)) if (!util::isEmailValid(email))
{ {
throw std::runtime_error("Invalid Email"); throw std::runtime_error("Invalid Email");
@@ -31,6 +75,14 @@ void EmployeeManagementService::createEmployee(Enums::EmployeeType employeeType,
{ {
throw std::runtime_error("Invalid Phone"); throw std::runtime_error("Invalid Phone");
} }
if (isEmailDuplicate(email, employees))
{
throw std::runtime_error("Duplicate Email");
}
if (isPhoneDuplicate(phone, employees))
{
throw std::runtime_error("Duplicate Phone Number!");
}
switch (employeeType) switch (employeeType)
{ {
case Enums::EmployeeType::HR: case Enums::EmployeeType::HR:
@@ -42,7 +94,6 @@ void EmployeeManagementService::createEmployee(Enums::EmployeeType employeeType,
Config::Payroll::HR_MANAGER_EMPLOYER_PF_CONTRIBUTION); Config::Payroll::HR_MANAGER_EMPLOYER_PF_CONTRIBUTION);
employee = Factory::getObject<HRManager>(name, phone, email, payroll); employee = Factory::getObject<HRManager>(name, phone, email, payroll);
break; break;
case Enums::EmployeeType::IT: case Enums::EmployeeType::IT:
case Enums::EmployeeType::FINANCE: case Enums::EmployeeType::FINANCE:
case Enums::EmployeeType::TEAM: case Enums::EmployeeType::TEAM: