From 69b636620af74a4055805908fcc2761945242154 Mon Sep 17 00:00:00 2001 From: Jose Pabl Domingo Aramburo Sanchez Date: Mon, 6 Aug 2018 00:53:03 -0600 Subject: [PATCH] [add] Saving person as employee --- .../src/application/EmployeeApplication.php | 63 +++++++++++++++++-- .../src/service/CryptographyService.php | 14 +++++ database/database.sql | 5 +- 3 files changed, 74 insertions(+), 8 deletions(-) diff --git a/api-payroll/src/application/EmployeeApplication.php b/api-payroll/src/application/EmployeeApplication.php index ce52e18..46e193a 100644 --- a/api-payroll/src/application/EmployeeApplication.php +++ b/api-payroll/src/application/EmployeeApplication.php @@ -31,6 +31,15 @@ class EmployeeApplication{ return $results; } + /** + * @param $firstName varbinary + * @param $middleName varbinary + * @param $lastName varbinary or null + * @param $birthDate date yyyy-mm-dd + * @param $email string + * @param $phone string + * @return integer + */ function saveNewPerson($firstName, $middleName, $lastName, $birthDate, $email, $phone){ $this->asserts->firstName($firstName); $this->asserts->middleName($middleName); @@ -57,6 +66,37 @@ class EmployeeApplication{ } } + /** + * @param $idEmployeeType integer + * @param $idPerson integer + * @param $code string + * @param $contractType string + * @return mixed + */ + function savePersonAsEmployee($idEmployeeType, $idPerson, $code, $contractType){ + try { + $stmt = $this->pdo->prepare("INSERT INTO employees (idEmployeeType, idPerson, code, contractType) + VALUES (:idEmployeeType, :idPerson, :code, :contractType)"); + $this->pdo->beginTransaction(); + $stmt->execute(array(':idEmployeeType' => $idEmployeeType, ':idPerson' => $idPerson, ':code' => $code, + ':contractType' => $contractType)); + $id = $this->pdo->lastInsertId(); + $this->pdo->commit(); + + return $id; + + $stmt = null; + } catch( PDOExecption $e ) { + $this->pdo->rollback(); + throw new Exception('There was an error while trying to save a new employee.'); + $this->logger->warning("There was an error in the EmployeeApplication->savePersonAsEmployee caused by: $e "); + } + } + + /** + * @param $requestData object + * @return array + */ function saveNewEmployee($requestData){ // Getting and validating the data $firstName = $requestData['firstName']; @@ -76,26 +116,37 @@ class EmployeeApplication{ $phone = $requestData['phone']; $this->asserts->phone($phone); - $employeeType = $requestData{'employeeType'}; + $idEmployeeType = $requestData{'idEmployeeType'}; $contractType = $requestData{'contractType'}; // Encrypting the sensitive data $securedFirstName = $this->cryptographyService->encryptString($firstName); $securedMiddleName = $this->cryptographyService->encryptString($middleName); - if(isset($lastName)){ + if (isset($lastName)) { $securedLastName = $this->cryptographyService->encryptString($lastName); - } - else { + } else { $securedLastName = null; } $securedEmail = $this->cryptographyService->encryptString($email); - $idNewperson = $this->saveNewPerson($securedFirstName, $securedMiddleName, $securedLastName, + // Here begins the saving process + $idNewPerson = $this->saveNewPerson($securedFirstName, $securedMiddleName, $securedLastName, $birthDate, $securedEmail, $phone); - return $idNewperson; + $employeeCode = $this->cryptographyService->pseudoRandomStringOpenssl(10); + $idEmployee = $this->savePersonAsEmployee($idEmployeeType, $idNewPerson, $employeeCode, $contractType); + + $response = array( + "fullName" => "$firstName $middleName $lastName", + "employeeCode" => $employeeCode, + "idEmployee" => $idEmployee, + "email" => $email, + "phone" => $phone + ); + + return $response; } } ?> \ No newline at end of file diff --git a/api-payroll/src/service/CryptographyService.php b/api-payroll/src/service/CryptographyService.php index 41e3e5d..95bec62 100644 --- a/api-payroll/src/service/CryptographyService.php +++ b/api-payroll/src/service/CryptographyService.php @@ -85,4 +85,18 @@ class CryptographyService{ function decryptPassword($plainPassword, $encryptedPassword) { return password_verify($plainPassword, $encryptedPassword); } + + /** + * Generates a psudo random string using openssl + * + * @param $length integer + * @return string + */ + function pseudoRandomStringOpenssl($length){ + + $string = openssl_random_pseudo_bytes($length); + $string = bin2hex($string); + + return substr($string, 0, $length); + } } \ No newline at end of file diff --git a/database/database.sql b/database/database.sql index 4effb71..c2f1084 100644 --- a/database/database.sql +++ b/database/database.sql @@ -64,11 +64,12 @@ DROP TABLE IF EXISTS employees; CREATE TABLE IF NOT EXISTS `employees` ( `id` INT UNSIGNED NOT NULL AUTO_INCREMENT, `idEmployeeType` INT UNSIGNED NOT NULL comment 'Defines the rol within the company', - `numero` VARCHAR(100) NOT NULL comment 'A code to reference the employee', + `idPerson` INT UNSIGNED NOT NULL comment 'Defines the rol within the company', + `code` VARCHAR(100) NOT NULL comment 'A code to reference the employee', `contractType` ENUM('INTERNO', 'EXTERNO') NOT NULL comment 'The type of contract', `status` ENUM('ACTIVE', 'INACTIVE') NOT NULL DEFAULT 'ACTIVE', `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP comment 'The date on which the registry was created', `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP comment 'The date of the last time the row was modified', PRIMARY KEY (`id`), - UNIQUE (`name`) + UNIQUE (`code`) );