From 7a2996d4e585cb22d9077dec6ff84ce858782d03 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Fri, 19 Jun 2020 04:16:45 +0000 Subject: [PATCH 1/2] Add getStatus to rider API. #428 --- config/routes/cmb_rider_api.yaml | 5 ++++ src/Controller/CMBRAPIController.php | 25 +++++++++++++++++++ .../RiderAPIHandler/CMBRiderAPIHandler.php | 25 +++++++++++++++++++ 3 files changed, 55 insertions(+) diff --git a/config/routes/cmb_rider_api.yaml b/config/routes/cmb_rider_api.yaml index eb4f2186..03e89f5b 100644 --- a/config/routes/cmb_rider_api.yaml +++ b/config/routes/cmb_rider_api.yaml @@ -129,3 +129,8 @@ cmb_rapi_jo_finish_photos_upload: path: /cmbrapi/uploadfinishphotos controller: App\Controller\CMBRAPIController::uploadFinishPhotos methods: [POST] + +cmb_rapi_status: + path: /cmbrapi/status + controller: App\Controller\CMBRAPIController::getStatus + methods: [GET] diff --git a/src/Controller/CMBRAPIController.php b/src/Controller/CMBRAPIController.php index 2d503831..ce5868c5 100644 --- a/src/Controller/CMBRAPIController.php +++ b/src/Controller/CMBRAPIController.php @@ -642,4 +642,29 @@ class CMBRAPIController extends Controller // response return $res->getReturnResponse(); } + + public function getStatus(Request $req, RiderAPIHandlerInterface $rapi_handler) + { + $res = new NewAPIResult(); + + $data = $rapi_handler->getStatus($req); + + if (isset($data['error'])) + { + $message = $data['error']; + $title = $data['title']; + + $res->setError(true) + ->setErrorTitle($title) + ->setErrorMessage($message); + } + else + { + $res->setData($data); + } + + // response + return $res->getReturnResponse(); + } + } diff --git a/src/Service/RiderAPIHandler/CMBRiderAPIHandler.php b/src/Service/RiderAPIHandler/CMBRiderAPIHandler.php index 8231d2ac..14da1e6e 100644 --- a/src/Service/RiderAPIHandler/CMBRiderAPIHandler.php +++ b/src/Service/RiderAPIHandler/CMBRiderAPIHandler.php @@ -1623,6 +1623,31 @@ class CMBRiderAPIHandler implements RiderAPIHandlerInterface return $data; } + public function getStatus(Request $req) + { + $required_params = []; + $data = $this->checkParamsAndKey($req, $required_params); + if (isset($data['error'])) + { + $data['title'] = 'Failed Get Status'; + return $data; + } + + $rider = $this->session->getRider(); + + $rider_status = $rider->isAvailable(); + + $status = 'Offline'; + if ($rider_status) + $status = 'Online'; + + $data = [ + 'status' => $status, + ]; + + return $data; + } + protected function checkMissingParameters(Request $req, $params = []) { $missing = []; From 332f9687a28527009b7c5c5cfa0e6a35a206b03f Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Fri, 19 Jun 2020 06:19:11 +0000 Subject: [PATCH 2/2] Add getOngoingJobOrder to rider API. #428 --- config/routes/cmb_rider_api.yaml | 5 + src/Controller/CMBRAPIController.php | 24 ++++ src/Entity/Rider.php | 5 + .../RiderAPIHandler/CMBRiderAPIHandler.php | 132 +++++++++++++++++- 4 files changed, 165 insertions(+), 1 deletion(-) diff --git a/config/routes/cmb_rider_api.yaml b/config/routes/cmb_rider_api.yaml index 03e89f5b..cb291bef 100644 --- a/config/routes/cmb_rider_api.yaml +++ b/config/routes/cmb_rider_api.yaml @@ -134,3 +134,8 @@ cmb_rapi_status: path: /cmbrapi/status controller: App\Controller\CMBRAPIController::getStatus methods: [GET] + +cmb_rapi_jo_ongoing: + path: /cmbrapi/joongoing + controller: App\Controller\CMBRAPIController::getOngoingJobOrder + methods: [GET] diff --git a/src/Controller/CMBRAPIController.php b/src/Controller/CMBRAPIController.php index ce5868c5..30d70ed0 100644 --- a/src/Controller/CMBRAPIController.php +++ b/src/Controller/CMBRAPIController.php @@ -667,4 +667,28 @@ class CMBRAPIController extends Controller return $res->getReturnResponse(); } + public function getOngoingJobOrder(Request $req, RiderAPIHandlerInterface $rapi_handler) + { + $res = new NewAPIResult(); + + $data = $rapi_handler->getOngoingJobOrder($req); + + if (isset($data['error'])) + { + $message = $data['error']; + $title = $data['title']; + + $res->setError(true) + ->setErrorTitle($title) + ->setErrorMessage($message); + } + else + { + $res->setData($data); + } + + // response + return $res->getReturnResponse(); + } + } diff --git a/src/Entity/Rider.php b/src/Entity/Rider.php index 502d93bc..6416a443 100644 --- a/src/Entity/Rider.php +++ b/src/Entity/Rider.php @@ -347,6 +347,11 @@ class Rider return $this->job_orders->matching($criteria)[0]; } + public function getRiderActiveJobOrder() + { + return $this->active_job_order; + } + public function getOpenJobOrders() { $active_status = [ diff --git a/src/Service/RiderAPIHandler/CMBRiderAPIHandler.php b/src/Service/RiderAPIHandler/CMBRiderAPIHandler.php index 14da1e6e..22d3dc34 100644 --- a/src/Service/RiderAPIHandler/CMBRiderAPIHandler.php +++ b/src/Service/RiderAPIHandler/CMBRiderAPIHandler.php @@ -718,6 +718,8 @@ class CMBRiderAPIHandler implements RiderAPIHandlerInterface return $data; } + // TODO: put JO in job queue + // TODO: refactor this into a jo handler class, so we don't have to repeat for control center // TODO: send mqtt event (?) // add event log @@ -1244,6 +1246,9 @@ class CMBRiderAPIHandler implements RiderAPIHandlerInterface return $data; } + // set jo status to in progress + $jo->setStatus(JOStatus::IN_PROGRESS); + // add event log $rider = $this->session->getRider(); $event = new JOEvent(); @@ -1648,6 +1653,131 @@ class CMBRiderAPIHandler implements RiderAPIHandlerInterface return $data; } + public function getOngoingJobOrder(Request $req) + { + $required_params = []; + $data = $this->checkParamsAndKey($req, $required_params); + if (isset($data['error'])) + { + $data['title'] = 'Failed Get Ongoing Job Order'; + return $data; + } + + // are we logged in? + if (!$this->session->hasRider()) + { + $data = [ + 'title' => 'Failed Get Ongoing Job Order', + 'error' => 'No logged in rider.' + ]; + return $data; + } + + $rider = $this->session->getRider(); + + // check if we have an active JO + $jo = $rider->getRiderActiveJobOrder(); + if ($jo == null) + { + $data = [ + 'title' => 'Failed Get Ongoing Job Order', + 'error' => 'No active job order.' + ]; + return $data; + } + + $coord = $jo->getCoordinates(); + $cust = $jo->getCustomer(); + $cv = $jo->getCustomerVehicle(); + $v = $cv->getVehicle(); + $inv = $jo->getInvoice(); + $promo = $inv->getPromo(); + + // invoice items + $inv_items = []; + foreach ($inv->getItems() as $item) + { + $item_batt = $item->getBattery(); + if ($item_batt == null) + $batt_id = null; + else + $batt_id = $item_batt->getID(); + + $inv_items[] = [ + 'id' => $item->getID(), + 'title' => $item->getTitle(), + 'qty' => $item->getQuantity(), + 'price' => $item->getPrice(), + 'batt_id' => $batt_id, + ]; + } + + // promo + if ($promo != null) + { + $promo_data = [ + 'id' => $promo->getID(), + 'name' => $promo->getName(), + 'code' => $promo->getCode(), + 'discount_rate' => $promo->getDiscountRate(), + 'discount_apply' => $promo->getDiscountApply(), + ]; + } + else + { + $promo_data = null; + } + + $trade_in_type = $jo->getTradeInType(); + if (empty($trade_in_type)) + $trade_in_type = 'none'; + + $data = [ + 'job_order' => [ + 'id' => $jo->getID(), + 'service_type' => $jo->getServiceType(), + 'date_schedule' => $jo->getDateSchedule()->format('Ymd H:i:s'), + 'longitude' => $coord->getLongitude(), + 'latitude' => $coord->getLatitude(), + 'status' => $jo->getStatus(), + 'customer' => [ + 'title' => $cust->getTitle(), + 'first_name' => $cust->getFirstName(), + 'last_name' => $cust->getLastName(), + 'phone_mobile' => $this->country_code . $cust->getPhoneMobile(), + ], + 'vehicle' => [ + 'manufacturer' => $v->getManufacturer()->getName(), + 'make' => $v->getMake(), + 'model' => $cv->getModelYear(), + 'plate_number' => $cv->getPlateNumber(), + 'color' => $cv->getColor(), + ], + 'or_num' => $jo->getORNum(), + 'or_name' => $jo->getORName(), + 'delivery_instructions' => $jo->getDeliveryInstructions(), + 'delivery_address' => $jo->getDeliveryAddress(), + 'landmark' => $jo->getLandmark(), + 'invoice' => [ + 'discount' => $inv->getDiscount(), + 'trade_in' => $inv->getTradeIn(), + 'total_price' => $inv->getTotalPrice(), + 'vat' => $inv->getVat(), + 'items' => $inv_items, + ], + 'mode_of_payment' => $jo->getModeOfPayment(), + 'trade_in_type' => $trade_in_type, + 'promo' => $promo_data, + // TODO: load the actual + 'has_warranty_doc' => false, + 'flag_coolant' => $jo->hasCoolant(), + 'has_motolite' => $cv->hasMotoliteBattery(), + ] + ]; + + return $data; + } + protected function checkMissingParameters(Request $req, $params = []) { $missing = []; @@ -1744,7 +1874,7 @@ class CMBRiderAPIHandler implements RiderAPIHandlerInterface $rider = $this->session->getRider(); // check if we have an active JO - $jo = $rider->getActiveJobOrder(); + $jo = $rider->getRiderActiveJobOrder(); if ($jo == null) { $data = [