From dd9c9dd0ecfd80891f3a452cfef5ac3cfee39c10 Mon Sep 17 00:00:00 2001 From: Ramon Gutierrez Date: Tue, 26 Sep 2023 04:53:13 +0800 Subject: [PATCH] Add endpoint for getting individual vehicle info #761 --- config/routes/apiv2.yaml | 5 + .../CustomerAppAPI/VehicleController.php | 134 +++++++++++------- 2 files changed, 91 insertions(+), 48 deletions(-) diff --git a/config/routes/apiv2.yaml b/config/routes/apiv2.yaml index 8f781ca7..ec999b90 100644 --- a/config/routes/apiv2.yaml +++ b/config/routes/apiv2.yaml @@ -45,6 +45,11 @@ apiv2_cust_vehicle_add: controller: App\Controller\CustomerAppAPI\VehicleController::addVehicle methods: [POST] +apiv2_cust_vehicle_info: + path: /apiv2/vehicles/{id} + controller: App\Controller\CustomerAppAPI\VehicleController::getVehicle + methods: [GET] + apiv2_cust_vehicle_update: path: /apiv2/vehicles/{id} controller: App\Controller\CustomerAppAPI\VehicleController::updateVehicle diff --git a/src/Controller/CustomerAppAPI/VehicleController.php b/src/Controller/CustomerAppAPI/VehicleController.php index d56a86ee..9835208a 100644 --- a/src/Controller/CustomerAppAPI/VehicleController.php +++ b/src/Controller/CustomerAppAPI/VehicleController.php @@ -107,6 +107,34 @@ class VehicleController extends ApiController } + public function getVehicle(Request $req, $id) + { + // check requirements + $validity = $this->validateRequest($req); + + if (!$validity['is_valid']) { + return new ApiResponse(false, $validity['error']); + } + + // get customer vehicle + $cv = $this->em->getRepository(CustomerVehicle::class)->find($id); + + // check if it exists + if ($cv == null) { + return new ApiResponse(false, 'Vehicle does not exist.'); + } + + // check if it's owned by customer + if ($cv->getCustomer()->getID() != $this->session->getCustomer()->getID()) { + return new ApiResponse(false, 'Invalid vehicle.'); + } + + // response + return new ApiResponse(true, '', [ + 'vehicle' => $this->generateVehicleInfo($cv, true), + ]); + } + public function updateVehicle(Request $req, $id) { // check requirements @@ -162,54 +190,7 @@ class VehicleController extends ApiController // only get the customer's vehicles whose flag_active is true $cvs = $this->em->getRepository(CustomerVehicle::class)->findBy(['flag_active' => true, 'customer' => $cust]); foreach ($cvs as $cv) { - $battery_id = null; - if ($cv->getCurrentBattery() != null) - $battery_id = $cv->getCurrentBattery()->getID(); - - $wty_ex = null; - if ($cv->getWarrantyExpiration() != null) - $wty_ex = $cv->getWarrantyExpiration()->format('Y-m-d'); - - $warranty = $this->findWarranty($cv->getPlateNumber()); - - $cv_name = ''; - if ($cv->getName() != null) - $cv_name = $cv->getName(); - - // get latest insurance row - $insurance = null; - $iobj = $cv->getLatestInsuranceApplication(); - if (!empty($iobj)) { - $gt = $iobj->getGatewayTransaction(); - - $insurance = [ - 'id' => $iobj->getID(), - 'status' => $iobj->getStatus(), - 'coc_url' => $iobj->getCOC(), - 'checkout_url' => $gt->getMetadata()['checkout_url'], - 'transaction_status' => $gt->getStatus(), - 'premium_amount' => (string)bcdiv($gt->getAmount(), 100), // NOTE: hard expressing as string so it's consistent - ]; - } - - $cv_list[] = [ - 'cv_id' => $cv->getID(), - 'mfg_id' => $cv->getVehicle()->getManufacturer()->getID(), - 'make_id' => $cv->getVehicle()->getID(), - 'name' => $cv_name, - 'plate_num' => $cv->getPlateNumber(), - 'model_year' => $cv->getModelYear(), - 'color' => $cv->getColor(), - 'condition' => $cv->getStatusCondition(), - 'fuel_type' => $cv->getFuelType(), - 'wty_code' => $cv->getWarrantyCode(), - 'wty_expire' => $wty_ex, - 'curr_batt_id' => $battery_id, - 'is_motolite' => $cv->hasMotoliteBattery() ? 1 : 0, - 'is_active' => $cv->isActive() ? 1 : 0, - 'warranty' => $warranty, - 'latest_insurance' => $insurance, - ]; + $cv_list[] = $this->generateVehicleInfo($cv); } // response @@ -302,6 +283,63 @@ class VehicleController extends ApiController return new ApiResponse(); } + protected function generateVehicleInfo(CustomerVehicle $cv, $include_insurance = false) + { + $battery_id = null; + if ($cv->getCurrentBattery() != null) + $battery_id = $cv->getCurrentBattery()->getID(); + + $wty_ex = null; + if ($cv->getWarrantyExpiration() != null) + $wty_ex = $cv->getWarrantyExpiration()->format('Y-m-d'); + + $warranty = $this->findWarranty($cv->getPlateNumber()); + + $cv_name = ''; + if ($cv->getName() != null) + $cv_name = $cv->getName(); + + $row = [ + 'cv_id' => $cv->getID(), + 'mfg_id' => $cv->getVehicle()->getManufacturer()->getID(), + 'make_id' => $cv->getVehicle()->getID(), + 'name' => $cv_name, + 'plate_num' => $cv->getPlateNumber(), + 'model_year' => $cv->getModelYear(), + 'color' => $cv->getColor(), + 'condition' => $cv->getStatusCondition(), + 'fuel_type' => $cv->getFuelType(), + 'wty_code' => $cv->getWarrantyCode(), + 'wty_expire' => $wty_ex, + 'curr_batt_id' => $battery_id, + 'is_motolite' => $cv->hasMotoliteBattery() ? 1 : 0, + 'is_active' => $cv->isActive() ? 1 : 0, + 'warranty' => $warranty, + ]; + + // get latest insurance row + if ($include_insurance) { + $insurance = null; + $iobj = $cv->getLatestInsuranceApplication(); + if (!empty($iobj)) { + $gt = $iobj->getGatewayTransaction(); + + $insurance = [ + 'id' => $iobj->getID(), + 'status' => $iobj->getStatus(), + 'coc_url' => $iobj->getCOC(), + 'checkout_url' => $gt->getMetadata()['checkout_url'], + 'transaction_status' => $gt->getStatus(), + 'premium_amount' => (string)bcdiv($gt->getAmount(), 100), // NOTE: hard expressing as string so it's consistent + ]; + } + + $row['latest_insurance'] = $insurance; + } + + return $row; + } + protected function checkVehicleRequirements(Request $req) { // validate params