From 68cf2d94b05a7b171498b68aac9fcdd93a3a69d6 Mon Sep 17 00:00:00 2001 From: Alexis Gousseau Date: Wed, 5 Aug 2026 12:47:39 +0200 Subject: [PATCH] fix: :bug: synchro CRM and project customers list #87 --- .../Controllers/Fo/Auth/LoginController.php | 50 +++++++++++++++++++ app/Models/Customer.php | 29 ++++++----- 2 files changed, 66 insertions(+), 13 deletions(-) diff --git a/app/Http/Controllers/Fo/Auth/LoginController.php b/app/Http/Controllers/Fo/Auth/LoginController.php index d843938..f49a190 100644 --- a/app/Http/Controllers/Fo/Auth/LoginController.php +++ b/app/Http/Controllers/Fo/Auth/LoginController.php @@ -4,17 +4,22 @@ namespace App\Http\Controllers\Fo\Auth; use App\Http\Controllers\Controller; use App\Http\Controllers\Fo\Klub\MesBrController; +use App\Models\Customer; use App\Models\Game; use App\Providers\RouteServiceProvider; +use Exception; use Illuminate\Contracts\Auth\StatefulGuard; use Illuminate\Contracts\View\View; use Illuminate\Database\Eloquent\Model; use Illuminate\Foundation\Auth\AuthenticatesUsers; use Illuminate\Foundation\Auth\ThrottlesLogins; use Illuminate\Http\Request; +use Illuminate\Support\Arr; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Cache; +use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Session; +use LDC\LDCCRM\Models\Customer as CrmCustomer; class LoginController extends Controller { @@ -99,4 +104,49 @@ class LoginController extends Controller { return response()->redirectToIntended()->getTargetUrl(); } + + /** + * Attempt to log the user into the application. + * + * @param \Illuminate\Http\Request $request + * @return boolean + */ + protected function attemptLogin(Request $request): bool + { + $credentials = $this->credentials($request); + $customerModel = Customer::query()->where('email', Arr::get($credentials, 'email'))->first(); + $crmCustomer = CrmCustomer::findWithEmail(Arr::get($credentials, 'email')); + + // Check database customer. + if (!$customerModel) { + // Check crm customer. + if (!$crmCustomer) { + return false; + } + + /** @var \App\Models\Customer $customerModel */ + $customerModel = new Customer(Arr::only($crmCustomer->jsonSerialize(), ['email'])); + $customerModel->hashcode = $crmCustomer->hashcode; + + if (!$customerModel->saveOrFail()) { + return false; + } + } else { + // Check crm customer. + if (!$crmCustomer) { + $customerModel->delete(); + return false; + } + } //end if + + try { + CrmCustomer::login(Arr::get($credentials, 'email'), Arr::get($credentials, 'password')); + } catch (Exception $ex) { + Log::error($ex); + return false; + } + + $this->guard()->loginUsingId($customerModel->getKey()); + return true; + } } diff --git a/app/Models/Customer.php b/app/Models/Customer.php index bf990bc..61e34dd 100644 --- a/app/Models/Customer.php +++ b/app/Models/Customer.php @@ -80,7 +80,9 @@ class Customer extends Authenticatable implements MustVerifyEmail } $model->notify(new DestroyedCustomer()); } else { - $model->notify(new DestroyCustomer()); + if ($model->existsInCrm()) { + $model->notify(new DestroyCustomer()); + } } }); static::restored(function () { @@ -222,7 +224,7 @@ class Customer extends Authenticatable implements MustVerifyEmail } $this->forceDelete(); throw ValidationException::withMessages([ - 'error' => trans('Le compte \'existe pas.') + 'error' => trans('Le compte n\'existe pas.') ]); } @@ -239,7 +241,7 @@ class Customer extends Authenticatable implements MustVerifyEmail get: fn (): CrmCustomer => self::cacheCrm($this->hashcode, "apiData", function () { /** @var \LDC\LDCCRM\CrmApiV4 */ $crm = app('crmAPI'); - $crmCustomer = $crm->getCustomer($this->hashcode); + $crmCustomer = $crm->getCustomer(\strval($this->hashcode)); if (!$crmCustomer) { throw new CrmException('CRM get Customer returned null'); } @@ -263,6 +265,7 @@ class Customer extends Authenticatable implements MustVerifyEmail // phpcs:enable /** @var \LDC\LDCCRM\Models\Customer */ $crmCustomer = $this->api_data; + if (!$crmCustomer) { throw new CrmException('CRM get Customer returned null'); } @@ -337,17 +340,17 @@ class Customer extends Authenticatable implements MustVerifyEmail parent::toArray(), (array)$this->apiData ))->intersectByKeys([ - 'email' => '', - 'firstname' => '', - 'lastname' => '', - 'address' => '', - 'address2' => '', - 'postcode' => '', + 'email' => '', + 'firstname' => '', + 'lastname' => '', + 'address' => '', + 'address2' => '', + 'postcode' => '', 'birth_date' => '', - 'city' => '', - 'country' => '', - 'opt_in' => '', - 'phone' => '' + 'city' => '', + 'country' => '', + 'opt_in' => '', + 'phone' => '' ])->toArray(); } else { return parent::toArray();