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 "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();
|
||||
@@ -64,7 +25,7 @@ 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))
|
||||
if (employeeType != Enums::EmployeeType::GENERAL && util::hasActiveEmployeeOfType(employeeType, employees))
|
||||
{
|
||||
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");
|
||||
}
|
||||
if (isEmailDuplicate(email, employees))
|
||||
if (util::isEmailDuplicate(email, employees))
|
||||
{
|
||||
throw std::runtime_error("Duplicate Email");
|
||||
}
|
||||
if (isPhoneDuplicate(phone, employees))
|
||||
if (util::isPhoneDuplicate(phone, employees))
|
||||
{
|
||||
throw std::runtime_error("Duplicate Phone Number!");
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#include <string>
|
||||
#include <cctype>
|
||||
#include "Validator.h"
|
||||
#include "Employee.h"
|
||||
#include "ApplicationConfig.h"
|
||||
|
||||
bool util::isPhoneNumberValid(const std::string& phoneNumber) {
|
||||
@@ -69,3 +69,54 @@ bool util::isPasswordValid(const std::string& password)
|
||||
}
|
||||
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
|
||||
#include<string>
|
||||
#include<map>
|
||||
#include<memory>
|
||||
#include<vector>
|
||||
#include<algorithm>
|
||||
#include<cctype>
|
||||
#include "Enums.h"
|
||||
|
||||
class Employee;
|
||||
|
||||
namespace util
|
||||
{
|
||||
bool isPhoneNumberValid(const std::string&);
|
||||
bool isEmailValid(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,45 +9,59 @@
|
||||
#include "MenuHelper.h"
|
||||
#include "InputHelper.h"
|
||||
#include "OutputHelper.h"
|
||||
#include "Validator.h"
|
||||
#include "Enums.h"
|
||||
|
||||
void createEmployee(std::shared_ptr<ZenvyController> controller);
|
||||
|
||||
inline void updateProfile(std::shared_ptr<ZenvyController> m_zenvyController)
|
||||
{
|
||||
int choice;
|
||||
std::string name, phone;
|
||||
name = m_zenvyController->getCurrentEmployee()->getEmployeeName();
|
||||
phone = m_zenvyController->getCurrentEmployee()->getEmployeePhone();
|
||||
while (true)
|
||||
int choice;
|
||||
std::string name, phone;
|
||||
name = m_zenvyController->getCurrentEmployee()->getEmployeeName();
|
||||
phone = m_zenvyController->getCurrentEmployee()->getEmployeePhone();
|
||||
while (true)
|
||||
{
|
||||
util::clear();
|
||||
std::cout << "Please choose the information you want to update:\n"
|
||||
"1. Name\n"
|
||||
"2. Phone Number\n"
|
||||
"3. Exit\n"
|
||||
"Enter your choice: ";
|
||||
util::read(choice);
|
||||
switch (choice)
|
||||
{
|
||||
case 1:
|
||||
std::cout << "Enter your updated Name :";
|
||||
util::read(name);
|
||||
m_zenvyController->updateProfile(name, phone);
|
||||
std::cout << "Profile Updated Successfully\n";
|
||||
break;
|
||||
case 2:
|
||||
std::cout << "Enter your updated phone Number :";
|
||||
util::read(phone);
|
||||
m_zenvyController->updateProfile(name, phone);
|
||||
std::cout << "Profile Updated Successfully\n";
|
||||
break;
|
||||
case 3:
|
||||
return;
|
||||
default:
|
||||
std::cout << "Enter a valid choice!" << std::endl;
|
||||
break;
|
||||
}
|
||||
util::clear();
|
||||
std::cout << "Please choose the information you want to update:\n"
|
||||
"1. Name\n"
|
||||
"2. Phone Number\n"
|
||||
"3. Exit\n"
|
||||
"Enter your choice: ";
|
||||
util::read(choice);
|
||||
switch (choice)
|
||||
{
|
||||
case 1:
|
||||
std::cout << "Enter your updated Name: ";
|
||||
util::read(name);
|
||||
m_zenvyController->updateProfile(name, phone);
|
||||
std::cout << "Profile Updated Successfully\n";
|
||||
util::pressEnter();
|
||||
break;
|
||||
case 2:
|
||||
std::cout << "Enter your updated phone Number: ";
|
||||
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);
|
||||
std::cout << "Profile Updated Successfully\n";
|
||||
util::pressEnter();
|
||||
break;
|
||||
case 3:
|
||||
return;
|
||||
default:
|
||||
std::cout << "Enter a valid choice!" << std::endl;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user