From bfbd4d4045044f3af81acd2574331c4202786de1 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Mon, 12 Jul 2021 09:44:46 +0000 Subject: [PATCH] Modify location support. #591 --- config/api_acl.yaml | 8 ++ config/routes/resqapi.yaml | 2 +- src/Controller/ResqAPI/JobOrderController.php | 105 +++++++----------- 3 files changed, 48 insertions(+), 67 deletions(-) diff --git a/config/api_acl.yaml b/config/api_acl.yaml index fb2c8e5d..cd1f9ae0 100644 --- a/config/api_acl.yaml +++ b/config/api_acl.yaml @@ -154,3 +154,11 @@ access_keys: label: Cancel Job Order - id: mobile_jo.get.history label: Get Job Order History + - id: mobile_jo.get.invoice + label: Get Job Order Invoice + - id: mobile_jo.location.support + label: Check Location Support + - id: mobile_jo.nearest_hub.get + label: Get Nearest Hub and Slots + - id: mobile_jo.schedule_option.status + label: Schedule Option Status diff --git a/config/routes/resqapi.yaml b/config/routes/resqapi.yaml index 354440d2..43b2c45e 100644 --- a/config/routes/resqapi.yaml +++ b/config/routes/resqapi.yaml @@ -157,7 +157,7 @@ resqapi_jo_invoice: resqapi_location_support: path: /resqapi/location_support controller: App\Controller\ResqAPI\JobOrderController:locationSupport - methods: [GET] + methods: [POST] resqapi_nearest_hub_slots: path: /resqapi/hub_slots diff --git a/src/Controller/ResqAPI/JobOrderController.php b/src/Controller/ResqAPI/JobOrderController.php index 0daa1fc8..8a0fd4b7 100644 --- a/src/Controller/ResqAPI/JobOrderController.php +++ b/src/Controller/ResqAPI/JobOrderController.php @@ -476,93 +476,67 @@ class JobOrderController extends APIController return new APIResponse(true, 'Job order history found', $data); } - // TODO: modify for MobileUser - public function getJOInvoice(Request $req, EntityManagerInterface $em) + public function getJOInvoice(Request $req, EntityManagerInterface $em, MobileAPIHandler $mah) { + $this->denyAccessUnlessGranted('mobile_jo.get.invoice', null, 'No access.'); + $required_params = [ 'jo_id', ]; - $res = $this->checkParamsAndKey($req, $em, $required_params); - if ($res->isError()) - return $res->getReturnResponse(); - // get job order - $jo_id = $req->query->get('jo_id'); - $jo = $em->getRepository(JobOrder::class)->find($jo_id); - if ($jo == null) - { - $res->setError(true) - ->setErrorMessage('No job order found'); - return $res->getReturnResponse(); - } + $msg = $this->checkRequiredParameters($req, $required_params); + if ($msg) + return new APIResponse(false, $msg); - // get customer - $cust = $this->session->getCustomer(); + // get capi user to link to mobile user + $user_id = $this->getUser()->getID(); + + // get mobile user + $mobile_user = $mah->findMobileUser($user_id); + + if ($mobile_user == null) + return new APIResponse(false, 'No mobile user found.'); + + // customer + $cust = $mobile_user->getCustomer(); if ($cust == null) - { - $res->setError(true) - ->setErrorMessage('No customer information found'); - return $res->getReturnResponse(); - } + return new APIResponse(false, 'No customer information found'); // check that the customer owns the job order $jo_cust = $jo->getCustomer(); if ($jo_cust->getID() != $cust->getID()) - { - $res->setError(true) - ->setErrorMessage('Job order was not initiated by customer'); - return $res->getReturnResponse(); - } + return new APIResponse(false, 'Job order was not initiated by customer'); $invoice = $jo->getInvoice(); - // make invoice json data - $data = [ - 'total_price' => (float) $invoice->getTotalPrice(), - 'vat_ex_price' => (float) $invoice->getVATExclusivePrice(), - 'vat' => (float) $invoice->getVAT(), - 'discount' => (float) $invoice->getDiscount(), - 'trade_in' => (float) $invoice->getTradeIn(), - ]; - $items = $invoice->getItems(); - $items_data = []; - foreach ($items as $item) - { - $my_data = [ - 'title' => $item->getTitle(), - 'qty' => (int) $item->getQuantity() + 0, - 'price' => (float) $item->getPrice() + 0.0, - ]; + $data = $this->makeInvoiceData($invoice, $mah, $req); - $item_batt = $item->getBattery(); - if ($item_batt != null) - { - $my_data['image_url'] = $this->getBatteryImageURL($req, $item_batt); - } - - $items_data[] = $my_data; - } - - $data['items'] = $items_data; - - // set data - $res->setData($data); - - return $res->getReturnResponse(); + return new APIResponse(true, 'Job order invoice found', $data); } - public function locationSupport(Request $req, GeofenceTracker $geo, EntityManagerInterface $em) + public function locationSupport(Request $req, GeofenceTracker $geo, EntityManagerInterface $em, + MobileAPIHandler $mah) { $required_params = [ 'longitude', 'latitude', ]; - $res = $this->checkParamsAndKey($req, $em, $required_params); - if ($res->isError()) - return $res->getReturnResponse(); - $long = $req->query->get('longitude'); - $lat = $req->query->get('latitude'); + $msg = $this->checkRequiredParameters($req, $required_params); + if ($msg) + return new APIResponse(false, $msg); + + // get capi user to link to mobile user + $user_id = $this->getUser()->getID(); + + // get mobile user + $mobile_user = $mah->findMobileUser($user_id); + + if ($mobile_user == null) + return new APIResponse(false, 'No mobile user found.'); + + $long = $req->request->get('longitude'); + $lat = $req->request->get('latitude'); // geofence $is_covered = $geo->isCovered($long, $lat); @@ -572,9 +546,8 @@ class JobOrderController extends APIController 'latitude' => $lat, 'supported' => $is_covered, ]; - $res->setData($data); - return $res->getReturnResponse(); + return new APIResponse(true, 'Location checked', $data); } // TODO: do we make this use the HubCriteria and HubSelector? YES?