Add job order queueing for riders assignments. #270
This commit is contained in:
parent
174f0142c9
commit
4549e124eb
4 changed files with 129 additions and 23 deletions
|
|
@ -153,3 +153,9 @@ services:
|
||||||
|
|
||||||
# customer generator interface
|
# customer generator interface
|
||||||
App\Service\CustomerHandlerInterface: "@App\\Service\\CustomerHandler\\CMBCustomerHandler"
|
App\Service\CustomerHandlerInterface: "@App\\Service\\CustomerHandler\\CMBCustomerHandler"
|
||||||
|
|
||||||
|
# rider assignment
|
||||||
|
App\Service\RiderAssignmentHandler\CMBRiderAssignmentHandler: ~
|
||||||
|
|
||||||
|
# rider assignment interface
|
||||||
|
App\Service\RiderAssignmentHandlerInterface: "@App\\Service\\RiderAssignmentHandler\\CMBRiderAssignmentHandler"
|
||||||
|
|
|
||||||
|
|
@ -39,6 +39,7 @@ use App\Ramcar\JORejectionReason;
|
||||||
|
|
||||||
use App\Service\InvoiceGeneratorInterface;
|
use App\Service\InvoiceGeneratorInterface;
|
||||||
use App\Service\JobOrderHandlerInterface;
|
use App\Service\JobOrderHandlerInterface;
|
||||||
|
use App\Service\RiderAssignmentHandlerInterface;
|
||||||
use App\Service\MQTTClient;
|
use App\Service\MQTTClient;
|
||||||
use App\Service\APNSClient;
|
use App\Service\APNSClient;
|
||||||
use App\Service\MapTools;
|
use App\Service\MapTools;
|
||||||
|
|
@ -61,18 +62,20 @@ class CMBJobOrderHandler implements JobOrderHandlerInterface
|
||||||
protected $security;
|
protected $security;
|
||||||
protected $validator;
|
protected $validator;
|
||||||
protected $translator;
|
protected $translator;
|
||||||
|
protected $rah;
|
||||||
|
|
||||||
protected $template_hash;
|
protected $template_hash;
|
||||||
|
|
||||||
public function __construct(Security $security, EntityManagerInterface $em,
|
public function __construct(Security $security, EntityManagerInterface $em,
|
||||||
InvoiceGeneratorInterface $ic, ValidatorInterface $validator,
|
InvoiceGeneratorInterface $ic, ValidatorInterface $validator,
|
||||||
TranslatorInterface $translator)
|
TranslatorInterface $translator, RiderAssignmentHandlerInterface $rah)
|
||||||
{
|
{
|
||||||
$this->em = $em;
|
$this->em = $em;
|
||||||
$this->ic = $ic;
|
$this->ic = $ic;
|
||||||
$this->security = $security;
|
$this->security = $security;
|
||||||
$this->validator = $validator;
|
$this->validator = $validator;
|
||||||
$this->translator = $translator;
|
$this->translator = $translator;
|
||||||
|
$this->rah = $rah;
|
||||||
|
|
||||||
$this->loadTemplates();
|
$this->loadTemplates();
|
||||||
}
|
}
|
||||||
|
|
@ -584,9 +587,6 @@ class CMBJobOrderHandler implements JobOrderHandlerInterface
|
||||||
|
|
||||||
if (empty($error_array))
|
if (empty($error_array))
|
||||||
{
|
{
|
||||||
// set rider unavailable
|
|
||||||
$rider->setAvailable(false);
|
|
||||||
|
|
||||||
// the event
|
// the event
|
||||||
$event = new JOEvent();
|
$event = new JOEvent();
|
||||||
$event->setDateHappen(new DateTime())
|
$event->setDateHappen(new DateTime())
|
||||||
|
|
@ -603,21 +603,16 @@ class CMBJobOrderHandler implements JobOrderHandlerInterface
|
||||||
// validated! save the entity
|
// validated! save the entity
|
||||||
$em->flush();
|
$em->flush();
|
||||||
|
|
||||||
// send event to mobile app
|
// call rider assignment handler's assignJobOrder
|
||||||
$payload = [
|
$this->rah->assignJobOrder($obj, $rider);
|
||||||
'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.");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $error_array;
|
return $error_array;
|
||||||
}
|
}
|
||||||
|
|
||||||
// fulfill job order
|
// 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)
|
public function fulfillJobOrder(Request $req, $id, MQTTClient $mclient)
|
||||||
{
|
{
|
||||||
// initialize error list
|
// initialize error list
|
||||||
|
|
@ -694,16 +689,8 @@ class CMBJobOrderHandler implements JobOrderHandlerInterface
|
||||||
if ($rider->getImageFile() != null)
|
if ($rider->getImageFile() != null)
|
||||||
$image_url = $req->getScheme() . '://' . $req->getHttpHost() . $req->getBasePath() . '/uploads/' . $rider->getImageFile();
|
$image_url = $req->getScheme() . '://' . $req->getHttpHost() . $req->getBasePath() . '/uploads/' . $rider->getImageFile();
|
||||||
|
|
||||||
// send to mqtt
|
// call rider assignment handler's fulfillJobOrder
|
||||||
$payload = [
|
$this->rah->fulfillJobOrder($obj, $image_url, $rider);
|
||||||
'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);
|
|
||||||
|
|
||||||
// create the warranty if new battery only
|
// create the warranty if new battery only
|
||||||
if ($obj->getServiceType () == CMBServiceType::BATTERY_REPLACEMENT_NEW)
|
if ($obj->getServiceType () == CMBServiceType::BATTERY_REPLACEMENT_NEW)
|
||||||
|
|
|
||||||
|
|
@ -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.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
15
src/Service/RiderAssignmentHandlerInterface.php
Normal file
15
src/Service/RiderAssignmentHandlerInterface.php
Normal 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);
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue