Move logout and getJobOrder to the service. #311
This commit is contained in:
parent
c3662cd185
commit
3e5ea688bc
4 changed files with 210 additions and 140 deletions
|
|
@ -185,156 +185,49 @@ class RAPIController extends Controller
|
||||||
|
|
||||||
// response
|
// response
|
||||||
return $res->getReturnResponse();
|
return $res->getReturnResponse();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function logout(Request $req, RiderCache $rcache)
|
public function logout(Request $req, RiderAPIHandlerInterface $rapi_handler)
|
||||||
{
|
{
|
||||||
$required_params = [];
|
$res = new APIResult();
|
||||||
$em = $this->getDoctrine()->getManager();
|
|
||||||
$res = $this->checkParamsAndKey($req, $em, $required_params);
|
|
||||||
if ($res->isError())
|
|
||||||
return $res->getReturnResponse();
|
|
||||||
|
|
||||||
// make rider unavailable
|
$data = $rapi_handler->logout($req);
|
||||||
$rider = $this->session->getRider();
|
|
||||||
$rider->setAvailable(false);
|
|
||||||
|
|
||||||
// remove from cache
|
if (isset($data['error']))
|
||||||
$rcache->removeActiveRider($rider->getID());
|
|
||||||
|
|
||||||
// remove rider from session
|
|
||||||
$this->session->setRider(null);
|
|
||||||
|
|
||||||
// TODO: log rider logging out
|
|
||||||
|
|
||||||
$em->flush();
|
|
||||||
|
|
||||||
return $res->getReturnResponse();
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getJobOrder(Request $req)
|
|
||||||
{
|
{
|
||||||
// get the job order of the rider assigned to this session
|
$message = $data['error'];
|
||||||
$required_params = [];
|
|
||||||
$em = $this->getDoctrine()->getManager();
|
|
||||||
$res = $this->checkParamsAndKey($req, $em, $required_params);
|
|
||||||
if ($res->isError())
|
|
||||||
return $res->getReturnResponse();
|
|
||||||
|
|
||||||
// are we logged in?
|
|
||||||
if (!$this->session->hasRider())
|
|
||||||
{
|
|
||||||
$res->setError(true)
|
$res->setError(true)
|
||||||
->setErrorMessage('No logged in rider.');
|
->setErrorMessage($message);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
$res->setData($data);
|
||||||
|
}
|
||||||
|
|
||||||
|
// response
|
||||||
return $res->getReturnResponse();
|
return $res->getReturnResponse();
|
||||||
}
|
}
|
||||||
|
|
||||||
$rider = $this->session->getRider();
|
public function getJobOrder(Request $req, RiderAPIHandlerInterface $rapi_handler)
|
||||||
|
|
||||||
// do we have a job order?
|
|
||||||
$jo = $rider->getActiveJobOrder();
|
|
||||||
if ($jo == null)
|
|
||||||
{
|
{
|
||||||
$data = [
|
$res = new APIResult();
|
||||||
'job_order' => null
|
|
||||||
];
|
$data = $rapi_handler->getJobOrder($req);
|
||||||
|
|
||||||
|
if (isset($data['error']))
|
||||||
|
{
|
||||||
|
$message = $data['error'];
|
||||||
|
|
||||||
|
$res->setError(true)
|
||||||
|
->setErrorMessage($message);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
$coord = $jo->getCoordinates();
|
|
||||||
$cust = $jo->getCustomer();
|
|
||||||
$cv = $jo->getCustomerVehicle();
|
|
||||||
$v = $cv->getVehicle();
|
|
||||||
$inv = $jo->getInvoice();
|
|
||||||
$promo = $inv->getPromo();
|
|
||||||
|
|
||||||
// invoice items
|
|
||||||
$inv_items = [];
|
|
||||||
foreach ($inv->getItems() as $item)
|
|
||||||
{
|
|
||||||
$item_batt = $item->getBattery();
|
|
||||||
if ($item_batt == null)
|
|
||||||
$batt_id = null;
|
|
||||||
else
|
|
||||||
$batt_id = $item_batt->getID();
|
|
||||||
|
|
||||||
$inv_items[] = [
|
|
||||||
'id' => $item->getID(),
|
|
||||||
'title' => $item->getTitle(),
|
|
||||||
'qty' => $item->getQuantity(),
|
|
||||||
'price' => $item->getPrice(),
|
|
||||||
'batt_id' => $batt_id,
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
// promo
|
|
||||||
if ($promo != null)
|
|
||||||
{
|
|
||||||
$promo_data = [
|
|
||||||
'id' => $promo->getID(),
|
|
||||||
'name' => $promo->getName(),
|
|
||||||
'code' => $promo->getCode(),
|
|
||||||
'discount_rate' => $promo->getDiscountRate(),
|
|
||||||
'discount_apply' => $promo->getDiscountApply(),
|
|
||||||
];
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
$promo_data = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
$trade_in_type = $jo->getTradeInType();
|
|
||||||
if (empty($trade_in_type))
|
|
||||||
$trade_in_type = 'none';
|
|
||||||
|
|
||||||
|
|
||||||
$data = [
|
|
||||||
'job_order' => [
|
|
||||||
'id' => $jo->getID(),
|
|
||||||
'service_type' => $jo->getServiceType(),
|
|
||||||
'date_schedule' => $jo->getDateSchedule()->format('Ymd H:i:s'),
|
|
||||||
'longitude' => $coord->getLongitude(),
|
|
||||||
'latitude' => $coord->getLatitude(),
|
|
||||||
'status' => $jo->getStatus(),
|
|
||||||
'customer' => [
|
|
||||||
'title' => $cust->getTitle(),
|
|
||||||
'first_name' => $cust->getFirstName(),
|
|
||||||
'last_name' => $cust->getLastName(),
|
|
||||||
'phone_mobile' => '63' . $cust->getPhoneMobile(),
|
|
||||||
],
|
|
||||||
'vehicle' => [
|
|
||||||
'manufacturer' => $v->getManufacturer()->getName(),
|
|
||||||
'make' => $v->getMake(),
|
|
||||||
'model' => $cv->getModelYear(),
|
|
||||||
'plate_number' => $cv->getPlateNumber(),
|
|
||||||
'color' => $cv->getColor(),
|
|
||||||
],
|
|
||||||
'or_num' => $jo->getORNum(),
|
|
||||||
'or_name' => $jo->getORName(),
|
|
||||||
'delivery_instructions' => $jo->getDeliveryInstructions(),
|
|
||||||
'delivery_address' => $jo->getDeliveryAddress(),
|
|
||||||
'landmark' => $jo->getLandmark(),
|
|
||||||
'invoice' => [
|
|
||||||
'discount' => $inv->getDiscount(),
|
|
||||||
'trade_in' => $inv->getTradeIn(),
|
|
||||||
'total_price' => $inv->getTotalPrice(),
|
|
||||||
'vat' => $inv->getVat(),
|
|
||||||
'items' => $inv_items,
|
|
||||||
],
|
|
||||||
'mode_of_payment' => $jo->getModeOfPayment(),
|
|
||||||
'trade_in_type' => $trade_in_type,
|
|
||||||
'promo' => $promo_data,
|
|
||||||
// TODO: load the actual
|
|
||||||
'has_warranty_doc' => false,
|
|
||||||
'flag_coolant' => $jo->hasCoolant(),
|
|
||||||
'has_motolite' => $cv->hasMotoliteBattery(),
|
|
||||||
]
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
$res->setData($data);
|
$res->setData($data);
|
||||||
|
}
|
||||||
|
|
||||||
|
// response
|
||||||
return $res->getReturnResponse();
|
return $res->getReturnResponse();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -23,15 +23,18 @@ class CMBRiderAPIHandler implements RiderAPIHandlerInterface
|
||||||
protected $redis;
|
protected $redis;
|
||||||
protected $ef;
|
protected $ef;
|
||||||
protected $rcache;
|
protected $rcache;
|
||||||
|
protected $country_code;
|
||||||
protected $session;
|
protected $session;
|
||||||
|
|
||||||
public function __construct(EntityManagerInterface $em, RedisClientProvider $redis,
|
public function __construct(EntityManagerInterface $em, RedisClientProvider $redis,
|
||||||
EncoderFactoryInterface $ef, RiderCache $rcache)
|
EncoderFactoryInterface $ef, RiderCache $rcache,
|
||||||
|
string $country_code)
|
||||||
{
|
{
|
||||||
$this->em = $em;
|
$this->em = $em;
|
||||||
$this->redis = $redis;
|
$this->redis = $redis;
|
||||||
$this->ef = $ef;
|
$this->ef = $ef;
|
||||||
$this->rcache = $rcache;
|
$this->rcache = $rcache;
|
||||||
|
$this->country_code = $country_code;
|
||||||
|
|
||||||
// one device = one session, since we have control over the devices
|
// one device = one session, since we have control over the devices
|
||||||
// when a rider logs in, we just change the rider assigned to the device
|
// when a rider logs in, we just change the rider assigned to the device
|
||||||
|
|
@ -190,6 +193,152 @@ class CMBRiderAPIHandler implements RiderAPIHandlerInterface
|
||||||
return $data;
|
return $data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function logout(Request $req)
|
||||||
|
{
|
||||||
|
$required_params = [];
|
||||||
|
$data = $this->checkParamsAndKey($req, $required_params);
|
||||||
|
if (isset($data['error']))
|
||||||
|
return $data;
|
||||||
|
|
||||||
|
// make rider unavailable
|
||||||
|
$rider = $this->session->getRider();
|
||||||
|
$rider->setAvailable(false);
|
||||||
|
|
||||||
|
// remove from cache
|
||||||
|
$this->rcache->removeActiveRider($rider->getID());
|
||||||
|
|
||||||
|
// remove rider from session
|
||||||
|
$this->session->setRider(null);
|
||||||
|
|
||||||
|
// TODO: log rider logging out
|
||||||
|
|
||||||
|
$this->em->flush();
|
||||||
|
|
||||||
|
return $data;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getJobOrder(Request $req)
|
||||||
|
{
|
||||||
|
// get the job order of the rider assigned to this session
|
||||||
|
$required_params = [];
|
||||||
|
$data = $this->checkParamsAndKey($req, $required_params);
|
||||||
|
if (isset($data['error']))
|
||||||
|
return $data;
|
||||||
|
|
||||||
|
// are we logged in?
|
||||||
|
if (!$this->session->hasRider())
|
||||||
|
{
|
||||||
|
$data = [
|
||||||
|
'error' => 'No logged in rider.'
|
||||||
|
];
|
||||||
|
return $data;
|
||||||
|
}
|
||||||
|
|
||||||
|
$rider = $this->session->getRider();
|
||||||
|
|
||||||
|
// do we have a job order?
|
||||||
|
$jo = $rider->getActiveJobOrder();
|
||||||
|
if ($jo == null)
|
||||||
|
{
|
||||||
|
$data = [
|
||||||
|
'job_order' => null
|
||||||
|
];
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
$coord = $jo->getCoordinates();
|
||||||
|
$cust = $jo->getCustomer();
|
||||||
|
$cv = $jo->getCustomerVehicle();
|
||||||
|
$v = $cv->getVehicle();
|
||||||
|
$inv = $jo->getInvoice();
|
||||||
|
$promo = $inv->getPromo();
|
||||||
|
|
||||||
|
// invoice items
|
||||||
|
$inv_items = [];
|
||||||
|
foreach ($inv->getItems() as $item)
|
||||||
|
{
|
||||||
|
$item_batt = $item->getBattery();
|
||||||
|
if ($item_batt == null)
|
||||||
|
$batt_id = null;
|
||||||
|
else
|
||||||
|
$batt_id = $item_batt->getID();
|
||||||
|
|
||||||
|
$inv_items[] = [
|
||||||
|
'id' => $item->getID(),
|
||||||
|
'title' => $item->getTitle(),
|
||||||
|
'qty' => $item->getQuantity(),
|
||||||
|
'price' => $item->getPrice(),
|
||||||
|
'batt_id' => $batt_id,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
// promo
|
||||||
|
if ($promo != null)
|
||||||
|
{
|
||||||
|
$promo_data = [
|
||||||
|
'id' => $promo->getID(),
|
||||||
|
'name' => $promo->getName(),
|
||||||
|
'code' => $promo->getCode(),
|
||||||
|
'discount_rate' => $promo->getDiscountRate(),
|
||||||
|
'discount_apply' => $promo->getDiscountApply(),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
$promo_data = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
$trade_in_type = $jo->getTradeInType();
|
||||||
|
if (empty($trade_in_type))
|
||||||
|
$trade_in_type = 'none';
|
||||||
|
|
||||||
|
$data = [
|
||||||
|
'job_order' => [
|
||||||
|
'id' => $jo->getID(),
|
||||||
|
'service_type' => $jo->getServiceType(),
|
||||||
|
'date_schedule' => $jo->getDateSchedule()->format('Ymd H:i:s'),
|
||||||
|
'longitude' => $coord->getLongitude(),
|
||||||
|
'latitude' => $coord->getLatitude(),
|
||||||
|
'status' => $jo->getStatus(),
|
||||||
|
'customer' => [
|
||||||
|
'title' => $cust->getTitle(),
|
||||||
|
'first_name' => $cust->getFirstName(),
|
||||||
|
'last_name' => $cust->getLastName(),
|
||||||
|
'phone_mobile' => $this->country_code . $cust->getPhoneMobile(),
|
||||||
|
],
|
||||||
|
'vehicle' => [
|
||||||
|
'manufacturer' => $v->getManufacturer()->getName(),
|
||||||
|
'make' => $v->getMake(),
|
||||||
|
'model' => $cv->getModelYear(),
|
||||||
|
'plate_number' => $cv->getPlateNumber(),
|
||||||
|
'color' => $cv->getColor(),
|
||||||
|
],
|
||||||
|
'or_num' => $jo->getORNum(),
|
||||||
|
'or_name' => $jo->getORName(),
|
||||||
|
'delivery_instructions' => $jo->getDeliveryInstructions(),
|
||||||
|
'delivery_address' => $jo->getDeliveryAddress(),
|
||||||
|
'landmark' => $jo->getLandmark(),
|
||||||
|
'invoice' => [
|
||||||
|
'discount' => $inv->getDiscount(),
|
||||||
|
'trade_in' => $inv->getTradeIn(),
|
||||||
|
'total_price' => $inv->getTotalPrice(),
|
||||||
|
'vat' => $inv->getVat(),
|
||||||
|
'items' => $inv_items,
|
||||||
|
],
|
||||||
|
'mode_of_payment' => $jo->getModeOfPayment(),
|
||||||
|
'trade_in_type' => $trade_in_type,
|
||||||
|
'promo' => $promo_data,
|
||||||
|
// TODO: load the actual
|
||||||
|
'has_warranty_doc' => false,
|
||||||
|
'flag_coolant' => $jo->hasCoolant(),
|
||||||
|
'has_motolite' => $cv->hasMotoliteBattery(),
|
||||||
|
]
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
return $data;
|
||||||
|
}
|
||||||
|
|
||||||
protected function checkMissingParameters(Request $req, $params = [])
|
protected function checkMissingParameters(Request $req, $params = [])
|
||||||
{
|
{
|
||||||
$missing = [];
|
$missing = [];
|
||||||
|
|
|
||||||
|
|
@ -6,8 +6,8 @@ use Doctrine\ORM\EntityManagerInterface;
|
||||||
use Symfony\Component\HttpFoundation\Request;
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
|
use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
|
||||||
|
|
||||||
use App\Ramcar\CMBServiceType;
|
use App\Ramcar\ServiceType;
|
||||||
use App\Ramcar\CMBTradeInType;
|
use App\Ramcar\TradeInType;
|
||||||
|
|
||||||
use App\Service\RiderAPIHandlerInterface;
|
use App\Service\RiderAPIHandlerInterface;
|
||||||
use App\Service\RedisClientProvider;
|
use App\Service\RedisClientProvider;
|
||||||
|
|
@ -190,6 +190,30 @@ class ResqRiderAPIHandler implements RiderAPIHandlerInterface
|
||||||
return $data;
|
return $data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function logout(Request $req)
|
||||||
|
{
|
||||||
|
$required_params = [];
|
||||||
|
$data = $this->checkParamsAndKey($req, $required_params);
|
||||||
|
if (isset($data['error']))
|
||||||
|
return $data;
|
||||||
|
|
||||||
|
// make rider unavailable
|
||||||
|
$rider = $this->session->getRider();
|
||||||
|
$rider->setAvailable(false);
|
||||||
|
|
||||||
|
// remove from cache
|
||||||
|
$this->rcache->removeActiveRider($rider->getID());
|
||||||
|
|
||||||
|
// remove rider from session
|
||||||
|
$this->session->setRider(null);
|
||||||
|
|
||||||
|
// TODO: log rider logging out
|
||||||
|
|
||||||
|
$this->em->flush();
|
||||||
|
|
||||||
|
return $data;
|
||||||
|
}
|
||||||
|
|
||||||
protected function checkMissingParameters(Request $req, $params = [])
|
protected function checkMissingParameters(Request $req, $params = [])
|
||||||
{
|
{
|
||||||
$missing = [];
|
$missing = [];
|
||||||
|
|
|
||||||
|
|
@ -9,4 +9,8 @@ interface RiderAPIHandlerInterface
|
||||||
public function register(Request $req);
|
public function register(Request $req);
|
||||||
|
|
||||||
public function login(Request $req);
|
public function login(Request $req);
|
||||||
|
|
||||||
|
public function logout(Request $req);
|
||||||
|
|
||||||
|
public function getJobOrder(Request $req);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue