Resolve "APIController changes to handle duplicate customer mobile numbers" #1133

Merged
korina.cordero merged 2 commits from 277-apicontroller-changes-to-handle-duplicate-customer-mobile-numbers into master 2019-11-26 05:47:27 +00:00

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;
}
}