Move the status checking to the service. Add calls to callback when JO changes status. #737

This commit is contained in:
Korina Cordero 2023-02-20 06:20:43 +00:00
parent d569ea55d1
commit 8cacfea279
4 changed files with 101 additions and 70 deletions

View file

@ -32,6 +32,7 @@ use App\Service\JobOrderHandlerInterface;
use App\Service\InvoiceGeneratorInterface; use App\Service\InvoiceGeneratorInterface;
use App\Service\RisingTideGateway; use App\Service\RisingTideGateway;
use App\Service\RiderTracker; use App\Service\RiderTracker;
use App\Service\JobOrderCallbackRouter;
use App\Ramcar\ServiceType; use App\Ramcar\ServiceType;
use App\Ramcar\TradeInType; use App\Ramcar\TradeInType;
@ -388,7 +389,7 @@ class RiderAppController extends APIController
} }
public function acceptJobOrder(Request $req, EntityManagerInterface $em) public function acceptJobOrder(Request $req, EntityManagerInterface $em, JobOrderCallbackRouter $jo_callback)
{ {
$required_params = ['jo_id']; $required_params = ['jo_id'];
@ -406,6 +407,9 @@ class RiderAppController extends APIController
if (!empty($msg)) if (!empty($msg))
return new APIResponse(false, $msg); return new APIResponse(false, $msg);
// get previous status
$old_status = $jo->getStatus();
// TODO: refactor this into a jo handler class, so we don't have to repeat for control center // TODO: refactor this into a jo handler class, so we don't have to repeat for control center
// set jo status to in transit // set jo status to in transit
@ -432,12 +436,16 @@ class RiderAppController extends APIController
$em->flush(); $em->flush();
// get current status and send JO callback
$current_status = $jo->getStatus();
$jo_callback->sendJOStatusCallback($jo, $old_status, $current_status);
$data = []; $data = [];
return new APIResponse(true, 'Job order accepted.', $data); return new APIResponse(true, 'Job order accepted.', $data);
} }
public function cancelJobOrder(Request $req, EntityManagerInterface $em, MQTTClient $mclient) public function cancelJobOrder(Request $req, EntityManagerInterface $em, MQTTClient $mclient, JobOrderCallbackRouter $jo_callback)
{ {
$required_params = ['jo_id']; $required_params = ['jo_id'];
@ -456,6 +464,9 @@ class RiderAppController extends APIController
// TODO: this is a workaround for requeue, because rider app gets stuck in accept / decline screen // TODO: this is a workaround for requeue, because rider app gets stuck in accept / decline screen
return new APIResponse(true, $msg); return new APIResponse(true, $msg);
// get previous status
$old_status = $jo->getStatus();
// requeue it, instead of cancelling it // requeue it, instead of cancelling it
$jo->requeue(); $jo->requeue();
@ -483,6 +494,10 @@ class RiderAppController extends APIController
]; ];
$mclient->sendEvent($jo, $payload); $mclient->sendEvent($jo, $payload);
// get current status and send JO callback
$current_status = $jo->getStatus();
$jo_callback->sendJOStatusCallback($jo, $old_status, $current_status);
$data = []; $data = [];
return new APIResponse(true, 'Job order requeued.', $data); return new APIResponse(true, 'Job order requeued.', $data);
} }
@ -648,7 +663,7 @@ class RiderAppController extends APIController
} }
public function arrive(Request $req, EntityManagerInterface $em, MQTTClient $mclient) public function arrive(Request $req, EntityManagerInterface $em, MQTTClient $mclient, JobOrderCallbackRouter $jo_callback)
{ {
$required_params = ['jo_id']; $required_params = ['jo_id'];
@ -666,6 +681,9 @@ class RiderAppController extends APIController
if (!empty($msg)) if (!empty($msg))
return new APIResponse(false, $msg); return new APIResponse(false, $msg);
// get previous status
$old_status = $jo->getStatus();
// TODO: refactor this into a jo handler class, so we don't have to repeat for control center // TODO: refactor this into a jo handler class, so we don't have to repeat for control center
// set jo status to in progress // set jo status to in progress
@ -698,6 +716,10 @@ class RiderAppController extends APIController
]; ];
$mclient->sendEvent($jo, $payload); $mclient->sendEvent($jo, $payload);
// get current status and send JO callback
$current_status = $jo->getStatus();
$jo_callback->sendJOStatusCallback($jo, $old_status, $current_status);
$data = []; $data = [];
return new APIResponse(true, 'Rider arrived at customer location.', $data); return new APIResponse(true, 'Rider arrived at customer location.', $data);
} }
@ -749,7 +771,8 @@ class RiderAppController extends APIController
} }
public function payment(Request $req, EntityManagerInterface $em, JobOrderHandlerInterface $jo_handler, public function payment(Request $req, EntityManagerInterface $em, JobOrderHandlerInterface $jo_handler,
RisingTideGateway $rt, WarrantyHandler $wh, MQTTClient $mclient, TranslatorInterface $translator) RisingTideGateway $rt, WarrantyHandler $wh, MQTTClient $mclient, TranslatorInterface $translator,
JobOrderCallbackRouter $jo_callback)
{ {
$required_params = ['jo_id']; $required_params = ['jo_id'];
@ -767,6 +790,9 @@ class RiderAppController extends APIController
if (!empty($msg)) if (!empty($msg))
return new APIResponse(false, $msg); return new APIResponse(false, $msg);
// get previous status
$old_status = $jo->getStatus();
// set invoice to paid // set invoice to paid
$jo->getInvoice()->setStatus(InvoiceStatus::PAID); $jo->getInvoice()->setStatus(InvoiceStatus::PAID);
@ -815,6 +841,10 @@ class RiderAppController extends APIController
$em->flush(); $em->flush();
// get current status and send JO callback
$current_status = $jo->getStatus();
$jo_callback->sendJOStatusCallback($jo, $old_status, $current_status);
// create warranty // create warranty
if($jo_handler->checkIfNewBattery($jo)) if($jo_handler->checkIfNewBattery($jo))
{ {

View file

@ -383,7 +383,8 @@ class JobOrderController extends APIController
$em->flush(); $em->flush();
// send callback // send callback
$jo_callback->sendJOStatusCallback($jo); $old_status = null;
$jo_callback->sendJOStatusCallback($jo, $old_status, $jo->getStatus());
// make invoice json data // make invoice json data
$invoice_data = [ $invoice_data = [

View file

@ -8,29 +8,46 @@ use App\Ramcar\JOStatus;
class JobOrderCallbackRouter class JobOrderCallbackRouter
{ {
public function sendJOStatusCallback(JobOrder $jo) public function sendJOStatusCallback(JobOrder $jo, $old_status, $new_status)
{ {
// get the job order source // check status change
$source = $jo->getJOSource(); $is_status_change = $this->checkStatusChange($old_status, $new_status);
if ($source != null)
{
// check if source has a callback url
$callback_url = $source->getCallbackURL();
if ($callback_url != null)
{
// form the body for the callback
// jo id and jo status
$jo_data = [
'id' => $jo->getID(),
// 'status' => JOStatus::getName($jo->getStatus()),
];
// send status if ($is_status_change)
$this->sendJOInfo($jo_data, $callback_url); {
// get the job order source
$source = $jo->getJOSource();
if ($source != null)
{
// check if source has a callback url
$callback_url = $source->getCallbackURL();
if ($callback_url != null)
{
// form the body for the callback
// jo id and jo status
$jo_data = [
'id' => $jo->getID(),
// 'status' => JOStatus::getName($jo->getStatus()),
];
// send status
$this->sendJOInfo($jo_data, $callback_url);
}
} }
} }
} }
protected function checkStatusChange($old_status, $new_status)
{
// NOTE: what do we do if a third party JO has been cancelled and is then fulfilled?
// This is a request resq asked for, to be able to fulfill cancelled JOs.
if ($old_status != $new_status)
return true;
return false;
}
protected function sendJOInfo($jo_info, $callback_url) protected function sendJOInfo($jo_info, $callback_url)
{ {
$body = json_encode($jo_info); $body = json_encode($jo_info);

View file

@ -991,14 +991,9 @@ class ResqJobOrderHandler implements JobOrderHandlerInterface
// update redis hub jo count // update redis hub jo count
$this->hub_dist->incrementJoCountForHub($hub); $this->hub_dist->incrementJoCountForHub($hub);
// send callback url if source is third party // get current status and send JO callback
if ($obj->getJOSource() != null) $current_status = $obj->getStatus();
{ $this->jo_callback->sendJOStatusCallback($obj, $old_status, $current_status);
// need to check if the JO status changed
$current_status = $obj->getStatus();
if ($old_status != $current_status)
$this->jo_callback->sendJOStatusCallback($obj);
}
} }
return $error_array; return $error_array;
@ -1159,14 +1154,9 @@ class ResqJobOrderHandler implements JobOrderHandlerInterface
// call rider assignment handler's assignJobOrder // call rider assignment handler's assignJobOrder
$this->rah->assignJobOrder($obj, $rider); $this->rah->assignJobOrder($obj, $rider);
// send callback url if source is third party // get current status and send JO callback
if ($obj->getJOSource() != null) $current_status = $obj->getStatus();
{ $this->jo_callback->sendJOStatusCallback($obj, $old_status, $current_status);
// need to check if the JO status changed
$current_status = $obj->getStatus();
if ($old_status != $current_status)
$this->jo_callback->sendJOStatusCallback($obj);
}
} }
return $error_array; return $error_array;
@ -1299,14 +1289,9 @@ class ResqJobOrderHandler implements JobOrderHandlerInterface
// validated! save the entity // validated! save the entity
$em->flush(); $em->flush();
// send callback url if source is third party // get current status and send JO callback
if ($obj->getJOSource() != null) $current_status = $obj->getStatus();
{ $this->jo_callback->sendJOStatusCallback($obj, $old_status, $current_status);
// need to check if the JO status changed
$current_status = $obj->getStatus();
if ($old_status != $current_status)
$this->jo_callback->sendJOStatusCallback($obj);
}
// get rider // get rider
$rider = $obj->getRider(); $rider = $obj->getRider();
@ -1425,14 +1410,9 @@ class ResqJobOrderHandler implements JobOrderHandlerInterface
$mclient->sendEvent($obj, $payload); $mclient->sendEvent($obj, $payload);
$mclient->sendRiderEvent($obj, $payload); $mclient->sendRiderEvent($obj, $payload);
// send callback url if source is third party // get current status and send JO callback
if ($obj->getJOSource() != null) $current_status = $obj->getStatus();
{ $this->jo_callback->sendJOStatusCallback($obj, $old_status, $current_status);
// need to check if the JO status changed
$current_status = $obj->getStatus();
if ($old_status != $current_status)
$this->jo_callback->sendJOStatusCallback($obj);
}
} }
// set hub for job order // set hub for job order
@ -1621,14 +1601,9 @@ class ResqJobOrderHandler implements JobOrderHandlerInterface
if ($hub != null) if ($hub != null)
$this->hub_dist->incrementJoCountForHub($hub); $this->hub_dist->incrementJoCountForHub($hub);
// send callback url if source is third party // get current status and send JO callback
if ($obj->getJOSource() != null) $current_status = $obj->getStatus();
{ $this->jo_callback->sendJOStatusCallback($obj, $old_status, $current_status);
// need to check if the JO status changed
$current_status = $obj->getStatus();
if ($old_status != $current_status)
$this->jo_callback->sendJOStatusCallback($obj);
}
} }
return $error_array; return $error_array;
@ -1912,14 +1887,9 @@ class ResqJobOrderHandler implements JobOrderHandlerInterface
$mclient->sendEvent($obj, $payload); $mclient->sendEvent($obj, $payload);
$mclient->sendRiderEvent($obj, $payload); $mclient->sendRiderEvent($obj, $payload);
// send callback url if source is third party // get current status and send JO callback
if ($obj->getJOSource() != null) $current_status = $obj->getStatus();
{ $this->jo_callback->sendJOStatusCallback($obj, $old_status, $current_status);
// need to check if the JO status changed
$current_status = $obj->getStatus();
if ($old_status != $current_status)
$this->jo_callback->sendJOStatusCallback($obj);
}
} }
return $error_array; return $error_array;
@ -4061,6 +4031,9 @@ class ResqJobOrderHandler implements JobOrderHandlerInterface
if (empty($obj)) if (empty($obj))
throw new NotFoundHttpException('The item does not exist'); throw new NotFoundHttpException('The item does not exist');
// get previous status
$old_status = $obj->getStatus();
$obj->fulfill(); $obj->fulfill();
// the event // the event
@ -4079,6 +4052,16 @@ class ResqJobOrderHandler implements JobOrderHandlerInterface
$event->setUser($user); $event->setUser($user);
$em->persist($event); $em->persist($event);
$em->flush(); $em->flush();
// get current status and send JO callback
$current_status = $obj->getStatus();
$this->jo_callback->sendJOStatusCallback($obj, $old_status, $current_status);
$data = [
'error_array' => $error_array,
];
return $data;
} }
public function sendSMSToCustomer($phone_number) public function sendSMSToCustomer($phone_number)