added function for trigger 2
This commit is contained in:
@@ -46,6 +46,7 @@ class CustomerController extends SuperController
|
||||
{
|
||||
$data = $request->all();
|
||||
if (isset($data['mobile']) && !is_null($data['mobile'])){
|
||||
$data['mobile'] = Customer::normalizeMobile($data['mobile']);
|
||||
Customer::assertMobileUnique($data['mobile'], (int) $id);
|
||||
}
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ namespace App\Http\Controllers;
|
||||
use App\DB\Te\TableEngine;
|
||||
use App\Models\Cnt;
|
||||
use App\Models\Customer;
|
||||
use App\Models\User;
|
||||
use Illuminate\Http\Request;
|
||||
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
|
||||
|
||||
@@ -101,8 +102,13 @@ class SuperController extends Controller
|
||||
public function update(Request $request, $id)
|
||||
{
|
||||
$data= $request->all();
|
||||
if (isset($data['mobile']) && !is_null($data['mobile'])){
|
||||
Customer::assertMobileUnique($data['mobile'], (int) $id);
|
||||
if (isset($data['mobile']) && !is_null($data['mobile'])) {
|
||||
$data['mobile'] = Customer::normalizeMobile($data['mobile']);
|
||||
if ($this->name_model === 'customer') {
|
||||
Customer::assertMobileUnique($data['mobile'], (int) $id);
|
||||
} elseif ($this->name_model === 'user') {
|
||||
User::assertMobileUnique($data['mobile'], (int) $id);
|
||||
}
|
||||
}
|
||||
if (isset($data['fname']) && isset($data['lname'])){
|
||||
$data['name'] = $data['fname'] . ' ' . $data['lname'];
|
||||
|
||||
@@ -40,9 +40,13 @@ class ValidationMiddleware
|
||||
: $rule;
|
||||
}, $rules);
|
||||
}
|
||||
$validator = Validator::make($request->all(), $rules);
|
||||
$validator = Validator::make($request->all(), $rules, [
|
||||
'mobile.unique' => 'این شماره موبایل قبلاً ثبت شده است',
|
||||
]);
|
||||
if ($validator->fails()) {
|
||||
return response()->json($validator->errors(),422);
|
||||
return response()->json([
|
||||
'message' => $validator->errors()->first(),
|
||||
], 422);
|
||||
}
|
||||
}
|
||||
return $next($request);
|
||||
|
||||
+28
-5
@@ -208,14 +208,26 @@ class Customer extends Authenticatable implements JWTSubject
|
||||
|
||||
public static function isMobileTaken(?string $mobile, ?int $exceptId = null): bool
|
||||
{
|
||||
$mobile = self::normalizeMobile($mobile);
|
||||
if ($mobile === null) {
|
||||
return self::isMobileTakenOnTable(self::class, $mobile, $exceptId);
|
||||
}
|
||||
|
||||
public static function isMobileTakenOnTable(string $modelClass, ?string $mobile, ?int $exceptId = null): bool
|
||||
{
|
||||
$normalized = self::normalizeMobile($mobile);
|
||||
if ($normalized === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$query = static::query()
|
||||
->where('mobile', $mobile)
|
||||
->whereNull('deleted_at');
|
||||
if ($exceptId !== null) {
|
||||
$current = $modelClass::query()->whereNull('deleted_at')->find($exceptId);
|
||||
if ($current !== null && self::normalizeMobile($current->mobile ?? '') === $normalized) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
$query = $modelClass::query()
|
||||
->whereNull('deleted_at')
|
||||
->whereIn('mobile', self::mobileLookupVariants($normalized));
|
||||
|
||||
if ($exceptId !== null) {
|
||||
$query->where('id', '<>', $exceptId);
|
||||
@@ -224,6 +236,17 @@ class Customer extends Authenticatable implements JWTSubject
|
||||
return $query->exists();
|
||||
}
|
||||
|
||||
private static function mobileLookupVariants(string $normalized): array
|
||||
{
|
||||
$digits = ltrim($normalized, '0');
|
||||
|
||||
return array_values(array_unique(array_filter([
|
||||
$normalized,
|
||||
$digits,
|
||||
'0' . $digits,
|
||||
])));
|
||||
}
|
||||
|
||||
public static function assertMobileUnique(?string $mobile, ?int $exceptId = null): void
|
||||
{
|
||||
if (self::isMobileTaken($mobile, $exceptId)) {
|
||||
|
||||
@@ -9,6 +9,7 @@ use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
use Illuminate\Notifications\Notifiable;
|
||||
use Laravel\Sanctum\HasApiTokens;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
|
||||
use Tymon\JWTAuth\Contracts\JWTSubject;
|
||||
|
||||
|
||||
@@ -80,4 +81,16 @@ class User extends Authenticatable implements JWTSubject
|
||||
public function setPasswordAttribute($value){
|
||||
$this->attributes['password'] = Hash::make($value);
|
||||
}
|
||||
|
||||
public static function isMobileTaken(?string $mobile, ?int $exceptId = null): bool
|
||||
{
|
||||
return Customer::isMobileTakenOnTable(self::class, $mobile, $exceptId);
|
||||
}
|
||||
|
||||
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