diff --git a/config/services.yaml b/config/services.yaml index 29231ef0..dea32a8b 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -284,3 +284,8 @@ services: App\Service\HubFilterLogger: arguments: $em: "@doctrine.orm.entity_manager" + + # warranty api logger + App\Service\WarrantyAPILogger: + arguments: + $em: "@doctrine.orm.entity_manager" diff --git a/src/Controller/CAPI/WarrantyController.php b/src/Controller/CAPI/WarrantyController.php index 31dbec58..e88f7259 100644 --- a/src/Controller/CAPI/WarrantyController.php +++ b/src/Controller/CAPI/WarrantyController.php @@ -20,6 +20,9 @@ use App\Entity\PrivacyPolicy; use App\Entity\Customer; use App\Entity\CustomerVehicle; use App\Entity\Vehicle; +use App\Entity\WarrantyAPILog; + +use App\Service\WarrantyAPILogger; use App\Ramcar\NameValue; use App\Ramcar\WarrantyClass; @@ -142,7 +145,7 @@ class WarrantyController extends APIController return new APIResponse(true, 'Warranties found.', $data); } - public function register(Request $req, EntityManagerInterface $em) + public function register(Request $req, EntityManagerInterface $em, WarrantyAPILogger $logger) { $this->denyAccessUnlessGranted('warranty.register.battery', null, 'No access.'); @@ -159,10 +162,6 @@ class WarrantyController extends APIController 'battery_size_id', */ ]; - $msg = $this->checkRequiredParameters($req, $params); - error_log('msg - ' . $msg); - if ($msg) - return new APIResponse(false, $msg); $serial = $req->request->get('serial'); $date_expire_string = $req->request->get('date_expire'); @@ -175,6 +174,29 @@ class WarrantyController extends APIController $lname = $req->request->get('last_name', null); $mnum = $req->request->get('mobile_number', null); + // set up information for logging + // get user from header + $user_id = $_SERVER['HTTP_X_CATA_API_KEY']; + $log_data = [ + 'serial' => $serial, + 'date_expire' => $date_expire_string, + 'date_pur_string' => $date_pur_string, + 'warranty_class' => $warr_class, + 'plate_number' => $plate, + 'sku' => $sku, + 'first_name' => $fname, + 'last_name' => $lname, + 'moblie_number' => $mnum, + ]; + + $msg = $this->checkRequiredParameters($req, $params); + error_log('msg - ' . $msg); + if ($msg) + { + $logger->logWarrantyInfo($log_data, $msg, $user_id); + return new APIResponse(false, $msg); + } + /* $bmodel_id = $req->request->get('battery_model_id'); $bsize_id = $req->request->get('battery_size_id'); @@ -183,21 +205,33 @@ class WarrantyController extends APIController // wrong date expire format $date_expire = DateTime::createFromFormat('Ymd', $date_expire_string); if ($date_expire === false) + { + $logger->logWarrantyInfo($log_data, 'Wrong date format: date_expire.', $user_id); return new APIResponse(false, 'Wrong date format: date_expire.'); + } // wrong date purchase format $date_pur = DateTime::createFromFormat('Ymd', $date_pur_string); if ($date_pur === false) + { + $logger->logWarrantyInfo($log_data, 'Wrong date format: date_purchase', $user_id); return new APIResponse(false, 'Wrong date format: date_purchase.'); + } // valid warranty class if (!WarrantyClass::validate($warr_class)) + { + $logger->logWarrantyInfo($log_data, 'Invalid warranty class.', $user_id); return new APIResponse(false, 'Invalid warranty class.'); + } // plate number $plate = Warranty::cleanPlateNumber($plate); if (!$plate) + { + $logger->logWarrantyInfo($log_data, 'Invalid plate number.', $user_id); return new APIResponse(false, 'Invalid plate number.'); + } // check if sku is blank if ((empty($sku)) || ($sku == null)) @@ -207,7 +241,10 @@ class WarrantyController extends APIController // battery $batt = $em->getRepository(SAPBattery::class)->find($sku); if ($batt == null) + { + $logger->logWarrantyInfo($log_data, 'Invalid battery SKU.', $user_id); return new APIResponse(false, 'Invalid battery SKU.'); + } } /* @@ -253,6 +290,9 @@ class WarrantyController extends APIController 'warranty' => $this->generateWarrantyData($warr), ]; + // log creation of warranty data + $logger->logWarrantyInfo($log_data, '', $user_id); + return new APIResponse(true, 'Warranty registered.', $data); } @@ -705,4 +745,25 @@ class WarrantyController extends APIController return $customers; } + protected function logWarrantyInfo(EntityManagerInterface $em, $log_data, $error) + { + // get user from header + $user_id = $_SERVER['HTTP_X_CATA_API_KEY']; + + $log_entry = new WarrantyAPILog(); + + $err_aray = []; + $err_array[] = $error; + + $json_data = json_encode($log_data); + + $log_entry->setApiUser($user_id) + ->setErrors($err_array) + ->setAllData($json_data); + + $em->persist($log_entry); + $em->flush(); + + } + } diff --git a/src/Entity/WarrantyAPILog.php b/src/Entity/WarrantyAPILog.php new file mode 100644 index 00000000..d9106db8 --- /dev/null +++ b/src/Entity/WarrantyAPILog.php @@ -0,0 +1,127 @@ +date_create = new DateTime(); + $this->errors = new ArrayCollection(); + $this->all_data = []; + } + + public function getID() + { + return $this->id; + } + + public function setDateCreate(DateTime $date_create) + { + $this->date_create = $date_create; + return $this; + } + + public function getDateCreate() + { + return $this->date_create; + } + + public function setApiUser($api_user) + { + $this->api_user = $api_user; + return $this; + } + + public function getApiUser() + { + return $this->api_user; + } + + public function addAllData($id, $value) + { + $this->all_data[$id] = $value; + return $this; + } + + public function setAllData($all_data) + { + $this->all_data = $all_data; + return $this; + } + + public function getAllData($id) + { + // return null if we don't have it + if (!isset($this->all_data[$id])) + return null; + + return $this->all_data[$id]; + } + + public function getErrors() + { + return $this->errors; + } + + public function setErrors(array $errors) + { + $this->errors = new ArrayCollection(); + + foreach ($errors as $error) + { + $this->errors->add($error); + } + + return $this; + } + + public function clearErrors() + { + $this->errors = new ArrayCollection(); + return $this; + } +} diff --git a/src/Service/WarrantyAPILogger.php b/src/Service/WarrantyAPILogger.php new file mode 100644 index 00000000..ff989a6f --- /dev/null +++ b/src/Service/WarrantyAPILogger.php @@ -0,0 +1,35 @@ +em = $em; + } + + public function logWarrantyInfo($log_data, $error, $user_id) + { + $log_entry = new WarrantyAPILog(); + + $err_aray = []; + $err_array[] = $error; + + $json_data = json_encode($log_data); + + $log_entry->setApiUser($user_id) + ->setErrors($err_array) + ->setAllData($json_data); + + $this->em->persist($log_entry); + $this->em->flush(); + + } +}