feat: fix access scoping for clinic member doctors and add representation_id handling for clinics

This commit is contained in:
hamed
2026-06-19 13:42:24 +03:30
parent aeef59fe0d
commit d910a30a7e
7 changed files with 365 additions and 27 deletions
@@ -151,7 +151,7 @@ class RepresentationActionController extends BaseController
]
)]
#[Route('/api/v1/representation/clinic', methods: ['POST'])]
public function createClinic(Request $request): JsonResponse
public function createClinic(Request $request, #[CurrentUser] User $user): JsonResponse
{
$data = json_decode($request->getContent(), true) ?? [];
$mobile = trim((string) ($data['owner_mobile'] ?? ''));
@@ -182,6 +182,11 @@ class RepresentationActionController extends BaseController
if (!empty($data['address'])) $clinic->setAddress($data['address']);
if (!empty($data['info'])) $clinic->setInfo($data['info']);
$rep = $this->representationRepo->findByUser($user);
if ($rep !== null) {
$clinic->setRepresentationId($rep->getId());
}
$this->em->persist($clinic);
$this->em->flush();
@@ -192,6 +197,116 @@ class RepresentationActionController extends BaseController
]);
}
#[OA\Get(
path: '/api/v1/representation/doctors',
summary: 'پزشکانِ ثبت‌شده توسط نماینده‌ی جاری (paginated، شکلِ یکسان با /admin/doctors)',
security: [['bearerAuth' => []]],
parameters: [
new OA\Parameter(name: 'page', in: 'query', required: false, schema: new OA\Schema(type: 'integer', default: 1)),
new OA\Parameter(name: 'limit', in: 'query', required: false, schema: new OA\Schema(type: 'integer', default: 15)),
new OA\Parameter(name: 'search', in: 'query', required: false, schema: new OA\Schema(type: 'string')),
],
responses: [new OA\Response(response: 200, description: 'لیست پزشکان نماینده')]
)]
#[Route('/api/v1/representation/doctors', methods: ['GET'])]
public function doctors(Request $request, #[CurrentUser] User $user): JsonResponse
{
$rep = $this->representationRepo->findByUser($user);
if ($rep === null) {
return $this->error(ErrorCodes::ERR_NOT_FOUND_001, 'نماینده‌ای برای این کاربر یافت نشد', 404);
}
$page = max(1, (int) $request->query->get('page', 1));
$limit = min(100, max(1, (int) $request->query->get('limit', 15)));
$search = trim((string) $request->query->get('search', ''));
$qb = $this->em->createQueryBuilder()
->select('d.uuid, d.id, d.name, d.gender, d.degree, d.medicalSystemCode, d.mobileNumber, d.images, d.doctorRate, d.activeDoctorAppointment, d.createdAt')
->from(Doctor::class, 'd')
->where('d.representationId = :repId')
->setParameter('repId', $rep->getId())
->orderBy('d.createdAt', 'DESC');
if ($search !== '') {
$qb->andWhere('d.name LIKE :s OR d.mobileNumber LIKE :s')->setParameter('s', '%' . $search . '%');
}
$total = (clone $qb)->select('COUNT(d.id)')->getQuery()->getSingleScalarResult();
$rows = $qb->setFirstResult(($page - 1) * $limit)->setMaxResults($limit)
->getQuery()->getArrayResult();
$items = array_map(fn(array $d) => [
'uuid' => $d['uuid'],
'id' => (int) $d['id'],
'name' => $d['name'],
'gender' => $d['gender'],
'degree' => $d['degree'],
'medical_code' => $d['medicalSystemCode'],
'mobile' => $d['mobileNumber'],
'email' => null,
'is_active' => (bool) $d['activeDoctorAppointment'],
'rate' => (float) $d['doctorRate'],
'specialties' => [],
'profile_image' => !empty($d['images']) ? ($d['images'][0]['url'] ?? null) : null,
'created_at' => date('c', (int) $d['createdAt']),
], $rows);
return $this->paginated($items, (int) $total, $page, $limit);
}
#[OA\Get(
path: '/api/v1/representation/clinics',
summary: 'کلینیک‌های ثبت‌شده توسط نماینده‌ی جاری (paginated، شکلِ یکسان با /admin/clinics)',
security: [['bearerAuth' => []]],
parameters: [
new OA\Parameter(name: 'page', in: 'query', required: false, schema: new OA\Schema(type: 'integer', default: 1)),
new OA\Parameter(name: 'limit', in: 'query', required: false, schema: new OA\Schema(type: 'integer', default: 15)),
new OA\Parameter(name: 'search', in: 'query', required: false, schema: new OA\Schema(type: 'string')),
],
responses: [new OA\Response(response: 200, description: 'لیست کلینیک‌های نماینده')]
)]
#[Route('/api/v1/representation/clinics', methods: ['GET'])]
public function clinics(Request $request, #[CurrentUser] User $user): JsonResponse
{
$rep = $this->representationRepo->findByUser($user);
if ($rep === null) {
return $this->error(ErrorCodes::ERR_NOT_FOUND_001, 'نماینده‌ای برای این کاربر یافت نشد', 404);
}
$page = max(1, (int) $request->query->get('page', 1));
$limit = min(100, max(1, (int) $request->query->get('limit', 15)));
$search = trim((string) $request->query->get('search', ''));
$qb = $this->em->createQueryBuilder()
->select('c.uuid, c.id, c.name, c.telephone, c.clinicLogo, c.isActive, c.createdAt')
->from(Clinic::class, 'c')
->where('c.representationId = :repId')
->setParameter('repId', $rep->getId())
->orderBy('c.createdAt', 'DESC');
if ($search !== '') {
$qb->andWhere('c.name LIKE :s OR c.telephone LIKE :s')->setParameter('s', '%' . $search . '%');
}
$total = (clone $qb)->select('COUNT(c.id)')->getQuery()->getSingleScalarResult();
$rows = $qb->setFirstResult(($page - 1) * $limit)->setMaxResults($limit)
->getQuery()->getArrayResult();
$items = array_map(fn(array $c) => [
'uuid' => $c['uuid'],
'id' => (int) $c['id'],
'name' => $c['name'],
'telephone' => $c['telephone'],
'logo' => $c['clinicLogo'],
'clinic_logo' => $c['clinicLogo'],
'is_active' => (bool) $c['isActive'],
'doctors_count'=> 0,
'created_at' => date('c', (int) $c['createdAt']),
], $rows);
return $this->paginated($items, (int) $total, $page, $limit);
}
#[OA\Get(
path: '/api/v1/representation/appointments',
summary: 'نوبت‌های پزشکانِ زیرمجموعه‌ی نماینده‌ی جاری (paginated)',