diff --git a/src/Controller/CAPI/CustomerWarrantyController.php b/src/Controller/CAPI/CustomerWarrantyController.php index 52b305c9..32b1ade5 100644 --- a/src/Controller/CAPI/CustomerWarrantyController.php +++ b/src/Controller/CAPI/CustomerWarrantyController.php @@ -14,6 +14,7 @@ use Catalyst\APIBundle\Controller\APIController; use Catalyst\APIBundle\Response\APIResponse; use App\Service\RisingTideGateway; +use App\Service\WarrantyAPILogger; use App\Entity\WarrantySerial; use App\Entity\Warranty; @@ -73,13 +74,14 @@ class CustomerWarrantyController extends APIController return $missing; } - protected function checkRequiredParams(Request $req, $params) + protected function checkRequiredParams(Request $req, $params, $logger, $log_data, $user_id) { // check required parameters $missing = $this->checkMissingParameters($req, $params); if (count($missing) > 0) { $miss_string = implode(', ', $missing); + $logger->logWarrantyInfo($log_data, 'Missing parameter(s): ' . $miss_string, $user_id); return new APIResponse(false, 'Missing parameter(s): ' . $miss_string); } @@ -100,12 +102,20 @@ class CustomerWarrantyController extends APIController { // check required parameters $required_params = []; - $res = $this->checkRequiredParams($req, $required_params); + $res = $this->checkRequiredParams($req, $required_params, $logger, $log_data, $user_id); if (!$res) return $res; error_log('check warranty serial'); + //$keys = array_keys($_SERVER); + //foreach ($keys as $key) + //{ + // error_log($key); + //} + + //error_log($_SERVER['HTTP_X_CATA_API_KEY']); + // check if warranty serial is there $warr_serial = $em->getRepository(WarrantySerial::class)->find($serial); $warr = $em->getRepository(Warranty::class)->findOneBy(['serial' => $serial]); @@ -279,20 +289,36 @@ class CustomerWarrantyController extends APIController } - public function register($serial, EntityManagerInterface $em, Request $req, KernelInterface $kernel, RisingTideGateway $rt, TranslatorInterface $trans) + public function register($serial, EntityManagerInterface $em, Request $req, KernelInterface $kernel, RisingTideGateway $rt, TranslatorInterface $trans, + WarrantyAPILogger $logger) { error_log('HERE - register'); // check required parameters // TODO: maybe add vmake_id? since warranty cannot be created with no vmake // TODO: maye also add mobile and email since customer creation won't let mobile and email be null + // set up information for logging + // get user from header + $user_id = $_SERVER['HTTP_X_CATA_API_KEY']; + $log_data = [ + 'serial' => $serial, + 'plate_number' => $req->request->get('plate_num'), + 'first_name' => $req->request->get('first_name'), + 'last_name' => $req->request->get('last_name'), + 'vmake_id' => $req->request->get('vmake_id'), + 'contact_number' => $req->request->get('contact_num'), + 'email' => $req->request->get('email'), + 'invoice' => $req->request->get('invoice'), + ]; + $required_params = [ 'first_name', 'last_name', 'plate_num' ]; - $res = $this->checkRequiredParams($req, $required_params); - if (!$res) - return $res; + $res = $this->checkRequiredParams($req, $required_params, $logger, $log_data, $user_id); + + if (!$res) + return $res; // file uploads $invoice = $req->files->get('invoice'); @@ -305,12 +331,12 @@ class CustomerWarrantyController extends APIController $wcard_filename = $this->handlePictureUpload($warr_card, $upload_dir, $serial, 'wcard'); // do actual registering - $res = $this->updateWarranty($em, $rt, $trans, $req, $serial, $inv_filename, $wcard_filename); + $res = $this->updateWarranty($em, $rt, $trans, $req, $serial, $inv_filename, $wcard_filename, $logger, $log_data, $user_id); // flush to db $em->flush(); - return $res; + return $res; return new APIResponse(true, 'Warranty registered.'); } @@ -346,7 +372,7 @@ class CustomerWarrantyController extends APIController return $serial . '/' . $filename; } - protected function updateWarranty($em, $rt, $trans, $req, $serial, $inv_filename = null, $wcard_filename = null) + protected function updateWarranty($em, $rt, $trans, $req, $serial, $inv_filename = null, $wcard_filename = null, $logger, $log_data, $user_id) { $plate_num = $this->cleanPlateNumber($req->request->get('plate_num')); @@ -355,6 +381,7 @@ class CustomerWarrantyController extends APIController $warr_serial = $em->getRepository(WarrantySerial::class)->find($serial); if ($warr_serial == null) { + $logger->logWarrantyInfo($log_data, 'Invalid warranty serial code..', $user_id); return new APIResponse(false, 'Invalid warranty serial code.'); } @@ -370,6 +397,7 @@ class CustomerWarrantyController extends APIController if ($plate_num != $warr_plate_num) { + $logger->logWarrantyInfo($log_data, 'Plate number does not match vehicle registered to warranty.', $user_id); return new APIResponse(false, 'Plate number does not match vehicle registered to warranty.'); } @@ -393,10 +421,11 @@ class CustomerWarrantyController extends APIController if ($sku != null) { $sap_bty = $em->getRepository(SAPBattery::class)->find($sku); - //if ($sap_bty == null) - //{ - // return new APIResponse(false, 'Could not find battery entry for warranty.'); - //} + if ($sap_bty == null) + { + $logger->logWarrantyInfo($log_data, 'Cound not find battery entry for warranty.', $user_id); + return new APIResponse(false, 'Could not find battery entry for warranty.'); + } } // vehicle fetch @@ -407,6 +436,7 @@ class CustomerWarrantyController extends APIController $vehicle = $em->getRepository(Vehicle::class)->find($vmake_id); if ($vehicle == null) { + $logger->logWarrantyInfo($log_data, 'Could not find vehicle specified for warranty.', $user_id); return new APIResponse(false, 'Could not find vehicle specified for warranty.'); } } @@ -423,6 +453,7 @@ class CustomerWarrantyController extends APIController $date_pur_cust = DateTime::createFromFormat('Y-m-d', $req->request->get('date_purchase')); if (!$date_pur_cust) { + $logger->logWarrantyInfo($log_data, 'Invalid date format for date of purchase.', $user_id); return new APIResponse(false, 'Invalid date format for date of purchase.'); } } @@ -499,14 +530,15 @@ class CustomerWarrantyController extends APIController $em->persist($warr); + $logger->logWarrantyInfo($log_data, '', $user_id); + // TODO: check if we need to do anyting else $data = []; - // send sms confirmation $this->sendSMSConfirmation($rt, $req->request->get('contact_num'), $sms_message); - return new APIResponse(true, 'Warranty registered.', $data); + return new APIResponse(true, 'Warranty registered.', $data); } protected function sendSMSConfirmation($rt, $num, $message) diff --git a/src/Controller/CAPI/WarrantyController.php b/src/Controller/CAPI/WarrantyController.php index e88f7259..b72148b8 100644 --- a/src/Controller/CAPI/WarrantyController.php +++ b/src/Controller/CAPI/WarrantyController.php @@ -186,7 +186,7 @@ class WarrantyController extends APIController 'sku' => $sku, 'first_name' => $fname, 'last_name' => $lname, - 'moblie_number' => $mnum, + 'mobile_number' => $mnum, ]; $msg = $this->checkRequiredParameters($req, $params); @@ -296,7 +296,7 @@ class WarrantyController extends APIController return new APIResponse(true, 'Warranty registered.', $data); } - public function claim(Request $req, EntityManagerInterface $em, $id) + public function claim(Request $req, EntityManagerInterface $em, $id, WarrantyAPILogger $logger) { $this->denyAccessUnlessGranted('warranty.claim', null, 'No access.'); @@ -304,26 +304,48 @@ class WarrantyController extends APIController $params = [ 'serial', ]; + + $serial = $req->request->get('serial'); + + // set up information for logging + // get user from header + $user_id = $_SERVER['HTTP_X_CATA_API_KEY']; + $log_data = [ + 'serial' => $serial, + 'id' => $id, + ]; + $msg = $this->checkRequiredParameters($req, $params); if ($msg) + { + $logger->logWarrantyInfo($log_data, $msg, $user_id); return new APIResponse(false, $msg); + } // no warranty $warr = $em->getRepository(Warranty::class)->find($id); if ($warr == null) + { + $logger->logWarrantyInfo($log_data, 'No warranty found with that id.', $user_id); return new APIResponse(false, 'No warranty found with that id.', null, 404); + } // warranty is not active if (!$warr->canClaim()) + { + $logger->logWarrantyInfo($log_data, 'Warranty is not active.', $user_id); return new APIResponse(false, 'Warranty is not active.'); + } // check if new serial has been used - $serial = $req->request->get('serial'); $clean_serial = $this->cleanSerial($serial); $check_warr = $em->getRepository(Warranty::class)->findOneBy(['serial' => $clean_serial]); if ($check_warr != null) + { + $logger->logWarrantyInfo($log_data, 'Serial for replacement has already been used.', $user_id); return new APIResponse(false, 'Serial for replacement has already been used.'); + } // set status to claim $warr->setStatus(WarrantyStatus::CLAIMED) @@ -349,6 +371,7 @@ class WarrantyController extends APIController $em->flush(); // TODO: claim log + $logger->logWarrantyInfo($log_data, '', $user_id); return new APIResponse(true, 'Warranty claimed successfully.'); } @@ -744,26 +767,4 @@ class WarrantyController extends APIController $customers = $em->getRepository(Customer::class)->findBy(['phone_mobile' => $number]); 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(); - - } - }