diff --git a/src/Controller/CAPI/RiderAppController.php b/src/Controller/CAPI/RiderAppController.php index ec330176..6fc3adab 100644 --- a/src/Controller/CAPI/RiderAppController.php +++ b/src/Controller/CAPI/RiderAppController.php @@ -46,6 +46,7 @@ use DateTime; // third party API for rider class RiderAppController extends APIController { + /* public function register(Request $req, EntityManagerInterface $em, RedisClientProvider $redis) { // confirm parameters @@ -117,7 +118,7 @@ class RiderAppController extends APIController ]; return new APIResponse(true, 'Rider API user created.', $data); - } + } */ public function login(Request $req, EntityManagerInterface $em, EncoderFactoryInterface $ef, RiderCache $rcache, RiderTracker $rider_tracker, MQTTClient $mclient, @@ -128,12 +129,14 @@ class RiderAppController extends APIController // TODO: right now, no validation at all. Accept anything. // get capi user - $capi_user_id = $this->getUser()->getID(); - $capi_user = $em->getRepository(APIUser::class)->find($capi_user_id); + $capi_user = $this->getCAPIUser($this->getUser()->getID(), $em); + if ($capi_user == null) + return new APIResponse(false, 'User not found.'); // get rider id from capi user metadata - // TODO: uncomment once getMetadata is available - // $rider_id = $capi_user->getMetadata(); + $rider = $this->getRiderFromCAPI($capi_user, $em); + if ($rider == null) + return new APIResponse(false, 'No rider found.'); /* $missing = $this->checkMissingParameters($req, $required_params); @@ -239,22 +242,22 @@ class RiderAppController extends APIController return new APIResponse(false, 'Missing parameter(s): ' . $params); } - // get capi user to link to rider api user - $capi_user_id = $this->getUser()->getID(); + // get capi user + $capi_user = $this->getCAPIUser($this->getUser()->getID(), $em); + if ($capi_user == null) + return new APIResponse(false, 'User not found.'); - // check if capi user already has a rider api user - $rapi_session = $em->getRepository(RiderAPISession::class)->findOneBy(['capi_user_id' => $capi_user_id]); + // get rider id from capi user metadata + $rider = $this->getRiderFromCAPI($capi_user, $em); + if ($rider == null) + return new APIResponse(false, 'No rider found.'); // make rider unavailable - $rider = $rapi_session->getRider(); $rider->setAvailable(false); // remove from cache $rcache->removeActiveRider($rider->getID()); - // remove rider from session - $rapi_session->setRider(null); - // TODO: log rider logging out $em->flush(); @@ -281,17 +284,15 @@ class RiderAppController extends APIController return new APIResponse(false, 'Missing parameter(s): ' . $params); } - // get capi user to link to rider api user - $capi_user_id = $this->getUser()->getID(); + // get capi user + $capi_user = $this->getCAPIUser($this->getUser()->getID(), $em); + if ($capi_user == null) + return new APIResponse(false, 'User not found.'); - // check if capi user already has a rider api user - $rapi_session = $em->getRepository(RiderAPISession::class)->findOneBy(['capi_user_id' => $capi_user_id]); - - // are we logged in? - if (!$rapi_session->hasRider()) - return new APIResponse(false, 'No logged in rider.'); - - $rider = $rapi_session->getRider(); + // get rider id from capi user metadata + $rider = $this->getRiderFromCAPI($capi_user, $em); + if ($rider == null) + return new APIResponse(false, 'No rider found.'); // do we have a job order? $jo = $rider->getActiveJobOrder(); @@ -402,17 +403,15 @@ class RiderAppController extends APIController { $required_params = ['jo_id']; - // get capi user to link to rider api user - $capi_user_id = $this->getUser()->getID(); + // get capi user + $capi_user = $this->getCAPIUser($this->getUser()->getID(), $em); + if ($capi_user == null) + return new APIResponse(false, 'User not found.'); - // check if capi user already has a rider api user - $rapi_session = $em->getRepository(RiderAPISession::class)->findOneBy(['capi_user_id' => $capi_user_id]); - - // are we logged in? - if (!$rapi_session->hasRider()) - return new APIResponse(false, 'No logged in rider.'); - - $rider = $rapi_session->getRider(); + // get rider id from capi user metadata + $rider = $this->getRiderFromCAPI($capi_user, $em); + if ($rider == null) + return new APIResponse(false, 'No rider found.'); $msg = $this->checkJO($req, $required_params, $jo, $rider); if (!empty($msg)) @@ -453,17 +452,15 @@ class RiderAppController extends APIController { $required_params = ['jo_id']; - // get capi user to link to rider api user - $capi_user_id = $this->getUser()->getID(); + // get capi user + $capi_user = $this->getCAPIUser($this->getUser()->getID(), $em); + if ($capi_user == null) + return new APIResponse(false, 'User not found.'); - // check if capi user already has a rider api user - $rapi_session = $em->getRepository(RiderAPISession::class)->findOneBy(['capi_user_id' => $capi_user_id]); - - // are we logged in? - if (!$rapi_session->hasRider()) - return new APIResponse(false, 'No logged in rider.'); - - $rider = $rapi_session->getRider(); + // get rider id from capi user metadata + $rider = $this->getRiderFromCAPI($capi_user, $em); + if ($rider == null) + return new APIResponse(false, 'No rider found.'); $msg = $this->checkJO($req, $required_params, $jo, $rider); if (!empty($msg)) @@ -510,17 +507,15 @@ class RiderAppController extends APIController return new APIResponse(false, 'Missing parameter(s): ' . $params); } - // get capi user to link to rider api user - $capi_user_id = $this->getUser()->getID(); + // get capi user + $capi_user = $this->getCAPIUser($this->getUser()->getID(), $em); + if ($capi_user == null) + return new APIResponse(false, 'User not found.'); - // check if capi user already has a rider api user - $rapi_session = $em->getRepository(RiderAPISession::class)->findOneBy(['capi_user_id' => $capi_user_id]); - - // are we logged in? - if (!$rapi_session->hasRider()) - return new APIResponse(false, 'No logged in rider.'); - - $rider = $rapi_session->getRider(); + // get rider id from capi user metadata + $rider = $this->getRiderFromCAPI($capi_user, $em); + if ($rider == null) + return new APIResponse(false, 'No rider found.'); // get rider's current job order $jo = $rider->getCurrentJobOrder(); @@ -552,17 +547,15 @@ class RiderAppController extends APIController return new APIResponse(false, 'Missing parameter(s): ' . $params); } - // get capi user to link to rider api user - $capi_user_id = $this->getUser()->getID(); + // get capi user + $capi_user = $this->getCAPIUser($this->getUser()->getID(), $em); + if ($capi_user == null) + return new APIResponse(false, 'User not found.'); - // check if capi user already has a rider api user - $rapi_session = $em->getRepository(RiderAPISession::class)->findOneBy(['capi_user_id' => $capi_user_id]); - - // are we logged in? - if (!$rapi_session->hasRider()) - return new APIResponse(false, 'No logged in rider.'); - - $rider = $rapi_session->getRider(); + // get rider id from capi user metadata + $rider = $this->getRiderFromCAPI($capi_user, $em); + if ($rider == null) + return new APIResponse(false, 'No rider found.'); // get rider's current job order $jo = $rider->getCurrentJobOrder(); @@ -594,17 +587,15 @@ class RiderAppController extends APIController return new APIResponse(false, 'Missing parameter(s): ' . $params); } - // get capi user to link to rider api user - $capi_user_id = $this->getUser()->getID(); + // get capi user + $capi_user = $this->getCAPIUser($this->getUser()->getID(), $em); + if ($capi_user == null) + return new APIResponse(false, 'User not found.'); - // check if capi user already has a rider api user - $rapi_session = $em->getRepository(RiderAPISession::class)->findOneBy(['capi_user_id' => $capi_user_id]); - - // are we logged in? - if (!$rapi_session->hasRider()) - return new APIResponse(false, 'No logged in rider.'); - - $rider = $rapi_session->getRider(); + // get rider id from capi user metadata + $rider = $this->getRiderFromCAPI($capi_user, $em); + if ($rider == null) + return new APIResponse(false, 'No rider found.'); // get rider's current job order $jo = $rider->getCurrentJobOrder(); @@ -636,17 +627,15 @@ class RiderAppController extends APIController return new APIResponse(false, 'Missing parameter(s): ' . $params); } - // get capi user to link to rider api user - $capi_user_id = $this->getUser()->getID(); + // get capi user + $capi_user = $this->getCAPIUser($this->getUser()->getID(), $em); + if ($capi_user == null) + return new APIResponse(false, 'User not found.'); - // check if capi user already has a rider api user - $rapi_session = $em->getRepository(RiderAPISession::class)->findOneBy(['capi_user_id' => $capi_user_id]); - - // are we logged in? - if (!$rapi_session->hasRider()) - return new APIResponse(false, 'No logged in rider.'); - - $rider = $rapi_session->getRider(); + // get rider id from capi user metadata + $rider = $this->getRiderFromCAPI($capi_user, $em); + if ($rider == null) + return new APIResponse(false, 'No rider found.'); // get rider's current job order $jo = $rider->getCurrentJobOrder(); @@ -673,17 +662,15 @@ class RiderAppController extends APIController { $required_params = ['jo_id']; - // get capi user to link to rider api user - $capi_user_id = $this->getUser()->getID(); + // get capi user + $capi_user = $this->getCAPIUser($this->getUser()->getID(), $em); + if ($capi_user == null) + return new APIResponse(false, 'User not found.'); - // check if capi user already has a rider api user - $rapi_session = $em->getRepository(RiderAPISession::class)->findOneBy(['capi_user_id' => $capi_user_id]); - - // are we logged in? - if (!$rapi_session->hasRider()) - return new APIResponse(false, 'No logged in rider.'); - - $rider = $rapi_session->getRider(); + // get rider id from capi user metadata + $rider = $this->getRiderFromCAPI($capi_user, $em); + if ($rider == null) + return new APIResponse(false, 'No rider found.'); $msg = $this->checkJO($req, $required_params, $jo, $rider); if (!empty($msg)) @@ -735,17 +722,15 @@ class RiderAppController extends APIController return new APIResponse(false, 'Missing parameter(s): ' . $params); } - // get capi user to link to rider api user - $capi_user_id = $this->getUser()->getID(); + // get capi user + $capi_user = $this->getCAPIUser($this->getUser()->getID(), $em); + if ($capi_user == null) + return new APIResponse(false, 'User not found.'); - // check if capi user already has a rider api user - $rapi_session = $em->getRepository(RiderAPISession::class)->findOneBy(['capi_user_id' => $capi_user_id]); - - // are we logged in? - if (!$rapi_session->hasRider()) - return new APIResponse(false, 'No logged in rider.'); - - $rider = $rapi_session->getRider(); + // get rider id from capi user metadata + $rider = $this->getRiderFromCAPI($capi_user, $em); + if ($rider == null) + return new APIResponse(false, 'No rider found.'); // get rider's current job order $jo = $rider->getCurrentJobOrder(); @@ -778,17 +763,15 @@ class RiderAppController extends APIController { $required_params = ['jo_id']; - // get capi user to link to rider api user - $capi_user_id = $this->getUser()->getID(); + // get capi user + $capi_user = $this->getCAPIUser($this->getUser()->getID(), $em); + if ($capi_user == null) + return new APIResponse(false, 'User not found.'); - // check if capi user already has a rider api user - $rapi_session = $em->getRepository(RiderAPISession::class)->findOneBy(['capi_user_id' => $capi_user_id]); - - // are we logged in? - if (!$rapi_session->hasRider()) - return new APIResponse(false, 'No logged in rider.'); - - $rider = $rapi_session->getRider(); + // get rider id from capi user metadata + $rider = $this->getRiderFromCAPI($capi_user, $em); + if ($rider == null) + return new APIResponse(false, 'No rider found.'); $msg = $this->checkJO($req, $required_params, $jo, $rider); if (!empty($msg)) @@ -879,8 +862,8 @@ class RiderAppController extends APIController } } - // for riders, use rider session id - $user_id = $rapi_session->getID(); + // for riders, use rider id + $user_id = $rider->getID(); $source = WarrantySource::RAPI; $wh->createWarranty($serial, $plate_number, $first_name, $last_name, $mobile_number, $batt_list, $date_purchase, $warranty_class, $user_id, $source, $jo->getCustomer(), $jo->getCustomerVehicle()->getVehicle()); } @@ -913,17 +896,15 @@ class RiderAppController extends APIController return new APIResponse(false, 'Missing parameter(s): ' . $params); } - // get capi user to link to rider api user - $capi_user_id = $this->getUser()->getID(); + // get capi user + $capi_user = $this->getCAPIUser($this->getUser()->getID(), $em); + if ($capi_user == null) + return new APIResponse(false, 'User not found.'); - // check if capi user already has a rider api user - $rapi_session = $em->getRepository(RiderAPISession::class)->findOneBy(['capi_user_id' => $capi_user_id]); - - // are we logged in? - if (!$rapi_session->hasRider()) - return new APIResponse(false, 'No logged in rider.'); - - $rider = $rapi_session->getRider(); + // get rider id from capi user metadata + $rider = $this->getRiderFromCAPI($capi_user, $em); + if ($rider == null) + return new APIResponse(false, 'No rider found.'); // get rider's current job order $jo = $rider->getCurrentJobOrder(); @@ -956,17 +937,15 @@ class RiderAppController extends APIController return new APIResponse(false, 'Missing parameter(s): ' . $params); } - // get capi user to link to rider api user - $capi_user_id = $this->getUser()->getID(); + // get capi user + $capi_user = $this->getCAPIUser($this->getUser()->getID(), $em); + if ($capi_user == null) + return new APIResponse(false, 'User not found.'); - // check if capi user already has a rider api user - $rapi_session = $em->getRepository(RiderAPISession::class)->findOneBy(['capi_user_id' => $capi_user_id]); - - // are we logged in? - if (!$rapi_session->hasRider()) - return new APIResponse(false, 'No logged in rider.'); - - $rider = $rapi_session->getRider(); + // get rider id from capi user metadata + $rider = $this->getRiderFromCAPI($capi_user, $em); + if ($rider == null) + return new APIResponse(false, 'No rider found.'); // get rider's current job order $jo = $rider->getCurrentJobOrder(); @@ -998,17 +977,15 @@ class RiderAppController extends APIController return new APIResponse(false, 'Missing parameter(s): ' . $params); } - // get capi user to link to rider api user - $capi_user_id = $this->getUser()->getID(); + // get capi user + $capi_user = $this->getCAPIUser($this->getUser()->getID(), $em); + if ($capi_user == null) + return new APIResponse(false, 'User not found.'); - // check if capi user already has a rider api user - $rapi_session = $em->getRepository(RiderAPISession::class)->findOneBy(['capi_user_id' => $capi_user_id]); - - // are we logged in? - if (!$rapi_session->hasRider()) - return new APIResponse(false, 'No logged in rider.'); - - $rider = $rapi_session->getRider(); + // get rider id from capi user metadata + $rider = $this->getRiderFromCAPI($capi_user, $em); + if ($rider == null) + return new APIResponse(false, 'No rider found.'); $rider->setAvailable(true); @@ -1028,15 +1005,15 @@ class RiderAppController extends APIController return new APIResponse(false, 'Missing parameter(s): ' . $params); } - // get capi user to link to rider api user - $capi_user_id = $this->getUser()->getID(); + // get capi user + $capi_user = $this->getCAPIUser($this->getUser()->getID(), $em); + if ($capi_user == null) + return new APIResponse(false, 'User not found.'); - // check if capi user already has a rider api user - $rapi_session = $em->getRepository(RiderAPISession::class)->findOneBy(['capi_user_id' => $capi_user_id]); - - // are we logged in? - if (!$rapi_session->hasRider()) - return new APIResponse(false, 'No logged in rider.'); + // get rider id from capi user metadata + $rider = $this->getRiderFromCAPI($capi_user, $em); + if ($rider == null) + return new APIResponse(false, 'No rider found.'); $promos = $em->getRepository(Promo::class)->findAll(); @@ -1066,16 +1043,16 @@ class RiderAppController extends APIController $params = implode(', ', $missing); return new APIResponse(false, 'Missing parameter(s): ' . $params); } + + // get capi user + $capi_user = $this->getCAPIUser($this->getUser()->getID(), $em); + if ($capi_user == null) + return new APIResponse(false, 'User not found.'); - // get capi user to link to rider api user - $capi_user_id = $this->getUser()->getID(); - - // check if capi user already has a rider api user - $rapi_session = $em->getRepository(RiderAPISession::class)->findOneBy(['capi_user_id' => $capi_user_id]); - - // are we logged in? - if (!$rapi_session->hasRider()) - return new APIResponse(false, 'No logged in rider.'); + // get rider id from capi user metadata + $rider = $this->getRiderFromCAPI($capi_user, $em); + if ($rider == null) + return new APIResponse(false, 'No rider found.'); $batts = $em->getRepository(Battery::class)->findAll(); $models = $em->getRepository(BatteryModel::class)->findAll(); @@ -1126,17 +1103,15 @@ class RiderAppController extends APIController // allow rider to change service, promo, battery and trade-in options $required_params = ['jo_id', 'stype_id', 'promo_id']; - // get capi user to link to rider api user - $capi_user_id = $this->getUser()->getID(); + // get capi user + $capi_user = $this->getCAPIUser($this->getUser()->getID(), $em); + if ($capi_user == null) + return new APIResponse(false, 'User not found.'); - // check if capi user already has a rider api user - $rapi_session = $em->getRepository(RiderAPISession::class)->findOneBy(['capi_user_id' => $capi_user_id]); - - // are we logged in? - if (!$rapi_session->hasRider()) - return new APIResponse(false, 'No logged in rider.'); - - $rider = $rapi_session->getRider(); + // get rider id from capi user metadata + $rider = $this->getRiderFromCAPI($capi_user, $em); + if ($rider == null) + return new APIResponse(false, 'No rider found.'); $msg = $this->checkJO($req, $required_params, $jo, $rider); if (!empty($msg)) @@ -1262,6 +1237,26 @@ class RiderAppController extends APIController return new APIResponse(true, 'Job order service changed.', $data); } + protected function getCAPIUser($id, EntityManagerInterface $em) + { + $capi_user = $em->getRepository(APIUser::class)->find($id); + return $capi_user; + } + + protected function getRiderFromCAPI($capi_user, $em) + { + // TODO: uncomment once getMetadata is available + /* + $metadata = $capi_user->getMetadata(); + //get rider id from metadata + $rider_id = $metadata['rider_id']; + // get rider + $rider = $em->getRepository(Rider::class)->find($rider_id); + return $rider; + */ + + } + protected function checkMissingParameters(Request $req, $params = []) { $missing = [];