Modify JobOrderController. Separate sending of MQTT events from creation of JO events. #591
This commit is contained in:
parent
f45f7fd411
commit
3ebf6b6c96
2 changed files with 89 additions and 74 deletions
|
|
@ -148,3 +148,7 @@ access_keys:
|
|||
label: Request Job Order
|
||||
- id: mobile_jo.get.estimate
|
||||
label: Get Estimate
|
||||
- id: mobile_jo.get.ongoing
|
||||
label: Get Ongoing Job Order
|
||||
- id: mobile_jo.cancel
|
||||
label: Cancel Job Order
|
||||
|
|
|
|||
|
|
@ -12,6 +12,7 @@ use CrEOF\Spatial\PHP\Types\Geometry\Point;
|
|||
|
||||
use Catalyst\APIBundle\Controller\APIController;
|
||||
use Catalyst\APIBundle\Response\APIResponse;
|
||||
|
||||
use App\Ramcar\WarrantyClass;
|
||||
use App\Ramcar\JOStatus;
|
||||
use App\Ramcar\AdvanceOrderSlot;
|
||||
|
|
@ -130,7 +131,14 @@ class JobOrderController extends APIController
|
|||
$em->persist($jo);
|
||||
$em->persist($invoice);
|
||||
|
||||
$this->processEvents($em, $jo, $rah, $mclient);
|
||||
// create JO event logs
|
||||
$this->processEvents($em, $jo, $rah, $mclient, JOEventType::CREATE);
|
||||
|
||||
// send mqtt events
|
||||
$payload = [
|
||||
'event' => 'outlet_assign'
|
||||
];
|
||||
$this->processMQTTEvents($jo, $payload, $mclient, $rah);
|
||||
|
||||
$em->flush();
|
||||
|
||||
|
|
@ -249,7 +257,14 @@ class JobOrderController extends APIController
|
|||
$em->persist($jo);
|
||||
$em->persist($invoice);
|
||||
|
||||
$this->processEvents($em, $jo, $rah, $mclient);
|
||||
// create JO event logs
|
||||
$this->processEvents($em, $jo, $rah, $mclient, JOEventType::CREATE);
|
||||
|
||||
// send mqtt events
|
||||
$payload = [
|
||||
'event' => 'outlet_assign'
|
||||
];
|
||||
$this->processMQTTEvents($jo, $payload, $mclient, $rah);
|
||||
|
||||
$em->flush();
|
||||
|
||||
|
|
@ -313,31 +328,30 @@ class JobOrderController extends APIController
|
|||
return new APIResponse(true, 'Estimate found', $invoice_data);
|
||||
}
|
||||
|
||||
// TODO: modify for MobileUser
|
||||
public function getOngoing(Request $req, EntityManagerInterface $em)
|
||||
public function getOngoing(Request $req, EntityManagerInterface $em, MobileAPIHandler $mah)
|
||||
{
|
||||
$this->denyAccessUnlessGranted('mobile_jo.get.ongoing', null, 'No access.');
|
||||
|
||||
$required_params = [];
|
||||
$res = $this->checkParamsAndKey($req, $em, $required_params);
|
||||
if ($res->isError())
|
||||
return $res->getReturnResponse();
|
||||
$msg = $this->checkRequiredParameters($req, $required_params);
|
||||
if ($msg)
|
||||
return new APIResponse(false, $msg);
|
||||
|
||||
// get customer
|
||||
$cust = $this->session->getCustomer();
|
||||
// get capi user to link to mobile user
|
||||
$user_id = $this->getUser()->getID();
|
||||
|
||||
// get mobile user
|
||||
$mobile_user = $mah->findMobileUser($user_id);
|
||||
|
||||
if ($mobile_user == null)
|
||||
return new APIResponse(false, 'No mobile user found.');
|
||||
|
||||
// customer
|
||||
$cust = $mobile_user->getCustomer();
|
||||
if ($cust == null)
|
||||
{
|
||||
$res->setError(true)
|
||||
->setErrorMessage('No customer information found');
|
||||
return $res->getReturnResponse();
|
||||
}
|
||||
return new APIResponse(false, 'No customer information found');
|
||||
|
||||
/*
|
||||
// check if we have an ongoing job order
|
||||
$ongoing_jos = $em->getRepository(JobOrder::class)->findBy([
|
||||
'customer' => $cust,
|
||||
'status' => [JOStatus::PENDING, JOStatus::RIDER_ASSIGN, JOStatus::IN_TRANSIT, JOStatus::ASSIGNED, JOStatus::IN_PROGRESS],
|
||||
]);
|
||||
*/
|
||||
$ongoing_jos = $this->getOngoingJobOrders($cust, $em);
|
||||
$ongoing_jos = $mah->getOngoingJobOrders($cust);
|
||||
|
||||
// initialize data
|
||||
$data = [];
|
||||
|
|
@ -356,76 +370,68 @@ class JobOrderController extends APIController
|
|||
];
|
||||
}
|
||||
|
||||
$res->setData($data);
|
||||
|
||||
return $res->getReturnResponse();
|
||||
return new APIResponse(true, 'Ongoing job orders found.', $data);
|
||||
}
|
||||
|
||||
// TODO: modify for MobileUser
|
||||
public function cancelJobOrder(Request $req, MQTTClient $mclient, EntityManagerInterface $em)
|
||||
public function cancelJobOrder(Request $req, MQTTClient $mclient, EntityManagerInterface $em,
|
||||
MobileAPIHandler $mah, RiderAssignmentHandlerInterface $rah)
|
||||
{
|
||||
$this->denyAccessUnlessGranted('mobile_jo.cancel', null, 'No access.');
|
||||
|
||||
$required_params = [
|
||||
'jo_id',
|
||||
'reason'
|
||||
];
|
||||
$res = $this->checkParamsAndKey($req, $em, $required_params);
|
||||
if ($res->isError())
|
||||
return $res->getReturnResponse();
|
||||
|
||||
$msg = $this->checkRequiredParameters($req, $required_params);
|
||||
if ($msg)
|
||||
return new APIResponse(false, $msg);
|
||||
|
||||
// get capi user to link to mobile user
|
||||
$user_id = $this->getUser()->getID();
|
||||
|
||||
// get mobile user
|
||||
$mobile_user = $mah->findMobileUser($user_id);
|
||||
|
||||
if ($mobile_user == null)
|
||||
return new APIResponse(false, 'No mobile user found.');
|
||||
|
||||
// get job order
|
||||
$jo_id = $req->request->get('jo_id');
|
||||
$jo = $em->getRepository(JobOrder::class)->find($jo_id);
|
||||
if ($jo == null)
|
||||
{
|
||||
$res->setError(true)
|
||||
->setErrorMessage('No job order found');
|
||||
return $res->getReturnResponse();
|
||||
}
|
||||
return new APIResponse(false, 'No job order found');
|
||||
|
||||
// get customer
|
||||
$cust = $this->session->getCustomer();
|
||||
$cust = $mobile_user->getCustomer();
|
||||
if ($cust == null)
|
||||
{
|
||||
$res->setError(true)
|
||||
->setErrorMessage('No customer information found');
|
||||
return $res->getReturnResponse();
|
||||
}
|
||||
new APIResponse(false, 'No customer information found');
|
||||
|
||||
// check that the customer owns the job order
|
||||
$jo_cust = $jo->getCustomer();
|
||||
if ($jo_cust->getID() != $cust->getID())
|
||||
{
|
||||
$res->setError(true)
|
||||
->setErrorMessage('Job order was not initiated by customer');
|
||||
return $res->getReturnResponse();
|
||||
}
|
||||
return new APIResponse(false, 'Job order was not initiated by customer');
|
||||
|
||||
// TODO: check job order status, if it's cancellable
|
||||
$cancel_reason = $req->request->get('reason');
|
||||
$cancel_reason = $req->request->get('reason');
|
||||
|
||||
$jo->cancel($cancel_reason);
|
||||
|
||||
// add event log
|
||||
$event = new JOEvent();
|
||||
$event->setDateHappen(new DateTime())
|
||||
->setTypeID(JOEventType::CANCEL)
|
||||
->setJobOrder($jo);
|
||||
$em->persist($event);
|
||||
// create event logs
|
||||
$this->processEvents($em, $jo, $rah, $mclient, JOEventType::CANCEL);
|
||||
|
||||
$em->flush();
|
||||
|
||||
// send mobile app event
|
||||
// send mqtt events
|
||||
$payload = [
|
||||
'event' => 'cancelled',
|
||||
'reason' => $cancel_reason,
|
||||
'jo_id' => $jo->getID(),
|
||||
];
|
||||
// $mclient->sendEvent($jo, $payload);
|
||||
$mclient->sendRiderEvent($jo, $payload);
|
||||
$this->processMQTTEvents($jo, $payload, $mclient, $rah);
|
||||
|
||||
$res->setData([]);
|
||||
$em->flush();
|
||||
|
||||
return $res->getReturnResponse();
|
||||
$data = [];
|
||||
return new APIResponse(true, 'Job order cancelled', $data);
|
||||
}
|
||||
|
||||
// TODO: modify for MobileUser
|
||||
|
|
@ -1254,12 +1260,13 @@ class JobOrderController extends APIController
|
|||
}
|
||||
|
||||
protected function processEvents(EntityManagerInterface $em, JobOrder $jo,
|
||||
RiderAssignmentHandlerInterface $rah, MQTTClient $mclient)
|
||||
RiderAssignmentHandlerInterface $rah, MQTTClient $mclient,
|
||||
$jo_event_type)
|
||||
{
|
||||
// add event log for JO
|
||||
$event = new JOEvent();
|
||||
$event->setDateHappen(new DateTime())
|
||||
->setTypeID(JOEventType::CREATE)
|
||||
->setTypeID($jo_event_type)
|
||||
->setJobOrder($jo);
|
||||
$em->persist($event);
|
||||
|
||||
|
|
@ -1280,14 +1287,6 @@ class JobOrderController extends APIController
|
|||
->setJobOrder($jo);
|
||||
|
||||
$em->persist($rider_assign_event);
|
||||
|
||||
// user mqtt event
|
||||
$payload = [
|
||||
'event' => 'outlet_assign'
|
||||
];
|
||||
$mclient->sendEvent($jo, $payload);
|
||||
|
||||
$rah->assignJobOrder($jo, $jo->getRider());
|
||||
}
|
||||
if ($jo->getStatus() == JOStatus::RIDER_ASSIGN)
|
||||
{
|
||||
|
|
@ -1298,12 +1297,24 @@ class JobOrderController extends APIController
|
|||
->setJobOrder($jo);
|
||||
|
||||
$em->persist($hub_assign_event);
|
||||
}
|
||||
}
|
||||
|
||||
// user mqtt event
|
||||
$payload = [
|
||||
'event' => 'outlet_assign'
|
||||
];
|
||||
protected function processMQTTEvents($jo, $payload, $mclient, $rah)
|
||||
{
|
||||
// check JO status
|
||||
if ($jo->getStatus() == JOStatus::ASSIGNED)
|
||||
{
|
||||
$mclient->sendEvent($jo, $payload);
|
||||
$rah->assignJobOrder($jo, $jo->getRider());
|
||||
}
|
||||
if ($jo->getStatus() == JOStatus::RIDER_ASSIGN)
|
||||
{
|
||||
$mclient->sendEvent($jo, $payload);
|
||||
}
|
||||
if ($jo->getStatus() == JOStatus::CANCELLED)
|
||||
{
|
||||
$mclient->sendRiderEvent($jo, $payload);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue