refactor(errors): centralise legacy string error codes in ErrorCodes (M21)
~27 ad-hoc error codes (SLOT_TAKEN, USER_NOT_FOUND, VALIDATION, …) were raw strings, so ErrorCodes::message() returned the "unknown" fallback for them. Register all 14 distinct codes as constants with their messages and replace the raw usages across AdminApiController, MyAppointmentsController, CategoryController, PreRegistrationController and ClinicInvitationController. Wire values are kept identical (verified no consumer — admin SPA, nobat724_front, tauri — switches on these strings), so this is backward compatible. Regression: tests/Shared/ErrorCodesTest (wire values preserved + message resolves). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
namespace App\Appointment\Controller;
|
||||
|
||||
use App\Appointment\Entity\Appointment;
|
||||
use App\Shared\Constant\ErrorCodes;
|
||||
use App\Appointment\Repository\AppointmentRepository;
|
||||
use App\Appointment\Repository\SlotTakenException;
|
||||
use App\Appointment\Service\SlotCalculatorService;
|
||||
@@ -42,7 +43,7 @@ class MyAppointmentsController extends BaseController
|
||||
$roles = $user->getRoles();
|
||||
$allowed = ['ROLE_DOCTOR', 'ROLE_CLINIC', 'ROLE_SECRETARY', 'ROLE_ADMIN'];
|
||||
if (!array_intersect($allowed, $roles)) {
|
||||
return $this->error('FORBIDDEN', 'دسترسی ندارید', 403);
|
||||
return $this->error(ErrorCodes::FORBIDDEN, 'دسترسی ندارید', 403);
|
||||
}
|
||||
|
||||
$data = json_decode($request->getContent(), true) ?? [];
|
||||
@@ -53,18 +54,18 @@ class MyAppointmentsController extends BaseController
|
||||
$patientName = trim($data['patient_name'] ?? '');
|
||||
|
||||
if (empty($doctorUuid) || $slotStart <= 0 || $slotEnd <= $slotStart || empty($mobile) || empty($patientName)) {
|
||||
return $this->error('VALIDATION', 'همه فیلدها الزامی است', 422);
|
||||
return $this->error(ErrorCodes::VALIDATION, 'همه فیلدها الزامی است', 422);
|
||||
}
|
||||
|
||||
if ($slotStart < time()) {
|
||||
return $this->error('SLOT_PAST', 'زمان این اسلات گذشته است', 422);
|
||||
return $this->error(ErrorCodes::SLOT_PAST, 'زمان این اسلات گذشته است', 422);
|
||||
}
|
||||
|
||||
$doctor = $this->doctorRepo->findByUuid($doctorUuid);
|
||||
if (!$doctor) return $this->error('DOCTOR_NOT_FOUND', 'پزشک یافت نشد', 404);
|
||||
if (!$doctor) return $this->error(ErrorCodes::DOCTOR_NOT_FOUND, 'پزشک یافت نشد', 404);
|
||||
|
||||
if (!$this->canBookForDoctor($user, $doctor)) {
|
||||
return $this->error('FORBIDDEN', 'برای این پزشک مجاز به ثبت نوبت نیستید', 403);
|
||||
return $this->error(ErrorCodes::FORBIDDEN, 'برای این پزشک مجاز به ثبت نوبت نیستید', 403);
|
||||
}
|
||||
|
||||
$patient = $this->em->getRepository(User::class)->findOneBy(['mobileNumber' => $mobile]);
|
||||
@@ -83,7 +84,7 @@ class MyAppointmentsController extends BaseController
|
||||
try {
|
||||
$this->appointmentRepo->bookAtomically($appointment);
|
||||
} catch (SlotTakenException) {
|
||||
return $this->error('SLOT_TAKEN', 'این نوبت قبلاً رزرو شده است', 409);
|
||||
return $this->error(ErrorCodes::SLOT_TAKEN, 'این نوبت قبلاً رزرو شده است', 409);
|
||||
}
|
||||
|
||||
return $this->success([
|
||||
|
||||
Reference in New Issue
Block a user