Move acceptJobOrder to the service. #311
This commit is contained in:
parent
35ccaa73c3
commit
becd8d0c17
4 changed files with 156 additions and 23 deletions
|
|
@ -269,32 +269,25 @@ class RAPIController extends Controller
|
||||||
return $res;
|
return $res;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function acceptJobOrder(Request $req)
|
public function acceptJobOrder(Request $req, RiderAPIHandlerInterface $rapi_handler)
|
||||||
{
|
{
|
||||||
$em = $this->getDoctrine()->getManager();
|
$res = new APIResult();
|
||||||
$required_params = ['jo_id'];
|
|
||||||
$res = $this->checkJO($req, $required_params, $jo);
|
|
||||||
if ($res->isError())
|
|
||||||
return $res->getReturnResponse();
|
|
||||||
|
|
||||||
// TODO: refactor this into a jo handler class, so we don't have to repeat for control center
|
$data = $rapi_handler->acceptJobOrder($req);
|
||||||
|
|
||||||
// set jo status to in transit
|
if (isset($data['error']))
|
||||||
$jo->setStatus(JOStatus::IN_TRANSIT);
|
{
|
||||||
|
$message = $data['error'];
|
||||||
// TODO: send mqtt event (?)
|
|
||||||
|
|
||||||
// add event log
|
$res->setError(true)
|
||||||
$rider = $this->session->getRider();
|
->setErrorMessage($message);
|
||||||
$event = new JOEvent();
|
}
|
||||||
$event->setDateHappen(new DateTime())
|
else
|
||||||
->setTypeID(JOEventType::RIDER_ACCEPT)
|
{
|
||||||
->setJobOrder($jo)
|
$res->setData($data);
|
||||||
->setRider($rider);
|
}
|
||||||
$em->persist($event);
|
|
||||||
|
|
||||||
$em->flush();
|
|
||||||
|
|
||||||
|
// response
|
||||||
return $res->getReturnResponse();
|
return $res->getReturnResponse();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -339,6 +339,35 @@ class CMBRiderAPIHandler implements RiderAPIHandlerInterface
|
||||||
return $data;
|
return $data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function acceptJobOrder(Request $req)
|
||||||
|
{
|
||||||
|
$required_params = ['jo_id'];
|
||||||
|
$data = $this->checkJO($req, $required_params, $jo);
|
||||||
|
if (isset($data['error']))
|
||||||
|
return $data;
|
||||||
|
|
||||||
|
// TODO: refactor this into a jo handler class, so we don't have to repeat for control center
|
||||||
|
|
||||||
|
// set jo status to in transit
|
||||||
|
$jo->setStatus(JOStatus::IN_TRANSIT);
|
||||||
|
|
||||||
|
// TODO: send mqtt event (?)
|
||||||
|
|
||||||
|
// add event log
|
||||||
|
$rider = $this->session->getRider();
|
||||||
|
$event = new JOEvent();
|
||||||
|
$event->setDateHappen(new DateTime())
|
||||||
|
->setTypeID(JOEventType::RIDER_ACCEPT)
|
||||||
|
->setJobOrder($jo)
|
||||||
|
->setRider($rider);
|
||||||
|
$this->em->persist($event);
|
||||||
|
|
||||||
|
$this->em->flush();
|
||||||
|
|
||||||
|
return $data;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
protected function checkMissingParameters(Request $req, $params = [])
|
protected function checkMissingParameters(Request $req, $params = [])
|
||||||
{
|
{
|
||||||
$missing = [];
|
$missing = [];
|
||||||
|
|
@ -406,7 +435,6 @@ class CMBRiderAPIHandler implements RiderAPIHandlerInterface
|
||||||
return $data;
|
return $data;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: type hint entity manager
|
|
||||||
protected function checkAPIKey($api_key)
|
protected function checkAPIKey($api_key)
|
||||||
{
|
{
|
||||||
// find the api key (session id)
|
// find the api key (session id)
|
||||||
|
|
@ -416,4 +444,45 @@ class CMBRiderAPIHandler implements RiderAPIHandlerInterface
|
||||||
|
|
||||||
return $session;
|
return $session;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected function checkJO(Request $req, $required_params, &$jo = null)
|
||||||
|
{
|
||||||
|
// set jo status to in transit
|
||||||
|
$data = $this->checkParamsAndKey($req, $required_params);
|
||||||
|
if (isset($data['error']))
|
||||||
|
return $data;
|
||||||
|
|
||||||
|
// are we logged in?
|
||||||
|
if (!$this->session->hasRider())
|
||||||
|
{
|
||||||
|
$data = [
|
||||||
|
'error' => 'No logged in rider.'
|
||||||
|
];
|
||||||
|
return $data;
|
||||||
|
}
|
||||||
|
|
||||||
|
$rider = $this->session->getRider();
|
||||||
|
|
||||||
|
// check if we have an active JO
|
||||||
|
$jo = $rider->getActiveJobOrder();
|
||||||
|
if ($jo == null)
|
||||||
|
{
|
||||||
|
$data = [
|
||||||
|
'error' => 'No active job order.'
|
||||||
|
];
|
||||||
|
return $data;
|
||||||
|
}
|
||||||
|
|
||||||
|
// check if the jo_id sent is the same as our active jo
|
||||||
|
if ($req->request->get('jo_id') != $jo->getID())
|
||||||
|
{
|
||||||
|
$data = [
|
||||||
|
'error' => 'Job order selected is not active job order.'
|
||||||
|
];
|
||||||
|
return $data;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $data;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -339,6 +339,35 @@ class ResqRiderAPIHandler implements RiderAPIHandlerInterface
|
||||||
return $data;
|
return $data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function acceptJobOrder(Request $req)
|
||||||
|
{
|
||||||
|
$required_params = ['jo_id'];
|
||||||
|
$data = $this->checkJO($req, $required_params, $jo);
|
||||||
|
if (isset($data['error']))
|
||||||
|
return $data;
|
||||||
|
|
||||||
|
// TODO: refactor this into a jo handler class, so we don't have to repeat for control center
|
||||||
|
|
||||||
|
// set jo status to in transit
|
||||||
|
$jo->setStatus(JOStatus::IN_TRANSIT);
|
||||||
|
|
||||||
|
// TODO: send mqtt event (?)
|
||||||
|
|
||||||
|
// add event log
|
||||||
|
$rider = $this->session->getRider();
|
||||||
|
$event = new JOEvent();
|
||||||
|
$event->setDateHappen(new DateTime())
|
||||||
|
->setTypeID(JOEventType::RIDER_ACCEPT)
|
||||||
|
->setJobOrder($jo)
|
||||||
|
->setRider($rider);
|
||||||
|
$this->em->persist($event);
|
||||||
|
|
||||||
|
$this->em->flush();
|
||||||
|
|
||||||
|
return $data;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
protected function checkMissingParameters(Request $req, $params = [])
|
protected function checkMissingParameters(Request $req, $params = [])
|
||||||
{
|
{
|
||||||
$missing = [];
|
$missing = [];
|
||||||
|
|
@ -406,7 +435,6 @@ class ResqRiderAPIHandler implements RiderAPIHandlerInterface
|
||||||
return $data;
|
return $data;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: type hint entity manager
|
|
||||||
protected function checkAPIKey($api_key)
|
protected function checkAPIKey($api_key)
|
||||||
{
|
{
|
||||||
// find the api key (session id)
|
// find the api key (session id)
|
||||||
|
|
@ -416,4 +444,45 @@ class ResqRiderAPIHandler implements RiderAPIHandlerInterface
|
||||||
|
|
||||||
return $session;
|
return $session;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected function checkJO(Request $req, $required_params, &$jo = null)
|
||||||
|
{
|
||||||
|
// set jo status to in transit
|
||||||
|
$data = $this->checkParamsAndKey($req, $required_params);
|
||||||
|
if (isset($data['error']))
|
||||||
|
return $data;
|
||||||
|
|
||||||
|
// are we logged in?
|
||||||
|
if (!$this->session->hasRider())
|
||||||
|
{
|
||||||
|
$data = [
|
||||||
|
'error' => 'No logged in rider.'
|
||||||
|
];
|
||||||
|
return $data;
|
||||||
|
}
|
||||||
|
|
||||||
|
$rider = $this->session->getRider();
|
||||||
|
|
||||||
|
// check if we have an active JO
|
||||||
|
$jo = $rider->getActiveJobOrder();
|
||||||
|
if ($jo == null)
|
||||||
|
{
|
||||||
|
$data = [
|
||||||
|
'error' => 'No active job order.'
|
||||||
|
];
|
||||||
|
return $data;
|
||||||
|
}
|
||||||
|
|
||||||
|
// check if the jo_id sent is the same as our active jo
|
||||||
|
if ($req->request->get('jo_id') != $jo->getID())
|
||||||
|
{
|
||||||
|
$data = [
|
||||||
|
'error' => 'Job order selected is not active job order.'
|
||||||
|
];
|
||||||
|
return $data;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $data;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -13,4 +13,6 @@ interface RiderAPIHandlerInterface
|
||||||
public function logout(Request $req);
|
public function logout(Request $req);
|
||||||
|
|
||||||
public function getJobOrder(Request $req);
|
public function getJobOrder(Request $req);
|
||||||
|
|
||||||
|
public function acceptJobOrder(Request $req);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue