diff --git a/src/Command/UpdateUnacceptedJobOrdersCommand.php b/src/Command/UpdateUnacceptedJobOrdersCommand.php index 8a52f4b8..b4bed2f3 100644 --- a/src/Command/UpdateUnacceptedJobOrdersCommand.php +++ b/src/Command/UpdateUnacceptedJobOrdersCommand.php @@ -9,15 +9,21 @@ use Symfony\Component\Console\Output\OutputInterface; use Doctrine\ORM\EntityManagerInterface; +use App\Service\MQTTClient; + use App\Entity\JobOrder; +use PDO; + class UpdateUnacceptedJobOrdersCommand extends Command { protected $em; + protected $mclient; - public function __construct(EntityManagerInterface $em) + public function __construct(EntityManagerInterface $em, MQTTClient $mclient) { $this->em = $em; + $this->mclient = $mclient; parent::__construct(); } @@ -32,26 +38,69 @@ class UpdateUnacceptedJobOrdersCommand extends Command 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(); - // 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 - $sql = 'UPDATE job_order SET status=:jo_status, rider_id=null WHERE status=\'assigned\' and TIMESTAMPDIFF(MINUTE, date_assign, NOW()) >= :timeout'; + // since we need the actual job orders for mqtt events, we need to get the ids of the job orders + // that will be updated + $query_sql = 'SELECT id, rider_id FROM job_order WHERE status = :current_status and TIMESTAMPDIFF(MINUTE, date_assign, NOW()) >= :timeout'; - $stmt = $db->prepare($sql); - $stmt->execute([ - 'jo_status' => 'rider_assign', + $query_stmt = $db->prepare($query_sql); + $query_stmt->execute([ + 'current_status' => $current_status, 'timeout' => $timeout, ]); - // send notifications to rider app, telling rider that jo has been requeued + // go through rows + $requeued_jos = []; + while ($row = $query_stmt->fetch(PDO::FETCH_NUM)) + { + // $row[0] is the jo id + // $row[1] is the rider id + // store the ids for now, until the jos have been updated + $requeued_jos[] = [ + 'jo_id' => $row[0], + 'rider_id' => $row[1], + ]; + } - // send notification to mobile app for customer, telling customer that jo has been requeued + // 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_id = $jo_info['jo_id']; + $rider_id = $jo_info['rider_id']; + + $jo = $em->getRepository(JobOrder::class)->find($jo_id); + if ($jo != null) + { + $output->writeln($jo->getID()); + // send notifications to rider app, telling rider that jo has been requeued + + + // send notification to mobile app for customer, telling customer that jo has been requeued + //$mobile_payload = [ + //]; + + //$mclient->sendEvent(); + } + } return 0; }