fix: 🐛 add more customer verification to check if is in the local database

This commit is contained in:
Nicolas Demangeat 2025-08-19 14:49:20 +02:00
parent 2c1c3eb2e6
commit 8810523832
4 changed files with 41 additions and 10 deletions

View file

@ -143,7 +143,13 @@ class CrmUserProvider extends EloquentUserProvider implements UserProviderContra
]);
$model->hashcode = $crmCustomer->hashcode;
$model->email_verified_at = $crmCustomer->status ? now() : null;
$model->saveOrFail();
// Check if model is in local databse.
$exists = $model::query()->where('email', $model->email)->exists();
if (!$exists) {
$model->saveOrFail();
} else {
throw new \Exception('Le modèle existe déjà dans la base locale.');
}
return $model;
}

View file

@ -18,6 +18,7 @@ use LDC\LDCCRM\Enums\ApiError;
use LDC\LDCCRM\Exceptions\ApiErrorException;
use LDC\LDCCRM\Exceptions\ValidationException as CrmValidationException;
use LDC\LDCCRM\Models\Customer as CrmCustomer;
use Illuminate\Database\QueryException;
class RegisterController extends Controller
{
@ -81,6 +82,7 @@ class RegisterController extends Controller
* @return \App\Models\Customer
* @throws IlluminateValidationException If there are validation errors.
* @throws \RuntimeException On unhandled api Error.
* @throws IlluminateValidationException If there are already an email.
*/
protected function create(array $data): Customer
{
@ -138,7 +140,16 @@ class RegisterController extends Controller
throw IlluminateValidationException::withMessages([
'error' => $e->getMessage()
]);
} //end try
} catch (QueryException $e) {
if ($e->getCode() === '23000') {
throw IlluminateValidationException::withMessages([
'error' => trans('Un compte existe déjà avec cette adresse.')
]);
}
report($e);
Log::error(__CLASS__ . '::create reached end of possibilities.');
throw new \RuntimeException(__CLASS__ . '::create reached end of possibilities.');
}//end try
}
/**

View file

@ -10,6 +10,7 @@ use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Auth;
use Illuminate\Validation\Rule;
use Illuminate\Validation\Rules\Password;
use Illuminate\Validation\Validator;
use Kwaadpepper\Enum\Rules\EnumIsValidRule;
use LDC\LDCCRM\Enums\Country;
use LDC\LDCCRM\Models\Customer as CrmCustomer;
@ -99,13 +100,6 @@ class CreateAccountRequest extends FormRequest
'required',
'email:rfc,dns,spoof',
'max:255',
// * Does email already exists in CRM ?
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundBeforeLastUsed
function (string $attribute, $value, \Closure $fail) {
if (CrmCustomer::findWithEmail(\strval($value))) {
$fail('Un compte existe déjà avec cette adresse.');
}
},
'unique:customers,email',
],
'password' => [
@ -119,6 +113,26 @@ class CreateAccountRequest extends FormRequest
];
}
/**
* Get the "after" validation callables for the request.
*
* @return array
*/
public function after(): array
{
return [
function (Validator $validator) {
if (CrmCustomer::findWithEmail((string) $this->input('email'))) {
$validator->errors()->add(
'email',
'Un compte existe déjà avec cette adresse dans la CRM.'
);
}
},
];
}
/**
* Get custom attributes for validator errors.
*

View file

@ -264,7 +264,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.')
]);
}