Add job order queueing for riders assignments. #270

This commit is contained in:
Korina Cordero 2019-11-14 05:54:37 +00:00
parent 174f0142c9
commit 4549e124eb
4 changed files with 129 additions and 23 deletions

View file

@ -153,3 +153,9 @@ services:
# customer generator interface
App\Service\CustomerHandlerInterface: "@App\\Service\\CustomerHandler\\CMBCustomerHandler"
# rider assignment
App\Service\RiderAssignmentHandler\CMBRiderAssignmentHandler: ~
# rider assignment interface
App\Service\RiderAssignmentHandlerInterface: "@App\\Service\\RiderAssignmentHandler\\CMBRiderAssignmentHandler"

View file

@ -39,6 +39,7 @@ use App\Ramcar\JORejectionReason;
use App\Service\InvoiceGeneratorInterface;
use App\Service\JobOrderHandlerInterface;
use App\Service\RiderAssignmentHandlerInterface;
use App\Service\MQTTClient;
use App\Service\APNSClient;
use App\Service\MapTools;
@ -61,18 +62,20 @@ class CMBJobOrderHandler implements JobOrderHandlerInterface
protected $security;
protected $validator;
protected $translator;
protected $rah;
protected $template_hash;
public function __construct(Security $security, EntityManagerInterface $em,
InvoiceGeneratorInterface $ic, ValidatorInterface $validator,
TranslatorInterface $translator)
TranslatorInterface $translator, RiderAssignmentHandlerInterface $rah)
{
$this->em = $em;
$this->ic = $ic;
$this->security = $security;
$this->validator = $validator;
$this->translator = $translator;
$this->rah = $rah;
$this->loadTemplates();
}
@ -584,9 +587,6 @@ class CMBJobOrderHandler implements JobOrderHandlerInterface
if (empty($error_array))
{
// set rider unavailable
$rider->setAvailable(false);
// the event
$event = new JOEvent();
$event->setDateHappen(new DateTime())
@ -603,21 +603,16 @@ class CMBJobOrderHandler implements JobOrderHandlerInterface
// validated! save the entity
$em->flush();
// send event to mobile app
$payload = [
'event' => 'driver_assigned'
];
$mclient->sendEvent($obj, $payload);
$mclient->sendRiderEvent($obj, $payload);
// sned push notification
$aclient->sendPush($obj, "A RESQ rider is on his way to you.");
// call rider assignment handler's assignJobOrder
$this->rah->assignJobOrder($obj, $rider);
}
return $error_array;
}
// fulfill job order
// TODO: event sending has been moved to rider assignment handler for cmb. Might need
// to consider resq implementation for event sending for the other methods.
public function fulfillJobOrder(Request $req, $id, MQTTClient $mclient)
{
// initialize error list
@ -694,16 +689,8 @@ class CMBJobOrderHandler implements JobOrderHandlerInterface
if ($rider->getImageFile() != null)
$image_url = $req->getScheme() . '://' . $req->getHttpHost() . $req->getBasePath() . '/uploads/' . $rider->getImageFile();
// send to mqtt
$payload = [
'event' => 'fulfilled',
'jo_id' => $obj->getID(),
'driver_image' => $image_url,
'driver_name' => $rider->getFullName(),
'driver_id' => $rider->getID(),
];
$mclient->sendEvent($obj, $payload);
$mclient->sendRiderEvent($obj, $payload);
// call rider assignment handler's fulfillJobOrder
$this->rah->fulfillJobOrder($obj, $image_url, $rider);
// create the warranty if new battery only
if ($obj->getServiceType () == CMBServiceType::BATTERY_REPLACEMENT_NEW)

View file

@ -0,0 +1,98 @@
<?php
namespace App\Service\RiderAssignmentHandler;
use Doctrine\ORM\EntityManagerInterface;
use App\Service\RiderAssignmentHandlerInterface;
use App\Service\MQTTClient;
use App\Service\APNSClient;
use App\Entity\JobOrder;
use App\Entity\Rider;
use App\Ramcar\JOStatus;
class CMBRiderAssignmentHandler implements RiderAssignmentHandlerInterface
{
protected $em;
protected $aclient;
protected $mclient;
public function __construct(EntityManagerInterface $em, MQTTClient $mclient,
APNSClient $aclient)
{
$this->em = $em;
$this->mclient = $mclient;
$this->aclient = $aclient;
}
// assign job order to rider
public function assignJobOrder(JobOrder $obj, Rider $rider)
{
// create the payload
$payload = [
'event' => 'driver_assigned'
];
// send event
$this->mclient->sendEvent($obj, $payload);
// check if rider is available
if ($rider->isAvailable())
{
error_log('set rider availability to false');
// set rider to unavailable
$rider->setAvailable(false);
// send event to rider
$this->mclient->sendRiderEvent($obj, $payload);
// send push notification
$this->aclient->sendPush($obj, "A RESQ rider is on his way to you.");
}
}
// complete job order
public function fulfillJobOrder(JobOrder $obj, string $image_url, Rider $rider)
{
// send to mqtt
$payload = [
'event' => 'fulfilled',
'jo_id' => $obj->getID(),
'driver_image' => $image_url,
'driver_name' => $rider->getFullName(),
'driver_id' => $rider->getID(),
];
$this->mclient->sendEvent($obj, $payload);
// send fulfill/complete event to rider
$this->mclient->sendRiderEvent($obj, $payload);
// search for the JO assigned to rider with JOStatus::ASSIGNED and sort by assign date
$jo_results = $this->em->getRepository(JobOrder::class)->findBy(['status' => JOStatus::ASSIGNED, 'rider' => $rider->getID()],
['date_assign' => 'ASC']);
// check if jo_results is empty
if (!empty($jo_results))
{
error_log('rider has another JO in queue');
// get first entry
$jo = current($jo_results);
// form the payload for the next job order
$jo_payload = [
'event' => 'driver_assigned'
];
// set rider to unavailable
$rider->setAvailable(false);
// send event to rider
$this->mclient->sendRiderEvent($jo, $jo_payload);
// send push notification
$this->aclient->sendPush($obj, "A RESQ rider is on his way to you.");
}
}
}

View file

@ -0,0 +1,15 @@
<?php
namespace App\Service;
use App\Entity\JobOrder;
use App\Entity\Rider;
interface RiderAssignmentHandlerInterface
{
// assign job order to rider
public function assignJobOrder(JobOrder $obj, Rider $rider);
// complete job order
public function fulfillJobOrder(JobOrder $obj, string $image_url, Rider $rider);
}