diff --git a/src/Command/UpdateUnacceptedJobOrdersCommand.php b/src/Command/UpdateUnacceptedJobOrdersCommand.php index a8ea5a3a..b23eb215 100644 --- a/src/Command/UpdateUnacceptedJobOrdersCommand.php +++ b/src/Command/UpdateUnacceptedJobOrdersCommand.php @@ -75,25 +75,21 @@ class UpdateUnacceptedJobOrdersCommand extends Command $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, + ]); } - // update the assigned job orders and have been unaccepted for at least 3 minutes - // change the jo status from assigned to rider_assign and rider id to null - $update_sql = 'UPDATE job_order SET status = :new_status, rider_id = null WHERE status = :current_status and TIMESTAMPDIFF(MINUTE, date_assign, NOW()) >= :timeout'; - - $update_stmt = $db->prepare($update_sql); - $update_stmt->execute([ - 'new_status' => $new_status, - 'current_status' => $current_status, - 'timeout' => $timeout, - ]); - foreach ($requeued_jos as $jo_info) { $jo = $jo_info['jo']; if ($jo != null) { - // $output->writeln('Requeuing for rider assignment ' . $jo->getID()); + $output->writeln('Requeuing for rider assignment ' . $jo->getID()); $id = $jo->getID(); // send notifications to rider app, telling rider that jo has been requeued @@ -104,6 +100,7 @@ class UpdateUnacceptedJobOrdersCommand extends Command ]; $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', @@ -115,11 +112,19 @@ class UpdateUnacceptedJobOrdersCommand extends Command $rider = $jo->getRider(); if ($rider != null) { - // set rider's availability to true - $rider->setAvailable(true); + // 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(); + // set rider's current job order to null + $rider->setCurrentJobOrder(); + } + } } } 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();