feat(migrations): add ownership fields to doctors table for IRIMC import
- Introduced new columns: owner_status, source, source_ref, managed_by, and claimed_at to the doctors table. - Created indexes for owner_status and source to optimize queries related to unclaimed doctors. feat(auth): implement SystemOwnerCommand for managing system-owner user - Added command to create, activate, and deactivate a system-owner user for IRIMC crawler. - Ensured the user has ROLE_ADMIN to access import endpoints. - Handled password setting and user status management within the command.
This commit is contained in:
@@ -10,6 +10,7 @@ use App\Shared\Service\InputValidator;
|
||||
use App\Location\Entity\City;
|
||||
use App\Clinic\Entity\Clinic;
|
||||
use App\Doctor\Entity\Doctor;
|
||||
use App\Location\Entity\Province;
|
||||
use App\Specialty\Entity\Specialty;
|
||||
use App\Payment\Entity\Payment;
|
||||
use App\Rating\Entity\Comment;
|
||||
@@ -28,6 +29,7 @@ use Symfony\Component\HttpFoundation\JsonResponse;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\StreamedResponse;
|
||||
use Symfony\Component\Routing\Attribute\Route;
|
||||
use Symfony\Component\Security\Http\Attribute\CurrentUser;
|
||||
use Symfony\Component\Security\Http\Attribute\IsGranted;
|
||||
|
||||
#[OA\Tag(name: 'Admin')]
|
||||
@@ -442,6 +444,129 @@ class AdminApiController extends BaseController
|
||||
return $this->success(['uuid' => $doctor->getUuid()], 201);
|
||||
}
|
||||
|
||||
/**
|
||||
* ایمپورت یک پزشک از سازمان نظام پزشکی (بدون شماره موبایل).
|
||||
*
|
||||
* برخلاف createDoctor، این اندپوینت موبایل نمیخواهد: برای هر پزشک یک «کاربر
|
||||
* جانشین» غیرفعال با شناسهٔ مصنوعی ساخته میشود و پروفایل در وضعیت unclaimed
|
||||
* ذخیره میگردد تا بعداً به پزشک واقعی منتقل شود. idempotent بر پایهٔ
|
||||
* (source, medical_system_code): اجرای مجدد، رکورد موجود را بهروزرسانی میکند.
|
||||
*/
|
||||
#[OA\Post(
|
||||
path: '/api/v1/admin/doctors/import',
|
||||
summary: 'Import an IRIMC doctor without a mobile number (unclaimed profile)',
|
||||
security: [['bearerAuth' => []]],
|
||||
requestBody: new OA\RequestBody(
|
||||
required: true,
|
||||
content: new OA\JsonContent(
|
||||
required: ['name', 'medical_system_code'],
|
||||
properties: [
|
||||
new OA\Property(property: 'name', type: 'string'),
|
||||
new OA\Property(property: 'medical_system_code', type: 'string'),
|
||||
new OA\Property(property: 'source', type: 'string', default: 'irimc'),
|
||||
new OA\Property(property: 'source_ref', type: 'string', nullable: true, description: 'profile_url یا شناسهٔ مبدأ'),
|
||||
new OA\Property(property: 'gender', type: 'string', nullable: true),
|
||||
new OA\Property(property: 'degree', type: 'string', nullable: true),
|
||||
new OA\Property(property: 'info', type: 'string', nullable: true),
|
||||
new OA\Property(property: 'specialties', type: 'array', items: new OA\Items(type: 'integer'), nullable: true),
|
||||
new OA\Property(property: 'states', type: 'array', items: new OA\Items(type: 'integer'), nullable: true),
|
||||
new OA\Property(property: 'cities', type: 'array', items: new OA\Items(type: 'integer'), nullable: true),
|
||||
]
|
||||
)
|
||||
),
|
||||
responses: [
|
||||
new OA\Response(response: 201, description: 'Doctor imported (created)'),
|
||||
new OA\Response(response: 200, description: 'Doctor already existed (updated or skipped)'),
|
||||
new OA\Response(response: 422, description: 'Validation error'),
|
||||
]
|
||||
)]
|
||||
#[Route('/api/v1/admin/doctors/import', methods: ['POST'])]
|
||||
public function importDoctor(Request $request, #[CurrentUser] User $admin): JsonResponse
|
||||
{
|
||||
$data = json_decode($request->getContent(), true) ?? [];
|
||||
$name = trim((string) ($data['name'] ?? ''));
|
||||
$code = trim((string) ($data['medical_system_code'] ?? $data['medicalSystemCode'] ?? ''));
|
||||
$source = trim((string) ($data['source'] ?? 'irimc')) ?: 'irimc';
|
||||
|
||||
if ($name === '') {
|
||||
return $this->error(ErrorCodes::VALIDATION, 'نام الزامی است', 422, 'name');
|
||||
}
|
||||
if ($code === '') {
|
||||
return $this->error(ErrorCodes::VALIDATION, 'کد نظام پزشکی الزامی است', 422, 'medical_system_code');
|
||||
}
|
||||
|
||||
$doctorRepo = $this->em->getRepository(Doctor::class);
|
||||
$userRepo = $this->em->getRepository(User::class);
|
||||
|
||||
// idempotency: همان پزشکِ منبع → بهروزرسانی، نه ساخت تکراری
|
||||
$doctor = $doctorRepo->findOneBy(['source' => $source, 'medicalSystemCode' => $code]);
|
||||
$created = false;
|
||||
|
||||
// پروفایل تصاحبشده را با ایمپورت مجدد بازنویسی نکن (مالک واقعی اولویت دارد)
|
||||
if ($doctor !== null && $doctor->getOwnerStatus() === 'claimed') {
|
||||
return $this->success(['uuid' => $doctor->getUuid(), 'created' => false, 'skipped' => 'claimed']);
|
||||
}
|
||||
|
||||
if ($doctor === null) {
|
||||
// کاربر جانشینِ یکتا و غیرفعال؛ شناسهٔ مصنوعی قطعی از روی کد نظام پزشکی
|
||||
$synthetic = 'imp_' . substr(md5($source . ':' . $code), 0, 14); // ≤ ۱۸ کاراکتر، ASCII، یکتا
|
||||
$user = $userRepo->findOneBy(['mobileNumber' => $synthetic]);
|
||||
if ($user === null) {
|
||||
$user = new User($synthetic);
|
||||
$user->setRealName($name);
|
||||
$user->setStatus(0); // جانشین: هرگز لاگین نمیکند
|
||||
$this->em->persist($user);
|
||||
}
|
||||
$doctor = new Doctor($user, $name);
|
||||
$doctor->setSource($source);
|
||||
$doctor->setOwnerStatus('unclaimed');
|
||||
$doctor->setActiveDoctorAppointment(false); // تا مالک واقعی برنامهٔ کاری بسازد
|
||||
$created = true;
|
||||
}
|
||||
|
||||
// فیلدهای مشترک
|
||||
$doctor->setName($name);
|
||||
$doctor->setMedicalSystemCode($code);
|
||||
$doctor->setManagedBy($admin->getId());
|
||||
if (array_key_exists('source_ref', $data) || array_key_exists('profile_url', $data)) {
|
||||
$doctor->setSourceRef($data['source_ref'] ?? $data['profile_url'] ?? null);
|
||||
}
|
||||
if (!empty($data['gender'])) $doctor->setGender($data['gender']);
|
||||
if (!empty($data['degree'])) $doctor->setDegree($data['degree']);
|
||||
if (array_key_exists('info', $data)) $doctor->setInfo($data['info']);
|
||||
|
||||
// روابط بر پایهٔ شناسههای مرجع (تخصص/استان/شهر)
|
||||
$this->syncRefCollection($doctor->getSpecialties(), $data['specialties'] ?? null, Specialty::class);
|
||||
$this->syncRefCollection($doctor->getProvinces(), $data['states'] ?? null, Province::class);
|
||||
$this->syncRefCollection($doctor->getCities(), $data['cities'] ?? null, City::class);
|
||||
|
||||
$this->em->persist($doctor);
|
||||
$this->em->flush();
|
||||
|
||||
return $this->success(
|
||||
['uuid' => $doctor->getUuid(), 'created' => $created],
|
||||
$created ? 201 : 200
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* یک مجموعهٔ ManyToMany پزشک را با آرایهای از شناسههای مرجع همگام میکند.
|
||||
* اگر $ids null باشد دست نمیخورد؛ اگر آرایه باشد، پاک و از نو پر میشود.
|
||||
*/
|
||||
private function syncRefCollection(\Doctrine\Common\Collections\Collection $col, ?array $ids, string $class): void
|
||||
{
|
||||
if ($ids === null) {
|
||||
return;
|
||||
}
|
||||
$col->clear();
|
||||
foreach ($ids as $id) {
|
||||
$ref = $this->em->getRepository($class)->find((int) $id);
|
||||
if ($ref !== null && !$col->contains($ref)) {
|
||||
$col->add($ref);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ── Clinics ───────────────────────────────────────────────────────────────
|
||||
|
||||
#[Route('/api/v1/admin/clinics', methods: ['GET'])]
|
||||
|
||||
Reference in New Issue
Block a user