Merge branch '277-apicontroller-changes-to-handle-duplicate-customer-mobile-numbers' into 'master'

Resolve "APIController changes to handle duplicate customer mobile numbers"

Closes #277

See merge request jankstudio/resq!322
This commit is contained in:
Kendrick Chan 2019-11-26 05:47:25 +00:00
commit 50cb1d8617

View file

@ -334,7 +334,6 @@ class APIController extends Controller
// TODO: check if we have the number registered before and merge
// TODO: check if mobile matches mobile of customer
$dupe_sess = $this->findNumberSession($this->session->getPhoneNumber());
if ($dupe_sess != null)
{
@ -342,6 +341,15 @@ class APIController extends Controller
$this->session->setCustomer($dupe_cust);
}
// TODO: check if mobile matches mobile of customer
$customer = $this->findCustomerByNumber($this->session->getPhoneNumber());
if ($customer != null)
{
// TODO: if there is a dupe_sess, do we need to check if
// dupe_cust is the same as the customer we found?
$this->session->setCustomer($customer);
}
$em->flush();
// response
@ -2180,4 +2188,29 @@ class APIController extends Controller
return $res->getReturnResponse();
}
protected function findCustomerByNumber($number)
{
$em = $this->getDoctrine()->getManager();
$customers = $em->getRepository(Customer::class)->findBy(['phone_mobile' => $number]);
// find the customer with the most number of cars
$car_count = 0;
$cust = null;
foreach($customers as $customer)
{
$vehicles = $customer->getVehicles();
if (count($vehicles) > $car_count)
{
$car_count = count($vehicles);
// "save" customer object
$cust = $customer;
}
}
return $cust;
}
}