Add sending of mqtt event when JO is assigned to rider. #424

This commit is contained in:
Korina Cordero 2020-06-22 10:05:01 +00:00
parent a3cf1572f1
commit 42d086ff5d
3 changed files with 24 additions and 6 deletions

View file

@ -867,14 +867,14 @@ class JobOrderController extends Controller
return $this->render($template, $params);
}
public function oneStepSubmit(Request $req, JobOrderHandlerInterface $jo_handler)
public function oneStepSubmit(Request $req, JobOrderHandlerInterface $jo_handler, MQTTClient $mclient)
{
$this->denyAccessUnlessGranted('jo_onestep.form', null, 'No access.');
// initialize error list
$error_array = [];
$id = -1;
$error_array = $jo_handler->processOneStepJobOrder($req, $id);
$error_array = $jo_handler->processOneStepJobOrder($req, $id, $mclient);
// check if any errors were found
if (!empty($error_array)) {
@ -913,12 +913,12 @@ class JobOrderController extends Controller
return $this->render($template, $params);
}
public function oneStepEditSubmit(Request $req, JobOrderHandlerInterface $jo_handler, $id)
public function oneStepEditSubmit(Request $req, JobOrderHandlerInterface $jo_handler, $id, MQTTClient $mclient)
{
$this->denyAccessUnlessGranted('jo_onestep.edit', null, 'No access.');
$error_array = [];
$error_array = $jo_handler->processOneStepJobOrder($req, $id);
$error_array = $jo_handler->processOneStepJobOrder($req, $id, $mclient);
// check if any errors were found
if (!empty($error_array)) {

View file

@ -416,7 +416,7 @@ class CMBJobOrderHandler implements JobOrderHandlerInterface
return $error_array;
}
public function processOneStepJobOrder(Request $req, $id)
public function processOneStepJobOrder(Request $req, $id, MQTTClient $mclient)
{
// initialize error list
$error_array = [];
@ -424,11 +424,16 @@ class CMBJobOrderHandler implements JobOrderHandlerInterface
$em = $this->em;
$jo = $em->getRepository(JobOrder::class)->find($id);
$old_rider = null;
if (empty($jo))
{
// new job order
$jo = new JobOrder();
}
else
{
$old_rider = $jo->getRider();
}
// check if lat and lng are provided
if (empty($req->request->get('coord_lng')) || empty($req->request->get('coord_lat'))) {
@ -665,6 +670,19 @@ class CMBJobOrderHandler implements JobOrderHandlerInterface
$em->persist($event);
$em->flush();
// check if JO has been reassigned
if ($old_rider != $rider)
{
// TODO: refactor later
$channel = '/rider/' . $rider->getID() . '/events';
$payload = [
'event' => 'new_jo',
'jo_id' => $jo->getID(),
];
$mclient->publish($channel, json_encode($payload));
}
}
}

View file

@ -25,7 +25,7 @@ interface JobOrderHandlerInterface
public function generateJobOrder(Request $req, int $id);
// process one step job order
public function processOneStepJobOrder(Request $req, int $id);
//public function processOneStepJobOrder(Request $req, int $id, MQTTClient $mclient);
// dispatch job order
public function dispatchJobOrder(Request $req, int $id, MQTTClient $mclient);