Create rider api session entity. Add register for new rider controller. #617
This commit is contained in:
parent
55c8c393b2
commit
446065880c
2 changed files with 336 additions and 0 deletions
202
src/Controller/CAPI/RiderController.php
Normal file
202
src/Controller/CAPI/RiderController.php
Normal file
|
|
@ -0,0 +1,202 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Controller\CAPI;
|
||||||
|
|
||||||
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
|
||||||
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
|
use Doctrine\ORM\Query;
|
||||||
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
|
use Catalyst\APIBundle\Controller\APIController;
|
||||||
|
use Catalyst\APIBundle\Response\APIResponse;
|
||||||
|
|
||||||
|
use App\Entity\Rider;
|
||||||
|
use App\Entity\JOEvent;
|
||||||
|
use App\Entity\Promo;
|
||||||
|
use App\Entity\Battery;
|
||||||
|
use App\Entity\BatteryModel;
|
||||||
|
use App\Entity\BatterySize;
|
||||||
|
use App\Entity\RiderAPISession;
|
||||||
|
|
||||||
|
use App\Service\RedisClientProvider;
|
||||||
|
use App\Service\RiderCache;
|
||||||
|
use App\Service\MQTTClient;
|
||||||
|
use App\Service\WarrantyHandler;
|
||||||
|
use App\Service\JobOrderHandlerInterface;
|
||||||
|
use App\Service\InvoiceGeneratorInterface;
|
||||||
|
use App\Service\RisingTideGateway;
|
||||||
|
use App\Service\RiderTracker;
|
||||||
|
|
||||||
|
// third party API for rider
|
||||||
|
class RiderController extends APIController
|
||||||
|
{
|
||||||
|
public function register(Request $req, EntityManagerInterface $em, RedisClientProvider $redis)
|
||||||
|
{
|
||||||
|
// confirm parameters
|
||||||
|
$required_params = [
|
||||||
|
'phone_number',
|
||||||
|
'device_push_id'
|
||||||
|
];
|
||||||
|
|
||||||
|
$missing = $this->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;
|
||||||
|
}
|
||||||
|
}
|
||||||
134
src/Entity/RiderAPISession.php
Normal file
134
src/Entity/RiderAPISession.php
Normal file
|
|
@ -0,0 +1,134 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Entity;
|
||||||
|
|
||||||
|
use Doctrine\ORM\Mapping as ORM;
|
||||||
|
use DateTime;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ORM\Entity
|
||||||
|
* @ORM\Table(name="rider_api_session")
|
||||||
|
*/
|
||||||
|
class RiderAPISession
|
||||||
|
{
|
||||||
|
// unique id
|
||||||
|
/**
|
||||||
|
* @ORM\Id
|
||||||
|
* @ORM\Column(type="string", length=13)
|
||||||
|
*/
|
||||||
|
protected $id;
|
||||||
|
|
||||||
|
// device id or push id used by device
|
||||||
|
/**
|
||||||
|
* @ORM\Column(type="string", length=50)
|
||||||
|
*/
|
||||||
|
protected $device_push_id;
|
||||||
|
|
||||||
|
// link to customer
|
||||||
|
/**
|
||||||
|
* @ORM\ManyToOne(targetEntity="Rider", inversedBy="api_sessions")
|
||||||
|
* @ORM\JoinColumn(name="rider_id", referencedColumnName="id", nullable=true)
|
||||||
|
*/
|
||||||
|
protected $rider;
|
||||||
|
|
||||||
|
// phone number
|
||||||
|
/**
|
||||||
|
* @ORM\Column(type="string", length=12, nullable=true)
|
||||||
|
*/
|
||||||
|
protected $phone_number;
|
||||||
|
|
||||||
|
// is this device active
|
||||||
|
/**
|
||||||
|
* @ORM\Column(type="boolean")
|
||||||
|
*/
|
||||||
|
protected $is_active;
|
||||||
|
|
||||||
|
// capi user id loosely associated to mobile user
|
||||||
|
/**
|
||||||
|
* @ORM\Column(type="integer", nullable=true)
|
||||||
|
*/
|
||||||
|
protected $capi_user_id;
|
||||||
|
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
// default date generated to now
|
||||||
|
$this->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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Loading…
Reference in a new issue