diff --git a/config/api_acl.yaml b/config/api_acl.yaml index cd1f9ae0..2da7b9df 100644 --- a/config/api_acl.yaml +++ b/config/api_acl.yaml @@ -162,3 +162,12 @@ access_keys: label: Get Nearest Hub and Slots - id: mobile_jo.schedule_option.status label: Schedule Option Status + - id: mobile_warranty + label: Mobile Warranty Access + acls: + - id: mobile_warranty.register.serial + label: Register Warranty Serial + - id: mobile_warranty.check + label: Check Warranty Serial + - id: mobile_warranty.activate + label: Activate Warranty diff --git a/src/Controller/ResqAPI/WarrantyController.php b/src/Controller/ResqAPI/WarrantyController.php index 95322264..10c17a14 100644 --- a/src/Controller/ResqAPI/WarrantyController.php +++ b/src/Controller/ResqAPI/WarrantyController.php @@ -11,10 +11,8 @@ use Doctrine\ORM\Query; use Doctrine\ORM\EntityManagerInterface; use Catalyst\APIBundle\Controller\APIController; -// TODO: what do we use for response? APIResponse or APIResult? -// APIResult is what is used by APIController. APIResponse is what is used by CAPI use Catalyst\APIBundle\Response\APIResponse; -use App\Ramcar\APIResult; + use App\Ramcar\WarrantySource; use App\Entity\Warranty; @@ -24,6 +22,7 @@ use App\Entity\CustomerVehicle; use App\Service\RisingTideGateway; use App\Service\WarrantyAPILogger; +use App\Service\MobileAPIHandler; use Catalyst\APIBundle\Access\Generator as ACLGenerator; @@ -39,9 +38,11 @@ class WarrantyController extends APIController } public function warrantyRegister($serial, EntityManagerInterface $em, Request $req, KernelInterface $kernel, RisingTideGateway $rt, - TranslatorInterface $trans, WarrantyAPILogger $logger) + TranslatorInterface $trans, WarrantyAPILogger $logger, MobileAPIHandler $mah) { - // check required parameters and api key + $this->denyAccessUnlessGranted('mobile_jo.request', null, 'No access.'); + + // check required parameters $required_params = [ 'first_name', 'last_name', @@ -61,7 +62,16 @@ class WarrantyController extends APIController $inv_filename = $this->handlePictureUpload($invoice, $upload_dir, $serial, 'invoice'); $wcard_filename = $this->handlePictureUpload($warr_card, $upload_dir, $serial, 'wcard'); - $user_id = $req->query->get('api_key'); + // get capi user to link to mobile user + $capi_user_id = $this->getUser()->getID(); + + // get mobile user + $mobile_user = $mah->findMobileUser($capi_user_id); + if ($mobile_user == null) + return new APIResponse(false, 'No mobile user found.'); + + $user_id = $mobile_user->getID(); + $log_data = [ 'plate_number' => $req->request->get('plate_num'), 'first_name' => $req->request->get('first_name'), @@ -71,27 +81,29 @@ class WarrantyController extends APIController $action = 'create'; $source = WarrantySource::MOBILE; - $res = $this->checkParamsAndKey($req, $em, $required_params); - if ($res->isError()) + $msg = $this->checkRequiredParameters($req, $required_params); + if ($msg) { - $logger->logWarrantyInfo($log_data, $res->getErrorMessage(), $user_id, $action, $source); - return $res->getReturnResponse(); + $logger->logWarrantyInfo($log_data, $msg, $user_id, $action, $source); + return new APIResponse(false, $msg); } // update customer information // $cust = $this->updateCustomerInfo($req, $em); // update warranty - $res = $this->updateWarranty($res, $em, $rt, $trans, $req, $serial, $inv_filename, $wcard_filename, - $logger, $log_data, $user_id, $action, $source); + $data = []; + $msg = $this->updateWarranty($data, $em, $rt, $trans, $req, $serial, $inv_filename, $wcard_filename, + $logger, $log_data, $user_id, $action, $source, $mobile_user); + if ($msg != null) + return new APIResponse(false, $msg); $em->flush(); - return $res->getReturnResponse(); + return new APIResponse(true, 'Warranty registered', $data); } - // TODO: needs to be modified for mobile user - public function warrantyCheck($serial, EntityManagerInterface $em, Request $req) + public function warrantyCheck($serial, EntityManagerInterface $em, Request $req, MobileAPIHandler $mah) { // check required parameters and api key $required_params = []; @@ -332,18 +344,16 @@ class WarrantyController extends APIController return $serial . '/' . $filename; } - // TODO: needs to be modified for mobile user - protected function updateWarranty($res, $em, $rt, $trans, $req, $serial, $inv_filename = null, $wcard_filename = null, - $logger, $log_data, $user_id, $action, $source) + protected function updateWarranty($data, $em, $rt, $trans, $req, $serial, $inv_filename = null, $wcard_filename = null, + $logger, $log_data, $user_id, $action, $source, $mobile_user) { // get serial $warr_serial = $em->getRepository(WarrantySerial::class)->find($serial); if ($warr_serial == null) { - $res->setError(true) - ->setErrorMessage('Invalid warranty serial code.'); - $logger->logWarrantyInfo($log_data, $res->getErrorMessage(), $user_id, $action, $source); - return $res; + $msg = 'Invalid warranty serial code.'; + $logger->logWarrantyInfo($log_data, $msg, $user_id, $action, $source); + return $msg; } // check if warranty exists already @@ -361,15 +371,14 @@ class WarrantyController extends APIController // check if warranty is registered to a serial owned by customer $warr_plate = $warr->getPlateNumber(); - $cust = $this->session->getCustomer(); + $cust = $mobile_user->getCustomer(); $is_customer_warranty = $this->checkCustomerPlateNumber($warr_plate, $cust); if (!$is_customer_warranty) { - $res->setError(true) - ->setErrorMessage('Warranty registered to a vehicle not in your list of vehicles.'); - $logger->logWarrantyInfo($log_data, $res->getErrorMessage(), $user_id, $action, $source); - return $res; + $msg = 'Warranty registered to a vehicle not in your list of vehicles.'; + $logger->logWarrantyInfo($log_data, $msg, $user_id, $action, $source); + return $msg; } $sms_msg = $trans->trans('warranty_update_confirm'); @@ -391,10 +400,9 @@ class WarrantyController extends APIController $sap_bty = $em->getRepository(SAPBattery::class)->find($sku); if ($sap_bty == null) { - $res->setError(true) - ->setErrorMessage('Could not find battery entry for warranty.'); - $logger->logWarrantyInfo($log_data, $res->getErrorMessage(), $user_id, $action, $source); - return $res; + $msg = 'Could not find battery entry for warranty.'; + $logger->logWarrantyInfo($log_data, $msg, $user_id, $action, $source); + return $msg; } } @@ -406,13 +414,12 @@ class WarrantyController extends APIController $date_pur_cust = DateTime::createFromFormat('Y-m-d', $req->request->get('date_purchase')); if (!$date_pur_cust) { - $res->setError(true) - ->setErrorMessage('Invalid date format for date of purchase.'); - $logger->logWarrantyInfo($log_data, $res->getErrorMessage(), $user_id, $action, $source); - return $res; + $msg = 'Invalid date format for date of purchase.'; + $logger->logWarrantyInfo($log_data, $msg, $user_id, $action, $source); + return $msg; } - $customer = $this->session->getCustomer(); + $customer = $mobile_user->getCustomer(); if ($customer != null) { $warr->setCustomer($customer); @@ -437,7 +444,7 @@ class WarrantyController extends APIController // ->setBatterySize() // ->setBatteryModel() ->setSAPBattery($sap_bty) - ->setMobileNumber(substr($this->session->getPhoneNumber(), 2)) + ->setMobileNumber(substr($mobile_user->getPhoneNumber(), 2)) ->setActivated(true) // files @@ -457,19 +464,17 @@ class WarrantyController extends APIController $em->persist($warr); - // TODO: check if we need to do anyting else + // TODO: check if we need to do anything else + // TODO: put warranty data into data $data = []; - // set data to retrun to user - $res->setData($data); - $logger->logWarrantyInfo($log_data, '', $user_id, $action, $source); // send sms - error_log('sending sms to - ' . $this->session->getPhoneNumber()); - $rt->sendSMS($this->session->getPhoneNumber(), 'MOTOLITE', $sms_msg); + error_log('sending sms to - ' . $mobile_user->getPhoneNumber()); + $rt->sendSMS($mobile_user->getPhoneNumber(), 'MOTOLITE', $sms_msg); - return $res; + return $data; } protected function findCustomerVehicle($em, $customer, $plate_number)