feat: Implement SMS sending functionality with KavehNegar and Rangineh providers
- Add SendSmsMessage class for encapsulating SMS message data. - Create KavehNegarProvider and RanginehProvider classes implementing SmsProviderInterface for sending SMS. - Implement SmsLogRepository and SmsTemplateRepository for managing SMS logs and templates. - Develop SendSmsHandler for handling SMS sending messages. - Create SmsService to manage SMS dispatching and logging. - Add UserProfileController for managing user profiles with CRUD operations. - Implement UserProfile entity and repository for user profile data management. - Update symfony.lock and bootstrap.php for project dependencies and environment setup.
This commit is contained in:
@@ -0,0 +1,367 @@
|
||||
<?php
|
||||
|
||||
namespace App\Doctor\Controller;
|
||||
|
||||
use App\Auth\Entity\User;
|
||||
use App\Auth\Repository\UserRepository;
|
||||
use App\Category\Repository\CategoryRepository;
|
||||
use App\Doctor\Entity\Doctor;
|
||||
use App\Doctor\Entity\DoctorAddress;
|
||||
use App\Doctor\Repository\DoctorAddressRepository;
|
||||
use App\Doctor\Repository\DoctorRepository;
|
||||
use App\Shared\Constant\ErrorCodes;
|
||||
use App\Shared\Controller\BaseController;
|
||||
use App\Shared\Service\FileValidatorService;
|
||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\Routing\Attribute\Route;
|
||||
use Symfony\Component\Security\Http\Attribute\CurrentUser;
|
||||
use Symfony\Component\Security\Http\Attribute\IsGranted;
|
||||
|
||||
class DoctorController extends BaseController
|
||||
{
|
||||
public function __construct(
|
||||
private readonly DoctorRepository $doctorRepo,
|
||||
private readonly DoctorAddressRepository $addressRepo,
|
||||
private readonly CategoryRepository $categoryRepo,
|
||||
private readonly UserRepository $userRepo,
|
||||
private readonly FileValidatorService $fileValidator,
|
||||
private readonly string $projectDir,
|
||||
) {}
|
||||
|
||||
// ── Doctor CRUD ───────────────────────────────────────────────────────────
|
||||
|
||||
#[Route('/api/v1/doctor', methods: ['POST'])]
|
||||
#[IsGranted('IS_AUTHENTICATED_FULLY')]
|
||||
public function create(Request $request, #[CurrentUser] User $user): JsonResponse
|
||||
{
|
||||
if ($this->doctorRepo->findByUser($user) !== null) {
|
||||
return $this->error(ErrorCodes::ERR_VALIDATION_001, 'پروفایل دکتر قبلاً ایجاد شده است', 409);
|
||||
}
|
||||
|
||||
$data = json_decode($request->getContent(), true) ?? [];
|
||||
$name = trim($data['title'] ?? $data['name'] ?? '');
|
||||
|
||||
if ($name === '') {
|
||||
return $this->error(ErrorCodes::ERR_VALIDATION_001, 'نام دکتر الزامی است', 422, 'title');
|
||||
}
|
||||
|
||||
$doctor = new Doctor($user, $name);
|
||||
$this->hydrateDoctor($doctor, $data);
|
||||
$this->doctorRepo->save($doctor);
|
||||
|
||||
// Grant ROLE_DOCTOR to user
|
||||
$roles = $user->getRoles();
|
||||
if (!in_array('ROLE_DOCTOR', $roles, true)) {
|
||||
$roles[] = 'ROLE_DOCTOR';
|
||||
$user->setRoles(array_values(array_unique($roles)));
|
||||
$this->userRepo->save($user);
|
||||
}
|
||||
|
||||
return $this->success(['data' => $doctor->toDetailArray()], 201);
|
||||
}
|
||||
|
||||
#[Route('/api/v1/doctor/{uuid}', methods: ['GET'])]
|
||||
public function show(string $uuid): JsonResponse
|
||||
{
|
||||
$doctor = $this->doctorRepo->findByUuid($uuid);
|
||||
if ($doctor === null) {
|
||||
return $this->error(ErrorCodes::ERR_VALIDATION_002, 'دکتر یافت نشد', 404);
|
||||
}
|
||||
|
||||
return $this->success(['data' => $doctor->toDetailArray()]);
|
||||
}
|
||||
|
||||
#[Route('/api/v1/doctors', methods: ['GET'])]
|
||||
public function list(Request $request): JsonResponse
|
||||
{
|
||||
$filters = $request->query->all();
|
||||
$result = $this->doctorRepo->findWithFilters($filters);
|
||||
|
||||
return $this->paginated(
|
||||
array_map(fn(Doctor $d) => $d->toListArray(), $result['items']),
|
||||
$result['total'],
|
||||
$result['page'],
|
||||
$result['limit']
|
||||
);
|
||||
}
|
||||
|
||||
#[Route('/api/v1/doctor/{uuid}', methods: ['PATCH'])]
|
||||
#[IsGranted('IS_AUTHENTICATED_FULLY')]
|
||||
public function update(string $uuid, Request $request, #[CurrentUser] User $user): JsonResponse
|
||||
{
|
||||
$doctor = $this->doctorRepo->findByUuid($uuid);
|
||||
if ($doctor === null) {
|
||||
return $this->error(ErrorCodes::ERR_VALIDATION_002, 'دکتر یافت نشد', 404);
|
||||
}
|
||||
|
||||
if ($doctor->getUser()->getId() !== $user->getId() && !$user->hasRole('ROLE_ADMIN')) {
|
||||
return $this->error(ErrorCodes::ERR_AUTH_006, 'دسترسی ممنوع', 403);
|
||||
}
|
||||
|
||||
$data = json_decode($request->getContent(), true) ?? [];
|
||||
if (!empty($data['title'])) $doctor->setName($data['title']);
|
||||
|
||||
$this->hydrateDoctor($doctor, $data);
|
||||
$this->doctorRepo->save($doctor);
|
||||
|
||||
return $this->success(['data' => $doctor->toDetailArray()]);
|
||||
}
|
||||
|
||||
#[Route('/api/v1/doctor/{uuid}', methods: ['DELETE'])]
|
||||
#[IsGranted('ROLE_ADMIN')]
|
||||
public function delete(string $uuid): JsonResponse
|
||||
{
|
||||
$doctor = $this->doctorRepo->findByUuid($uuid);
|
||||
if ($doctor === null) {
|
||||
return $this->error(ErrorCodes::ERR_VALIDATION_002, 'دکتر یافت نشد', 404);
|
||||
}
|
||||
|
||||
$this->doctorRepo->remove($doctor);
|
||||
return $this->success(['message' => 'دکتر با موفقیت حذف شد']);
|
||||
}
|
||||
|
||||
// ── File Upload ───────────────────────────────────────────────────────────
|
||||
|
||||
#[Route('/file/upload/clinic_pro/doctor/field_image', methods: ['POST'])]
|
||||
#[IsGranted('IS_AUTHENTICATED_FULLY')]
|
||||
public function uploadImage(Request $request): JsonResponse
|
||||
{
|
||||
$content = $request->getContent();
|
||||
$disposition = $request->headers->get('Content-Disposition', '');
|
||||
preg_match('/filename=["\']?([^"\';\s]+)["\']?/i', $disposition, $m);
|
||||
$filename = $m[1] ?? 'upload.jpg';
|
||||
|
||||
// Write to a tmp file for magic bytes validation
|
||||
$tmpPath = sys_get_temp_dir() . '/' . uniqid('upload_', true);
|
||||
file_put_contents($tmpPath, $content);
|
||||
|
||||
try {
|
||||
// validate() checks size, magic bytes, and sanitizes filename
|
||||
$safeFilename = $this->fileValidator->validate($content, $filename);
|
||||
$mime = $this->fileValidator->detectMimeType($tmpPath);
|
||||
|
||||
$year = date('Y');
|
||||
$month = date('m');
|
||||
$dir = $this->projectDir . '/public/uploads/doctors/' . $year . '-' . $month;
|
||||
if (!is_dir($dir)) {
|
||||
mkdir($dir, 0755, true);
|
||||
}
|
||||
|
||||
$storedName = uniqid('', true) . '_' . $safeFilename;
|
||||
$fullPath = $dir . '/' . $storedName;
|
||||
rename($tmpPath, $fullPath);
|
||||
|
||||
$filesize = filesize($fullPath);
|
||||
$url = '/uploads/doctors/' . $year . '-' . $month . '/' . $storedName;
|
||||
|
||||
return $this->success([
|
||||
'fid' => time(),
|
||||
'uuid' => \Symfony\Component\Uid\Uuid::v4()->toRfc4122(),
|
||||
'url' => $url,
|
||||
'filename' => $safeFilename,
|
||||
'filemime' => $mime,
|
||||
'filesize' => $filesize,
|
||||
]);
|
||||
} catch (\Throwable $e) {
|
||||
if (file_exists($tmpPath)) unlink($tmpPath);
|
||||
return $this->error(ErrorCodes::ERR_VALIDATION_001, $e->getMessage(), 422);
|
||||
}
|
||||
}
|
||||
|
||||
// ── Doctor Addresses ──────────────────────────────────────────────────────
|
||||
|
||||
#[Route('/api/v1/clinic-pro/doctor-address', methods: ['POST'])]
|
||||
#[IsGranted('IS_AUTHENTICATED_FULLY')]
|
||||
public function createAddress(Request $request, #[CurrentUser] User $user): JsonResponse
|
||||
{
|
||||
$doctor = $this->doctorRepo->findByUser($user);
|
||||
if ($doctor === null && !$user->hasRole('ROLE_ADMIN')) {
|
||||
return $this->error(ErrorCodes::ERR_AUTH_006, 'فقط دکتر میتواند آدرس اضافه کند', 403);
|
||||
}
|
||||
|
||||
$data = json_decode($request->getContent(), true) ?? [];
|
||||
|
||||
// Admin can specify doctor_id/doctor_uuid
|
||||
if ($doctor === null && $user->hasRole('ROLE_ADMIN')) {
|
||||
$doctorUuid = $data['doctor_uuid'] ?? null;
|
||||
if (!$doctorUuid) {
|
||||
return $this->error(ErrorCodes::ERR_VALIDATION_001, 'doctor_uuid الزامی است', 422);
|
||||
}
|
||||
$doctor = $this->doctorRepo->findByUuid($doctorUuid);
|
||||
if ($doctor === null) {
|
||||
return $this->error(ErrorCodes::ERR_VALIDATION_002, 'دکتر یافت نشد', 404);
|
||||
}
|
||||
}
|
||||
|
||||
$address = new DoctorAddress($doctor);
|
||||
$this->hydrateAddress($address, $data);
|
||||
$this->addressRepo->save($address);
|
||||
|
||||
return $this->success(['data' => $address->toArray()], 201);
|
||||
}
|
||||
|
||||
#[Route('/api/v1/clinic-pro/doctor-address/{id}', methods: ['GET'])]
|
||||
#[IsGranted('IS_AUTHENTICATED_FULLY')]
|
||||
public function showAddress(int $id): JsonResponse
|
||||
{
|
||||
$address = $this->addressRepo->find($id);
|
||||
if ($address === null) {
|
||||
return $this->error(ErrorCodes::ERR_VALIDATION_002, 'آدرس یافت نشد', 404);
|
||||
}
|
||||
|
||||
return $this->success(['data' => $address->toArray()]);
|
||||
}
|
||||
|
||||
#[Route('/api/v1/clinic-pro/doctor-address/{id}', methods: ['PATCH'])]
|
||||
#[IsGranted('IS_AUTHENTICATED_FULLY')]
|
||||
public function updateAddress(int $id, Request $request, #[CurrentUser] User $user): JsonResponse
|
||||
{
|
||||
$address = $this->addressRepo->find($id);
|
||||
if ($address === null) {
|
||||
return $this->error(ErrorCodes::ERR_VALIDATION_002, 'آدرس یافت نشد', 404);
|
||||
}
|
||||
|
||||
if ($address->getDoctor()->getUser()->getId() !== $user->getId() && !$user->hasRole('ROLE_ADMIN')) {
|
||||
return $this->error(ErrorCodes::ERR_AUTH_006, 'دسترسی ممنوع', 403);
|
||||
}
|
||||
|
||||
$data = json_decode($request->getContent(), true) ?? [];
|
||||
$this->hydrateAddress($address, $data);
|
||||
$this->addressRepo->save($address);
|
||||
|
||||
return $this->success(['data' => $address->toArray()]);
|
||||
}
|
||||
|
||||
#[Route('/api/v1/clinic-pro/doctor-address/{id}', methods: ['DELETE'])]
|
||||
#[IsGranted('IS_AUTHENTICATED_FULLY')]
|
||||
public function deleteAddress(int $id, #[CurrentUser] User $user): JsonResponse
|
||||
{
|
||||
$address = $this->addressRepo->find($id);
|
||||
if ($address === null) {
|
||||
return $this->error(ErrorCodes::ERR_VALIDATION_002, 'آدرس یافت نشد', 404);
|
||||
}
|
||||
|
||||
if ($address->getDoctor()->getUser()->getId() !== $user->getId() && !$user->hasRole('ROLE_ADMIN')) {
|
||||
return $this->error(ErrorCodes::ERR_AUTH_006, 'دسترسی ممنوع', 403);
|
||||
}
|
||||
|
||||
$this->addressRepo->remove($address);
|
||||
return $this->success(['message' => 'آدرس با موفقیت حذف شد']);
|
||||
}
|
||||
|
||||
#[Route('/api/v1/clinic-pro/doctor-addresses/{doctorId}', methods: ['GET'])]
|
||||
public function listAddresses(int $doctorId): JsonResponse
|
||||
{
|
||||
$doctor = $this->doctorRepo->find($doctorId);
|
||||
if ($doctor === null) {
|
||||
return $this->error(ErrorCodes::ERR_VALIDATION_002, 'دکتر یافت نشد', 404);
|
||||
}
|
||||
|
||||
$addresses = array_map(fn(DoctorAddress $a) => $a->toArray(), $doctor->getAddresses()->toArray());
|
||||
|
||||
return $this->success(['data' => $addresses]);
|
||||
}
|
||||
|
||||
// ── Clinic/Doctor list (stub — implemented fully in Task 06) ──────────────
|
||||
|
||||
#[Route('/api/v1/clinic/doctor-list/{clinicUuid}', methods: ['GET'])]
|
||||
public function clinicDoctorList(string $clinicUuid): JsonResponse
|
||||
{
|
||||
// Full implementation in Task 06 (Clinic entity not yet created)
|
||||
return $this->success(['data' => []]);
|
||||
}
|
||||
|
||||
// ── Helpers ───────────────────────────────────────────────────────────────
|
||||
|
||||
private function hydrateDoctor(Doctor $doctor, array $data): void
|
||||
{
|
||||
if (array_key_exists('gender', $data)) $doctor->setGender($data['gender']);
|
||||
if (array_key_exists('medical_system_code', $data)) $doctor->setMedicalSystemCode($data['medical_system_code']);
|
||||
if (array_key_exists('mobile_number', $data)) $doctor->setMobileNumber($data['mobile_number']);
|
||||
if (array_key_exists('activity_time', $data)) $doctor->setActivityTime((int) $data['activity_time']);
|
||||
if (array_key_exists('degree', $data)) $doctor->setDegree($data['degree']);
|
||||
if (array_key_exists('info', $data)) $doctor->setInfo($data['info']);
|
||||
if (array_key_exists('detail', $data)) $doctor->setInfo($data['detail']);
|
||||
if (array_key_exists('active', $data)) $doctor->setActiveDoctorAppointment((bool) $data['active']);
|
||||
|
||||
// Images array (from file upload response)
|
||||
if (array_key_exists('image_data', $data)) {
|
||||
$existing = $doctor->getImages() ?? [];
|
||||
$existing[] = $data['image_data'];
|
||||
$doctor->setImages($existing);
|
||||
}
|
||||
if (array_key_exists('images', $data) && is_array($data['images'])) {
|
||||
$doctor->setImages($data['images']);
|
||||
}
|
||||
|
||||
// Specialties (array of category IDs)
|
||||
if (array_key_exists('specialties', $data) && is_array($data['specialties'])) {
|
||||
$doctor->getSpecialties()->clear();
|
||||
foreach ($data['specialties'] as $catId) {
|
||||
$cat = $this->categoryRepo->find((int) $catId);
|
||||
if ($cat !== null) {
|
||||
$doctor->getSpecialties()->add($cat);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Expertise / doctor_services
|
||||
if (array_key_exists('doctor_services', $data) && is_array($data['doctor_services'])) {
|
||||
$doctor->getExpertise()->clear();
|
||||
foreach ($data['doctor_services'] as $catId) {
|
||||
$cat = is_numeric($catId)
|
||||
? $this->categoryRepo->find((int) $catId)
|
||||
: $this->categoryRepo->findOneBy(['label' => $catId, 'bundle' => 'doctor_services']);
|
||||
if ($cat !== null) {
|
||||
$doctor->getExpertise()->add($cat);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (array_key_exists('expertise', $data) && is_array($data['expertise'])) {
|
||||
$doctor->getExpertise()->clear();
|
||||
foreach ($data['expertise'] as $catId) {
|
||||
$cat = $this->categoryRepo->find((int) $catId);
|
||||
if ($cat !== null) {
|
||||
$doctor->getExpertise()->add($cat);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// States
|
||||
if (array_key_exists('states', $data) && is_array($data['states'])) {
|
||||
$doctor->getStates()->clear();
|
||||
foreach ($data['states'] as $catId) {
|
||||
$cat = $this->categoryRepo->find((int) $catId);
|
||||
if ($cat !== null) {
|
||||
$doctor->getStates()->add($cat);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Cities
|
||||
if (array_key_exists('cities', $data) && is_array($data['cities'])) {
|
||||
$doctor->getCities()->clear();
|
||||
foreach ($data['cities'] as $catId) {
|
||||
$cat = $this->categoryRepo->find((int) $catId);
|
||||
if ($cat !== null) {
|
||||
$doctor->getCities()->add($cat);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function hydrateAddress(DoctorAddress $address, array $data): void
|
||||
{
|
||||
if (array_key_exists('name', $data)) $address->setName($data['name']);
|
||||
if (array_key_exists('address', $data)) $address->setAddress($data['address']);
|
||||
if (array_key_exists('telephone', $data)) $address->setTelephone($data['telephone']);
|
||||
|
||||
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']);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,232 @@
|
||||
<?php
|
||||
|
||||
namespace App\Doctor\Entity;
|
||||
|
||||
use App\Auth\Entity\User;
|
||||
use App\Category\Entity\Category;
|
||||
use Doctrine\Common\Collections\ArrayCollection;
|
||||
use Doctrine\Common\Collections\Collection;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
use Symfony\Component\Uid\Uuid;
|
||||
|
||||
#[ORM\Entity]
|
||||
#[ORM\Table(name: 'doctors')]
|
||||
#[ORM\UniqueConstraint(name: 'idx_doctors_user', columns: ['user_id'])]
|
||||
#[ORM\Index(columns: ['active_doctor_appointment'], name: 'idx_doctors_active')]
|
||||
class Doctor
|
||||
{
|
||||
public const DEGREES = ['expert', 'general', 'specialist', 'subspecialistplus'];
|
||||
public const GENDERS = ['man', 'woman'];
|
||||
|
||||
#[ORM\Id]
|
||||
#[ORM\GeneratedValue]
|
||||
#[ORM\Column(type: 'integer')]
|
||||
private ?int $id = null;
|
||||
|
||||
#[ORM\Column(type: 'string', length: 36, unique: true)]
|
||||
private string $uuid;
|
||||
|
||||
#[ORM\OneToOne(targetEntity: User::class)]
|
||||
#[ORM\JoinColumn(name: 'user_id', referencedColumnName: 'id', nullable: false)]
|
||||
private User $user;
|
||||
|
||||
#[ORM\Column(type: 'string', length: 255)]
|
||||
private string $name;
|
||||
|
||||
#[ORM\Column(type: 'string', length: 10, nullable: true)]
|
||||
private ?string $gender = null;
|
||||
|
||||
#[ORM\Column(name: 'medical_system_code', type: 'string', length: 25, nullable: true)]
|
||||
private ?string $medicalSystemCode = null;
|
||||
|
||||
#[ORM\Column(name: 'mobile_number', type: 'string', length: 15, nullable: true)]
|
||||
private ?string $mobileNumber = null;
|
||||
|
||||
#[ORM\Column(name: 'activity_time', type: 'integer', nullable: true)]
|
||||
private ?int $activityTime = null;
|
||||
|
||||
#[ORM\Column(type: 'string', length: 30, nullable: true)]
|
||||
private ?string $degree = null;
|
||||
|
||||
#[ORM\Column(type: 'text', nullable: true)]
|
||||
private ?string $info = null;
|
||||
|
||||
#[ORM\Column(type: 'json', nullable: true)]
|
||||
private ?array $images = null;
|
||||
|
||||
#[ORM\Column(name: 'doctor_rate', type: 'float')]
|
||||
private float $doctorRate = 3.5;
|
||||
|
||||
#[ORM\Column(name: 'doctor_rate_percentage', type: 'float')]
|
||||
private float $doctorRatePercentage = 60.0;
|
||||
|
||||
#[ORM\Column(name: 'active_doctor_appointment', type: 'boolean')]
|
||||
private bool $activeDoctorAppointment = true;
|
||||
|
||||
#[ORM\Column(name: 'representation_id', type: 'integer', nullable: true)]
|
||||
private ?int $representationId = null;
|
||||
|
||||
#[ORM\Column(name: 'created_at', type: 'integer')]
|
||||
private int $createdAt;
|
||||
|
||||
#[ORM\Column(name: 'updated_at', type: 'integer')]
|
||||
private int $updatedAt;
|
||||
|
||||
#[ORM\ManyToMany(targetEntity: Category::class)]
|
||||
#[ORM\JoinTable(
|
||||
name: 'doctor_specialties',
|
||||
joinColumns: [new ORM\JoinColumn(name: 'doctor_id', referencedColumnName: 'id')],
|
||||
inverseJoinColumns: [new ORM\JoinColumn(name: 'category_id', referencedColumnName: 'id')]
|
||||
)]
|
||||
private Collection $specialties;
|
||||
|
||||
#[ORM\ManyToMany(targetEntity: Category::class)]
|
||||
#[ORM\JoinTable(
|
||||
name: 'doctor_expertise',
|
||||
joinColumns: [new ORM\JoinColumn(name: 'doctor_id', referencedColumnName: 'id')],
|
||||
inverseJoinColumns: [new ORM\JoinColumn(name: 'category_id', referencedColumnName: 'id')]
|
||||
)]
|
||||
private Collection $expertise;
|
||||
|
||||
#[ORM\ManyToMany(targetEntity: Category::class)]
|
||||
#[ORM\JoinTable(
|
||||
name: 'doctor_states',
|
||||
joinColumns: [new ORM\JoinColumn(name: 'doctor_id', referencedColumnName: 'id')],
|
||||
inverseJoinColumns: [new ORM\JoinColumn(name: 'category_id', referencedColumnName: 'id')]
|
||||
)]
|
||||
private Collection $states;
|
||||
|
||||
#[ORM\ManyToMany(targetEntity: Category::class)]
|
||||
#[ORM\JoinTable(
|
||||
name: 'doctor_cities',
|
||||
joinColumns: [new ORM\JoinColumn(name: 'doctor_id', referencedColumnName: 'id')],
|
||||
inverseJoinColumns: [new ORM\JoinColumn(name: 'category_id', referencedColumnName: 'id')]
|
||||
)]
|
||||
private Collection $cities;
|
||||
|
||||
#[ORM\OneToMany(targetEntity: DoctorAddress::class, mappedBy: 'doctor', cascade: ['remove'])]
|
||||
private Collection $addresses;
|
||||
|
||||
public function __construct(User $user, string $name)
|
||||
{
|
||||
$this->uuid = Uuid::v4()->toRfc4122();
|
||||
$this->user = $user;
|
||||
$this->name = $name;
|
||||
$this->createdAt = time();
|
||||
$this->updatedAt = time();
|
||||
$this->specialties = new ArrayCollection();
|
||||
$this->expertise = new ArrayCollection();
|
||||
$this->states = new ArrayCollection();
|
||||
$this->cities = new ArrayCollection();
|
||||
$this->addresses = new ArrayCollection();
|
||||
}
|
||||
|
||||
public function getId(): ?int { return $this->id; }
|
||||
public function getUuid(): string { return $this->uuid; }
|
||||
public function getUser(): User { return $this->user; }
|
||||
public function getName(): string { return $this->name; }
|
||||
public function getGender(): ?string { return $this->gender; }
|
||||
public function getMedicalSystemCode(): ?string { return $this->medicalSystemCode; }
|
||||
public function getMobileNumber(): ?string { return $this->mobileNumber; }
|
||||
public function getActivityTime(): ?int { return $this->activityTime; }
|
||||
public function getDegree(): ?string { return $this->degree; }
|
||||
public function getInfo(): ?string { return $this->info; }
|
||||
public function getImages(): ?array { return $this->images; }
|
||||
public function getDoctorRate(): float { return $this->doctorRate; }
|
||||
public function getDoctorRatePercentage(): float { return $this->doctorRatePercentage; }
|
||||
public function isActiveDoctorAppointment(): bool { return $this->activeDoctorAppointment; }
|
||||
public function getRepresentationId(): ?int { return $this->representationId; }
|
||||
public function getCreatedAt(): int { return $this->createdAt; }
|
||||
public function getUpdatedAt(): int { return $this->updatedAt; }
|
||||
public function getSpecialties(): Collection { return $this->specialties; }
|
||||
public function getExpertise(): Collection { return $this->expertise; }
|
||||
public function getStates(): Collection { return $this->states; }
|
||||
public function getCities(): Collection { return $this->cities; }
|
||||
public function getAddresses(): Collection { return $this->addresses; }
|
||||
|
||||
public function setName(string $v): self { $this->name = $v; return $this; }
|
||||
public function setGender(?string $v): self { $this->gender = $v; $this->touch(); return $this; }
|
||||
public function setMedicalSystemCode(?string $v): self { $this->medicalSystemCode = $v; $this->touch(); return $this; }
|
||||
public function setMobileNumber(?string $v): self { $this->mobileNumber = $v; $this->touch(); return $this; }
|
||||
public function setActivityTime(?int $v): self { $this->activityTime = $v; $this->touch(); return $this; }
|
||||
public function setDegree(?string $v): self { $this->degree = $v; $this->touch(); return $this; }
|
||||
public function setInfo(?string $v): self { $this->info = $v; $this->touch(); return $this; }
|
||||
public function setImages(?array $v): self { $this->images = $v; $this->touch(); return $this; }
|
||||
public function setDoctorRate(float $v): self { $this->doctorRate = $v; $this->touch(); return $this; }
|
||||
public function setDoctorRatePercentage(float $v): self { $this->doctorRatePercentage = $v; $this->touch(); return $this; }
|
||||
public function setActiveDoctorAppointment(bool $v): self { $this->activeDoctorAppointment = $v; $this->touch(); return $this; }
|
||||
public function setRepresentationId(?int $v): self { $this->representationId = $v; $this->touch(); return $this; }
|
||||
|
||||
private function touch(): void { $this->updatedAt = time(); }
|
||||
|
||||
public function getExperience(): int
|
||||
{
|
||||
if ($this->activityTime === null) {
|
||||
return 0;
|
||||
}
|
||||
return max(0, (int)((time() - $this->activityTime) / (365.25 * 24 * 3600)));
|
||||
}
|
||||
|
||||
public function toListArray(): array
|
||||
{
|
||||
return [
|
||||
'id' => (string) $this->id,
|
||||
'uuid' => $this->uuid,
|
||||
'name' => $this->name,
|
||||
'gender' => $this->gender,
|
||||
'degree' => $this->degree,
|
||||
'img' => $this->images ?? [],
|
||||
'specialties' => $this->formatCategories($this->specialties),
|
||||
'satisfaction' => (string) $this->doctorRatePercentage,
|
||||
'point' => (string) $this->doctorRate,
|
||||
'free_turn' => 'نوبت آزادی موجود نیست',
|
||||
'hours_of_work' => 'برنامه کاری تنظیم نشده',
|
||||
'active' => $this->activeDoctorAppointment,
|
||||
];
|
||||
}
|
||||
|
||||
public function toDetailArray(): array
|
||||
{
|
||||
return [
|
||||
'id' => (string) $this->id,
|
||||
'uuid' => $this->uuid,
|
||||
'name' => $this->name,
|
||||
'gender' => $this->gender,
|
||||
'experience' => $this->getExperience(),
|
||||
'activity_time' => $this->activityTime !== null ? (string) $this->activityTime : null,
|
||||
'medical_system_code' => $this->medicalSystemCode,
|
||||
'detail' => $this->info,
|
||||
'degree' => $this->degree,
|
||||
'specialties' => $this->formatCategories($this->specialties),
|
||||
'img' => $this->images ?? [],
|
||||
'expertise' => $this->formatCategories($this->expertise),
|
||||
'satisfaction' => (string) $this->doctorRatePercentage,
|
||||
'point' => (string) $this->doctorRate,
|
||||
'free_turn' => 'نوبت آزادی موجود نیست',
|
||||
'hours_of_work' => 'برنامه کاری تنظیم نشده',
|
||||
'address' => array_map(fn(DoctorAddress $a) => $a->toArray(), $this->addresses->toArray()),
|
||||
'average_rate' => ['total_rates' => null],
|
||||
'state' => $this->formatCategories($this->states),
|
||||
'city' => $this->formatCategoriesWithParent($this->cities),
|
||||
];
|
||||
}
|
||||
|
||||
private function formatCategories(Collection $collection): array
|
||||
{
|
||||
return array_map(fn(Category $c) => [
|
||||
'uuid' => $c->getUuid(),
|
||||
'id' => (string) $c->getId(),
|
||||
'name' => $c->getLabel(),
|
||||
], $collection->toArray());
|
||||
}
|
||||
|
||||
private function formatCategoriesWithParent(Collection $collection): array
|
||||
{
|
||||
return array_map(fn(Category $c) => [
|
||||
'uuid' => $c->getUuid(),
|
||||
'id' => (string) $c->getId(),
|
||||
'name' => $c->getLabel(),
|
||||
'parent' => $c->getParentId() !== null ? (string) $c->getParentId() : null,
|
||||
], $collection->toArray());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
<?php
|
||||
|
||||
namespace App\Doctor\Entity;
|
||||
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
use Symfony\Component\Uid\Uuid;
|
||||
|
||||
#[ORM\Entity]
|
||||
#[ORM\Table(name: 'doctor_addresses')]
|
||||
#[ORM\Index(columns: ['doctor_id'], name: 'idx_doctor_addresses_doctor')]
|
||||
class DoctorAddress
|
||||
{
|
||||
#[ORM\Id]
|
||||
#[ORM\GeneratedValue]
|
||||
#[ORM\Column(type: 'integer')]
|
||||
private ?int $id = null;
|
||||
|
||||
#[ORM\Column(type: 'string', length: 36, unique: true)]
|
||||
private string $uuid;
|
||||
|
||||
#[ORM\ManyToOne(targetEntity: Doctor::class, inversedBy: 'addresses')]
|
||||
#[ORM\JoinColumn(name: 'doctor_id', referencedColumnName: 'id', nullable: false, onDelete: 'CASCADE')]
|
||||
private Doctor $doctor;
|
||||
|
||||
#[ORM\Column(type: 'string', length: 255, nullable: true)]
|
||||
private ?string $name = null;
|
||||
|
||||
#[ORM\Column(type: 'text', nullable: true)]
|
||||
private ?string $address = null;
|
||||
|
||||
#[ORM\Column(type: 'string', length: 50, nullable: true)]
|
||||
private ?string $telephone = null;
|
||||
|
||||
#[ORM\Column(type: 'float', nullable: true)]
|
||||
private ?float $latitude = null;
|
||||
|
||||
#[ORM\Column(type: 'float', nullable: true)]
|
||||
private ?float $longitude = null;
|
||||
|
||||
#[ORM\Column(name: 'created_at', type: 'integer')]
|
||||
private int $createdAt;
|
||||
|
||||
#[ORM\Column(name: 'updated_at', type: 'integer')]
|
||||
private int $updatedAt;
|
||||
|
||||
public function __construct(Doctor $doctor)
|
||||
{
|
||||
$this->uuid = Uuid::v4()->toRfc4122();
|
||||
$this->doctor = $doctor;
|
||||
$this->createdAt = time();
|
||||
$this->updatedAt = time();
|
||||
}
|
||||
|
||||
public function getId(): ?int { return $this->id; }
|
||||
public function getUuid(): string { return $this->uuid; }
|
||||
public function getDoctor(): Doctor { return $this->doctor; }
|
||||
public function getName(): ?string { return $this->name; }
|
||||
public function getAddress(): ?string { return $this->address; }
|
||||
public function getTelephone(): ?string { return $this->telephone; }
|
||||
public function getLatitude(): ?float { return $this->latitude; }
|
||||
public function getLongitude(): ?float { return $this->longitude; }
|
||||
|
||||
public function setName(?string $v): self { $this->name = $v; return $this; }
|
||||
public function setAddress(?string $v): self { $this->address = $v; $this->touch(); return $this; }
|
||||
public function setTelephone(?string $v): self { $this->telephone = $v; $this->touch(); return $this; }
|
||||
public function setLatitude(?float $v): self { $this->latitude = $v; $this->touch(); return $this; }
|
||||
public function setLongitude(?float $v): self { $this->longitude = $v; $this->touch(); return $this; }
|
||||
|
||||
private function touch(): void { $this->updatedAt = time(); }
|
||||
|
||||
public function toArray(): array
|
||||
{
|
||||
return [
|
||||
'id' => (string) $this->id,
|
||||
'uuid' => $this->uuid,
|
||||
'name' => $this->name,
|
||||
'map' => [
|
||||
'latitude' => $this->latitude !== null ? (string) $this->latitude : null,
|
||||
'longitude' => $this->longitude !== null ? (string) $this->longitude : null,
|
||||
],
|
||||
'address' => $this->address,
|
||||
'telephone' => $this->telephone,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace App\Doctor\Repository;
|
||||
|
||||
use App\Doctor\Entity\DoctorAddress;
|
||||
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
|
||||
use Doctrine\Persistence\ManagerRegistry;
|
||||
|
||||
class DoctorAddressRepository extends ServiceEntityRepository
|
||||
{
|
||||
public function __construct(ManagerRegistry $registry)
|
||||
{
|
||||
parent::__construct($registry, DoctorAddress::class);
|
||||
}
|
||||
|
||||
public function save(DoctorAddress $address, bool $flush = true): void
|
||||
{
|
||||
$this->getEntityManager()->persist($address);
|
||||
if ($flush) {
|
||||
$this->getEntityManager()->flush();
|
||||
}
|
||||
}
|
||||
|
||||
public function remove(DoctorAddress $address, bool $flush = true): void
|
||||
{
|
||||
$this->getEntityManager()->remove($address);
|
||||
if ($flush) {
|
||||
$this->getEntityManager()->flush();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
<?php
|
||||
|
||||
namespace App\Doctor\Repository;
|
||||
|
||||
use App\Auth\Entity\User;
|
||||
use App\Doctor\Entity\Doctor;
|
||||
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
|
||||
use Doctrine\ORM\Tools\Pagination\Paginator;
|
||||
use Doctrine\Persistence\ManagerRegistry;
|
||||
|
||||
class DoctorRepository extends ServiceEntityRepository
|
||||
{
|
||||
public function __construct(ManagerRegistry $registry)
|
||||
{
|
||||
parent::__construct($registry, Doctor::class);
|
||||
}
|
||||
|
||||
public function findByUuid(string $uuid): ?Doctor
|
||||
{
|
||||
return $this->findOneBy(['uuid' => $uuid]);
|
||||
}
|
||||
|
||||
public function findByUser(User $user): ?Doctor
|
||||
{
|
||||
return $this->findOneBy(['user' => $user]);
|
||||
}
|
||||
|
||||
public function findWithFilters(array $filters): array
|
||||
{
|
||||
$page = max(1, (int) ($filters['page'] ?? 1));
|
||||
$limit = min(50, max(1, (int) ($filters['limit'] ?? 10)));
|
||||
$sort = strtoupper($filters['sort'] ?? 'DESC') === 'ASC' ? 'ASC' : 'DESC';
|
||||
|
||||
$qb = $this->createQueryBuilder('d')
|
||||
->leftJoin('d.specialties', 's')
|
||||
->leftJoin('d.states', 'st')
|
||||
->leftJoin('d.cities', 'ci')
|
||||
->distinct();
|
||||
|
||||
if (!empty($filters['state'])) {
|
||||
$qb->andWhere('st.id = :state')->setParameter('state', (int) $filters['state']);
|
||||
}
|
||||
if (!empty($filters['city'])) {
|
||||
$qb->andWhere('ci.id = :city')->setParameter('city', (int) $filters['city']);
|
||||
}
|
||||
if (!empty($filters['specialty'])) {
|
||||
$qb->andWhere('s.id = :specialty')->setParameter('specialty', (int) $filters['specialty']);
|
||||
}
|
||||
if (!empty($filters['gender'])) {
|
||||
$qb->andWhere('d.gender = :gender')->setParameter('gender', $filters['gender']);
|
||||
}
|
||||
if (!empty($filters['degree'])) {
|
||||
$qb->andWhere('d.degree = :degree')->setParameter('degree', $filters['degree']);
|
||||
}
|
||||
if (!empty($filters['name'])) {
|
||||
$qb->andWhere('d.name LIKE :name')->setParameter('name', '%' . $filters['name'] . '%');
|
||||
}
|
||||
if (isset($filters['active'])) {
|
||||
$qb->andWhere('d.activeDoctorAppointment = :active')
|
||||
->setParameter('active', (bool) $filters['active']);
|
||||
}
|
||||
|
||||
$qb->orderBy('d.doctorRate', $sort);
|
||||
|
||||
$total = (new Paginator($qb))->count();
|
||||
$results = $qb->setFirstResult(($page - 1) * $limit)
|
||||
->setMaxResults($limit)
|
||||
->getQuery()
|
||||
->getResult();
|
||||
|
||||
return [
|
||||
'items' => $results,
|
||||
'total' => $total,
|
||||
'page' => $page,
|
||||
'limit' => $limit,
|
||||
'totalPages' => (int) ceil($total / $limit),
|
||||
];
|
||||
}
|
||||
|
||||
public function save(Doctor $doctor, bool $flush = true): void
|
||||
{
|
||||
$this->getEntityManager()->persist($doctor);
|
||||
if ($flush) {
|
||||
$this->getEntityManager()->flush();
|
||||
}
|
||||
}
|
||||
|
||||
public function remove(Doctor $doctor, bool $flush = true): void
|
||||
{
|
||||
$this->getEntityManager()->remove($doctor);
|
||||
if ($flush) {
|
||||
$this->getEntityManager()->flush();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user