diff --git a/config/routes/resqapi.yaml b/config/routes/resqapi.yaml index 43b2c45e..df45f624 100644 --- a/config/routes/resqapi.yaml +++ b/config/routes/resqapi.yaml @@ -162,7 +162,7 @@ resqapi_location_support: resqapi_nearest_hub_slots: path: /resqapi/hub_slots controller: App\Controller\ResqAPI\JobOrderController::getNearestHubAndSlots - methods: [GET] + methods: [POST] resqapi_new_jo_request: path: /resqapi/new_job_order diff --git a/src/Controller/ResqAPI/JobOrderController.php b/src/Controller/ResqAPI/JobOrderController.php index 8a0fd4b7..5fa7a992 100644 --- a/src/Controller/ResqAPI/JobOrderController.php +++ b/src/Controller/ResqAPI/JobOrderController.php @@ -52,6 +52,7 @@ use App\Entity\Hub; use Catalyst\APIBundle\Access\Generator as ACLGenerator; use DateTime; +use DateInterval; class JobOrderController extends APIController { @@ -550,28 +551,45 @@ class JobOrderController extends APIController return new APIResponse(true, 'Location checked', $data); } - // TODO: do we make this use the HubCriteria and HubSelector? YES? public function getNearestHubAndSlots(Request $req, EntityManagerInterface $em, - MapTools $map_tools) + MobileAPIHandler $mah, HubSelector $hub_select) { + $this->denyAccessUnlessGranted('mobile_jo.nearest_hub.get', null, 'No access.'); + $required_params = [ 'longitude', 'latitude', ]; - $res = $this->checkParamsAndKey($req, $em, $required_params); - if ($res->isError()) - return $res->getReturnResponse(); + $msg = $this->checkRequiredParameters($req, $required_params); + if ($msg) + return new APIResponse(false, $msg); - $coordinates = new Point($req->query->get('longitude'), $req->query->get('latitude')); - $nearest_hub_slots = $this->findAdvanceNearestHubAndSlots($coordinates, $em, $map_tools); + // 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.'); + + $coordinates = new Point($req->request->get('longitude'), $req->request->get('latitude')); + + $hub_criteria = new HubCriteria(); + // get the nearest 10 hubs + // TODO: do we need to check for inventory? if so, we need battery id + $hub_criteria->setPoint($coordinates) + ->setLimitResults(10); + + // find nearest hubs + $nearest_hubs = $hub_select->find($hub_criteria); + + // get the nearest hub and slots + $nearest_hub_slots = $this->findAdvanceNearestHubAndSlots($nearest_hubs, $em); if (empty($nearest_hub_slots['hub'])) - { - $res->setError(true) - ->setErrorMessage('Thank you for reaching out to us. Due to the General Community Quarantine, our Operations are from 8AM to 6PM only. Please expect a call from us tomorrow and we will assist you with your request. Thank you and stay safe!'); - return $res->getReturnResponse(); - } + return new APIResponse(false, 'Thank you for reaching out to us. Due to the General Community Quarantine, our Operations are from 8AM to 6PM only. Please expect a call from us tomorrow and we will assist you with your request. Thank you and stay safe!'); // make hub data $data = [ @@ -579,10 +597,7 @@ class JobOrderController extends APIController 'hub_slots' => $nearest_hub_slots['slots'], ]; - $res->setData($data); - - return $res->getReturnResponse(); - + return new APIResponse(true, 'Hub and slot found', $data); } public function scheduleOptionStatus(Request $req, EntityManagerInterface $em) @@ -614,87 +629,6 @@ class JobOrderController extends APIController return $res->getReturnResponse(); } - protected function getOngoingJobOrders($cust, $em) - { - $ongoing_jos = $em->getRepository(JobOrder::class)->findBy([ - 'customer' => $cust, - 'status' => [JOStatus::PENDING, JOStatus::RIDER_ASSIGN, JOStatus::IN_TRANSIT, JOStatus::ASSIGNED, JOStatus::IN_PROGRESS], - ]); - - return $ongoing_jos; - } - - // TODO: what to do with this? listVehicles in CustomerVehicleController als calls this - protected function findWarranty($plate_number, $em) - { - // 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']); - - $warr = []; - - // check if warranty_results is empty - if (empty($warranty_results)) - { - /* - $res->setError(true) - ->setErrorMessage('No warranty found for plate number'); - return $res->getReturnResponse(); - */ - - return $warr; - } - - // 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->getBatterySize()->getName(); - } - if (!(is_null($warranty->getSAPBattery()))) { - $sap_batt = $warranty->getSAPBattery()->getID(); - } - if (!(is_null($warranty->getDateClaim()))) { - $claim_date = $warranty->getDateClaim()->format("d M Y"); - } - if (!(is_null($warranty->getDateExpire()))) { - $expiry_date = $warranty->getDateExpire()->format("d M Y"); - } - - $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"), - 'date_expire' => $expiry_date, - 'date_claim' => $claim_date, - 'claim_from' => $warranty->getClaimedFrom(), - 'is_activated' => $warranty->isActivated() ? 1 : 0, - ]; - - return $warr; - } - protected function randomizeRider($riders) { // TODO: get redis to track the sales per rider per day @@ -707,26 +641,14 @@ class JobOrderController extends APIController return $selected_rider; } - // TODO: this might become irrelevant if we use HubCriteria (which I think we should, my blooper) - protected function findAdvanceNearestHubAndSlots(Point $coordinates, EntityManagerInterface $em, MapTools $map_tools) + protected function findAdvanceNearestHubAndSlots($nearest_hubs, EntityManagerInterface $em) { - // get the nearest 10 hubs - $hub_data = []; - $nearest_hubs_with_distance = []; - $hubs = $map_tools->getClosestOpenHubs($coordinates, 10); - - foreach ($hubs as $hub) - { - $nearest_hubs_with_distance[] = $hub; - // TODO: insert checking for branch code here when inventory manager is up - } - $nearest = null; $slot_found = false; // find the nearest hub - if (!empty($nearest_hubs_with_distance)) + if (!empty($nearest_hubs)) { - foreach ($nearest_hubs_with_distance as $nhd) + foreach ($nearest_hubs as $nhd) { if (empty($nearest)) $nearest = $nhd;