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:
hamed
2026-06-28 21:12:23 +03:30
co-authored by Claude Opus 4.8
parent 96a86dbfed
commit 4b80616a17
8 changed files with 102 additions and 29 deletions
+20 -19
View File
@@ -3,6 +3,7 @@
namespace App\Admin\Controller;
use App\Appointment\Entity\Appointment;
use App\Shared\Constant\ErrorCodes;
use App\Appointment\Repository\SlotTakenException;
use App\Auth\Entity\User;
use App\Shared\Service\InputValidator;
@@ -103,7 +104,7 @@ class AdminApiController extends BaseController
public function toggleUserStatus(string $uuid): JsonResponse
{
$user = $this->em->getRepository(User::class)->findOneBy(['uuid' => $uuid]);
if (!$user) return $this->error('USER_NOT_FOUND', 'کاربر یافت نشد', 404);
if (!$user) return $this->error(ErrorCodes::USER_NOT_FOUND, 'کاربر یافت نشد', 404);
$user->setStatus($user->getStatus() === 1 ? 0 : 1);
$this->em->flush();
@@ -115,7 +116,7 @@ class AdminApiController extends BaseController
public function updateUserRole(string $uuid, Request $request): JsonResponse
{
$user = $this->em->getRepository(User::class)->findOneBy(['uuid' => $uuid]);
if (!$user) return $this->error('USER_NOT_FOUND', 'کاربر یافت نشد', 404);
if (!$user) return $this->error(ErrorCodes::USER_NOT_FOUND, 'کاربر یافت نشد', 404);
$data = json_decode($request->getContent(), true) ?? [];
$role = (string) ($data['role'] ?? '');
@@ -129,7 +130,7 @@ class AdminApiController extends BaseController
];
if (!isset($roleMap[$role])) {
return $this->error('INVALID_ROLE', 'نقش نامعتبر است', 422);
return $this->error(ErrorCodes::INVALID_ROLE, 'نقش نامعتبر است', 422);
}
$user->setRoles($roleMap[$role]);
@@ -142,7 +143,7 @@ class AdminApiController extends BaseController
public function userDetail(string $uuid): JsonResponse
{
$user = $this->em->getRepository(User::class)->findOneBy(['uuid' => $uuid]);
if (!$user) return $this->error('USER_NOT_FOUND', 'کاربر یافت نشد', 404);
if (!$user) return $this->error(ErrorCodes::USER_NOT_FOUND, 'کاربر یافت نشد', 404);
return $this->success([
'uuid' => $user->getUuid(),
@@ -162,7 +163,7 @@ class AdminApiController extends BaseController
public function updateUser(string $uuid, Request $request): JsonResponse
{
$user = $this->em->getRepository(User::class)->findOneBy(['uuid' => $uuid]);
if (!$user) return $this->error('USER_NOT_FOUND', 'کاربر یافت نشد', 404);
if (!$user) return $this->error(ErrorCodes::USER_NOT_FOUND, 'کاربر یافت نشد', 404);
$data = json_decode($request->getContent(), true) ?? [];
@@ -190,7 +191,7 @@ class AdminApiController extends BaseController
public function deleteUser(string $uuid): JsonResponse
{
$user = $this->em->getRepository(User::class)->findOneBy(['uuid' => $uuid]);
if (!$user) return $this->error('USER_NOT_FOUND', 'کاربر یافت نشد', 404);
if (!$user) return $this->error(ErrorCodes::USER_NOT_FOUND, 'کاربر یافت نشد', 404);
$this->em->remove($user);
$this->em->flush();
@@ -284,7 +285,7 @@ class AdminApiController extends BaseController
public function toggleDoctorStatus(string $uuid): JsonResponse
{
$doctor = $this->em->getRepository(Doctor::class)->findOneBy(['uuid' => $uuid]);
if (!$doctor) return $this->error('DOCTOR_NOT_FOUND', 'پزشک یافت نشد', 404);
if (!$doctor) return $this->error(ErrorCodes::DOCTOR_NOT_FOUND, 'پزشک یافت نشد', 404);
$doctor->setActiveDoctorAppointment(!$doctor->isActiveDoctorAppointment());
$this->em->flush();
@@ -390,10 +391,10 @@ class AdminApiController extends BaseController
$name = trim((string) ($data['name'] ?? ''));
if ($mobile === '' || $name === '') {
return $this->error('VALIDATION', 'موبایل و نام الزامی هستند', 422);
return $this->error(ErrorCodes::VALIDATION, 'موبایل و نام الزامی هستند', 422);
}
if (!InputValidator::isValidIranMobile($mobile)) {
return $this->error('VALIDATION', 'شماره موبایل نامعتبر است', 422, 'mobile');
return $this->error(ErrorCodes::VALIDATION, 'شماره موبایل نامعتبر است', 422, 'mobile');
}
$user = $this->em->getRepository(User::class)->findOneBy(['mobileNumber' => $mobile]);
@@ -406,7 +407,7 @@ class AdminApiController extends BaseController
$existing = $this->em->getRepository(Doctor::class)->findOneBy(['user' => $user]);
if ($existing) {
return $this->error('DOCTOR_EXISTS', 'این کاربر قبلاً پروفایل پزشک دارد', 409);
return $this->error(ErrorCodes::DOCTOR_EXISTS, 'این کاربر قبلاً پروفایل پزشک دارد', 409);
}
$doctor = new Doctor($user, $name);
@@ -503,13 +504,13 @@ class AdminApiController extends BaseController
$name = trim((string) ($data['name'] ?? ''));
if ($mobile === '') {
return $this->error('VALIDATION', 'شماره موبایل الزامی است', 422);
return $this->error(ErrorCodes::VALIDATION, 'شماره موبایل الزامی است', 422);
}
if (!InputValidator::isValidIranMobile($mobile)) {
return $this->error('VALIDATION', 'شماره موبایل نامعتبر است', 422, 'owner_mobile');
return $this->error(ErrorCodes::VALIDATION, 'شماره موبایل نامعتبر است', 422, 'owner_mobile');
}
if ($name === '') {
return $this->error('VALIDATION', 'نام کلینیک الزامی است', 422);
return $this->error(ErrorCodes::VALIDATION, 'نام کلینیک الزامی است', 422);
}
$user = $this->em->getRepository(User::class)->findOneBy(['mobileNumber' => $mobile]);
@@ -540,7 +541,7 @@ class AdminApiController extends BaseController
public function toggleClinicStatus(string $uuid): JsonResponse
{
$clinic = $this->em->getRepository(Clinic::class)->findOneBy(['uuid' => $uuid]);
if (!$clinic) return $this->error('CLINIC_NOT_FOUND', 'کلینیک یافت نشد', 404);
if (!$clinic) return $this->error(ErrorCodes::CLINIC_NOT_FOUND, 'کلینیک یافت نشد', 404);
$clinic->setIsActive(!$clinic->isActive());
$this->em->flush();
@@ -552,7 +553,7 @@ class AdminApiController extends BaseController
public function deleteClinic(string $uuid): JsonResponse
{
$clinic = $this->em->getRepository(Clinic::class)->findOneBy(['uuid' => $uuid]);
if (!$clinic) return $this->error('CLINIC_NOT_FOUND', 'کلینیک یافت نشد', 404);
if (!$clinic) return $this->error(ErrorCodes::CLINIC_NOT_FOUND, 'کلینیک یافت نشد', 404);
$this->insuranceCleanup->purgeForEntity(\App\Insurance\Entity\TenantInsurance::TYPE_CLINIC, $clinic->getId());
$this->em->remove($clinic);
@@ -794,11 +795,11 @@ class AdminApiController extends BaseController
$patientName = trim($data['patient_name'] ?? '');
if (empty($doctorUuid) || $slotStart <= 0 || $slotEnd <= $slotStart || empty($mobile) || empty($patientName)) {
return $this->error('VALIDATION', 'doctor_uuid، slot_start، slot_end، patient_mobile و patient_name الزامی است', 422);
return $this->error(ErrorCodes::VALIDATION, 'doctor_uuid، slot_start، slot_end، patient_mobile و patient_name الزامی است', 422);
}
$doctor = $this->em->getRepository(Doctor::class)->findOneBy(['uuid' => $doctorUuid]);
if (!$doctor) return $this->error('DOCTOR_NOT_FOUND', 'پزشک یافت نشد', 404);
if (!$doctor) return $this->error(ErrorCodes::DOCTOR_NOT_FOUND, 'پزشک یافت نشد', 404);
$patient = $this->em->getRepository(User::class)->findOneBy(['mobileNumber' => $mobile]);
if (!$patient) {
@@ -816,7 +817,7 @@ class AdminApiController extends BaseController
try {
$this->em->getRepository(Appointment::class)->bookAtomically($appointment);
} catch (SlotTakenException) {
return $this->error('SLOT_TAKEN', 'این نوبت قبلاً رزرو شده است', 409);
return $this->error(ErrorCodes::SLOT_TAKEN, 'این نوبت قبلاً رزرو شده است', 409);
}
return $this->success([
@@ -1515,7 +1516,7 @@ class AdminApiController extends BaseController
->getQuery()->getArrayResult();
if (empty($row)) {
return $this->error('NOT_FOUND', 'درخواست تسویه یافت نشد', 404);
return $this->error(ErrorCodes::NOT_FOUND, 'درخواست تسویه یافت نشد', 404);
}
$s = $row[0];