fix: 🐛 synchro CRM and project customers list #87

This commit is contained in:
Alexis Gousseau 2026-08-05 12:47:39 +02:00
parent cd2d35e305
commit 68cf2d94b0
No known key found for this signature in database
GPG key ID: 7F3C786EE12DBB72
2 changed files with 66 additions and 13 deletions

View file

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

View file

@ -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();