feat(migrations): add clinic_id context to weekly_schedules, date_overrides, and holidays
- Introduced clinic_id to weekly_schedules, date_overrides, and holidays to differentiate between personal and clinic schedules. - Updated unique constraints and indexes to accommodate the new clinic context. feat(command): create AssignScheduleClinicCommand to move schedules - Added a command to move a doctor's personal weekly schedule into a clinic context. - Implemented checks to ensure sessions align with the target clinic. feat(context): implement EntityContext and EntityContextResolver - Created EntityContext to represent the effective working environment of a request (doctor or clinic). - Developed EntityContextResolver to determine the execution context based on user roles and active contexts. test: add ServiceModeContextTest for appointment scheduling - Implemented tests to ensure service booking respects clinic and personal contexts. - Verified that financial data is omitted in clinic contexts in InvitedDoctorDashboardScopeTest.
This commit is contained in:
@@ -8,6 +8,7 @@ use App\Appointment\Repository\AppointmentRepository;
|
||||
use App\Appointment\Repository\SlotTakenException;
|
||||
use App\Appointment\Repository\WeeklyScheduleRepository;
|
||||
use App\Appointment\Service\SlotCalculatorService;
|
||||
use App\Clinic\Entity\Clinic;
|
||||
use App\Doctor\Entity\Doctor;
|
||||
use App\Auth\Entity\User;
|
||||
use App\Doctor\Repository\DoctorRepository;
|
||||
@@ -32,6 +33,8 @@ class AppointmentController extends BaseController
|
||||
private readonly SlotCalculatorService $slotCalculator,
|
||||
private readonly PatientService $patientService,
|
||||
private readonly WeeklyScheduleRepository $scheduleRepo,
|
||||
private readonly \App\Clinic\Repository\ClinicRepository $clinicRepo,
|
||||
private readonly \App\Doctor\Repository\DoctorAddressRepository $addressRepo,
|
||||
private readonly \App\Representation\Service\DomainContextResolver $domainResolver,
|
||||
private readonly \App\ClinicService\Repository\ServiceSectionRepository $sectionRepo,
|
||||
private readonly \App\ClinicService\Repository\ServiceItemRepository $itemRepo,
|
||||
@@ -153,10 +156,12 @@ class AppointmentController extends BaseController
|
||||
return $this->error(ErrorCodes::ERR_VALIDATION_001, 'فرمت تاریخ نادرست است (Y-m-d)', 422, 'date');
|
||||
}
|
||||
|
||||
$sessions = $this->slotCalculator->getAllSlotsWithAvailability($doctor, $date);
|
||||
$clinic = $this->bookingClinic($doctor, $request->query->get('clinic_uuid'));
|
||||
$sessions = $this->slotCalculator->getAllSlotsWithAvailability($doctor, $date, $clinic);
|
||||
|
||||
return $this->success([
|
||||
'doctor_uuid' => $doctorUuid,
|
||||
'clinic_uuid' => $clinic?->getUuid(),
|
||||
'date' => $date,
|
||||
'sessions' => $sessions,
|
||||
]);
|
||||
@@ -182,7 +187,8 @@ class AppointmentController extends BaseController
|
||||
return $this->error(ErrorCodes::ERR_VALIDATION_001, 'فرمت تاریخ نادرست است (Y-m-d)', 422, 'date');
|
||||
}
|
||||
|
||||
$schedule = $this->scheduleRepo->findByDoctor($doctor);
|
||||
$clinic = $this->bookingClinic($doctor, $request->query->get('clinic_uuid'));
|
||||
$schedule = $this->scheduleRepo->findByDoctorAndClinic($doctor, $clinic);
|
||||
$mode = ($schedule ? $schedule->getMeta() : WeeklySchedule::DEFAULT_META)['booking_mode'] ?? WeeklySchedule::MODE_SLOT;
|
||||
if ($mode !== WeeklySchedule::MODE_SERVICE) {
|
||||
return $this->error(ErrorCodes::ERR_VALIDATION_001, 'این پزشک در حالت نوبتدهی سرویسی نیست', 422);
|
||||
@@ -221,7 +227,8 @@ class AppointmentController extends BaseController
|
||||
'date' => $date,
|
||||
'total_duration_minutes' => $totalMinutes,
|
||||
'buffer_minutes' => (int) $meta['buffer_minutes'],
|
||||
'start_times' => $this->slotCalculator->getServiceStartTimes($doctor, $date, $totalMinutes),
|
||||
'clinic_uuid' => $clinic?->getUuid(),
|
||||
'start_times' => $this->slotCalculator->getServiceStartTimes($doctor, $date, $totalMinutes, $clinic),
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -232,32 +239,68 @@ class AppointmentController extends BaseController
|
||||
* GET /api/v1/appointment-booking-services/{doctorUuid}
|
||||
*/
|
||||
#[Route('/api/v1/appointment-booking-services/{doctorUuid}', methods: ['GET'])]
|
||||
public function bookingServices(string $doctorUuid): JsonResponse
|
||||
public function bookingServices(string $doctorUuid, Request $request): JsonResponse
|
||||
{
|
||||
$doctor = $this->doctorRepo->findByUuid($doctorUuid);
|
||||
if ($doctor === null) {
|
||||
return $this->error(ErrorCodes::ERR_VALIDATION_002, 'دکتر یافت نشد', 404);
|
||||
}
|
||||
|
||||
$schedule = $this->scheduleRepo->findByDoctor($doctor);
|
||||
$clinic = $this->bookingClinic($doctor, $request->query->get('clinic_uuid'));
|
||||
$schedule = $this->scheduleRepo->findByDoctorAndClinic($doctor, $clinic);
|
||||
$meta = $schedule ? $schedule->getMeta() : WeeklySchedule::DEFAULT_META;
|
||||
|
||||
$services = array_map(function (\App\ClinicService\Entity\ServiceItem $i) {
|
||||
$section = $i->getSection();
|
||||
return [
|
||||
'uuid' => $i->getUuid(),
|
||||
'name' => $i->getName(),
|
||||
'duration_minutes' => $i->getDurationMinutes(),
|
||||
'price_rials' => $i->getPriceRials(),
|
||||
'service_section' => ['uuid' => $section->getUuid(), 'name' => $section->getName()],
|
||||
];
|
||||
}, $this->itemRepo->findBookableByEntity('doctor', $doctor->getId()));
|
||||
|
||||
return $this->success([
|
||||
'doctor_uuid' => $doctorUuid,
|
||||
'clinic_uuid' => $clinic?->getUuid(),
|
||||
'booking_mode' => $meta['booking_mode'],
|
||||
'buffer_minutes' => (int) $meta['buffer_minutes'],
|
||||
'services' => $services,
|
||||
'services' => $this->bookableServices($doctor, $clinic),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* عمومی: همهٔ محلهای نوبتدهی یک پزشک — مطب شخصی و هر کلینیکی که در آن برنامهٔ
|
||||
* فعال دارد. سایت باید همه را نشان دهد؛ انتخاب یکی و پنهانکردن بقیه یعنی حذف
|
||||
* بخشی از ظرفیت واقعی پزشک.
|
||||
*
|
||||
* GET /api/v1/appointment-booking-locations/{doctorUuid}
|
||||
*/
|
||||
#[Route('/api/v1/appointment-booking-locations/{doctorUuid}', methods: ['GET'])]
|
||||
public function bookingLocations(string $doctorUuid): JsonResponse
|
||||
{
|
||||
$doctor = $this->doctorRepo->findByUuid($doctorUuid);
|
||||
if ($doctor === null) {
|
||||
return $this->error(ErrorCodes::ERR_VALIDATION_002, 'دکتر یافت نشد', 404);
|
||||
}
|
||||
|
||||
$locations = [];
|
||||
foreach ($this->scheduleRepo->findAllByDoctor($doctor) as $schedule) {
|
||||
$clinic = $schedule->getClinic();
|
||||
$meta = $schedule->getMeta();
|
||||
$address = $this->addressRepo->findForContext($doctor, $clinic?->getId())[0] ?? null;
|
||||
|
||||
$locations[] = [
|
||||
'location_uuid' => $address?->getUuid(),
|
||||
'type' => $clinic === null ? 'personal' : 'clinic',
|
||||
'title' => $clinic?->getName() ?? ($address?->getName() ?: 'مطب شخصی'),
|
||||
'address' => $address?->getAddress(),
|
||||
'clinic_uuid' => $clinic?->getUuid(),
|
||||
'booking_mode' => $meta['booking_mode'],
|
||||
'buffer_minutes' => (int) $meta['buffer_minutes'],
|
||||
'services' => $meta['booking_mode'] === WeeklySchedule::MODE_SERVICE
|
||||
? $this->bookableServices($doctor, $clinic)
|
||||
: [],
|
||||
'next_available_at' => $this->nextAvailableAt($doctor, $clinic),
|
||||
];
|
||||
}
|
||||
|
||||
// پیشفرضِ سایت = زودترین نوبت آزاد؛ محلهای بدون ظرفیت به انتها میروند.
|
||||
usort($locations, fn(array $a, array $b) => ($a['next_available_at'] ?? PHP_INT_MAX) <=> ($b['next_available_at'] ?? PHP_INT_MAX));
|
||||
|
||||
return $this->success([
|
||||
'doctor_uuid' => $doctorUuid,
|
||||
'booking_locations' => $locations,
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -275,24 +318,26 @@ class AppointmentController extends BaseController
|
||||
return $this->error(ErrorCodes::ERR_VALIDATION_001, 'سال یا ماه نامعتبر است', 422, 'month');
|
||||
}
|
||||
|
||||
$clinic = $this->bookingClinic($doctor, $request->query->get('clinic_uuid'));
|
||||
$daysInMonth = (int) date('t', (int) strtotime(sprintf('%04d-%02d-01', $year, $month)));
|
||||
|
||||
$disabled = [];
|
||||
$enabled = [];
|
||||
for ($day = 1; $day <= $daysInMonth; $day++) {
|
||||
$date = sprintf('%04d-%02d-%02d', $year, $month, $day);
|
||||
if ($this->slotCalculator->hasAnyAvailability($doctor, $date)) {
|
||||
if ($this->slotCalculator->hasAnyAvailability($doctor, $date, $clinic)) {
|
||||
$enabled[] = $date;
|
||||
} else {
|
||||
$disabled[] = $date;
|
||||
}
|
||||
}
|
||||
|
||||
$schedule = $this->scheduleRepo->findByDoctor($doctor);
|
||||
$schedule = $this->scheduleRepo->findByDoctorAndClinic($doctor, $clinic);
|
||||
$meta = $schedule ? $schedule->getMeta() : WeeklySchedule::DEFAULT_META;
|
||||
|
||||
return $this->success([
|
||||
'year' => $year,
|
||||
'clinic_uuid' => $clinic?->getUuid(),
|
||||
'month' => $month,
|
||||
'disabled_dates' => $disabled,
|
||||
'enabled_dates' => $enabled,
|
||||
@@ -348,6 +393,7 @@ class AppointmentController extends BaseController
|
||||
$doctorUuid = trim($data['doctor_uuid'] ?? '');
|
||||
$slotStart = (int) ($data['slot_start'] ?? 0);
|
||||
$slotEnd = (int) ($data['slot_end'] ?? 0);
|
||||
$clinicUuid = $data['clinic_uuid'] ?? null;
|
||||
|
||||
// حالت نوبتدهی سرویسی: مدت نوبت = مجموع مدت سرویسهای bookableِ انتخابشده،
|
||||
// و slot_end سمت سرور محاسبه میشود (به مقدار کلاینت اعتماد نمیشود).
|
||||
@@ -385,6 +431,14 @@ class AppointmentController extends BaseController
|
||||
return $this->error(ErrorCodes::ERR_VALIDATION_002, 'دکتر یافت نشد', 404);
|
||||
}
|
||||
|
||||
$bookingClinic = $this->bookingClinic($doctor, $clinicUuid);
|
||||
|
||||
// سرویس باید متعلق به همان محلی باشد که نوبت در آن ثبت میشود؛ وگرنه بیمار
|
||||
// میتوانست سرویس کلینیک را روی نوبت مطب شخصی بنشاند.
|
||||
if ($serviceItem !== null && ($err = $this->assertServicesMatchContext($serviceUuids, $doctor, $bookingClinic)) !== null) {
|
||||
return $err;
|
||||
}
|
||||
|
||||
$forSelf = (bool) ($data['for_self'] ?? true);
|
||||
|
||||
// کد ملی و جنسیت بیمار همیشه الزامی است (چه برای خود، چه برای دیگری).
|
||||
@@ -420,7 +474,7 @@ class AppointmentController extends BaseController
|
||||
}
|
||||
|
||||
// آدرس نوبت از روی session متناظر در برنامهی هفتگی تعیین میشود (location_id).
|
||||
$locationId = $this->resolveSlotLocationId($doctor, $slotStart);
|
||||
$locationId = $this->slotCalculator->resolveSlotLocationId($doctor, $slotStart, $bookingClinic);
|
||||
if ($locationId !== null) {
|
||||
$appointment->setAddressId($locationId);
|
||||
}
|
||||
@@ -610,11 +664,75 @@ class AppointmentController extends BaseController
|
||||
|| $user->hasRole('ROLE_ADMIN');
|
||||
}
|
||||
|
||||
private function resolveSlotLocationId(Doctor $doctor, int $slotStart): ?int
|
||||
/**
|
||||
* محلِ نوبتدهی این درخواست. بدون clinic_uuid یعنی مطب شخصی پزشک — نه «هر محلی
|
||||
* که پیدا شد»: با چند برنامهٔ همزمان، حدسزدن محل یعنی ثبت خاموشِ نوبت در جای
|
||||
* اشتباه.
|
||||
*/
|
||||
private function bookingClinic(Doctor $doctor, ?string $clinicUuid): ?Clinic
|
||||
{
|
||||
return $this->slotCalculator->resolveSlotLocationId($doctor, $slotStart);
|
||||
if ($clinicUuid === null || trim($clinicUuid) === '') {
|
||||
return null;
|
||||
}
|
||||
|
||||
$clinic = $this->clinicRepo->findByUuid(trim($clinicUuid));
|
||||
if ($clinic === null || !$clinic->hasDoctor($doctor)) {
|
||||
throw new \App\Shared\Exception\AppException(ErrorCodes::ERR_VALIDATION_002, 'محل نوبتدهی یافت نشد', 404);
|
||||
}
|
||||
|
||||
return $clinic;
|
||||
}
|
||||
|
||||
private function assertServicesMatchContext(array $serviceUuids, Doctor $doctor, ?Clinic $clinic): ?JsonResponse
|
||||
{
|
||||
[$type, $id] = $clinic !== null
|
||||
? ['clinic', $clinic->getId()]
|
||||
: ['doctor', $doctor->getId()];
|
||||
|
||||
foreach ($serviceUuids as $uuid) {
|
||||
$section = $this->itemRepo->findByUuid($uuid)?->getSection();
|
||||
if ($section === null || $section->getEntityType() !== $type || $section->getEntityId() !== $id) {
|
||||
return $this->error(ErrorCodes::ERR_VALIDATION_001, 'سرویس انتخابشده به این محل نوبتدهی تعلق ندارد', 422, 'service_item_uuids');
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/** @return array<int, array<string, mixed>> */
|
||||
private function bookableServices(Doctor $doctor, ?Clinic $clinic): array
|
||||
{
|
||||
[$type, $id] = $clinic !== null
|
||||
? ['clinic', $clinic->getId()]
|
||||
: ['doctor', $doctor->getId()];
|
||||
|
||||
return array_map(function (\App\ClinicService\Entity\ServiceItem $i): array {
|
||||
$section = $i->getSection();
|
||||
return [
|
||||
'uuid' => $i->getUuid(),
|
||||
'name' => $i->getName(),
|
||||
'duration_minutes' => $i->getDurationMinutes(),
|
||||
'price_rials' => $i->getPriceRials(),
|
||||
'service_section' => ['uuid' => $section->getUuid(), 'name' => $section->getName()],
|
||||
];
|
||||
}, $this->itemRepo->findBookableByEntity($type, $id));
|
||||
}
|
||||
|
||||
/** زودترین اسلات آزاد در ۳۰ روز آینده، یا null اگر ظرفیتی نباشد. */
|
||||
private function nextAvailableAt(Doctor $doctor, ?Clinic $clinic): ?int
|
||||
{
|
||||
for ($i = 0; $i < 30; $i++) {
|
||||
$date = date('Y-m-d', strtotime("today +{$i} day"));
|
||||
$slots = $this->slotCalculator->getAvailableSlots($doctor, $date, $clinic);
|
||||
if (!empty($slots)) {
|
||||
return (int) $slots[0]['start'];
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
#[OA\Patch(
|
||||
path: '/api/v1/appointment/{uuid}/status',
|
||||
summary: 'Update the status of an appointment',
|
||||
|
||||
Reference in New Issue
Block a user