Refactored validation logic and centralized employee validators
<SRS> SRS02 : Employee Management </SRS> <Changes> - Moved employee validation logic (active type check, email and phone duplication) to Validator utility - Updated service to use util::hasActiveEmployeeOfType, util::isEmailDuplicate, and util::isPhoneDuplicate - Integrated validator usage in MenuHelper for input validation - Cleaned up formatting and minor inconsistencies </Changes> <Review> Smitha Mohan </Review>
This commit is contained in:
@@ -14,45 +14,6 @@
|
|||||||
#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();
|
auto& employees = m_dataStore.getEmployees();
|
||||||
@@ -64,7 +25,7 @@ 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))
|
if (employeeType != Enums::EmployeeType::GENERAL && util::hasActiveEmployeeOfType(employeeType, employees))
|
||||||
{
|
{
|
||||||
throw std::runtime_error("Cannot create more than one employee of type " + Enums::getEmployeeTypeString(employeeType));
|
throw std::runtime_error("Cannot create more than one employee of type " + Enums::getEmployeeTypeString(employeeType));
|
||||||
}
|
}
|
||||||
@@ -76,11 +37,11 @@ void EmployeeManagementService::createEmployee(Enums::EmployeeType employeeType,
|
|||||||
{
|
{
|
||||||
throw std::runtime_error("Invalid Phone");
|
throw std::runtime_error("Invalid Phone");
|
||||||
}
|
}
|
||||||
if (isEmailDuplicate(email, employees))
|
if (util::isEmailDuplicate(email, employees))
|
||||||
{
|
{
|
||||||
throw std::runtime_error("Duplicate Email");
|
throw std::runtime_error("Duplicate Email");
|
||||||
}
|
}
|
||||||
if (isPhoneDuplicate(phone, employees))
|
if (util::isPhoneDuplicate(phone, employees))
|
||||||
{
|
{
|
||||||
throw std::runtime_error("Duplicate Phone Number!");
|
throw std::runtime_error("Duplicate Phone Number!");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
#include <string>
|
|
||||||
#include <cctype>
|
#include <cctype>
|
||||||
#include "Validator.h"
|
#include "Validator.h"
|
||||||
|
#include "Employee.h"
|
||||||
#include "ApplicationConfig.h"
|
#include "ApplicationConfig.h"
|
||||||
|
|
||||||
bool util::isPhoneNumberValid(const std::string& phoneNumber) {
|
bool util::isPhoneNumberValid(const std::string& phoneNumber) {
|
||||||
@@ -69,3 +69,54 @@ bool util::isPasswordValid(const std::string& password)
|
|||||||
}
|
}
|
||||||
return hasUpper && hasLower && hasDigit && hasSpecial;
|
return hasUpper && hasLower && hasDigit && hasSpecial;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool util::hasActiveEmployeeOfType(Enums::EmployeeType employeeType, const std::map<std::string, std::shared_ptr<Employee>> & employees)
|
||||||
|
{
|
||||||
|
for (const auto& employeePair : employees)
|
||||||
|
{
|
||||||
|
const auto& employee = employeePair.second;
|
||||||
|
if (employee->getEmployeeType() == employeeType && employee->getEmployeeAccountStatus() == Enums::AccountStatus::ACTIVE)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool util::isEmailDuplicate(const std::string& email, const std::map<std::string, std::shared_ptr<Employee>>& employees)
|
||||||
|
{
|
||||||
|
for (const auto& employeePair : employees)
|
||||||
|
{
|
||||||
|
const auto& employee = employeePair.second;
|
||||||
|
if (employee->getEmployeeEmail() == email)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool util::isPhoneDuplicate(const std::string& phone, const std::map<std::string, std::shared_ptr<Employee>>& employees)
|
||||||
|
{
|
||||||
|
for (const auto& employeePair : employees)
|
||||||
|
{
|
||||||
|
const auto& employee = employeePair.second;
|
||||||
|
if (employee->getEmployeePhone() == phone)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool util::isPhoneDuplicate(const std::string& phone, const std::vector<std::shared_ptr<const Employee>>& employees)
|
||||||
|
{
|
||||||
|
for (const auto& employee : employees)
|
||||||
|
{
|
||||||
|
if (employee->getEmployeePhone() == phone)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,11 +1,21 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include<string>
|
#include<string>
|
||||||
|
#include<map>
|
||||||
|
#include<memory>
|
||||||
|
#include<vector>
|
||||||
#include<algorithm>
|
#include<algorithm>
|
||||||
#include<cctype>
|
#include<cctype>
|
||||||
|
#include "Enums.h"
|
||||||
|
|
||||||
|
class Employee;
|
||||||
|
|
||||||
namespace util
|
namespace util
|
||||||
{
|
{
|
||||||
bool isPhoneNumberValid(const std::string&);
|
bool isPhoneNumberValid(const std::string&);
|
||||||
bool isEmailValid(const std::string&);
|
bool isEmailValid(const std::string&);
|
||||||
bool isPasswordValid(const std::string&);
|
bool isPasswordValid(const std::string&);
|
||||||
|
bool hasActiveEmployeeOfType(Enums::EmployeeType, const std::map<std::string, std::shared_ptr<Employee>>&);
|
||||||
|
bool isEmailDuplicate(const std::string&, const std::map<std::string, std::shared_ptr<Employee>>&);
|
||||||
|
bool isPhoneDuplicate(const std::string&, const std::map<std::string, std::shared_ptr<Employee>>&);
|
||||||
|
bool isPhoneDuplicate(const std::string&, const std::vector<std::shared_ptr<const Employee>>&);
|
||||||
}
|
}
|
||||||
@@ -9,6 +9,7 @@
|
|||||||
#include "MenuHelper.h"
|
#include "MenuHelper.h"
|
||||||
#include "InputHelper.h"
|
#include "InputHelper.h"
|
||||||
#include "OutputHelper.h"
|
#include "OutputHelper.h"
|
||||||
|
#include "Validator.h"
|
||||||
#include "Enums.h"
|
#include "Enums.h"
|
||||||
|
|
||||||
void createEmployee(std::shared_ptr<ZenvyController> controller);
|
void createEmployee(std::shared_ptr<ZenvyController> controller);
|
||||||
@@ -31,16 +32,29 @@ inline void updateProfile(std::shared_ptr<ZenvyController> m_zenvyController)
|
|||||||
switch (choice)
|
switch (choice)
|
||||||
{
|
{
|
||||||
case 1:
|
case 1:
|
||||||
std::cout << "Enter your updated Name :";
|
std::cout << "Enter your updated Name: ";
|
||||||
util::read(name);
|
util::read(name);
|
||||||
m_zenvyController->updateProfile(name, phone);
|
m_zenvyController->updateProfile(name, phone);
|
||||||
std::cout << "Profile Updated Successfully\n";
|
std::cout << "Profile Updated Successfully\n";
|
||||||
|
util::pressEnter();
|
||||||
break;
|
break;
|
||||||
case 2:
|
case 2:
|
||||||
std::cout << "Enter your updated phone Number :";
|
std::cout << "Enter your updated phone Number: ";
|
||||||
util::read(phone);
|
util::read(phone);
|
||||||
|
if (!util::isPhoneNumberValid(phone))
|
||||||
|
{
|
||||||
|
std::cout << "Error: Invalid Phone Number";
|
||||||
|
util::pressEnter();
|
||||||
|
}
|
||||||
|
if (util::isPhoneDuplicate(phone, m_zenvyController->getEmployees()))
|
||||||
|
{
|
||||||
|
std::cout << "Error: Duplicate Phone Number!";
|
||||||
|
util::pressEnter();
|
||||||
|
return;
|
||||||
|
}
|
||||||
m_zenvyController->updateProfile(name, phone);
|
m_zenvyController->updateProfile(name, phone);
|
||||||
std::cout << "Profile Updated Successfully\n";
|
std::cout << "Profile Updated Successfully\n";
|
||||||
|
util::pressEnter();
|
||||||
break;
|
break;
|
||||||
case 3:
|
case 3:
|
||||||
return;
|
return;
|
||||||
|
|||||||
Reference in New Issue
Block a user