server create account recheak
This commit is contained in:
@@ -8,6 +8,7 @@ use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Rooyesh\Wallet\models\Wallet;
|
||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
|
||||
use Tymon\JWTAuth\Contracts\JWTSubject;
|
||||
|
||||
|
||||
@@ -154,4 +155,49 @@ class Customer extends Authenticatable implements JWTSubject
|
||||
return $this->belongsTo(Customer::class, 'agent_id');
|
||||
}
|
||||
|
||||
protected static function booted(): void
|
||||
{
|
||||
static::saving(function (self $customer) {
|
||||
$mobile = self::normalizeMobile($customer->mobile);
|
||||
if ($mobile === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
$customer->mobile = $mobile;
|
||||
self::assertMobileUnique($mobile, $customer->exists ? (int) $customer->id : null);
|
||||
});
|
||||
}
|
||||
|
||||
public static function normalizeMobile(?string $mobile): ?string
|
||||
{
|
||||
$mobile = trim((string) $mobile);
|
||||
|
||||
return $mobile === '' ? null : $mobile;
|
||||
}
|
||||
|
||||
public static function isMobileTaken(?string $mobile, ?int $exceptId = null): bool
|
||||
{
|
||||
$mobile = self::normalizeMobile($mobile);
|
||||
if ($mobile === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$query = static::query()
|
||||
->where('mobile', $mobile)
|
||||
->whereNull('deleted_at');
|
||||
|
||||
if ($exceptId !== null) {
|
||||
$query->where('id', '<>', $exceptId);
|
||||
}
|
||||
|
||||
return $query->exists();
|
||||
}
|
||||
|
||||
public static function assertMobileUnique(?string $mobile, ?int $exceptId = null): void
|
||||
{
|
||||
if (self::isMobileTaken($mobile, $exceptId)) {
|
||||
throw new UnprocessableEntityHttpException('این شماره موبایل قبلاً ثبت شده است');
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user