Add checking if mobile session belongs to an existing customer. #277

This commit is contained in:
Korina Cordero 2019-11-21 06:16:31 +00:00
parent 3babcd765f
commit 0adffd92d7

View file

@ -334,7 +334,6 @@ class APIController extends Controller
// TODO: check if we have the number registered before and merge // 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()); $dupe_sess = $this->findNumberSession($this->session->getPhoneNumber());
if ($dupe_sess != null) if ($dupe_sess != null)
{ {
@ -342,6 +341,15 @@ class APIController extends Controller
$this->session->setCustomer($dupe_cust); $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(); $em->flush();
// response // response
@ -2180,4 +2188,28 @@ class APIController extends Controller
return $res->getReturnResponse(); return $res->getReturnResponse();
} }
protected function findCustomerByNumber($number)
{
$customers = $this->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;
}
} }