From 446065880cc031ff84bde4679ed309cfcb63dfae Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Wed, 18 Aug 2021 06:28:19 +0000 Subject: [PATCH] Create rider api session entity. Add register for new rider controller. #617 --- src/Controller/CAPI/RiderController.php | 202 ++++++++++++++++++++++++ src/Entity/RiderAPISession.php | 134 ++++++++++++++++ 2 files changed, 336 insertions(+) create mode 100644 src/Controller/CAPI/RiderController.php create mode 100644 src/Entity/RiderAPISession.php diff --git a/src/Controller/CAPI/RiderController.php b/src/Controller/CAPI/RiderController.php new file mode 100644 index 00000000..81b85ee6 --- /dev/null +++ b/src/Controller/CAPI/RiderController.php @@ -0,0 +1,202 @@ +checkMissingParameters($req, $required_params); + if (count($missing) > 0) + { + $params = implode(', ', $missing); + return new APIResponse(false, 'Missing parameter(s): ' . $params); + } + + // 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 + $rider_api_user = $em->getRepository(RiderAPISession::class)->findOneBy(['capi_user_id' => $capi_user_id]); + + if ($rider_api_user != null) + return new APIResponse(false, 'User already registered'); + + // retry until we get a unique id + while (true) + { + try + { + // instantiate session + $sess = new RiderAPISession(); + $sess->setPhoneNumber($req->request->get('phone_number')) + ->setDevicePushID($req->request->get('device_push_id')) + ->setCapiUserId($capi_user_id); + + // reopen in case we get an exception + if (!$em->isOpen()) + { + $em = $em->create( + $em->getConnection(), + $em->getConfiguration() + ); + } + + // save + $em->persist($sess); + $em->flush(); + + // create redis entry for the session + $redis_client = $redis->getRedisClient(); + $redis_key = 'rider.id.' . $sess->getID(); + error_log('redis_key: ' . $redis_key); + $redis_client->set($redis_key, ''); + } + catch (DBALException $e) + { + error_log($e->getMessage()); + // delay one second and try again + sleep(1); + continue; + } + + break; + } + + // return data + $data = [ + 'session_id' => $sess->getID() + ]; + + return new APIResponse(true, 'Rider API user created.', $data); + } + + public function login(Request $req) + { + } + + public function logout(Request $req) + { + } + + public function getJobOrder(Request $req) + { + } + + public function acceptJobOrder(Request $req) + { + } + + public function cancelJobOrder(Request $req) + { + } + + public function arrive(Request $req) + { + } + + public function hubArrive(Request $req) + { + } + + public function payment(Request $req) + { + } + + public function available(Request $req) + { + } + + public function getPromos(Request $req) + { + } + + public function getBatteries(Request $req) + { + } + + public function changeService(Request $req) + { + } + + public function hubDepart(Request $req) + { + } + + public function preHubArrive(Request $req) + { + } + + public function preHubDepart(Request $req) + { + } + + public function startJobOrder(Request $req) + { + } + + public function postHubArrive(Request $req) + { + } + + public function postHubDepart(Request $req) + { + } + + protected function checkMissingParameters(Request $req, $params = []) + { + $missing = []; + + // check if parameters are there + foreach ($params as $param) + { + if ($req->getMethod() == 'GET') + { + $check = $req->query->get($param); + if ($check == null) + $missing[] = $param; + } + else if ($req->getMethod() == 'POST') + { + $check = $req->request->get($param); + if ($check == null) + $missing[] = $param; + } + else + return $params; + } + + return $missing; + } +} diff --git a/src/Entity/RiderAPISession.php b/src/Entity/RiderAPISession.php new file mode 100644 index 00000000..98228268 --- /dev/null +++ b/src/Entity/RiderAPISession.php @@ -0,0 +1,134 @@ +id = $this->generateKeyID(); + $this->rider = null; + $this->is_active = true; + $this->capi_user_id = 0; + } + + public function generateKeyID() + { + // use uniqid for now, since primary key dupes will trigger exceptions + return uniqid(); + } + + public function getID() + { + return $this->id; + } + + public function setDevicePushID($id) + { + $this->device_push_id = $id; + return $this; + } + + public function getDevicePushID() + { + return $this->device_push_id; + } + + public function setRider(Rider $rider = null) + { + $this->rider = $rider; + return $this; + } + + public function getRider() + { + return $this->rider; + } + + public function setPhoneNumber($num) + { + $this->phone_number = $num; + return $this; + } + + public function getPhoneNumber() + { + return $this->phone_number; + } + + public function setActive($flag = true) + { + $this->is_active = $flag; + return $this; + } + + public function isActive() + { + return $this->is_active; + } + + public function hasRider() + { + if ($this->rider == null) + return false; + + return true; + } + + public function setCapiUserId($capi_user_id) + { + $this->capi_user_id = $capi_user_id; + } + + public function getCapiUserId() + { + return $this->capi_user_id; + } +} +