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:
@@ -13,8 +13,48 @@
|
||||
#include "FileManager.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)
|
||||
{
|
||||
auto& employees = m_dataStore.getEmployees();
|
||||
std::shared_ptr<Employee> authenticatedEmployee = m_dataStore.getAuthenticatedEmployee();
|
||||
if (!authenticatedEmployee)
|
||||
{
|
||||
@@ -23,6 +63,10 @@ void EmployeeManagementService::createEmployee(Enums::EmployeeType employeeType,
|
||||
Enums::EmployeeType authenticatedEmployeeType = authenticatedEmployee->getEmployeeType();
|
||||
std::shared_ptr<Employee> employee;
|
||||
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))
|
||||
{
|
||||
throw std::runtime_error("Invalid Email");
|
||||
@@ -31,6 +75,14 @@ void EmployeeManagementService::createEmployee(Enums::EmployeeType employeeType,
|
||||
{
|
||||
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)
|
||||
{
|
||||
case Enums::EmployeeType::HR:
|
||||
@@ -42,7 +94,6 @@ void EmployeeManagementService::createEmployee(Enums::EmployeeType employeeType,
|
||||
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:
|
||||
|
||||
Reference in New Issue
Block a user