Compare commits
8 Commits
dataFromEm
...
employeeDa
| Author | SHA1 | Date | |
|---|---|---|---|
| dadea504d0 | |||
| 1390427ec0 | |||
| 6c4e42e337 | |||
| f4d1ce1ab7 | |||
| ba307555f0 | |||
| 57ee1fbd72 | |||
| 63a7186464 | |||
| 4a8df33184 |
@@ -1,6 +1,8 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace App\Application;
|
namespace App\Application;
|
||||||
|
|
||||||
|
use phpDocumentor\Reflection\Types\Integer;
|
||||||
|
|
||||||
class EmployeeApplication{
|
class EmployeeApplication{
|
||||||
private $pdo;
|
private $pdo;
|
||||||
private $cryptographyService;
|
private $cryptographyService;
|
||||||
@@ -40,9 +42,9 @@ class EmployeeApplication{
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $firstName varbinary
|
* @param $firstName binary
|
||||||
* @param $middleName varbinary
|
* @param $middleName binary
|
||||||
* @param $lastName varbinary or null
|
* @param $lastName binary or null
|
||||||
* @param $birthDate date yyyy-mm-dd
|
* @param $birthDate date yyyy-mm-dd
|
||||||
* @param $email string
|
* @param $email string
|
||||||
* @param $phone string
|
* @param $phone string
|
||||||
@@ -157,6 +159,30 @@ class EmployeeApplication{
|
|||||||
return $response;
|
return $response;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $idEmployee
|
||||||
|
* @return Integer
|
||||||
|
*/
|
||||||
|
function getIdPersonByIdEmployee($idEmployee){
|
||||||
|
$stmt = $this->pdo->prepare("SELECT
|
||||||
|
COALESCE((SELECT
|
||||||
|
idPerson
|
||||||
|
FROM
|
||||||
|
employees
|
||||||
|
WHERE
|
||||||
|
id = :idEmployee),
|
||||||
|
0) AS id");
|
||||||
|
|
||||||
|
$stmt->execute(array(':idEmployee' => $idEmployee));
|
||||||
|
$results = $stmt->fetchAll();
|
||||||
|
if(!$results){
|
||||||
|
exit($this->databaseSelectQueryErrorMessage);
|
||||||
|
}
|
||||||
|
$stmt = null;
|
||||||
|
|
||||||
|
return $results[0]['id'];
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $code
|
* @param $code
|
||||||
* @return mixed
|
* @return mixed
|
||||||
@@ -241,7 +267,191 @@ class EmployeeApplication{
|
|||||||
);
|
);
|
||||||
|
|
||||||
return $response;
|
return $response;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $code string
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
function getEmployeeDataByCode($code){
|
||||||
|
$idEmployee = $this->getIdEmployeeTypeByCode($code);
|
||||||
|
|
||||||
|
return $this->proxyGetEmployeeDataById($idEmployee);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $idPerson integer
|
||||||
|
* @param $firstName binary
|
||||||
|
* @param $middleName binary
|
||||||
|
* @param $lastName binary
|
||||||
|
* @param $birthDate date
|
||||||
|
* @param $email binary
|
||||||
|
* @param $phone string
|
||||||
|
*/
|
||||||
|
function updatePerson($idPerson, $firstName, $middleName, $lastName, $birthDate, $email, $phone){
|
||||||
|
try {
|
||||||
|
$stmt = $this->pdo->prepare("UPDATE persons
|
||||||
|
SET
|
||||||
|
firstName = :firstName,
|
||||||
|
middleName = :middleName,
|
||||||
|
lastName = :lastName,
|
||||||
|
birthDate = :birthDate,
|
||||||
|
email = :email,
|
||||||
|
phone = :phone
|
||||||
|
WHERE
|
||||||
|
id = :idPerson");
|
||||||
|
$this->pdo->beginTransaction();
|
||||||
|
$stmt->execute(array(':firstName' => $firstName, ':middleName' => $middleName, ':lastName' => $lastName,
|
||||||
|
':birthDate' => $birthDate, ':email' => $email, ':phone' => $phone, ':idPerson' => $idPerson));
|
||||||
|
$this->pdo->commit();
|
||||||
|
|
||||||
|
$stmt = null;
|
||||||
|
} catch( PDOExecption $e ) {
|
||||||
|
$this->pdo->rollback();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $idEmployee integer
|
||||||
|
* @param $code string
|
||||||
|
* @param $idEmployeeType integer
|
||||||
|
* @param $contractType string
|
||||||
|
*/
|
||||||
|
function updateEmployee($idEmployee, $code, $idEmployeeType, $contractType){
|
||||||
|
try {
|
||||||
|
$stmt = $this->pdo->prepare("UPDATE employees
|
||||||
|
SET
|
||||||
|
idEmployeeType = :idEmployeeType,
|
||||||
|
code = :code,
|
||||||
|
contractType = :contractType
|
||||||
|
WHERE
|
||||||
|
id = :idEmployee");
|
||||||
|
$this->pdo->beginTransaction();
|
||||||
|
$stmt->execute(array(':idEmployeeType' => $idEmployeeType, ':code' => $code, ':contractType' => $contractType,
|
||||||
|
':idEmployee' => $idEmployee));
|
||||||
|
$this->pdo->commit();
|
||||||
|
|
||||||
|
$stmt = null;
|
||||||
|
} catch( PDOExecption $e ) {
|
||||||
|
$this->pdo->rollback();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $requestData object
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
function updateEmployeeData($requestData){
|
||||||
|
// Getting and validating the data
|
||||||
|
$idEmployee = $requestData['idEmployee'];
|
||||||
|
$idPerson = $this->getIdPersonByIdEmployee($idEmployee);
|
||||||
|
$code = $requestData['code'];
|
||||||
|
|
||||||
|
$firstName = $requestData['firstName'];
|
||||||
|
$this->asserts->firstName($firstName);
|
||||||
|
|
||||||
|
$middleName = $requestData['middleName'];
|
||||||
|
$this->asserts->middleName($middleName);
|
||||||
|
|
||||||
|
$lastName = isset($requestData['lastName']) ? $requestData['lastName'] : null;
|
||||||
|
|
||||||
|
$birthDate = $requestData['birthDate'];
|
||||||
|
$this->asserts->birthDate($birthDate);
|
||||||
|
|
||||||
|
$email = $requestData['email'];
|
||||||
|
$this->asserts->email($email);
|
||||||
|
|
||||||
|
$phone = $requestData['phone'];
|
||||||
|
$this->asserts->phone($phone);
|
||||||
|
|
||||||
|
$idEmployeeType = $requestData{'idEmployeeType'};
|
||||||
|
$contractType = $requestData{'contractType'};
|
||||||
|
|
||||||
|
// Encrypting the sensitive data
|
||||||
|
$securedFirstName = $this->cryptographyService->encryptString($firstName);
|
||||||
|
$securedMiddleName = $this->cryptographyService->encryptString($middleName);
|
||||||
|
|
||||||
|
if (isset($lastName)) {
|
||||||
|
$securedLastName = $this->cryptographyService->encryptString($lastName);
|
||||||
|
} else {
|
||||||
|
$securedLastName = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
$securedEmail = $this->cryptographyService->encryptString($email);
|
||||||
|
|
||||||
|
// Update process
|
||||||
|
$this->updatePerson($idPerson, $securedFirstName, $securedMiddleName, $securedLastName,
|
||||||
|
$birthDate, $securedEmail, $phone);
|
||||||
|
|
||||||
|
$this->updateEmployee($idEmployee, $code, $idEmployeeType, $contractType);
|
||||||
|
|
||||||
|
$response = array(
|
||||||
|
"fullName" => "$firstName $middleName $lastName",
|
||||||
|
"idEmployee" => $idEmployee,
|
||||||
|
"email" => $email,
|
||||||
|
"phone" => $phone,
|
||||||
|
"birthDate" => $birthDate,
|
||||||
|
"idEmployeeType" => $idEmployeeType,
|
||||||
|
"contractType" => $contractType
|
||||||
|
);
|
||||||
|
|
||||||
|
return $response;
|
||||||
|
}
|
||||||
|
|
||||||
|
function disableEmployeeRecord($idEmployee){
|
||||||
|
try {
|
||||||
|
$stmt = $this->pdo->prepare("UPDATE employees
|
||||||
|
SET
|
||||||
|
status = 'INACTIVE'
|
||||||
|
WHERE
|
||||||
|
id = :idEmployee");
|
||||||
|
$this->pdo->beginTransaction();
|
||||||
|
$stmt->execute(array(':idEmployee' => $idEmployee));
|
||||||
|
$this->pdo->commit();
|
||||||
|
|
||||||
|
$stmt = null;
|
||||||
|
} catch( PDOExecption $e ) {
|
||||||
|
$this->pdo->rollback();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Intended for internal use
|
||||||
|
*
|
||||||
|
* This method will bring a list of ids of all the employees that are
|
||||||
|
* currently active in the system
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
function getIdEmployeeFromAllActiveEmployees(){
|
||||||
|
$stmt = $this->pdo->prepare("SELECT
|
||||||
|
id
|
||||||
|
FROM
|
||||||
|
employees
|
||||||
|
WHERE
|
||||||
|
status = 'ACTIVE';");
|
||||||
|
$stmt->execute();
|
||||||
|
|
||||||
|
$results = $stmt->fetchAll();
|
||||||
|
|
||||||
|
if(!$results){
|
||||||
|
exit($this->databaseSelectQueryErrorMessage);
|
||||||
|
}
|
||||||
|
$stmt = null;
|
||||||
|
|
||||||
|
return $results;
|
||||||
|
}
|
||||||
|
|
||||||
|
function listAllActiveEmployees(){
|
||||||
|
$ids = $this->getIdEmployeeFromAllActiveEmployees();
|
||||||
|
|
||||||
|
$result = array();
|
||||||
|
|
||||||
|
foreach($ids as $row){
|
||||||
|
$result[] = $this->proxyGetEmployeeDataById($row['id']);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
?>
|
?>
|
||||||
@@ -41,6 +41,12 @@ $app->get('/api/employee/types', function (Request $request, Response $response,
|
|||||||
->write(json_encode($this->employeeApplication->listEmployeeTypes()));
|
->write(json_encode($this->employeeApplication->listEmployeeTypes()));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
$app->get('/api/employee/all', function (Request $request, Response $response, array $args) {
|
||||||
|
return $response->withStatus(200)
|
||||||
|
->withHeader('Content-Type', 'application/json')
|
||||||
|
->write(json_encode($this->employeeApplication->listAllActiveEmployees()));
|
||||||
|
});
|
||||||
|
|
||||||
$app->post('/api/employee', function ($request, $response) {
|
$app->post('/api/employee', function ($request, $response) {
|
||||||
$requestData = $request->getParsedBody();
|
$requestData = $request->getParsedBody();
|
||||||
|
|
||||||
@@ -49,6 +55,22 @@ $app->post('/api/employee', function ($request, $response) {
|
|||||||
->write(json_encode($this->employeeApplication->saveNewEmployee($requestData)));
|
->write(json_encode($this->employeeApplication->saveNewEmployee($requestData)));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
$app->put('/api/employee', function ($request, $response) {
|
||||||
|
$requestData = $request->getParsedBody();
|
||||||
|
|
||||||
|
return $response->withStatus(200)
|
||||||
|
->withHeader('Content-Type', 'application/json')
|
||||||
|
->write(json_encode($this->employeeApplication->updateEmployeeData($requestData)));
|
||||||
|
});
|
||||||
|
|
||||||
|
$app->DELETE('/api/employee/{idEmployee}', function (Request $request, Response $response, array $args) {
|
||||||
|
$idEmployee = $args['idEmployee'];
|
||||||
|
|
||||||
|
return $response->withStatus(200)
|
||||||
|
->withHeader('Content-Type', 'application/json')
|
||||||
|
->write(json_encode($this->employeeApplication->disableEmployeeRecord($idEmployee)));
|
||||||
|
});
|
||||||
|
|
||||||
$app->get('/api/employee/type/{code}', function (Request $request, Response $response, array $args) {
|
$app->get('/api/employee/type/{code}', function (Request $request, Response $response, array $args) {
|
||||||
$code = $args['code'];
|
$code = $args['code'];
|
||||||
|
|
||||||
@@ -57,10 +79,18 @@ $app->get('/api/employee/type/{code}', function (Request $request, Response $res
|
|||||||
->write(json_encode($this->employeeApplication->getIdEmployeeTypeByCode($code)));
|
->write(json_encode($this->employeeApplication->getIdEmployeeTypeByCode($code)));
|
||||||
});
|
});
|
||||||
|
|
||||||
$app->get('/api/employee/{idEmployee}', function (Request $request, Response $response, array $args) {
|
$app->get('/api/employee/id/{idEmployee}', function (Request $request, Response $response, array $args) {
|
||||||
$idEmployee = $args['idEmployee'];
|
$idEmployee = $args['idEmployee'];
|
||||||
|
|
||||||
return $response->withStatus(200)
|
return $response->withStatus(200)
|
||||||
->withHeader('Content-Type', 'application/json')
|
->withHeader('Content-Type', 'application/json')
|
||||||
->write(json_encode($this->employeeApplication->proxyGetEmployeeDataById($idEmployee)));
|
->write(json_encode($this->employeeApplication->proxyGetEmployeeDataById($idEmployee)));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
$app->get('/api/employee/code/{code}', function (Request $request, Response $response, array $args) {
|
||||||
|
$code = $args['code'];
|
||||||
|
|
||||||
|
return $response->withStatus(200)
|
||||||
|
->withHeader('Content-Type', 'application/json')
|
||||||
|
->write(json_encode($this->employeeApplication->getEmployeeDataByCode($code)));
|
||||||
|
});
|
||||||
@@ -43,7 +43,7 @@ return [
|
|||||||
|
|
||||||
// Employee settings
|
// Employee settings
|
||||||
'employee' => [
|
'employee' => [
|
||||||
'codeLength' => '5',
|
'codeLength' => '3',
|
||||||
],
|
],
|
||||||
],
|
],
|
||||||
];
|
];
|
||||||
|
|||||||
Reference in New Issue
Block a user