Merge branch '281-create-customers-via-third-party-api' into 'master'
Resolve "Create customers via third party API" Closes #281 See merge request jankstudio/resq!327
This commit is contained in:
commit
ec5b8951da
4 changed files with 231 additions and 1 deletions
|
|
@ -41,7 +41,6 @@ class TestAPICommand extends Command
|
||||||
|
|
||||||
// TODO: shift this out of the bundle, since it's project specific
|
// TODO: shift this out of the bundle, since it's project specific
|
||||||
|
|
||||||
|
|
||||||
// warranty register
|
// warranty register
|
||||||
$serial = 'AJ34LJADR12134LKJL5';
|
$serial = 'AJ34LJADR12134LKJL5';
|
||||||
$plate_num = 'XEN918';
|
$plate_num = 'XEN918';
|
||||||
|
|
@ -119,5 +118,19 @@ class TestAPICommand extends Command
|
||||||
// privacy policy
|
// privacy policy
|
||||||
$privacy_policy_id = 2;
|
$privacy_policy_id = 2;
|
||||||
$api->get('/capi/privacy_policy/' . $privacy_policy_id );
|
$api->get('/capi/privacy_policy/' . $privacy_policy_id );
|
||||||
|
|
||||||
|
// register new customer
|
||||||
|
$params = [
|
||||||
|
'first_name' => 'Krispups',
|
||||||
|
'last_name' =>'Porzindog',
|
||||||
|
'mobile_number' => '9221111111',
|
||||||
|
'v_make_id' => '22241',
|
||||||
|
'v_model_year' => '2018',
|
||||||
|
'v_plate_number' => 'KPP1234',
|
||||||
|
'v_color' => 'White',
|
||||||
|
'v_condition' => 'new',
|
||||||
|
'v_fuel_type' => 'gas',
|
||||||
|
];
|
||||||
|
$api->post('/capi/quick_registration', $params);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
|
|
|
||||||
|
|
@ -135,3 +135,11 @@ capi_privacy_policy:
|
||||||
path: /capi/privacy_policy/{id}
|
path: /capi/privacy_policy/{id}
|
||||||
controller: App\Controller\CAPI\PrivacyPolicyController::getPrivacyPolicy
|
controller: App\Controller\CAPI\PrivacyPolicyController::getPrivacyPolicy
|
||||||
methods: [GET]
|
methods: [GET]
|
||||||
|
|
||||||
|
# customer
|
||||||
|
|
||||||
|
# register customer and customer vehicle
|
||||||
|
capi_customer_register:
|
||||||
|
path: /capi/quick_registration
|
||||||
|
controller: App\Controller\CAPI\CustomerController::register
|
||||||
|
methods: [POST]
|
||||||
|
|
|
||||||
204
src/Controller/CAPI/CustomerController.php
Normal file
204
src/Controller/CAPI/CustomerController.php
Normal file
|
|
@ -0,0 +1,204 @@
|
||||||
|
<?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)
|
||||||
|
{
|
||||||
|
$this->denyAccessUnlessGranted('customer.register', null, 'No access.');
|
||||||
|
|
||||||
|
// required parameters
|
||||||
|
$params = [
|
||||||
|
'first_name',
|
||||||
|
'last_name',
|
||||||
|
'mobile_number',
|
||||||
|
'v_make_id',
|
||||||
|
'v_model_year',
|
||||||
|
'v_plate_number',
|
||||||
|
'v_color',
|
||||||
|
'v_condition',
|
||||||
|
'v_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('v_make_id');
|
||||||
|
$model_year = $req->request->get('v_model_year');
|
||||||
|
$plate_number = $this->cleanPlateNumber($req->request->get('v_plate_number'));
|
||||||
|
$color = $req->request->get('v_color');
|
||||||
|
$condition = $req->request->get('v_condition');
|
||||||
|
$fuel_type = $req->request->get('v_fuel_type');
|
||||||
|
|
||||||
|
// check if vehicle exists
|
||||||
|
$vehicle = $em->getRepository(Vehicle::class)->find($make_id);
|
||||||
|
if ($vehicle == null)
|
||||||
|
return new APIResponse(false, 'Invalid vehicle make.');
|
||||||
|
|
||||||
|
// clean up mobile number
|
||||||
|
// does it fit our 09XXXXXXXXX pattern?
|
||||||
|
if (preg_match('/^09[0-9]{9}$/', $mobile_number))
|
||||||
|
{
|
||||||
|
// remove first '0'
|
||||||
|
$mobile_number = substr($mobile_number, 1);
|
||||||
|
error_log("CONVERTED TO $mobile_number");
|
||||||
|
}
|
||||||
|
|
||||||
|
// does it fit our 9XXXXXXXXX pattern?
|
||||||
|
if (!preg_match('/^9[0-9]{9}$/', $mobile_number))
|
||||||
|
return new APIResponse(false, 'Invalid mobile number.');
|
||||||
|
|
||||||
|
/*
|
||||||
|
// min length 2
|
||||||
|
// TODO: we need to check proper phone number format
|
||||||
|
// format should be '9XXXXXXXXX'
|
||||||
|
// TODO: if format doesn't fit and there's a 0 or 63 prefix, we should be able to detect and convert
|
||||||
|
if (strlen($mobile_number <= 2))
|
||||||
|
continue;
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
$data = [];
|
||||||
|
$message = '';
|
||||||
|
// check if customer already exists
|
||||||
|
$customers = $em->getRepository(Customer::class)->findBy(['phone_mobile' => $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
|
||||||
|
$message = 'Customer found.';
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// customer already exists but not customer vehicle
|
||||||
|
// add customer vehicle
|
||||||
|
$new_cv = new CustomerVehicle();
|
||||||
|
|
||||||
|
$new_cv->setCustomer($customer)
|
||||||
|
->setPlateNumber($plate_number)
|
||||||
|
->setStatusCondition($condition)
|
||||||
|
->setModelYear($model_year)
|
||||||
|
->setColor($color)
|
||||||
|
->setFuelType($fuel_type)
|
||||||
|
->setHasMotoliteBattery(true)
|
||||||
|
->setVehicle($vehicle);
|
||||||
|
|
||||||
|
$em->persist($new_cv);
|
||||||
|
|
||||||
|
$message = 'Vehicle added.';
|
||||||
|
$data[] = [
|
||||||
|
'make_id' => $make_id,
|
||||||
|
'model_year' => $model_year,
|
||||||
|
'plate_number' => $plate_number,
|
||||||
|
'color' => $color,
|
||||||
|
'condition' => $condition,
|
||||||
|
'fuel_type' => $fuel_type,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// customer not found
|
||||||
|
$new_cust = new Customer();
|
||||||
|
$new_cust->setFirstName($first_name)
|
||||||
|
->setLastName($last_name)
|
||||||
|
->setPhoneMobile($mobile_number);
|
||||||
|
|
||||||
|
$em->persist($new_cust);
|
||||||
|
|
||||||
|
// add customer vehicle
|
||||||
|
$new_cv = new CustomerVehicle();
|
||||||
|
|
||||||
|
$new_cv->setCustomer($new_cust)
|
||||||
|
->setPlateNumber($plate_number)
|
||||||
|
->setStatusCondition($condition)
|
||||||
|
->setModelYear($model_year)
|
||||||
|
->setColor($color)
|
||||||
|
->setFuelType($fuel_type)
|
||||||
|
->setHasMotoliteBattery(true)
|
||||||
|
->setVehicle($vehicle);
|
||||||
|
|
||||||
|
$em->persist($new_cv);
|
||||||
|
|
||||||
|
$message = 'Customer and vehicle added.';
|
||||||
|
$data[] = [
|
||||||
|
'first_name' => $first_name,
|
||||||
|
'last_name' => $last_name,
|
||||||
|
'mobile_number' => $mobile_number,
|
||||||
|
'make_id' => $make_id,
|
||||||
|
'model_year' => $model_year,
|
||||||
|
'plate_number' => $plate_number,
|
||||||
|
'color' => $color,
|
||||||
|
'condition' => $condition,
|
||||||
|
'fuel_type' => $fuel_type,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
$em->flush();
|
||||||
|
$em->clear();
|
||||||
|
|
||||||
|
return new APIResponse(true, $message, $data);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function cleanPlateNumber($plate)
|
||||||
|
{
|
||||||
|
// remove spaces and make upper case
|
||||||
|
return strtoupper(str_replace(' ', '', $plate));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue