Break down requestJobOrder into smaller functions. #591

This commit is contained in:
Korina Cordero 2021-07-12 04:25:14 +00:00
parent bfc2ee0890
commit 782a0493fc
2 changed files with 20 additions and 10 deletions

View file

@ -21,6 +21,7 @@ use App\Ramcar\TransactionOrigin;
use App\Ramcar\TradeInType; use App\Ramcar\TradeInType;
use App\Ramcar\JOEventType; use App\Ramcar\JOEventType;
use App\Ramcar\HubCriteria; use App\Ramcar\HubCriteria;
use App\Ramcar\ModeOfPayment;
use App\Service\InvoiceGeneratorInterface; use App\Service\InvoiceGeneratorInterface;
use App\Service\RisingTideGateway; use App\Service\RisingTideGateway;
@ -41,9 +42,12 @@ use App\Entity\CustomerVehicle;
use App\Entity\Promo; use App\Entity\Promo;
use App\Entity\Battery; use App\Entity\Battery;
use App\Entity\JOEvent; use App\Entity\JOEvent;
use App\Entity\Customer;
use Catalyst\APIBundle\Access\Generator as ACLGenerator; use Catalyst\APIBundle\Access\Generator as ACLGenerator;
use DateTime;
class JobOrderController extends APIController class JobOrderController extends APIController
{ {
protected $acl_gen; protected $acl_gen;
@ -87,7 +91,7 @@ class JobOrderController extends APIController
return new APIResponse(false, 'No mobile user found.'); return new APIResponse(false, 'No mobile user found.');
// validate request // validate request
$msg = $this->validateJORequest($req); $msg = $this->validateJORequest($req, $geo);
if ($msg) if ($msg)
return new APIResponse(false, $msg); return new APIResponse(false, $msg);
@ -113,13 +117,13 @@ class JobOrderController extends APIController
// set hub criteria details // set hub criteria details
$hub_criteria = new HubCriteria(); $hub_criteria = new HubCriteria();
$this->setHubCriteria($em, $hub_geofence, $hub_criteria, $jo); $this->setHubCriteria($em, $hub_geofence, $hub_criteria, $jo, $req);
// find nearest hubs // find nearest hubs
$nearest_hubs = $hub_select->find($hub_criteria); $nearest_hubs = $hub_select->find($hub_criteria);
if (!empty($nearest_hubs)) if (!empty($nearest_hubs))
$this->assignRider($nearest_hubs, $jo, $hub_filter_logger); $this->assignRider($nearest_hubs, $jo, $hub_filter_logger, $hub_dist);
$em->persist($jo); $em->persist($jo);
$em->persist($invoice); $em->persist($invoice);
@ -154,7 +158,7 @@ class JobOrderController extends APIController
'invoice' => $invoice_data 'invoice' => $invoice_data
]; ];
$this->removeCustomerTag($jo, $cv, $promo_logger, $mobile_user->getID()); $this->removeCustomerTag($jo, $jo->getCustomerVehicle(), $promo_logger, $mobile_user->getID());
return new APIResponse(true, 'Job order creatd', $data); return new APIResponse(true, 'Job order creatd', $data);
} }
@ -1403,9 +1407,11 @@ class JobOrderController extends APIController
return $time_selected; return $time_selected;
} }
protected function validateJORequest(Request $req) protected function validateJORequest(Request $req, GeofenceTracker $geo)
{ {
// geofence // geofence
$long = $req->request->get('long');
$lat = $req->request->get('lat');
$is_covered = $geo->isCovered($long, $lat); $is_covered = $geo->isCovered($long, $lat);
if (!$is_covered) if (!$is_covered)
{ {
@ -1507,7 +1513,7 @@ class JobOrderController extends APIController
$batt = $em->getRepository(Battery::class)->find($batt_id); $batt = $em->getRepository(Battery::class)->find($batt_id);
if ($batt == null) if ($batt == null)
{ {
$msg = 'Invalid battery id'); $msg = 'Invalid battery id';
return $msg; return $msg;
} }
} }
@ -1521,6 +1527,7 @@ class JobOrderController extends APIController
// check trade-in // check trade-in
// only allow motolite, other, none // only allow motolite, other, none
$trade_in = $req->request->get('trade_in');
switch ($trade_in) switch ($trade_in)
{ {
case TradeInType::MOTOLITE: case TradeInType::MOTOLITE:
@ -1538,7 +1545,7 @@ class JobOrderController extends APIController
} }
protected function setHubCriteria(EntityManagerInterface $em, HubFilteringGeoChecker $hub_geofence, protected function setHubCriteria(EntityManagerInterface $em, HubFilteringGeoChecker $hub_geofence,
HubCriteria $hub_criteria, JobOrder $jo) HubCriteria $hub_criteria, JobOrder $jo, Request $req)
{ {
$hub_criteria->setPoint($jo->getCoordinates()); $hub_criteria->setPoint($jo->getCoordinates());
$long = $jo->getCoordinates()->getLongitude(); $long = $jo->getCoordinates()->getLongitude();
@ -1569,7 +1576,8 @@ class JobOrderController extends APIController
} }
} }
protected function assignRider($nearest_hubs, JobOrder $jo, HubFilterLogger $hub_filter_logger) protected function assignRider($nearest_hubs, JobOrder $jo, HubFilterLogger $hub_filter_logger,
HubDistributor $hub_dist)
{ {
// try to assin rider // try to assin rider
// go through the hub list, find the nearest hub // go through the hub list, find the nearest hub
@ -1615,7 +1623,7 @@ class JobOrderController extends APIController
else else
{ {
// log hub into hub_filter_log // log hub into hub_filter_log
$hub_filter_logger->logFilteredHub($nearest_hub['hub'], 'no_available_rider'); $hub_filter_logger->logFilteredHub($nearest_hub['hub'], 'no_available_rider', $jo->getID(), $jo->getCustomer()->getID());
// continue to go through list to find hub with an available rider // continue to go through list to find hub with an available rider
} }
} }

View file

@ -123,7 +123,9 @@ class ResqInvoiceGenerator implements InvoiceGeneratorInterface
// get current user // get current user
$user = $this->security->getUser(); $user = $this->security->getUser();
if ($user != null) // check if user is User or APIUser
//if ($user != null)
if ($user instanceof User)
{ {
$invoice->setCreatedBy($user); $invoice->setCreatedBy($user);
} }