diff --git a/config/services.yaml b/config/services.yaml index 06ce277a..0c620dd1 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -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" diff --git a/src/Service/JobOrderHandler/CMBJobOrderHandler.php b/src/Service/JobOrderHandler/CMBJobOrderHandler.php index bba63f31..d6ef2bd0 100644 --- a/src/Service/JobOrderHandler/CMBJobOrderHandler.php +++ b/src/Service/JobOrderHandler/CMBJobOrderHandler.php @@ -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) diff --git a/src/Service/RiderAssignmentHandler/CMBRiderAssignmentHandler.php b/src/Service/RiderAssignmentHandler/CMBRiderAssignmentHandler.php new file mode 100644 index 00000000..0a8a8112 --- /dev/null +++ b/src/Service/RiderAssignmentHandler/CMBRiderAssignmentHandler.php @@ -0,0 +1,98 @@ +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."); + } + } +} diff --git a/src/Service/RiderAssignmentHandlerInterface.php b/src/Service/RiderAssignmentHandlerInterface.php new file mode 100644 index 00000000..08e0e816 --- /dev/null +++ b/src/Service/RiderAssignmentHandlerInterface.php @@ -0,0 +1,15 @@ +