Add retrieval of job orders to be updated. #630

This commit is contained in:
Korina Cordero 2021-10-19 09:19:27 +00:00
parent 0065bc94ef
commit c2066b86c9

View file

@ -9,15 +9,21 @@ use Symfony\Component\Console\Output\OutputInterface;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use App\Service\MQTTClient;
use App\Entity\JobOrder; use App\Entity\JobOrder;
use PDO;
class UpdateUnacceptedJobOrdersCommand extends Command class UpdateUnacceptedJobOrdersCommand extends Command
{ {
protected $em; protected $em;
protected $mclient;
public function __construct(EntityManagerInterface $em) public function __construct(EntityManagerInterface $em, MQTTClient $mclient)
{ {
$this->em = $em; $this->em = $em;
$this->mclient = $mclient;
parent::__construct(); parent::__construct();
} }
@ -32,26 +38,69 @@ class UpdateUnacceptedJobOrdersCommand extends Command
protected function execute(InputInterface $input, OutputInterface $output) protected function execute(InputInterface $input, OutputInterface $output)
{ {
$em = $this->em; $em = $this->em;
$mclient = $this->mclient;
// TODO: get the timeout limit from .env // TODO: get the timeout limit from .env
$timeout = 3; $timeout = 3;
$current_status = 'assigned';
$new_status = 'rider_assign';
// pdo connection // pdo connection
$db = $em->getConnection(); $db = $em->getConnection();
// update the assigned job orders and have been unaccepted for at least 3 minutes // since we need the actual job orders for mqtt events, we need to get the ids of the job orders
// change the jo status from assigned to rider_assign and rider id to null // that will be updated
$sql = 'UPDATE job_order SET status=:jo_status, rider_id=null WHERE status=\'assigned\' and TIMESTAMPDIFF(MINUTE, date_assign, NOW()) >= :timeout'; $query_sql = 'SELECT id, rider_id FROM job_order WHERE status = :current_status and TIMESTAMPDIFF(MINUTE, date_assign, NOW()) >= :timeout';
$stmt = $db->prepare($sql); $query_stmt = $db->prepare($query_sql);
$stmt->execute([ $query_stmt->execute([
'jo_status' => 'rider_assign', 'current_status' => $current_status,
'timeout' => $timeout, '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; return 0;
} }