feat: create patient record without prior signup

Extend POST /api/v1/patient to resolve user by uuid or mobile, and
create a new ROLE_USER (name + optional national code, no password)
when no user exists. Admin modal shows a new-patient form on lookup miss.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
hamed
2026-06-23 15:20:56 +03:30
co-authored by Claude Opus 4.8
parent 59fb590d65
commit 51d7e24e04
4 changed files with 231 additions and 23 deletions
+27 -12
View File
@@ -92,21 +92,36 @@ class PatientController extends BaseController
$data = json_decode($request->getContent(), true) ?? [];
$userUuid = trim($data['user_uuid'] ?? '');
if ($userUuid === '') {
return $this->error(ErrorCodes::ERR_VALIDATION_001, 'user_uuid الزامی است', 422);
}
$patient = $this->userRepo->findByUuid($userUuid);
if ($patient === null) {
return $this->error(ErrorCodes::ERR_NOT_FOUND_001, 'کاربر یافت نشد', 404);
}
$mobile = trim($data['mobile'] ?? '');
$name = trim($data['name'] ?? '');
$nationalCode = trim((string) ($data['national_code'] ?? ''));
if ($nationalCode !== '' && $patient->getNationalCode() === null) {
if (!preg_match('/^\d{10}$/', $nationalCode)) {
return $this->error(ErrorCodes::ERR_VALIDATION_001, 'کد ملی باید ۱۰ رقم باشد', 422);
if ($nationalCode !== '' && !preg_match('/^\d{10}$/', $nationalCode)) {
return $this->error(ErrorCodes::ERR_VALIDATION_001, 'کد ملی باید ۱۰ رقم باشد', 422);
}
$patient = null;
if ($userUuid !== '') {
$patient = $this->userRepo->findByUuid($userUuid);
} elseif ($mobile !== '') {
if (!preg_match('/^09\d{9}$/', $mobile)) {
return $this->error(ErrorCodes::ERR_VALIDATION_001, 'شماره موبایل نامعتبر است', 422);
}
$patient = $this->userRepo->findByMobile($mobile);
}
// بیمار جدید بدون ثبت‌نام قبلی: موبایل + نام آمده ولی کاربری وجود ندارد
if ($patient === null) {
if ($mobile === '' || $name === '') {
return $this->error(ErrorCodes::ERR_VALIDATION_001, 'برای ساخت بیمار جدید، شماره موبایل و نام الزامی است', 422);
}
$patient = new User($mobile);
$patient->setRealName($name);
if ($nationalCode !== '') {
$patient->setNationalCode($nationalCode);
}
$this->userRepo->save($patient);
} elseif ($nationalCode !== '' && $patient->getNationalCode() === null) {
$patient->setNationalCode($nationalCode);
$this->userRepo->save($patient);
}