Add route to register customer. Finish register function. #281
This commit is contained in:
parent
9efc0e3a75
commit
16cd59dbfd
3 changed files with 115 additions and 4 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',
|
||||||
|
'make_id' => '22241',
|
||||||
|
'model_year' => '2018',
|
||||||
|
'plate_number' => 'KPP1234',
|
||||||
|
'color' => 'White',
|
||||||
|
'condition' => 'new',
|
||||||
|
'fuel_type' => 'gas',
|
||||||
|
];
|
||||||
|
$api->post('/capi/customer', $params);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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/customer
|
||||||
|
controller: App\Controller\CAPI\CustomerController::register
|
||||||
|
methods: [POST]
|
||||||
|
|
|
||||||
|
|
@ -28,6 +28,8 @@ class CustomerController extends APIController
|
||||||
|
|
||||||
public function register(Request $req, EntityManagerInterface $em)
|
public function register(Request $req, EntityManagerInterface $em)
|
||||||
{
|
{
|
||||||
|
$this->denyAccessUnlessGranted('customer.register', null, 'No access.');
|
||||||
|
|
||||||
// required parameters
|
// required parameters
|
||||||
$params = [
|
$params = [
|
||||||
'first_name',
|
'first_name',
|
||||||
|
|
@ -62,8 +64,33 @@ class CustomerController extends APIController
|
||||||
if ($vehicle == null)
|
if ($vehicle == null)
|
||||||
return new APIResponse(false, 'Invalid vehicle make.');
|
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
|
// check if customer already exists
|
||||||
$customers = $em->getRepository(Customer::class)->findBy(['phone_mobile' => $number]);
|
$customers = $em->getRepository(Customer::class)->findBy(['phone_mobile' => $mobile_number]);
|
||||||
if (!empty($customers))
|
if (!empty($customers))
|
||||||
{
|
{
|
||||||
foreach($customers as $customer)
|
foreach($customers as $customer)
|
||||||
|
|
@ -93,16 +120,79 @@ class CustomerController extends APIController
|
||||||
if ($cv_found)
|
if ($cv_found)
|
||||||
{
|
{
|
||||||
// vehicle found, do nothing
|
// vehicle found, do nothing
|
||||||
// TODO: do we need to report that vehicle was found
|
$message = 'Customer found.';
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// customer already exists but not customer vehicle
|
// customer already exists but not customer vehicle
|
||||||
// add 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)
|
protected function cleanPlateNumber($plate)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue