diff --git a/src/Command/UpdateUnacceptedJobOrdersCommand.php b/src/Command/UpdateUnacceptedJobOrdersCommand.php new file mode 100644 index 00000000..2a9ba2c6 --- /dev/null +++ b/src/Command/UpdateUnacceptedJobOrdersCommand.php @@ -0,0 +1,135 @@ +em = $em; + $this->mclient = $mclient; + + parent::__construct(); + } + + protected function configure() + { + $this->setName('joborder:reassignunaccepted') + ->setDescription('Requeue for rider assignment assigned but unaccepted job orders that have been assigned for more than 3 mins.') + ->setHelp('Requeue for rider assignment assigned but unaccepted job orders that have been assigned for more than 3 mins.'); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $em = $this->em; + $mclient = $this->mclient; + + // TODO: get the timeout limit from .env + $timeout = 3; + $current_status = 'assigned'; + $new_status = 'rider_assign'; + + // pdo connection + $db = $em->getConnection(); + + // since we need the actual job orders for mqtt events, we need to get the ids of the job orders + // that will be updated + // need rider id to set rider to available since rider becomes unavailable + // the minute JO is assigned to rider + $query_sql = 'SELECT id FROM job_order WHERE status = :current_status and TIMESTAMPDIFF(MINUTE, date_assign, NOW()) >= :timeout'; + + $query_stmt = $db->prepare($query_sql); + $query_stmt->execute([ + 'current_status' => $current_status, + 'timeout' => $timeout, + ]); + + // go through rows + $requeued_jos = []; + while ($row = $query_stmt->fetch(PDO::FETCH_NUM)) + { + // $row[0] is the jo id + // store the jos for now for the event sending after update of JOs + // and the updating of rider's availability + $jo_id = $row[0]; + + $requeued_jo = $em->getRepository(JobOrder::class)->find($jo_id); + + $requeued_jos[] = [ + 'jo' => $requeued_jo, + ]; + + $update_sql = 'UPDATE job_order SET status = :new_status, rider_id = null WHERE id = :jo_id'; + $update_stmt = $db->prepare($update_sql); + $update_stmt->execute([ + 'new_status' => $new_status, + 'jo_id' => $jo_id, + ]); + } + + foreach ($requeued_jos as $jo_info) + { + $jo = $jo_info['jo']; + if ($jo != null) + { + // $output->writeln('Requeuing for rider assignment ' . $jo->getID()); + $id = $jo->getID(); + + // send notifications to rider app, telling rider that jo has been requeued + $rider_payload = [ + 'event' => 'cancelled', + 'reason' => 'Reassigned', + 'jo_id' => $id, + ]; + $mclient->sendRiderEvent($jo, $rider_payload); + + + // send outlet assign since order should go back to hub and await reassignment to another rider + $payload = [ + 'event' => 'outlet_assign', + 'jo_id' => $id, + ]; + $mclient->sendEvent($jo, $payload); + } + + $rider = $jo->getRider(); + if ($rider != null) + { + // check rider's current job order before changing rider's availability + // since rider's current job order is set when JO is assigned to rider + if ($rider->getCurrentJobOrder() != null) + { + if ($rider->getCurrentJobOrder()->getID() == $jo->getID()) + { + // reset rider's availability to true + $rider->setAvailable(true); + + // set rider's current job order to null + $rider->setCurrentJobOrder(); + } + } + } + } + + $em->flush(); + + return 0; + } +} diff --git a/src/Controller/APIController.php b/src/Controller/APIController.php index 375794c4..b072d8e4 100644 --- a/src/Controller/APIController.php +++ b/src/Controller/APIController.php @@ -2957,6 +2957,9 @@ class APIController extends Controller implements LoggedController $jo->setStatusAutoAssign(AutoAssignStatus::HUB_AND_RIDER_ASSIGNED); $jo->setDeliveryStatus(DeliveryStatus::RIDER_ASSIGN); + // set date_assigned for job order + $jo->setDateAssign(new DateTime()); + $assigned_rider->setAvailable(false); // set rider's current job order diff --git a/src/Controller/RiderController.php b/src/Controller/RiderController.php index b7d7fadd..c53ac626 100644 --- a/src/Controller/RiderController.php +++ b/src/Controller/RiderController.php @@ -565,6 +565,8 @@ class RiderController extends Controller public function riderActiveJO(EntityManagerInterface $em, MQTTClient $mclient, Rider $rider, $jo_id) { $jo = $em->getRepository(JobOrder::class)->find($jo_id); + // TODO: change this to setCurrentJobOrder since this is what is being used to set rider's + // job order. It is no longer using setActiveJobOrder. $rider->setActiveJobOrder($jo); $em->flush(); diff --git a/src/Entity/JobOrder.php b/src/Entity/JobOrder.php index fcbe5ddc..b2dc6f27 100644 --- a/src/Entity/JobOrder.php +++ b/src/Entity/JobOrder.php @@ -21,7 +21,8 @@ use App\Ramcar\WillingToWaitContent; * @ORM\Index(name="plate_number_idx", columns={"plate_number"}), * @ORM\Index(name="first_name_idx", columns={"first_name"}), * @ORM\Index(name="last_name_idx", columns={"last_name"}), - * @ORM\Index(name="phone_mobile_idx", columns={"phone_mobile"}) + * @ORM\Index(name="phone_mobile_idx", columns={"phone_mobile"}), + * @ORM\Index(name="status_idx", columns={"status"}), * }) */ class JobOrder diff --git a/src/Service/MQTTClient.php b/src/Service/MQTTClient.php index 13c884a0..00229f5f 100644 --- a/src/Service/MQTTClient.php +++ b/src/Service/MQTTClient.php @@ -35,8 +35,8 @@ class MQTTClient public function sendEvent(JobOrder $job_order, $payload) { - error_log('sending mqtt event: '); - error_log(print_r($payload, true)); + //error_log('sending mqtt event: '); + //error_log(print_r($payload, true)); $sessions = $job_order->getCustomer()->getMobileSessions(); if (count($sessions) == 0) diff --git a/src/Service/RiderAssignmentHandler/ResqRiderAssignmentHandler.php b/src/Service/RiderAssignmentHandler/ResqRiderAssignmentHandler.php index 0b101a17..ddaf985e 100644 --- a/src/Service/RiderAssignmentHandler/ResqRiderAssignmentHandler.php +++ b/src/Service/RiderAssignmentHandler/ResqRiderAssignmentHandler.php @@ -50,6 +50,8 @@ class ResqRiderAssignmentHandler implements RiderAssignmentHandlerInterface // send push notification $this->aclient->sendPush($obj, "A RESQ rider is on his way to you."); + + $this->em->flush(); } } diff --git a/utils/update_jo_date_assign/update_jo_date_assign.sql b/utils/update_jo_date_assign/update_jo_date_assign.sql new file mode 100644 index 00000000..5c30b3ae --- /dev/null +++ b/utils/update_jo_date_assign/update_jo_date_assign.sql @@ -0,0 +1 @@ +UPDATE job_order SET date_assign = date_create WHERE date_assign is null;