Move cancelJobOrder to the service. #311

This commit is contained in:
Korina Cordero 2020-01-31 08:16:11 +00:00
parent de4bf9f271
commit b651d06ec8
3 changed files with 83 additions and 29 deletions

View file

@ -291,40 +291,27 @@ class RAPIController extends Controller
return $res->getReturnResponse(); return $res->getReturnResponse();
} }
public function cancelJobOrder(Request $req, MQTTClient $mclient) public function cancelJobOrder(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();
// $jo->cancel("rider cancelled"); $data = $rapi_handler->cancelJobOrder($req);
// requeue it, instead of cancelling it
$jo->requeue();
// add event log
$rider = $this->session->getRider();
$event = new JOEvent();
$event->setDateHappen(new DateTime())
->setTypeID(JOEventType::REQUEUE)
->setJobOrder($jo)
->setRider($rider);
$em->persist($event);
$em->flush();
// send mqtt event
// send outlet assign since order should go back to hub and await reassignment to another rider
$payload = [
'event' => 'outlet_assign',
'jo_id' => $jo->getID(),
];
$mclient->sendEvent($jo, $payload);
if (isset($data['error']))
{
$message = $data['error'];
$res->setError(true)
->setErrorMessage($message);
}
else
{
$res->setData($data);
}
// response
return $res->getReturnResponse(); return $res->getReturnResponse();
} }
public function arrive(Request $req, MQTTClient $mclient) public function arrive(Request $req, MQTTClient $mclient)

View file

@ -14,6 +14,7 @@ use App\Ramcar\JOEventType;
use App\Service\RiderAPIHandlerInterface; use App\Service\RiderAPIHandlerInterface;
use App\Service\RedisClientProvider; use App\Service\RedisClientProvider;
use App\Service\RiderCache; use App\Service\RiderCache;
use App\Service\MQTTClient;
use App\Entity\RiderSession; use App\Entity\RiderSession;
use App\Entity\Rider; use App\Entity\Rider;
@ -29,17 +30,19 @@ class CMBRiderAPIHandler implements RiderAPIHandlerInterface
protected $ef; protected $ef;
protected $rcache; protected $rcache;
protected $country_code; protected $country_code;
protected $mclient;
protected $session; protected $session;
public function __construct(EntityManagerInterface $em, RedisClientProvider $redis, public function __construct(EntityManagerInterface $em, RedisClientProvider $redis,
EncoderFactoryInterface $ef, RiderCache $rcache, EncoderFactoryInterface $ef, RiderCache $rcache,
string $country_code) string $country_code, MQTTClient $mclient)
{ {
$this->em = $em; $this->em = $em;
$this->redis = $redis; $this->redis = $redis;
$this->ef = $ef; $this->ef = $ef;
$this->rcache = $rcache; $this->rcache = $rcache;
$this->country_code = $country_code; $this->country_code = $country_code;
$this->mclient = $mclient;
// one device = one session, since we have control over the devices // one device = one session, since we have control over the devices
// when a rider logs in, we just change the rider assigned to the device // when a rider logs in, we just change the rider assigned to the device
@ -370,7 +373,39 @@ class CMBRiderAPIHandler implements RiderAPIHandlerInterface
$this->em->flush(); $this->em->flush();
return $data; return $data;
}
public function cancelJobOrder(Request $req)
{
$required_params = ['jo_id'];
$data = $this->checkJO($req, $required_params, $jo);
if (isset($data['error']))
return $data;
// $jo->cancel("rider cancelled");
// requeue it, instead of cancelling it
$jo->requeue();
// add event log
$rider = $this->session->getRider();
$event = new JOEvent();
$event->setDateHappen(new DateTime())
->setTypeID(JOEventType::REQUEUE)
->setJobOrder($jo)
->setRider($rider);
$this->em->persist($event);
$this->em->flush();
// send mqtt event
// send outlet assign since order should go back to hub and await reassignment to another rider
$payload = [
'event' => 'outlet_assign',
'jo_id' => $jo->getID(),
];
$this->mclient->sendEvent($jo, $payload);
return $data;
} }
protected function checkMissingParameters(Request $req, $params = []) protected function checkMissingParameters(Request $req, $params = [])

View file

@ -370,7 +370,39 @@ class ResqRiderAPIHandler implements RiderAPIHandlerInterface
$this->em->flush(); $this->em->flush();
return $data; return $data;
}
public function cancelJobOrder(Request $req)
{
$required_params = ['jo_id'];
$data = $this->checkJO($req, $required_params, $jo);
if (isset($data['error']))
return $data;
// $jo->cancel("rider cancelled");
// requeue it, instead of cancelling it
$jo->requeue();
// add event log
$rider = $this->session->getRider();
$event = new JOEvent();
$event->setDateHappen(new DateTime())
->setTypeID(JOEventType::REQUEUE)
->setJobOrder($jo)
->setRider($rider);
$this->em->persist($event);
$this->em->flush();
// send mqtt event
// send outlet assign since order should go back to hub and await reassignment to another rider
$payload = [
'event' => 'outlet_assign',
'jo_id' => $jo->getID(),
];
$this->mclient->sendEvent($jo, $payload);
return $data;
} }
protected function checkMissingParameters(Request $req, $params = []) protected function checkMissingParameters(Request $req, $params = [])