Add controllers for battery, partner, promo, rider, and service. #591
This commit is contained in:
parent
ece60b177d
commit
fead12bcab
5 changed files with 1081 additions and 0 deletions
168
src/Controller/ResqAPI/BatteryController.php
Normal file
168
src/Controller/ResqAPI/BatteryController.php
Normal file
|
|
@ -0,0 +1,168 @@
|
|||
<?php
|
||||
|
||||
namespace App\Controller\ResqAPI;
|
||||
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
|
||||
use Doctrine\ORM\Query;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
|
||||
use Catalyst\APIBundle\Controller\APIController;
|
||||
// TODO: what do we use for response? APIResponse or APIResult?
|
||||
// APIResult is what is used by APIController. APIResponse is what is used by CAPI
|
||||
use Catalyst\APIBundle\Response\APIResponse;
|
||||
use App\Ramcar\APIResult;
|
||||
|
||||
use App\Entity\Vehicle;
|
||||
|
||||
use Catalyst\APIBundle\Access\Generator as ACLGenerator;
|
||||
|
||||
class BatteryController extends APIController
|
||||
{
|
||||
protected $acl_gen;
|
||||
|
||||
public function __construct(ACLGenerator $acl_gen)
|
||||
{
|
||||
$this->acl_gen = $acl_gen;
|
||||
}
|
||||
|
||||
public function getCompatibleBatteries(Request $req, $vid, EntityManagerInterface $em)
|
||||
{
|
||||
// check required parameters and api key
|
||||
$required_params = [];
|
||||
$res = $this->checkParamsAndKey($req, $em, $required_params);
|
||||
if ($res->isError())
|
||||
return $res->getReturnResponse();
|
||||
|
||||
// get vehicle
|
||||
$vehicle = $em->getRepository(Vehicle::class)->find($vid);
|
||||
if ($vehicle == null)
|
||||
{
|
||||
$res->setError(true)
|
||||
->setErrorMessage('Invalid vehicle');
|
||||
return $res->getReturnResponse();
|
||||
}
|
||||
|
||||
// batteries
|
||||
$batt_list = [];
|
||||
$batts = $vehicle->getBatteries();
|
||||
foreach ($batts as $batt)
|
||||
{
|
||||
// TODO: Add warranty_tnv to battery information
|
||||
$batt_list[] = [
|
||||
'id' => $batt->getID(),
|
||||
'mfg_id' => $batt->getManufacturer()->getID(),
|
||||
'mfg_name' => $batt->getManufacturer()->getName(),
|
||||
'model_id' => $batt->getModel()->getID(),
|
||||
'model_name' => $batt->getModel()->getName(),
|
||||
'size_id' => $batt->getSize()->getID(),
|
||||
'size_name' => $batt->getSize()->getName(),
|
||||
'price' => $batt->getSellingPrice(),
|
||||
'wty_private' => $batt->getWarrantyPrivate(),
|
||||
'wty_commercial' => $batt->getWarrantyCommercial(),
|
||||
'image_url' => $this->getBatteryImageURL($req, $batt),
|
||||
];
|
||||
}
|
||||
|
||||
// data
|
||||
$data = [
|
||||
'vehicle' => [
|
||||
'id' => $vehicle->getID(),
|
||||
'mfg_id' => $vehicle->getManufacturer()->getID(),
|
||||
'mfg_name' => $vehicle->getManufacturer()->getName(),
|
||||
'make' => $vehicle->getMake(),
|
||||
'model_year_from' => $vehicle->getModelYearFrom(),
|
||||
'model_year_to' => $vehicle->getModelYearTo(),
|
||||
],
|
||||
'batteries' => $batt_list,
|
||||
];
|
||||
$res->setData($data);
|
||||
|
||||
return $res->getReturnResponse();
|
||||
}
|
||||
|
||||
// TODO: since we broke the functions into separate files, we need
|
||||
// to figure out how to make this accessible to all ResqAPI controllers
|
||||
protected function checkParamsAndKey(Request $req, $em, $params)
|
||||
{
|
||||
// TODO: depends on what we decide to return
|
||||
// returns APIResult object
|
||||
$res = new APIResult();
|
||||
|
||||
// check for api_key in query string
|
||||
$api_key = $req->query->get('api_key');
|
||||
if (empty($api_key))
|
||||
{
|
||||
$res->setError(true)
|
||||
->setErrorMessage('Missing API key');
|
||||
return $res;
|
||||
}
|
||||
|
||||
// check missing parameters
|
||||
$missing = $this->checkMissingParameters($req, $params);
|
||||
if (count($missing) > 0)
|
||||
{
|
||||
$miss_string = implode(', ', $missing);
|
||||
$res->setError(true)
|
||||
->setErrorMessage('Missing parameter(s): ' . $miss_string);
|
||||
return $res;
|
||||
}
|
||||
|
||||
// check api key
|
||||
$mobile_user = $this->checkAPIKey($em, $req->query->get('api_key'));
|
||||
if ($mobile_user == null)
|
||||
{
|
||||
$res->setError(true)
|
||||
->setErrorMessage('Invalid API Key');
|
||||
return $res;
|
||||
}
|
||||
|
||||
// store session
|
||||
$this->session = $sess;
|
||||
|
||||
return $res;
|
||||
}
|
||||
|
||||
// TODO: this might not be needed if we use APIController's checkRequiredParameters
|
||||
// or we put this into a service?
|
||||
protected function checkMissingParameters(Request $req, $params = [])
|
||||
{
|
||||
$missing = [];
|
||||
|
||||
// check if parameters are there
|
||||
foreach ($params as $param)
|
||||
{
|
||||
if ($req->getMethod() == 'GET')
|
||||
{
|
||||
$check = $req->query->get($param);
|
||||
if (empty($check))
|
||||
$missing[] = $param;
|
||||
}
|
||||
else if ($req->getMethod() == 'POST')
|
||||
{
|
||||
$check = $req->request->get($param);
|
||||
if (empty($check))
|
||||
$missing[] = $param;
|
||||
}
|
||||
else
|
||||
return $params;
|
||||
}
|
||||
|
||||
return $missing;
|
||||
}
|
||||
|
||||
// TODO: type hint entity manager
|
||||
// TODO: since we broke the functions into separate files, we need
|
||||
// to figure out how to make this accessible to all ResqAPI controllers
|
||||
protected function checkAPIKey($em, $api_key)
|
||||
{
|
||||
// find the api key (session id)
|
||||
// TODO: user validation needs to be changed
|
||||
$m_user = $em->getRepository(MobileUser::class)->find($api_key);
|
||||
if ($m_user == null)
|
||||
return null;
|
||||
|
||||
return $m_user;
|
||||
}
|
||||
}
|
||||
257
src/Controller/ResqAPI/PartnerController.php
Normal file
257
src/Controller/ResqAPI/PartnerController.php
Normal file
|
|
@ -0,0 +1,257 @@
|
|||
<?php
|
||||
|
||||
namespace App\Controller\ResqAPI;
|
||||
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
|
||||
use Doctrine\ORM\Query;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
|
||||
use Catalyst\APIBundle\Controller\APIController;
|
||||
// TODO: what do we use for response? APIResponse or APIResult?
|
||||
// APIResult is what is used by APIController. APIResponse is what is used by CAPI
|
||||
use Catalyst\APIBundle\Response\APIResponse;
|
||||
use App\Ramcar\APIResult;
|
||||
|
||||
use App\Entity\Partner;
|
||||
use App\Entity\Review;
|
||||
|
||||
use Catalyst\APIBundle\Access\Generator as ACLGenerator;
|
||||
|
||||
class PartnerController extends APIController
|
||||
{
|
||||
protected $acl_gen;
|
||||
|
||||
public function __construct(ACLGenerator $acl_gen)
|
||||
{
|
||||
$this->acl_gen = $acl_gen;
|
||||
}
|
||||
|
||||
public function getClosestPartners(Request $req, EntityManagerInterface $em)
|
||||
{
|
||||
$required_params = [
|
||||
'longitude',
|
||||
'latitude',
|
||||
'service_id',
|
||||
'limit',
|
||||
];
|
||||
$res = $this->checkParamsAndKey($req, $em, $required_params);
|
||||
if ($res->isError())
|
||||
return $res->getReturnResponse();
|
||||
|
||||
$long = $req->query->get('longitude');
|
||||
$lat = $req->query->get('latitude');
|
||||
$service_id = $req->query->get('service_id');
|
||||
$limit = $req->query->get('limit');
|
||||
|
||||
// get partners within range
|
||||
$query = $em->createQuery('SELECT p, st_distance(p.coordinates, point(:lng, :lat)) as dist FROM App\Entity\Partner p
|
||||
JOIN App\Entity\Service s where s.id = :service_id ORDER BY dist')
|
||||
->setParameter('lat', $lat)
|
||||
->setParameter('lng', $long)
|
||||
->setParameter('service_id', $service_id);
|
||||
|
||||
$query->setMaxResults($limit);
|
||||
$result = $query->getResult();
|
||||
|
||||
$data = [];
|
||||
$partners = [];
|
||||
foreach($result as $row)
|
||||
{
|
||||
$partners[] = [
|
||||
'id' => $row[0]->getID(),
|
||||
'name' => $row[0]->getName(),
|
||||
'branch' => $row[0]->getBranch(),
|
||||
'address' => $row[0]->getAddress(),
|
||||
'contact_nums' => $row[0]->getContactNumbers(),
|
||||
'time_open' => $row[0]->getTimeOpen()->format("g:i A"),
|
||||
'time_close' => $row[0]->getTimeClose()->format("g:i A"),
|
||||
'longitude' => $row[0]->getCoordinates()->getLongitude(),
|
||||
'latitude' => $row[0]->getCoordinates()->getLatitude(),
|
||||
'db_distance' => $row['dist'],
|
||||
];
|
||||
}
|
||||
|
||||
$data['partners'] = $partners;
|
||||
|
||||
$res->setData($data);
|
||||
|
||||
return $res->getReturnResponse();
|
||||
}
|
||||
|
||||
public function getPartnerInformation(Request $req, $pid, EntityManagerInterface $em)
|
||||
{
|
||||
$required_params = [];
|
||||
$res = $this->checkParamsAndKey($req, $em, $required_params);
|
||||
if ($res->isError())
|
||||
return $res->getReturnResponse();
|
||||
|
||||
// get partner
|
||||
$partner = $em->getRepository(Partner::class)->findOneBy(['id' => $pid]);
|
||||
if ($partner == null)
|
||||
{
|
||||
$res->setError(true)
|
||||
->setErrorMessage('No partner found.');
|
||||
return $res->getReturnResponse();
|
||||
}
|
||||
|
||||
// get reviews for partner
|
||||
$reviews = $em->getRepository(Review::class)->findBy(['partner' => $partner]);
|
||||
|
||||
// get average rating for all reviews
|
||||
$average_rating = 0;
|
||||
if (!empty($reviews))
|
||||
{
|
||||
$rating = 0;
|
||||
foreach($reviews as $review)
|
||||
{
|
||||
$rating = $rating + $review->getRating();
|
||||
}
|
||||
|
||||
$average_rating = $rating / sizeof($reviews);
|
||||
}
|
||||
|
||||
$data['partner'] = [
|
||||
'id' => $partner->getID(),
|
||||
'name' => $partner->getName(),
|
||||
'branch' => $partner->getBranch(),
|
||||
'address' => $partner->getAddress(),
|
||||
'contact_nums' => $partner->getContactNumbers(),
|
||||
'time_open' => $partner->getTimeOpen()->format("g:i A"),
|
||||
'time_close' => $partner->getTimeClose()->format("g:i A"),
|
||||
'longitude' => $partner->getCoordinates()->getLongitude(),
|
||||
'latitude' => $partner->getCoordinates()->getLatitude(),
|
||||
'average_rating' => $average_rating,
|
||||
];
|
||||
|
||||
$res->setData($data);
|
||||
|
||||
return $res->getReturnResponse();
|
||||
}
|
||||
|
||||
public function reviewPartner($pid, Request $req, EntityManagerInterface $em)
|
||||
{
|
||||
$required_params = [
|
||||
'rating',
|
||||
'message',
|
||||
];
|
||||
|
||||
$res = $this->checkParamsAndKey($req, $em, $required_params);
|
||||
if ($res->isError())
|
||||
return $res->getReturnResponse();
|
||||
|
||||
$rating = $req->request->get('rating');
|
||||
$msg = $req->request->get('message');
|
||||
|
||||
// TODO: check rating if 1 - 5
|
||||
|
||||
// check if partner exists
|
||||
$partner = $em->getRepository(Partner::class)->find($pid);
|
||||
if ($partner == null)
|
||||
{
|
||||
$res->setError(true)
|
||||
->setErrorMessage('No partner found.');
|
||||
return $res->getReturnResponse();
|
||||
}
|
||||
|
||||
$rev = new Review();
|
||||
$rev->setRating($rating)
|
||||
->setMessage($msg)
|
||||
->setPartner($partner)
|
||||
->setMobileSession($this->session);
|
||||
|
||||
// save to db
|
||||
$em->persist($rev);
|
||||
$em->flush();
|
||||
|
||||
$data = [];
|
||||
$res->setData($data);
|
||||
|
||||
return $res->getReturnResponse();
|
||||
}
|
||||
|
||||
// TODO: since we broke the functions into separate files, we need
|
||||
// to figure out how to make this accessible to all ResqAPI controllers
|
||||
protected function checkParamsAndKey(Request $req, $em, $params)
|
||||
{
|
||||
// TODO: depends on what we decide to return
|
||||
// returns APIResult object
|
||||
$res = new APIResult();
|
||||
|
||||
// check for api_key in query string
|
||||
$api_key = $req->query->get('api_key');
|
||||
if (empty($api_key))
|
||||
{
|
||||
$res->setError(true)
|
||||
->setErrorMessage('Missing API key');
|
||||
return $res;
|
||||
}
|
||||
|
||||
// check missing parameters
|
||||
$missing = $this->checkMissingParameters($req, $params);
|
||||
if (count($missing) > 0)
|
||||
{
|
||||
$miss_string = implode(', ', $missing);
|
||||
$res->setError(true)
|
||||
->setErrorMessage('Missing parameter(s): ' . $miss_string);
|
||||
return $res;
|
||||
}
|
||||
|
||||
// check api key
|
||||
$mobile_user = $this->checkAPIKey($em, $req->query->get('api_key'));
|
||||
if ($mobile_user == null)
|
||||
{
|
||||
$res->setError(true)
|
||||
->setErrorMessage('Invalid API Key');
|
||||
return $res;
|
||||
}
|
||||
|
||||
// store session
|
||||
$this->session = $sess;
|
||||
|
||||
return $res;
|
||||
}
|
||||
|
||||
// TODO: this might not be needed if we use APIController's checkRequiredParameters
|
||||
// or we put this into a service?
|
||||
protected function checkMissingParameters(Request $req, $params = [])
|
||||
{
|
||||
$missing = [];
|
||||
|
||||
// check if parameters are there
|
||||
foreach ($params as $param)
|
||||
{
|
||||
if ($req->getMethod() == 'GET')
|
||||
{
|
||||
$check = $req->query->get($param);
|
||||
if (empty($check))
|
||||
$missing[] = $param;
|
||||
}
|
||||
else if ($req->getMethod() == 'POST')
|
||||
{
|
||||
$check = $req->request->get($param);
|
||||
if (empty($check))
|
||||
$missing[] = $param;
|
||||
}
|
||||
else
|
||||
return $params;
|
||||
}
|
||||
|
||||
return $missing;
|
||||
}
|
||||
|
||||
// TODO: type hint entity manager
|
||||
// TODO: since we broke the functions into separate files, we need
|
||||
// to figure out how to make this accessible to all ResqAPI controllers
|
||||
protected function checkAPIKey($em, $api_key)
|
||||
{
|
||||
// find the api key (session id)
|
||||
// TODO: user validation needs to be changed
|
||||
$m_user = $em->getRepository(MobileUser::class)->find($api_key);
|
||||
if ($m_user == null)
|
||||
return null;
|
||||
|
||||
return $m_user;
|
||||
}
|
||||
}
|
||||
124
src/Controller/ResqAPI/PromoController.php
Normal file
124
src/Controller/ResqAPI/PromoController.php
Normal file
|
|
@ -0,0 +1,124 @@
|
|||
<?php
|
||||
|
||||
namespace App\Controller\ResqAPI;
|
||||
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
|
||||
use Doctrine\ORM\Query;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
|
||||
use Catalyst\APIBundle\Controller\APIController;
|
||||
// TODO: what do we use for response? APIResponse or APIResult?
|
||||
// APIResult is what is used by APIController. APIResponse is what is used by CAPI
|
||||
use Catalyst\APIBundle\Response\APIResponse;
|
||||
use App\Ramcar\APIResult;
|
||||
|
||||
use App\Entity\Promo;
|
||||
|
||||
use Catalyst\APIBundle\Access\Generator as ACLGenerator;
|
||||
|
||||
class PromoController extends APIController
|
||||
{
|
||||
protected $acl_gen;
|
||||
|
||||
public function __construct(ACLGenerator $acl_gen)
|
||||
{
|
||||
$this->acl_gen = $acl_gen;
|
||||
}
|
||||
|
||||
public function listPromos(Request $req, EntityManagerInterface $em)
|
||||
{
|
||||
// check required parameters and api key
|
||||
$required_params = [];
|
||||
$res = $this->checkParamsAndKey($req, $em, $required_params);
|
||||
if ($res->isError())
|
||||
return $res->getReturnResponse();
|
||||
|
||||
return $res->getReturnResponse();
|
||||
}
|
||||
|
||||
// TODO: since we broke the functions into separate files, we need
|
||||
// to figure out how to make this accessible to all ResqAPI controllers
|
||||
protected function checkParamsAndKey(Request $req, $em, $params)
|
||||
{
|
||||
// TODO: depends on what we decide to return
|
||||
// returns APIResult object
|
||||
$res = new APIResult();
|
||||
|
||||
// check for api_key in query string
|
||||
$api_key = $req->query->get('api_key');
|
||||
if (empty($api_key))
|
||||
{
|
||||
$res->setError(true)
|
||||
->setErrorMessage('Missing API key');
|
||||
return $res;
|
||||
}
|
||||
|
||||
// check missing parameters
|
||||
$missing = $this->checkMissingParameters($req, $params);
|
||||
if (count($missing) > 0)
|
||||
{
|
||||
$miss_string = implode(', ', $missing);
|
||||
$res->setError(true)
|
||||
->setErrorMessage('Missing parameter(s): ' . $miss_string);
|
||||
return $res;
|
||||
}
|
||||
|
||||
// check api key
|
||||
$mobile_user = $this->checkAPIKey($em, $req->query->get('api_key'));
|
||||
if ($mobile_user == null)
|
||||
{
|
||||
$res->setError(true)
|
||||
->setErrorMessage('Invalid API Key');
|
||||
return $res;
|
||||
}
|
||||
|
||||
// store session
|
||||
$this->session = $sess;
|
||||
|
||||
return $res;
|
||||
}
|
||||
|
||||
// TODO: this might not be needed if we use APIController's checkRequiredParameters
|
||||
// or we put this into a service?
|
||||
protected function checkMissingParameters(Request $req, $params = [])
|
||||
{
|
||||
$missing = [];
|
||||
|
||||
// check if parameters are there
|
||||
foreach ($params as $param)
|
||||
{
|
||||
if ($req->getMethod() == 'GET')
|
||||
{
|
||||
$check = $req->query->get($param);
|
||||
if (empty($check))
|
||||
$missing[] = $param;
|
||||
}
|
||||
else if ($req->getMethod() == 'POST')
|
||||
{
|
||||
$check = $req->request->get($param);
|
||||
if (empty($check))
|
||||
$missing[] = $param;
|
||||
}
|
||||
else
|
||||
return $params;
|
||||
}
|
||||
|
||||
return $missing;
|
||||
}
|
||||
|
||||
// TODO: type hint entity manager
|
||||
// TODO: since we broke the functions into separate files, we need
|
||||
// to figure out how to make this accessible to all ResqAPI controllers
|
||||
protected function checkAPIKey($em, $api_key)
|
||||
{
|
||||
// find the api key (session id)
|
||||
// TODO: user validation needs to be changed
|
||||
$m_user = $em->getRepository(MobileUser::class)->find($api_key);
|
||||
if ($m_user == null)
|
||||
return null;
|
||||
|
||||
return $m_user;
|
||||
}
|
||||
}
|
||||
368
src/Controller/ResqAPI/RiderController.php
Normal file
368
src/Controller/ResqAPI/RiderController.php
Normal file
|
|
@ -0,0 +1,368 @@
|
|||
<?php
|
||||
|
||||
namespace App\Controller\ResqAPI;
|
||||
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
|
||||
use Doctrine\ORM\Query;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
|
||||
use Catalyst\APIBundle\Controller\APIController;
|
||||
// TODO: what do we use for response? APIResponse or APIResult?
|
||||
// APIResult is what is used by APIController. APIResponse is what is used by CAPI
|
||||
use Catalyst\APIBundle\Response\APIResponse;
|
||||
use App\Ramcar\APIResult;
|
||||
|
||||
use App\Entity\Rider;
|
||||
use App\Entity\JobOrder;
|
||||
|
||||
use App\Service\RiderTracker;
|
||||
|
||||
use App\Ramcar\JOStatus;
|
||||
use App\Ramcar\APIRiderStatus;
|
||||
|
||||
use Catalyst\APIBundle\Access\Generator as ACLGenerator;
|
||||
|
||||
class RiderController extends APIController
|
||||
{
|
||||
protected $acl_gen;
|
||||
|
||||
public function __construct(ACLGenerator $acl_gen)
|
||||
{
|
||||
$this->acl_gen = $acl_gen;
|
||||
}
|
||||
|
||||
// TODO: needs to be modified for mobile user
|
||||
public function getRiderStatus(Request $req, RiderTracker $rt, EntityManagerInterface $em)
|
||||
{
|
||||
$required_params = [];
|
||||
$res = $this->checkParamsAndKey($req, $em, $required_params);
|
||||
if ($res->isError())
|
||||
return $res->getReturnResponse();
|
||||
|
||||
// get customer
|
||||
$cust = $this->session->getCustomer();
|
||||
if ($cust == null)
|
||||
{
|
||||
$res->setError(true)
|
||||
->setErrorMessage('No customer information found');
|
||||
return $res->getReturnResponse();
|
||||
}
|
||||
|
||||
$ongoing_jos = $this->getOngoingJobOrders($cust, $em);
|
||||
|
||||
if (count($ongoing_jos) <= 0)
|
||||
{
|
||||
try
|
||||
{
|
||||
// check if the latest fulfilled jo they have needs rider rating
|
||||
$query = $em->createQuery('select jo from App\Entity\JobOrder jo where jo.customer = :cust and jo.status = :status order by jo.date_fulfill desc');
|
||||
$fulfill_jo = $query->setParameters([
|
||||
'cust' => $cust,
|
||||
'status' => JOStatus::FULFILLED,
|
||||
])
|
||||
->setMaxResults(1)
|
||||
->getSingleResult();
|
||||
}
|
||||
catch (Exception $e)
|
||||
{
|
||||
// no pending
|
||||
$res->setData([
|
||||
'status' => APIRiderStatus::NO_PENDING_JO
|
||||
]);
|
||||
return $res->getReturnResponse();
|
||||
}
|
||||
|
||||
// we got a recently fulfilled job order
|
||||
if ($fulfill_jo)
|
||||
{
|
||||
// check if the rider has been rated
|
||||
if (!$fulfill_jo->hasRiderRating())
|
||||
{
|
||||
$dest = $fulfill_jo->getCoordinates();
|
||||
|
||||
$data = [
|
||||
'jo_id' => $fulfill_jo->getID(),
|
||||
'service_type' => $fulfill_jo->getServiceType(),
|
||||
'destination' => [
|
||||
'long' => $dest->getLongitude(),
|
||||
'lat' => $dest->getLatitude(),
|
||||
],
|
||||
'delivery_address' => $fulfill_jo->getDeliveryAddress(),
|
||||
'delivery_instructions' => $fulfill_jo->getDeliveryInstructions(),
|
||||
];
|
||||
|
||||
$rider = $fulfill_jo->getRider();
|
||||
|
||||
// default image url
|
||||
$url_prefix = $req->getSchemeAndHttpHost();
|
||||
$image_url = $url_prefix . '/assets/images/user.gif';
|
||||
if ($rider->getImageFile() != null)
|
||||
$image_url = $url_prefix . '/uploads/' . $rider->getImageFile();
|
||||
|
||||
$data['status'] = APIRiderStatus::RIDER_RATING;
|
||||
// default rider location to hub
|
||||
$data['rider'] = [
|
||||
'id' => $rider->getID(),
|
||||
'name' => $rider->getFullName(),
|
||||
'plate_num' => $rider->getPlateNumber(),
|
||||
'contact_num' => $rider->getContactNumber(),
|
||||
'image_url' => $image_url,
|
||||
];
|
||||
$res->setData($data);
|
||||
return $res->getReturnResponse();
|
||||
}
|
||||
}
|
||||
|
||||
// no pending
|
||||
$res->setData([
|
||||
'status' => APIRiderStatus::NO_PENDING_JO
|
||||
]);
|
||||
return $res->getReturnResponse();
|
||||
}
|
||||
|
||||
// get first jo that's pending
|
||||
$jo = $ongoing_jos[0];
|
||||
$dest = $jo->getCoordinates();
|
||||
|
||||
$data = [
|
||||
'jo_id' => $jo->getID(),
|
||||
'service_type' => $jo->getServiceType(),
|
||||
'destination' => [
|
||||
'long' => $dest->getLongitude(),
|
||||
'lat' => $dest->getLatitude(),
|
||||
],
|
||||
'delivery_address' => $jo->getDeliveryAddress(),
|
||||
'delivery_instructions' => $jo->getDeliveryInstructions(),
|
||||
];
|
||||
|
||||
switch ($jo->getStatus())
|
||||
{
|
||||
case JOStatus::PENDING:
|
||||
$data['status'] = APIRiderStatus::OUTLET_ASSIGN;
|
||||
$res->setData($data);
|
||||
return $res->getReturnResponse();
|
||||
case JOStatus::RIDER_ASSIGN:
|
||||
$data['status'] = APIRiderStatus::RIDER_ASSIGN;
|
||||
$res->setData($data);
|
||||
return $res->getReturnResponse();
|
||||
case JOStatus::ASSIGNED:
|
||||
case JOStatus::IN_TRANSIT:
|
||||
case JOStatus::IN_PROGRESS:
|
||||
$rider = $jo->getRider();
|
||||
// get rider coordinates from redis
|
||||
$coord = $rt->getRiderLocation($rider->getID());
|
||||
|
||||
// default image url
|
||||
$url_prefix = $req->getSchemeAndHttpHost();
|
||||
$image_url = $url_prefix . '/assets/images/user.gif';
|
||||
if ($rider->getImageFile() != null)
|
||||
$image_url = $url_prefix . '/uploads/' . $rider->getImageFile();
|
||||
|
||||
$data['status'] = APIRiderStatus::RIDER_PICK_UP;
|
||||
// TODO: fix this to actual location of rider
|
||||
// default rider location to hub
|
||||
$data['rider'] = [
|
||||
'id' => $rider->getID(),
|
||||
'name' => $rider->getFullName(),
|
||||
'plate_num' => $rider->getPlateNumber(),
|
||||
'contact_num' => $rider->getContactNumber(),
|
||||
'image_url' => $image_url,
|
||||
'location' => [
|
||||
'long' => $coord->getLongitude(),
|
||||
'lat' => $coord->getLatitude()
|
||||
]
|
||||
];
|
||||
$res->setData($data);
|
||||
return $res->getReturnResponse();
|
||||
}
|
||||
|
||||
$res->setData($data);
|
||||
|
||||
return $res->getReturnResponse();
|
||||
}
|
||||
|
||||
// TODO: needs to be modified for mobile user
|
||||
public function addRiderRating(Request $req, EntityManagerInterface $em)
|
||||
{
|
||||
$required_params = [
|
||||
'jo_id',
|
||||
'rating',
|
||||
];
|
||||
$res = $this->checkParamsAndKey($req, $em, $required_params);
|
||||
if ($res->isError())
|
||||
return $res->getReturnResponse();
|
||||
|
||||
// get customer
|
||||
$cust = $this->session->getCustomer();
|
||||
if ($cust == null)
|
||||
{
|
||||
$res->setError(true)
|
||||
->setErrorMessage('No customer information found');
|
||||
return $res->getReturnResponse();
|
||||
}
|
||||
|
||||
// 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();
|
||||
}
|
||||
|
||||
// get rider
|
||||
$rider = $jo->getRider();
|
||||
if ($rider == null)
|
||||
{
|
||||
$res->setError(true)
|
||||
->setErrorMessage('No rider found');
|
||||
return $res->getReturnResponse();
|
||||
}
|
||||
|
||||
// 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();
|
||||
}
|
||||
|
||||
// TODO: check job order status, if it's complete
|
||||
|
||||
// add rider rating
|
||||
$rating_num = $req->request->get('rating', -1);
|
||||
|
||||
// if rating is -1
|
||||
if ($rating_num == -1)
|
||||
{
|
||||
$jo->setHasRiderRating();
|
||||
$em->flush();
|
||||
|
||||
$res->setData([]);
|
||||
|
||||
return $res->getReturnResponse();
|
||||
}
|
||||
|
||||
|
||||
$rating = new RiderRating();
|
||||
$rating->setRider($rider)
|
||||
->setCustomer($cust)
|
||||
->setJobOrder($jo)
|
||||
->setRating($rating_num);
|
||||
|
||||
// rider rating comment
|
||||
$comment = $req->request->get('comment');
|
||||
if (!empty($comment))
|
||||
$rating->setComment($comment);
|
||||
|
||||
// mark jo as rider rated already
|
||||
$jo->setHasRiderRating();
|
||||
|
||||
$em->persist($rating);
|
||||
$em->flush();
|
||||
|
||||
// TODO: set average rating in rider entity
|
||||
|
||||
$res->setData([]);
|
||||
|
||||
return $res->getReturnResponse();
|
||||
}
|
||||
|
||||
protected function getOngoingJobOrders($cust, $em)
|
||||
{
|
||||
$ongoing_jos = $em->getRepository(JobOrder::class)->findBy([
|
||||
'customer' => $cust,
|
||||
'status' => [JOStatus::PENDING, JOStatus::RIDER_ASSIGN, JOStatus::IN_TRANSIT, JOStatus::ASSIGNED, JOStatus::IN_PROGRESS],
|
||||
]);
|
||||
|
||||
return $ongoing_jos;
|
||||
}
|
||||
|
||||
// TODO: since we broke the functions into separate files, we need
|
||||
// to figure out how to make this accessible to all ResqAPI controllers
|
||||
protected function checkParamsAndKey(Request $req, $em, $params)
|
||||
{
|
||||
// TODO: depends on what we decide to return
|
||||
// returns APIResult object
|
||||
$res = new APIResult();
|
||||
|
||||
// check for api_key in query string
|
||||
$api_key = $req->query->get('api_key');
|
||||
if (empty($api_key))
|
||||
{
|
||||
$res->setError(true)
|
||||
->setErrorMessage('Missing API key');
|
||||
return $res;
|
||||
}
|
||||
|
||||
// check missing parameters
|
||||
$missing = $this->checkMissingParameters($req, $params);
|
||||
if (count($missing) > 0)
|
||||
{
|
||||
$miss_string = implode(', ', $missing);
|
||||
$res->setError(true)
|
||||
->setErrorMessage('Missing parameter(s): ' . $miss_string);
|
||||
return $res;
|
||||
}
|
||||
|
||||
// check api key
|
||||
$mobile_user = $this->checkAPIKey($em, $req->query->get('api_key'));
|
||||
if ($mobile_user == null)
|
||||
{
|
||||
$res->setError(true)
|
||||
->setErrorMessage('Invalid API Key');
|
||||
return $res;
|
||||
}
|
||||
|
||||
// store session
|
||||
$this->session = $sess;
|
||||
|
||||
return $res;
|
||||
}
|
||||
|
||||
// TODO: this might not be needed if we use APIController's checkRequiredParameters
|
||||
// or we put this into a service?
|
||||
protected function checkMissingParameters(Request $req, $params = [])
|
||||
{
|
||||
$missing = [];
|
||||
|
||||
// check if parameters are there
|
||||
foreach ($params as $param)
|
||||
{
|
||||
if ($req->getMethod() == 'GET')
|
||||
{
|
||||
$check = $req->query->get($param);
|
||||
if (empty($check))
|
||||
$missing[] = $param;
|
||||
}
|
||||
else if ($req->getMethod() == 'POST')
|
||||
{
|
||||
$check = $req->request->get($param);
|
||||
if (empty($check))
|
||||
$missing[] = $param;
|
||||
}
|
||||
else
|
||||
return $params;
|
||||
}
|
||||
|
||||
return $missing;
|
||||
}
|
||||
|
||||
// TODO: type hint entity manager
|
||||
// TODO: since we broke the functions into separate files, we need
|
||||
// to figure out how to make this accessible to all ResqAPI controllers
|
||||
protected function checkAPIKey($em, $api_key)
|
||||
{
|
||||
// find the api key (session id)
|
||||
// TODO: user validation needs to be changed
|
||||
$m_user = $em->getRepository(MobileUser::class)->find($api_key);
|
||||
if ($m_user == null)
|
||||
return null;
|
||||
|
||||
return $m_user;
|
||||
}
|
||||
}
|
||||
164
src/Controller/ResqAPI/ServiceController.php
Normal file
164
src/Controller/ResqAPI/ServiceController.php
Normal file
|
|
@ -0,0 +1,164 @@
|
|||
<?php
|
||||
|
||||
namespace App\Controller\ResqAPI;
|
||||
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
|
||||
use Doctrine\ORM\Query;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
|
||||
use Catalyst\APIBundle\Controller\APIController;
|
||||
// TODO: what do we use for response? APIResponse or APIResult?
|
||||
// APIResult is what is used by APIController. APIResponse is what is used by CAPI
|
||||
use Catalyst\APIBundle\Response\APIResponse;
|
||||
use App\Ramcar\APIResult;
|
||||
|
||||
use App\Entity\Service;
|
||||
|
||||
use Catalyst\APIBundle\Access\Generator as ACLGenerator;
|
||||
|
||||
class ServiceController extends APIController
|
||||
{
|
||||
protected $acl_gen;
|
||||
|
||||
public function __construct(ACLGenerator $acl_gen)
|
||||
{
|
||||
$this->acl_gen = $acl_gen;
|
||||
}
|
||||
|
||||
public function listServices(Request $req, EntityManagerInterface $em)
|
||||
{
|
||||
$required_params = [];
|
||||
$res = $this->checkParamsAndKey($req, $em, $required_params);
|
||||
if ($res->isError())
|
||||
return $res->getReturnResponse();
|
||||
|
||||
// services
|
||||
$results = $em->getRepository(Service::class)->findAll();
|
||||
if (empty($results))
|
||||
{
|
||||
$res->setError(true)
|
||||
->setErrorMessage('No services available.');
|
||||
return $res->getReturnResponse();
|
||||
}
|
||||
|
||||
$services = [];
|
||||
foreach ($results as $result)
|
||||
{
|
||||
/*
|
||||
// get partners
|
||||
$partners = [];
|
||||
$service_partners = $result->getPartners();
|
||||
foreach($service_partners as $sp)
|
||||
{
|
||||
$partners[] = [
|
||||
'id' => $sp->getID(),
|
||||
'name' => $sp->getName(),
|
||||
'branch' => $sp->getBranch(),
|
||||
'address' => $sp->getAddress(),
|
||||
'contact_nums' => $sp->getContactNumbers(),
|
||||
'time_open' => $sp->getTimeOpen()->format("g:i A"),
|
||||
'time_close' => $sp->getTimeClose()->format("g:i A"),
|
||||
];
|
||||
}
|
||||
*/
|
||||
|
||||
$services[] = [
|
||||
'id' => $result->getID(),
|
||||
'name' => $result->getName(),
|
||||
// 'partners' => $partners,
|
||||
];
|
||||
}
|
||||
|
||||
$data['services'] = $services;
|
||||
|
||||
$res->setData($data);
|
||||
|
||||
return $res->getReturnResponse();
|
||||
}
|
||||
|
||||
// TODO: since we broke the functions into separate files, we need
|
||||
// to figure out how to make this accessible to all ResqAPI controllers
|
||||
protected function checkParamsAndKey(Request $req, $em, $params)
|
||||
{
|
||||
// TODO: depends on what we decide to return
|
||||
// returns APIResult object
|
||||
$res = new APIResult();
|
||||
|
||||
// check for api_key in query string
|
||||
$api_key = $req->query->get('api_key');
|
||||
if (empty($api_key))
|
||||
{
|
||||
$res->setError(true)
|
||||
->setErrorMessage('Missing API key');
|
||||
return $res;
|
||||
}
|
||||
|
||||
// check missing parameters
|
||||
$missing = $this->checkMissingParameters($req, $params);
|
||||
if (count($missing) > 0)
|
||||
{
|
||||
$miss_string = implode(', ', $missing);
|
||||
$res->setError(true)
|
||||
->setErrorMessage('Missing parameter(s): ' . $miss_string);
|
||||
return $res;
|
||||
}
|
||||
|
||||
// check api key
|
||||
$mobile_user = $this->checkAPIKey($em, $req->query->get('api_key'));
|
||||
if ($mobile_user == null)
|
||||
{
|
||||
$res->setError(true)
|
||||
->setErrorMessage('Invalid API Key');
|
||||
return $res;
|
||||
}
|
||||
|
||||
// store session
|
||||
$this->session = $sess;
|
||||
|
||||
return $res;
|
||||
}
|
||||
|
||||
// TODO: this might not be needed if we use APIController's checkRequiredParameters
|
||||
// or we put this into a service?
|
||||
protected function checkMissingParameters(Request $req, $params = [])
|
||||
{
|
||||
$missing = [];
|
||||
|
||||
// check if parameters are there
|
||||
foreach ($params as $param)
|
||||
{
|
||||
if ($req->getMethod() == 'GET')
|
||||
{
|
||||
$check = $req->query->get($param);
|
||||
if (empty($check))
|
||||
$missing[] = $param;
|
||||
}
|
||||
else if ($req->getMethod() == 'POST')
|
||||
{
|
||||
$check = $req->request->get($param);
|
||||
if (empty($check))
|
||||
$missing[] = $param;
|
||||
}
|
||||
else
|
||||
return $params;
|
||||
}
|
||||
|
||||
return $missing;
|
||||
}
|
||||
|
||||
// TODO: type hint entity manager
|
||||
// TODO: since we broke the functions into separate files, we need
|
||||
// to figure out how to make this accessible to all ResqAPI controllers
|
||||
protected function checkAPIKey($em, $api_key)
|
||||
{
|
||||
// find the api key (session id)
|
||||
// TODO: user validation needs to be changed
|
||||
$m_user = $em->getRepository(MobileUser::class)->find($api_key);
|
||||
if ($m_user == null)
|
||||
return null;
|
||||
|
||||
return $m_user;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue