feat: enhance clinic API to resolve contact fields from address record and add tests for contact field resolution
This commit is contained in:
@@ -160,9 +160,9 @@ class ClinicController extends BaseController
|
||||
return $this->error(ErrorCodes::ERR_VALIDATION_002, 'کلینیک یافت نشد', 404);
|
||||
}
|
||||
|
||||
[$stateData, $cityData, $map] = $this->loadLocationData($clinic);
|
||||
[$stateData, $cityData, $map, $street, $telephone] = $this->loadLocationData($clinic);
|
||||
|
||||
return $this->success(['data' => $clinic->toDetailArray($stateData, $cityData, $map)]);
|
||||
return $this->success(['data' => $clinic->toDetailArray($stateData, $cityData, $map, $street, $telephone)]);
|
||||
}
|
||||
|
||||
#[OA\Patch(
|
||||
@@ -237,9 +237,9 @@ class ClinicController extends BaseController
|
||||
$this->hydrateClinic($clinic, $data);
|
||||
$this->clinicRepo->save($clinic);
|
||||
|
||||
[$stateData, $cityData, $map] = $this->loadLocationData($clinic);
|
||||
[$stateData, $cityData, $map, $street, $telephone] = $this->loadLocationData($clinic);
|
||||
|
||||
return $this->success(['data' => $clinic->toDetailArray($stateData, $cityData, $map)]);
|
||||
return $this->success(['data' => $clinic->toDetailArray($stateData, $cityData, $map, $street, $telephone)]);
|
||||
}
|
||||
|
||||
#[OA\Get(
|
||||
@@ -284,8 +284,8 @@ class ClinicController extends BaseController
|
||||
|
||||
return $this->paginated(
|
||||
array_map(function (Clinic $c) use ($locations) {
|
||||
$loc = $locations[$c->getId()] ?? ['city' => null, 'state' => null];
|
||||
return $c->toListArray($loc['city'], $loc['state']);
|
||||
$loc = $locations[$c->getId()] ?? ['city' => null, 'state' => null, 'telephone' => null];
|
||||
return $c->toListArray($loc['city'], $loc['state'], $loc['telephone']);
|
||||
}, $result['items']),
|
||||
$result['total'],
|
||||
$result['page'],
|
||||
@@ -612,11 +612,17 @@ class ClinicController extends BaseController
|
||||
$provinceData = [];
|
||||
$cityData = [];
|
||||
$map = ['latitude' => null, 'longitude' => null];
|
||||
$street = null;
|
||||
$telephone = null;
|
||||
|
||||
// City/province/coordinates live on the clinic's address (DoctorAddress
|
||||
// linked by clinicId), not on the deprecated columns of the clinic itself.
|
||||
// The street text and phone come from the same record, otherwise one
|
||||
// response mixes two rows: city from the address, street from the clinic.
|
||||
$address = $this->addressRepo->findOneByClinic((int) $clinic->getId());
|
||||
if ($address !== null) {
|
||||
$street = $address->getAddress();
|
||||
$telephone = $address->getTelephone();
|
||||
$province = $address->getProvince();
|
||||
if ($province !== null) {
|
||||
$provinceData = ['uuid' => $province->getUuid(), 'id' => (string) $province->getId(), 'name' => $province->getName()];
|
||||
@@ -636,7 +642,7 @@ class ClinicController extends BaseController
|
||||
];
|
||||
}
|
||||
|
||||
return [$provinceData, $cityData, $map];
|
||||
return [$provinceData, $cityData, $map, $street, $telephone];
|
||||
}
|
||||
|
||||
private function handleFileUpload(Request $request, string $subDir): JsonResponse
|
||||
|
||||
@@ -187,20 +187,33 @@ class Clinic
|
||||
|
||||
private function touch(): void { $this->updatedAt = time(); }
|
||||
|
||||
public function toDetailArray(array $provinceData = [], array $cityData = [], ?array $map = null): array
|
||||
{
|
||||
/**
|
||||
* $street/$telephone come from the clinic's DoctorAddress record. The columns
|
||||
* on this entity are the deprecated legacy source and are only a fallback:
|
||||
* mixing the two makes a single response describe two different places.
|
||||
*/
|
||||
public function toDetailArray(
|
||||
array $provinceData = [],
|
||||
array $cityData = [],
|
||||
?array $map = null,
|
||||
?string $street = null,
|
||||
?string $telephone = null
|
||||
): array {
|
||||
$phone = $this->firstFilled($telephone, $this->telephone);
|
||||
$address = $this->firstFilled($street, $this->address);
|
||||
|
||||
return [
|
||||
'id' => (string) $this->id,
|
||||
'uuid' => $this->uuid,
|
||||
'name' => $this->name,
|
||||
'title' => $this->name,
|
||||
'is_active' => $this->isActive,
|
||||
'phone' => $this->telephone,
|
||||
'phone' => $phone,
|
||||
'logo' => $this->clinicLogo,
|
||||
'images_clinic' => $this->imagesClinic ?? [],
|
||||
'social_media' => $this->socialMedia,
|
||||
'clinic_logo' => $this->clinicLogo,
|
||||
'phone_number' => $this->telephone,
|
||||
'phone_number' => $phone,
|
||||
'caption' => $this->info,
|
||||
'list_bime' => array_map(fn(Insurance $i) => [
|
||||
'uuid' => $i->getUuid(), 'id' => (string) $i->getId(), 'name' => $i->getName(),
|
||||
@@ -220,7 +233,7 @@ class Clinic
|
||||
'doctor_list' => null,
|
||||
'city' => $cityData ? [$cityData] : [],
|
||||
'state' => $provinceData ? [$provinceData] : [],
|
||||
'location' => $this->address,
|
||||
'location' => $address,
|
||||
'map' => $map ?? [
|
||||
'latitude' => $this->latitude !== null ? (string) $this->latitude : null,
|
||||
'longitude' => $this->longitude !== null ? (string) $this->longitude : null,
|
||||
@@ -230,15 +243,28 @@ class Clinic
|
||||
];
|
||||
}
|
||||
|
||||
public function toListArray(?string $city = null, ?string $state = null): array
|
||||
private function firstFilled(?string ...$values): ?string
|
||||
{
|
||||
foreach ($values as $value) {
|
||||
if ($value !== null && trim($value) !== '') {
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function toListArray(?string $city = null, ?string $state = null, ?string $telephone = null): array
|
||||
{
|
||||
$phone = $this->firstFilled($telephone, $this->telephone);
|
||||
|
||||
return [
|
||||
'id' => (string) $this->id,
|
||||
'uuid' => $this->uuid,
|
||||
'name' => $this->name,
|
||||
'title' => $this->name,
|
||||
'phone' => $this->telephone,
|
||||
'phone_number' => $this->telephone,
|
||||
'phone' => $phone,
|
||||
'phone_number' => $phone,
|
||||
'logo' => $this->clinicLogo,
|
||||
'clinic_logo' => $this->clinicLogo,
|
||||
'images_clinic' => $this->imagesClinic ?? [],
|
||||
|
||||
@@ -101,7 +101,7 @@ class ClinicRepository extends ServiceEntityRepository
|
||||
}
|
||||
|
||||
$rows = $this->getEntityManager()->createQueryBuilder()
|
||||
->select('addr.clinicId AS clinic_id', 'cityCat.name AS city', 'provinceCat.name AS state')
|
||||
->select('addr.clinicId AS clinic_id', 'cityCat.name AS city', 'provinceCat.name AS state', 'addr.telephone AS telephone')
|
||||
->from(DoctorAddress::class, 'addr')
|
||||
->leftJoin('addr.city', 'cityCat')
|
||||
->leftJoin('addr.province', 'provinceCat')
|
||||
@@ -114,7 +114,7 @@ class ClinicRepository extends ServiceEntityRepository
|
||||
foreach ($rows as $row) {
|
||||
$id = (int) $row['clinic_id'];
|
||||
if (!isset($map[$id])) {
|
||||
$map[$id] = ['city' => $row['city'], 'state' => $row['state']];
|
||||
$map[$id] = ['city' => $row['city'], 'state' => $row['state'], 'telephone' => $row['telephone']];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user