fix(crm): use soft deletes and fix synchronization issues during registration
This commit is contained in:
parent
37278195e6
commit
12e6d2adf5
4 changed files with 30 additions and 10 deletions
|
|
@ -104,8 +104,11 @@ class CrmUserProvider extends EloquentUserProvider implements UserProviderContra
|
|||
// * Ne devrait pas survenir, $crm->login throws ApiError::authFailed()
|
||||
return $crm->login($credentials['email'], $credentials['password']);
|
||||
} catch (ApiErrorException $e) {
|
||||
if ($e->getApiError()->equals(ApiError::modelNotFound)) {
|
||||
$model->forceDelete();
|
||||
if (
|
||||
$e->getApiError()->equals(ApiError::modelNotFound) ||
|
||||
($e->getApiError()->equals(ApiError::authFailed) && !$model->existsInCrm())
|
||||
) {
|
||||
$model->delete();
|
||||
return false;
|
||||
}
|
||||
if ($e->getApiError()->equals(ApiError::authFailed)) {
|
||||
|
|
@ -113,6 +116,9 @@ class CrmUserProvider extends EloquentUserProvider implements UserProviderContra
|
|||
}
|
||||
// ! Assert Authenticable model must be valided (MustValidate trait).
|
||||
if ($e->getApiError()->equals(ApiError::customerNotActivated)) {
|
||||
$model->email_verified_at = null;
|
||||
$model->save();
|
||||
$model->sendEmailVerificationNotification();
|
||||
Session::flash('warning', \trans('front.messages.account_not_activated'));
|
||||
return true;
|
||||
}
|
||||
|
|
@ -138,11 +144,14 @@ class CrmUserProvider extends EloquentUserProvider implements UserProviderContra
|
|||
private function createLocalCustomer(\LDC\LDCCRM\Models\Customer $crmCustomer): Model
|
||||
{
|
||||
/** @var \App\Models\Customer $model */
|
||||
$model = $this->usedModel::firstOrNew([
|
||||
$model = $this->usedModel::withTrashed()->firstOrNew([
|
||||
'email' => $crmCustomer->email
|
||||
]);
|
||||
$model->hashcode = $crmCustomer->hashcode;
|
||||
$model->email_verified_at = $crmCustomer->status ? now() : null;
|
||||
if ($model->trashed()) {
|
||||
$model->restore();
|
||||
}
|
||||
$model->saveOrFail();
|
||||
|
||||
return $model;
|
||||
|
|
|
|||
|
|
@ -106,11 +106,15 @@ class RegisterController extends Controller
|
|||
|
||||
// * Enregistrement du Hashcode
|
||||
/** @var \App\Models\Customer $customer */
|
||||
$customer = new Customer(
|
||||
Arr::only($data, (new \ReflectionClass(\App\Models\Customer::class))
|
||||
->getProperty('fillable')->getDefaultValue())
|
||||
);
|
||||
$customer = Customer::withTrashed()->firstOrNew([
|
||||
'email' => $data['email']
|
||||
]);
|
||||
$customer->fill(Arr::only($data, $customer->getFillable()));
|
||||
$customer->hashcode = $crmCustomer->hashcode;
|
||||
if ($customer->trashed()) {
|
||||
$customer->restore();
|
||||
}
|
||||
$customer->email_verified_at = $crmCustomer->status ? now() : null;
|
||||
// ! Last connection sera null, pour identifier la deuxième connexion,
|
||||
// ! Pour accéder au partie du Klub.
|
||||
if ($customer->saveOrFail()) {
|
||||
|
|
|
|||
|
|
@ -108,9 +108,16 @@ class CreateAccountRequest extends FormRequest
|
|||
function (string $attribute, $value, \Closure $fail) {
|
||||
if (CrmCustomer::findWithEmail(\strval($value))) {
|
||||
$fail('Un compte existe déjà avec cette adresse.');
|
||||
} else {
|
||||
// ! Si le compte n'existe pas en CRM mais existe en local.
|
||||
// ! On le soft-delete pour permettre l'inscription et la restauration propre.
|
||||
$localCustomer = \App\Models\Customer::where('email', \strval($value))->first();
|
||||
if ($localCustomer) {
|
||||
$localCustomer->delete();
|
||||
}
|
||||
}
|
||||
},
|
||||
'unique:customers,email',
|
||||
Rule::unique('customers', 'email')->withoutTrashed(),
|
||||
],
|
||||
'password' => [
|
||||
'required',
|
||||
|
|
|
|||
|
|
@ -180,7 +180,7 @@ class Customer extends Authenticatable implements MustVerifyEmail
|
|||
private static function assertFieldIsUnique(mixed $id, string $field, mixed $value): void
|
||||
{
|
||||
Validator::make([$field => $value], [
|
||||
$field => Rule::unique('customers', $field)->ignore($id),
|
||||
$field => Rule::unique('customers', $field)->ignore($id)->withoutTrashed(),
|
||||
], [
|
||||
$field . '.unique' => trans('La valeur :value pour le champ :attribute est déjà utilisé', [
|
||||
'attribute' => $field,
|
||||
|
|
@ -266,7 +266,7 @@ class Customer extends Authenticatable implements MustVerifyEmail
|
|||
if ($isLogged) {
|
||||
\auth('frontend')->logout();
|
||||
}
|
||||
$this->forceDelete();
|
||||
$this->delete();
|
||||
throw ValidationException::withMessages([
|
||||
'error' => trans('Le compte \'existe pas.')
|
||||
]);
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue