feat(doctor): ensure default office address creation during doctor import
This commit is contained in:
@@ -4,6 +4,7 @@ namespace App\Doctor\Service;
|
||||
|
||||
use App\Auth\Entity\User;
|
||||
use App\Doctor\Entity\Doctor;
|
||||
use App\Doctor\Entity\DoctorAddress;
|
||||
use App\Location\Entity\City;
|
||||
use App\Location\Entity\Province;
|
||||
use App\Specialty\Entity\Specialty;
|
||||
@@ -106,6 +107,8 @@ class DoctorImportService
|
||||
$this->syncRefCollection($doctor->getProvinces(), $data['states'] ?? null, Province::class);
|
||||
$this->syncRefCollection($doctor->getCities(), $data['cities'] ?? null, City::class);
|
||||
|
||||
$this->ensureDefaultAddress($doctor, $name, $data);
|
||||
|
||||
$this->em->persist($doctor);
|
||||
$this->em->flush();
|
||||
|
||||
@@ -113,6 +116,38 @@ class DoctorImportService
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* هر پزشک ایمپورتشده باید دستکم یک آدرس «مطب دکتر …» با شهر و استان داشته باشد.
|
||||
* اگر آدرسی ندارد میسازد؛ فیلدهای خالیِ آدرس موجود را backfill میکند و
|
||||
* مقادیر واردشده توسط کاربر را بازنویسی نمیکند.
|
||||
*/
|
||||
private function ensureDefaultAddress(Doctor $doctor, string $name, array $data): void
|
||||
{
|
||||
$city = !empty($data['cities'])
|
||||
? $this->em->getRepository(City::class)->find((int) $data['cities'][0])
|
||||
: null;
|
||||
$province = !empty($data['states'])
|
||||
? $this->em->getRepository(Province::class)->find((int) $data['states'][0])
|
||||
: null;
|
||||
|
||||
$address = $doctor->getAddresses()->first() ?: null;
|
||||
if ($address === null) {
|
||||
$address = DoctorAddress::forDoctor($doctor);
|
||||
$doctor->getAddresses()->add($address);
|
||||
$this->em->persist($address);
|
||||
}
|
||||
|
||||
if ($address->getName() === null || $address->getName() === '') {
|
||||
$address->setName('مطب دکتر ' . $name);
|
||||
}
|
||||
if ($address->getCity() === null && $city !== null) {
|
||||
$address->setCity($city);
|
||||
}
|
||||
if ($address->getProvince() === null && $province !== null) {
|
||||
$address->setProvince($province);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* یک مجموعهٔ ManyToMany پزشک را با آرایهای از شناسههای مرجع همگام میکند.
|
||||
* اگر $ids null باشد دست نمیخورد؛ اگر آرایه باشد، پاک و از نو پر میشود.
|
||||
|
||||
Reference in New Issue
Block a user