From 13b955d0b6f472b0b0fdb99c05a4282a5232aa17 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Tue, 2 Jul 2019 09:15:06 +0000 Subject: [PATCH] Add route to activate warranty. Move the code to search for warranty to a separate function. Fix bug in Warranty entity when setting flag_activated. #227 --- config/routes/api.yaml | 5 + src/Controller/APIController.php | 209 ++++++++++++++++--------------- src/Entity/Warranty.php | 2 +- 3 files changed, 113 insertions(+), 103 deletions(-) diff --git a/config/routes/api.yaml b/config/routes/api.yaml index 50e50957..2d3a11bc 100644 --- a/config/routes/api.yaml +++ b/config/routes/api.yaml @@ -124,3 +124,8 @@ api_location_support: path: /api/location_support controller: App\Controller\APIController:locationSupport methods: [GET] + +api_activate_warranty: + path: /api/activate_warranty + controller: App\Controller\APIController:activateWarranty + methods: [POST] diff --git a/src/Controller/APIController.php b/src/Controller/APIController.php index 81f87f05..5134a884 100644 --- a/src/Controller/APIController.php +++ b/src/Controller/APIController.php @@ -674,55 +674,7 @@ class APIController extends Controller if ($cv->getWarrantyExpiration() != null) $wty_ex = $cv->getWarrantyExpiration()->format('Y-m-d'); - // NOTE: Modify the search for the latest warranty - // get latest warranty using plate number - $warranty_results = $em->getRepository(Warranty::class)->findBy(['plate_number' => $cv->getPlateNumber()], - ['date_create' => 'desc']); - - $warranty = current($warranty_results); - - // check for null values for battery and date claim and date expire - $batt_model = ''; - $batt_size = ''; - $sap_batt = ''; - $claim_date = ''; - $expiry_date = ''; - - if (!(is_null($warranty->getBatteryModel()))) { - $batt_model = $warranty->getBatteryModel()->getName(); - } - if (!(is_null($warranty->getBatterySize()))) { - $batt_size = $warranty->getBattyerSize()->getName(); - } - if (!(is_null($warranty->getSAPBattery()))) { - $sap_batt = $warranty->getSAPBattery()->getID(); - } - if (!(is_null($warranty->getDateClaim()))) { - $claim_date = $warranty->getDateClaim()->format("d M Y g:i A"); - } - if (!(is_null($warranty->getDateExpire()))) { - $expiry_date = $warranty->getDateExpire()->format("d M Y g:i A"); - } - - $warr[] = [ - 'id' => $warranty->getID(), - 'serial' => $warranty->getSerial(), - 'warranty_class' => $warranty->getWarrantyClass(), - 'plate_number' => $warranty->getPlateNumber(), - 'first_name' => $warranty->getFirstName(), - 'last_name' => $warranty->getLastName(), - 'mobile_number' => $warranty->getMobileNumber(), - 'battery_model' => $batt_model, - 'battery_size' => $batt_size, - 'sap_battery' => $sap_batt, - 'status' => $warranty->getStatus(), - 'date_create' => $warranty->getDateCreate()->format("d M Y g:i A"), - 'date_purchase' => $warranty->getDatePurchase()->format("d M Y g:i A"), - 'date_expire' => $expiry_date, - 'date_claim' => $claim_date, - 'claim_from' => $warranty->getClaimedFrom(), - 'is_activated' => $warranty->isActivated() ? 1 : 0, - ]; + $warranty = $this->findWarranty($cv->getPlateNumber()); $cv_list[] = [ 'cv_id' => $cv->getID(), @@ -739,7 +691,7 @@ class APIController extends Controller 'curr_batt_id' => $battery_id, 'is_motolite' => $cv->hasMotoliteBattery() ? 1 : 0, 'is_active' => $cv->isActive() ? 1 : 0, - 'warranty' => $warr, + 'warranty' => $warranty, ]; } @@ -1694,61 +1646,13 @@ class APIController extends Controller // customer vehicle and warranty $cv = $jo->getCustomerVehicle(); - // NOTE: Modify the search for the latest warranty // get latest warranty using plate number - $warranty_results = $em->getRepository(Warranty::class)->findBy(['plate_number' => $cv->getPlateNumber()], - ['date_create' => 'desc']); - - $warranty = current($warranty_results); - - // check for null values for battery and date claim and date expire - $batt_model = ''; - $batt_size = ''; - $sap_batt = ''; - $claim_date = ''; - $expiry_date = ''; - - if (!(is_null($warranty->getBatteryModel()))) { - $batt_model = $warranty->getBatteryModel()->getName(); - } - if (!(is_null($warranty->getBatterySize()))) { - $batt_size = $warranty->getBattyerSize()->getName(); - } - if (!(is_null($warranty->getSAPBattery()))) { - $sap_batt = $warranty->getSAPBattery()->getID(); - } - if (!(is_null($warranty->getDateClaim()))) { - $claim_date = $warranty->getDateClaim()->format("d M Y g:i A"); - } - if (!(is_null($warranty->getDateExpire()))) { - $expiry_date = $warranty->getDateExpire()->format("d M Y g:i A"); - } - - $warr = []; - $warr[] = [ - 'id' => $warranty->getID(), - 'serial' => $warranty->getSerial(), - 'warranty_class' => $warranty->getWarrantyClass(), - 'plate_number' => $warranty->getPlateNumber(), - 'first_name' => $warranty->getFirstName(), - 'last_name' => $warranty->getLastName(), - 'mobile_number' => $warranty->getMobileNumber(), - 'battery_model' => $batt_model, - 'battery_size' => $batt_size, - 'sap_battery' => $sap_batt, - 'status' => $warranty->getStatus(), - 'date_create' => $warranty->getDateCreate()->format("d M Y g:i A"), - 'date_purchase' => $warranty->getDatePurchase()->format("d M Y g:i A"), - 'date_expire' => $expiry_date, - 'date_claim' => $claim_date, - 'claim_from' => $warranty->getClaimedFrom(), - 'is_activated' => $warranty->isActivated() ? 1 : 0, - ]; + $warranty = $this->findWarranty($cv->getPlateNumber()); $jo_data['customer_vehicle'] = [ 'id' => $cv->getID(), 'plate_number' => $cv->getPlateNumber(), - 'warranty' => $warr, + 'warranty' => $warranty, ]; // rider @@ -1909,6 +1813,107 @@ class APIController extends Controller return $res->getReturnResponse(); } - // TODO mogol add public function activateWarranty that takes in - // a plate number. Search for the latest warranty with the plate number + public function activateWarranty(Request $req) + { + $required_params = ['plate_number']; + $em = $this->getDoctrine()->getManager(); + $res = $this->checkParamsAndKey($req, $em, $required_params); + if ($res->isError()) + return $res->getReturnResponse(); + + $plate_number = $req->request->get('plate_number'); + + // find warranty using plate number + $warranty_results = $em->getRepository(Warranty::class)->findBy(['plate_number' => $plate_number], + ['date_create' => 'desc']); + + // check if warranty_results is empty + if (empty($warranty_results)) + { + $res->setError(true) + ->setErrorMessage('No warranty found for plate number'); + return $res->getReturnResponse(); + } + + // get first entry + $warranty = current($warranty_results); + if ($warranty->isActivated()) + { + $res->setError(true) + ->setErrorMessage('Warranty already activated'); + return $res->getReturnResponse(); + } + + $warranty->setActivated(); + + $em->flush(); + + return $res->getReturnResponse(); + } + + protected function findWarranty($plate_number) + { + $em = $this->getDoctrine()->getManager(); + // NOTE: Modify the search for the latest warranty. This seems hacky. + // get latest warranty using plate number + $warranty_results = $em->getRepository(Warranty::class)->findBy(['plate_number' => $plate_number], + ['date_create' => 'desc']); + + // check if warranty_results is empty + if (empty($warranty_results)) + { + $res->setError(true) + ->setErrorMessage('No warranty found for plate number'); + return $res->getReturnResponse(); + } + + // get first entry + $warranty = current($warranty_results); + + // check for null values for battery and date claim and date expire + $batt_model = ''; + $batt_size = ''; + $sap_batt = ''; + $claim_date = ''; + $expiry_date = ''; + + if (!(is_null($warranty->getBatteryModel()))) { + $batt_model = $warranty->getBatteryModel()->getName(); + } + if (!(is_null($warranty->getBatterySize()))) { + $batt_size = $warranty->getBattyerSize()->getName(); + } + if (!(is_null($warranty->getSAPBattery()))) { + $sap_batt = $warranty->getSAPBattery()->getID(); + } + if (!(is_null($warranty->getDateClaim()))) { + $claim_date = $warranty->getDateClaim()->format("d M Y g:i A"); + } + if (!(is_null($warranty->getDateExpire()))) { + $expiry_date = $warranty->getDateExpire()->format("d M Y g:i A"); + } + + $warr = []; + $warr[] = [ + 'id' => $warranty->getID(), + 'serial' => $warranty->getSerial(), + 'warranty_class' => $warranty->getWarrantyClass(), + 'plate_number' => $warranty->getPlateNumber(), + 'first_name' => $warranty->getFirstName(), + 'last_name' => $warranty->getLastName(), + 'mobile_number' => $warranty->getMobileNumber(), + 'battery_model' => $batt_model, + 'battery_size' => $batt_size, + 'sap_battery' => $sap_batt, + 'status' => $warranty->getStatus(), + 'date_create' => $warranty->getDateCreate()->format("d M Y g:i A"), + 'date_purchase' => $warranty->getDatePurchase()->format("d M Y g:i A"), + 'date_expire' => $expiry_date, + 'date_claim' => $claim_date, + 'claim_from' => $warranty->getClaimedFrom(), + 'is_activated' => $warranty->isActivated() ? 1 : 0, + ]; + + return $warr; + } } diff --git a/src/Entity/Warranty.php b/src/Entity/Warranty.php index a0e71a99..1cfa242c 100644 --- a/src/Entity/Warranty.php +++ b/src/Entity/Warranty.php @@ -359,7 +359,7 @@ class Warranty public function setActivated($flag_activated = true) { $this->flag_activated = $flag_activated; - return this; + return $this; } public function isActivated()