Move the status checking to the service. Add calls to callback when JO changes status. #737
This commit is contained in:
parent
d569ea55d1
commit
8cacfea279
4 changed files with 101 additions and 70 deletions
|
|
@ -32,6 +32,7 @@ use App\Service\JobOrderHandlerInterface;
|
|||
use App\Service\InvoiceGeneratorInterface;
|
||||
use App\Service\RisingTideGateway;
|
||||
use App\Service\RiderTracker;
|
||||
use App\Service\JobOrderCallbackRouter;
|
||||
|
||||
use App\Ramcar\ServiceType;
|
||||
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'];
|
||||
|
||||
|
|
@ -406,6 +407,9 @@ class RiderAppController extends APIController
|
|||
if (!empty($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
|
||||
|
||||
// set jo status to in transit
|
||||
|
|
@ -432,12 +436,16 @@ class RiderAppController extends APIController
|
|||
|
||||
$em->flush();
|
||||
|
||||
// get current status and send JO callback
|
||||
$current_status = $jo->getStatus();
|
||||
$jo_callback->sendJOStatusCallback($jo, $old_status, $current_status);
|
||||
|
||||
$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'];
|
||||
|
||||
|
|
@ -456,6 +464,9 @@ class RiderAppController extends APIController
|
|||
// TODO: this is a workaround for requeue, because rider app gets stuck in accept / decline screen
|
||||
return new APIResponse(true, $msg);
|
||||
|
||||
// get previous status
|
||||
$old_status = $jo->getStatus();
|
||||
|
||||
// requeue it, instead of cancelling it
|
||||
$jo->requeue();
|
||||
|
||||
|
|
@ -483,6 +494,10 @@ class RiderAppController extends APIController
|
|||
];
|
||||
$mclient->sendEvent($jo, $payload);
|
||||
|
||||
// get current status and send JO callback
|
||||
$current_status = $jo->getStatus();
|
||||
$jo_callback->sendJOStatusCallback($jo, $old_status, $current_status);
|
||||
|
||||
$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'];
|
||||
|
||||
|
|
@ -666,6 +681,9 @@ class RiderAppController extends APIController
|
|||
if (!empty($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
|
||||
|
||||
// set jo status to in progress
|
||||
|
|
@ -698,6 +716,10 @@ class RiderAppController extends APIController
|
|||
];
|
||||
$mclient->sendEvent($jo, $payload);
|
||||
|
||||
// get current status and send JO callback
|
||||
$current_status = $jo->getStatus();
|
||||
$jo_callback->sendJOStatusCallback($jo, $old_status, $current_status);
|
||||
|
||||
$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,
|
||||
RisingTideGateway $rt, WarrantyHandler $wh, MQTTClient $mclient, TranslatorInterface $translator)
|
||||
RisingTideGateway $rt, WarrantyHandler $wh, MQTTClient $mclient, TranslatorInterface $translator,
|
||||
JobOrderCallbackRouter $jo_callback)
|
||||
{
|
||||
$required_params = ['jo_id'];
|
||||
|
||||
|
|
@ -767,6 +790,9 @@ class RiderAppController extends APIController
|
|||
if (!empty($msg))
|
||||
return new APIResponse(false, $msg);
|
||||
|
||||
// get previous status
|
||||
$old_status = $jo->getStatus();
|
||||
|
||||
// set invoice to paid
|
||||
$jo->getInvoice()->setStatus(InvoiceStatus::PAID);
|
||||
|
||||
|
|
@ -815,6 +841,10 @@ class RiderAppController extends APIController
|
|||
|
||||
$em->flush();
|
||||
|
||||
// get current status and send JO callback
|
||||
$current_status = $jo->getStatus();
|
||||
$jo_callback->sendJOStatusCallback($jo, $old_status, $current_status);
|
||||
|
||||
// create warranty
|
||||
if($jo_handler->checkIfNewBattery($jo))
|
||||
{
|
||||
|
|
|
|||
|
|
@ -383,7 +383,8 @@ class JobOrderController extends APIController
|
|||
$em->flush();
|
||||
|
||||
// send callback
|
||||
$jo_callback->sendJOStatusCallback($jo);
|
||||
$old_status = null;
|
||||
$jo_callback->sendJOStatusCallback($jo, $old_status, $jo->getStatus());
|
||||
|
||||
// make invoice json data
|
||||
$invoice_data = [
|
||||
|
|
|
|||
|
|
@ -8,29 +8,46 @@ use App\Ramcar\JOStatus;
|
|||
|
||||
class JobOrderCallbackRouter
|
||||
{
|
||||
public function sendJOStatusCallback(JobOrder $jo)
|
||||
public function sendJOStatusCallback(JobOrder $jo, $old_status, $new_status)
|
||||
{
|
||||
// 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()),
|
||||
];
|
||||
// check status change
|
||||
$is_status_change = $this->checkStatusChange($old_status, $new_status);
|
||||
|
||||
// send status
|
||||
$this->sendJOInfo($jo_data, $callback_url);
|
||||
if ($is_status_change)
|
||||
{
|
||||
// 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)
|
||||
{
|
||||
$body = json_encode($jo_info);
|
||||
|
|
|
|||
|
|
@ -991,14 +991,9 @@ class ResqJobOrderHandler implements JobOrderHandlerInterface
|
|||
// update redis hub jo count
|
||||
$this->hub_dist->incrementJoCountForHub($hub);
|
||||
|
||||
// send callback url if source is third party
|
||||
if ($obj->getJOSource() != null)
|
||||
{
|
||||
// need to check if the JO status changed
|
||||
$current_status = $obj->getStatus();
|
||||
if ($old_status != $current_status)
|
||||
$this->jo_callback->sendJOStatusCallback($obj);
|
||||
}
|
||||
// get current status and send JO callback
|
||||
$current_status = $obj->getStatus();
|
||||
$this->jo_callback->sendJOStatusCallback($obj, $old_status, $current_status);
|
||||
}
|
||||
|
||||
return $error_array;
|
||||
|
|
@ -1159,14 +1154,9 @@ class ResqJobOrderHandler implements JobOrderHandlerInterface
|
|||
// call rider assignment handler's assignJobOrder
|
||||
$this->rah->assignJobOrder($obj, $rider);
|
||||
|
||||
// send callback url if source is third party
|
||||
if ($obj->getJOSource() != null)
|
||||
{
|
||||
// need to check if the JO status changed
|
||||
$current_status = $obj->getStatus();
|
||||
if ($old_status != $current_status)
|
||||
$this->jo_callback->sendJOStatusCallback($obj);
|
||||
}
|
||||
// get current status and send JO callback
|
||||
$current_status = $obj->getStatus();
|
||||
$this->jo_callback->sendJOStatusCallback($obj, $old_status, $current_status);
|
||||
}
|
||||
|
||||
return $error_array;
|
||||
|
|
@ -1299,14 +1289,9 @@ class ResqJobOrderHandler implements JobOrderHandlerInterface
|
|||
// validated! save the entity
|
||||
$em->flush();
|
||||
|
||||
// send callback url if source is third party
|
||||
if ($obj->getJOSource() != null)
|
||||
{
|
||||
// need to check if the JO status changed
|
||||
$current_status = $obj->getStatus();
|
||||
if ($old_status != $current_status)
|
||||
$this->jo_callback->sendJOStatusCallback($obj);
|
||||
}
|
||||
// get current status and send JO callback
|
||||
$current_status = $obj->getStatus();
|
||||
$this->jo_callback->sendJOStatusCallback($obj, $old_status, $current_status);
|
||||
|
||||
// get rider
|
||||
$rider = $obj->getRider();
|
||||
|
|
@ -1425,14 +1410,9 @@ class ResqJobOrderHandler implements JobOrderHandlerInterface
|
|||
$mclient->sendEvent($obj, $payload);
|
||||
$mclient->sendRiderEvent($obj, $payload);
|
||||
|
||||
// send callback url if source is third party
|
||||
if ($obj->getJOSource() != null)
|
||||
{
|
||||
// need to check if the JO status changed
|
||||
$current_status = $obj->getStatus();
|
||||
if ($old_status != $current_status)
|
||||
$this->jo_callback->sendJOStatusCallback($obj);
|
||||
}
|
||||
// get current status and send JO callback
|
||||
$current_status = $obj->getStatus();
|
||||
$this->jo_callback->sendJOStatusCallback($obj, $old_status, $current_status);
|
||||
}
|
||||
|
||||
// set hub for job order
|
||||
|
|
@ -1621,14 +1601,9 @@ class ResqJobOrderHandler implements JobOrderHandlerInterface
|
|||
if ($hub != null)
|
||||
$this->hub_dist->incrementJoCountForHub($hub);
|
||||
|
||||
// send callback url if source is third party
|
||||
if ($obj->getJOSource() != null)
|
||||
{
|
||||
// need to check if the JO status changed
|
||||
$current_status = $obj->getStatus();
|
||||
if ($old_status != $current_status)
|
||||
$this->jo_callback->sendJOStatusCallback($obj);
|
||||
}
|
||||
// get current status and send JO callback
|
||||
$current_status = $obj->getStatus();
|
||||
$this->jo_callback->sendJOStatusCallback($obj, $old_status, $current_status);
|
||||
}
|
||||
|
||||
return $error_array;
|
||||
|
|
@ -1912,14 +1887,9 @@ class ResqJobOrderHandler implements JobOrderHandlerInterface
|
|||
$mclient->sendEvent($obj, $payload);
|
||||
$mclient->sendRiderEvent($obj, $payload);
|
||||
|
||||
// send callback url if source is third party
|
||||
if ($obj->getJOSource() != null)
|
||||
{
|
||||
// need to check if the JO status changed
|
||||
$current_status = $obj->getStatus();
|
||||
if ($old_status != $current_status)
|
||||
$this->jo_callback->sendJOStatusCallback($obj);
|
||||
}
|
||||
// get current status and send JO callback
|
||||
$current_status = $obj->getStatus();
|
||||
$this->jo_callback->sendJOStatusCallback($obj, $old_status, $current_status);
|
||||
}
|
||||
|
||||
return $error_array;
|
||||
|
|
@ -4061,6 +4031,9 @@ class ResqJobOrderHandler implements JobOrderHandlerInterface
|
|||
if (empty($obj))
|
||||
throw new NotFoundHttpException('The item does not exist');
|
||||
|
||||
// get previous status
|
||||
$old_status = $obj->getStatus();
|
||||
|
||||
$obj->fulfill();
|
||||
|
||||
// the event
|
||||
|
|
@ -4079,6 +4052,16 @@ class ResqJobOrderHandler implements JobOrderHandlerInterface
|
|||
$event->setUser($user);
|
||||
$em->persist($event);
|
||||
$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)
|
||||
|
|
|
|||
Loading…
Reference in a new issue