feat(doctor-address): add city and province associations to DoctorAddress entity
This commit is contained in:
@@ -4,6 +4,8 @@ namespace App\Doctor\Controller;
|
||||
|
||||
use App\Auth\Entity\User;
|
||||
use App\Auth\Repository\UserRepository;
|
||||
use App\Clinic\Entity\Clinic;
|
||||
use App\Clinic\Repository\ClinicRepository;
|
||||
use App\DoctorService\Repository\DoctorServiceRepository;
|
||||
use App\Doctor\Entity\Doctor;
|
||||
use App\Doctor\Entity\DoctorAddress;
|
||||
@@ -28,6 +30,7 @@ class DoctorController extends BaseController
|
||||
public function __construct(
|
||||
private readonly DoctorRepository $doctorRepo,
|
||||
private readonly DoctorAddressRepository $addressRepo,
|
||||
private readonly ClinicRepository $clinicRepo,
|
||||
private readonly SpecialtyRepository $specialtyRepo,
|
||||
private readonly DoctorServiceRepository $serviceRepo,
|
||||
private readonly ProvinceRepository $provinceRepo,
|
||||
@@ -142,7 +145,22 @@ class DoctorController extends BaseController
|
||||
return $this->error(ErrorCodes::ERR_VALIDATION_002, 'دکتر یافت نشد', 404);
|
||||
}
|
||||
|
||||
return $this->success(['data' => $doctor->toDetailArray()]);
|
||||
$clinics = $this->clinicRepo->findByDoctor($doctor);
|
||||
$clinicData = array_map(fn(Clinic $c) => [
|
||||
'id' => (string) $c->getId(),
|
||||
'uuid' => $c->getUuid(),
|
||||
'name' => $c->getName(),
|
||||
'address' => $c->getAddress(),
|
||||
'telephone' => $c->getTelephone(),
|
||||
'city_id' => $c->getCityId(),
|
||||
'province_id' => $c->getProvinceId(),
|
||||
'map' => [
|
||||
'latitude' => $c->getLatitude() !== null ? (string) $c->getLatitude() : null,
|
||||
'longitude' => $c->getLongitude() !== null ? (string) $c->getLongitude() : null,
|
||||
],
|
||||
], $clinics);
|
||||
|
||||
return $this->success(['data' => array_merge($doctor->toDetailArray(), ['clinics' => $clinicData])]);
|
||||
}
|
||||
|
||||
#[OA\Get(
|
||||
@@ -693,8 +711,65 @@ class DoctorController extends BaseController
|
||||
if (isset($data['map']['latitude'])) $address->setLatitude((float) $data['map']['latitude']);
|
||||
if (isset($data['map']['longitude'])) $address->setLongitude((float) $data['map']['longitude']);
|
||||
|
||||
// Also support flat keys
|
||||
if (array_key_exists('latitude', $data)) $address->setLatitude((float) $data['latitude']);
|
||||
if (array_key_exists('longitude', $data)) $address->setLongitude((float) $data['longitude']);
|
||||
|
||||
if (array_key_exists('city_id', $data)) {
|
||||
$city = $data['city_id'] !== null ? $this->cityRepo->find((int) $data['city_id']) : null;
|
||||
$address->setCity($city);
|
||||
}
|
||||
if (array_key_exists('province_id', $data)) {
|
||||
$province = $data['province_id'] !== null ? $this->provinceRepo->find((int) $data['province_id']) : null;
|
||||
$address->setProvince($province);
|
||||
}
|
||||
}
|
||||
|
||||
#[Route('/api/v1/clinic-pro/doctor-address/from-clinic/{clinicUuid}', methods: ['POST'])]
|
||||
#[IsGranted('IS_AUTHENTICATED_FULLY')]
|
||||
public function createAddressFromClinic(string $clinicUuid, #[CurrentUser] User $user): JsonResponse
|
||||
{
|
||||
$doctor = $this->doctorRepo->findByUser($user);
|
||||
if ($doctor === null) {
|
||||
return $this->error(ErrorCodes::ERR_AUTH_006, 'فقط دکتر میتواند آدرس اضافه کند', 403);
|
||||
}
|
||||
|
||||
$clinic = $this->clinicRepo->findByUuid($clinicUuid);
|
||||
if ($clinic === null) {
|
||||
return $this->error(ErrorCodes::ERR_VALIDATION_002, 'کلینیک یافت نشد', 404);
|
||||
}
|
||||
|
||||
// Verify doctor belongs to this clinic
|
||||
$clinics = $this->clinicRepo->findByDoctor($doctor);
|
||||
$belongs = array_filter($clinics, fn(Clinic $c) => $c->getUuid() === $clinicUuid);
|
||||
if (empty($belongs)) {
|
||||
return $this->error(ErrorCodes::ERR_AUTH_006, 'این دکتر عضو این کلینیک نیست', 403);
|
||||
}
|
||||
|
||||
// Prevent duplicate: if this doctor already has an address for this clinic (same name), skip
|
||||
foreach ($doctor->getAddresses() as $existing) {
|
||||
if ($existing->getName() === $clinic->getName()) {
|
||||
return $this->success(['data' => $existing->toArray()]);
|
||||
}
|
||||
}
|
||||
|
||||
$address = new DoctorAddress($doctor);
|
||||
$address->setName($clinic->getName());
|
||||
$address->setAddress($clinic->getAddress());
|
||||
$address->setTelephone($clinic->getTelephone());
|
||||
$address->setLatitude($clinic->getLatitude());
|
||||
$address->setLongitude($clinic->getLongitude());
|
||||
|
||||
if ($clinic->getCityId() !== null) {
|
||||
$city = $this->cityRepo->find($clinic->getCityId());
|
||||
$address->setCity($city);
|
||||
}
|
||||
if ($clinic->getProvinceId() !== null) {
|
||||
$province = $this->provinceRepo->find($clinic->getProvinceId());
|
||||
$address->setProvince($province);
|
||||
}
|
||||
|
||||
$this->addressRepo->save($address);
|
||||
|
||||
return $this->success(['data' => $address->toArray()], 201);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user