From 0adffd92d7bfd0d0e5a48d5ba9fa0c67456e1f20 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Thu, 21 Nov 2019 06:16:31 +0000 Subject: [PATCH] Add checking if mobile session belongs to an existing customer. #277 --- src/Controller/APIController.php | 34 +++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/src/Controller/APIController.php b/src/Controller/APIController.php index 0b360d9c..8ee33c0a 100644 --- a/src/Controller/APIController.php +++ b/src/Controller/APIController.php @@ -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,28 @@ class APIController extends Controller 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; + } + }