Add CustomerController to CAPI. #281

This commit is contained in:
Korina Cordero 2019-11-26 07:53:31 +00:00
parent 50cb1d8617
commit 9efc0e3a75
2 changed files with 119 additions and 0 deletions

View file

@ -50,3 +50,8 @@ access_keys:
acls: acls:
- id: privacypolicy.find - id: privacypolicy.find
label: Find Privacy Policy label: Find Privacy Policy
- id: customer
label: Customer
acls:
- id: customer.register
label: Register Customer

View file

@ -0,0 +1,114 @@
<?php
namespace App\Controller\CAPI;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Doctrine\ORM\Query;
use Doctrine\ORM\EntityManagerInterface;
use Catalyst\APIBundle\Controller\APIController;
use Catalyst\APIBundle\Response\APIResponse;
use App\Entity\Customer;
use App\Entity\CustomerVehicle;
use App\Entity\Vehicle;
use Catalyst\APIBundle\Access\Generator as ACLGenerator;
class CustomerController extends APIController
{
protected $acl_gen;
public function __construct(ACLGenerator $acl_gen)
{
$this->acl_gen = $acl_gen;
}
public function register(Request $req, EntityManagerInterface $em)
{
// required parameters
$params = [
'first_name',
'last_name',
'mobile_number',
'make_id',
'model_year',
'plate_number',
'color',
'condition',
'fuel_type',
];
$msg = $this->checkRequiredParameters($req, $params);
error_log('msg - ' . $msg);
if ($msg)
return new APIResponse(false, $msg);
$first_name = $req->request->get('first_name');
$last_name = $req->request->get('last_name');
$mobile_number = $req->request->get('mobile_number');
$make_id = $req->request->get('make_id');
$model_year = $req->request->get('model_year');
$plate_number = $this->cleanPlateNumber($req->request->get('plate_number'));
$color = $req->request->get('color');
$condition = $req->request->get('condition');
$fuel_type = $req->request->get('fuel_type');
// check if vehicle exists
$vehicle = $em->getRepository(Vehicle::class)->find($make_id);
if ($vehicle == null)
return new APIResponse(false, 'Invalid vehicle make.');
// check if customer already exists
$customers = $em->getRepository(Customer::class)->findBy(['phone_mobile' => $number]);
if (!empty($customers))
{
foreach($customers as $customer)
{
// get customer vehicles for customer
$c_vehicles = $customer->getVehicles();
$cv_found = false;
if (!empty($c_vehicles))
{
// check if plate number of customer vehicle matches plate number
foreach($c_vehicles as $c_vehicle)
{
$clean_cv_plate = $this->cleanPlateNumber($c_vehicle->getPlateNumber());
// check if it's already there
if ($clean_cv_plate == $plate_number)
{
// customer and customer vehicle already exists
$cv_found = true;
break;
}
}
}
// if there is a customer vehicle matched
if ($cv_found)
{
// vehicle found, do nothing
// TODO: do we need to report that vehicle was found
}
else
{
// customer already exists but not customer vehicle
// add customer vehicle
}
}
}
}
protected function cleanPlateNumber($plate)
{
// remove spaces and make upper case
return strtoupper(str_replace(' ', '', $plate));
}
}