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,268 @@
|
||||
<?php
|
||||
|
||||
namespace App\Clinic\Controller;
|
||||
|
||||
use App\Auth\Entity\User;
|
||||
use App\Auth\Repository\UserRepository;
|
||||
use App\Category\Repository\CategoryRepository;
|
||||
use App\Clinic\Entity\Clinic;
|
||||
use App\Clinic\Repository\ClinicRepository;
|
||||
use App\Doctor\Entity\Doctor;
|
||||
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;
|
||||
use Symfony\Component\Uid\Uuid;
|
||||
|
||||
class ClinicController extends BaseController
|
||||
{
|
||||
public function __construct(
|
||||
private readonly ClinicRepository $clinicRepo,
|
||||
private readonly DoctorRepository $doctorRepo,
|
||||
private readonly CategoryRepository $categoryRepo,
|
||||
private readonly UserRepository $userRepo,
|
||||
private readonly FileValidatorService $fileValidator,
|
||||
private readonly string $projectDir,
|
||||
) {}
|
||||
|
||||
#[Route('/api/v1/clinic', methods: ['POST'])]
|
||||
#[IsGranted('IS_AUTHENTICATED_FULLY')]
|
||||
public function create(Request $request, #[CurrentUser] User $user): JsonResponse
|
||||
{
|
||||
$data = json_decode($request->getContent(), true) ?? [];
|
||||
$clinic = new Clinic($user);
|
||||
$this->hydrateClinic($clinic, $data);
|
||||
$this->clinicRepo->save($clinic);
|
||||
|
||||
// Grant ROLE_CLINIC to user
|
||||
$roles = $user->getRoles();
|
||||
if (!in_array('ROLE_CLINIC', $roles, true)) {
|
||||
$roles[] = 'ROLE_CLINIC';
|
||||
$user->setRoles(array_values(array_unique($roles)));
|
||||
$this->userRepo->save($user);
|
||||
}
|
||||
|
||||
return $this->success(['data' => $clinic->toDetailArray()], 201);
|
||||
}
|
||||
|
||||
#[Route('/api/v1/clinic/{uuid}', methods: ['GET'])]
|
||||
public function show(string $uuid): JsonResponse
|
||||
{
|
||||
$clinic = $this->clinicRepo->findByUuid($uuid);
|
||||
if ($clinic === null) {
|
||||
return $this->error(ErrorCodes::ERR_VALIDATION_002, 'کلینیک یافت نشد', 404);
|
||||
}
|
||||
|
||||
[$stateData, $cityData] = $this->loadLocationData($clinic);
|
||||
|
||||
return $this->success(['data' => $clinic->toDetailArray($stateData, $cityData)]);
|
||||
}
|
||||
|
||||
#[Route('/api/v1/clinic/{uuid}', methods: ['PATCH'])]
|
||||
#[IsGranted('IS_AUTHENTICATED_FULLY')]
|
||||
public function update(string $uuid, Request $request, #[CurrentUser] User $user): JsonResponse
|
||||
{
|
||||
$clinic = $this->clinicRepo->findByUuid($uuid);
|
||||
if ($clinic === null) {
|
||||
return $this->error(ErrorCodes::ERR_VALIDATION_002, 'کلینیک یافت نشد', 404);
|
||||
}
|
||||
|
||||
if ($clinic->getUser()->getId() !== $user->getId() && !$user->hasRole('ROLE_ADMIN')) {
|
||||
return $this->error(ErrorCodes::ERR_AUTH_006, 'دسترسی ممنوع', 403);
|
||||
}
|
||||
|
||||
$data = json_decode($request->getContent(), true) ?? [];
|
||||
$this->hydrateClinic($clinic, $data);
|
||||
$this->clinicRepo->save($clinic);
|
||||
|
||||
[$stateData, $cityData] = $this->loadLocationData($clinic);
|
||||
|
||||
return $this->success(['data' => $clinic->toDetailArray($stateData, $cityData)]);
|
||||
}
|
||||
|
||||
#[Route('/api/v1/clinics', methods: ['GET'])]
|
||||
public function list(Request $request): JsonResponse
|
||||
{
|
||||
$filters = $request->query->all();
|
||||
$result = $this->clinicRepo->findWithFilters($filters);
|
||||
|
||||
return $this->paginated(
|
||||
array_map(fn(Clinic $c) => $c->toListArray(), $result['items']),
|
||||
$result['total'],
|
||||
$result['page'],
|
||||
$result['limit']
|
||||
);
|
||||
}
|
||||
|
||||
#[Route('/api/v1/clinic/doctor-list/{clinicUuid}', methods: ['GET'])]
|
||||
public function doctorList(string $clinicUuid): JsonResponse
|
||||
{
|
||||
$clinic = $this->clinicRepo->findByUuid($clinicUuid);
|
||||
if ($clinic === null) {
|
||||
return $this->error(ErrorCodes::ERR_VALIDATION_002, 'کلینیک یافت نشد', 404);
|
||||
}
|
||||
|
||||
$doctors = array_map(
|
||||
fn(Doctor $d) => $d->toListArray(),
|
||||
$clinic->getDoctors()->toArray()
|
||||
);
|
||||
|
||||
return $this->success(['data' => $doctors]);
|
||||
}
|
||||
|
||||
#[Route('/file/upload/clinic_pro/clinic/field_image_clinic', methods: ['POST'])]
|
||||
#[IsGranted('IS_AUTHENTICATED_FULLY')]
|
||||
public function uploadImage(Request $request): JsonResponse
|
||||
{
|
||||
return $this->handleFileUpload($request, 'clinics/gallery');
|
||||
}
|
||||
|
||||
#[Route('/file/upload/clinic_pro/clinic/field_clinic_logo', methods: ['POST'])]
|
||||
#[IsGranted('IS_AUTHENTICATED_FULLY')]
|
||||
public function uploadLogo(Request $request): JsonResponse
|
||||
{
|
||||
return $this->handleFileUpload($request, 'clinics/logo');
|
||||
}
|
||||
|
||||
// ── Helpers ───────────────────────────────────────────────────────────────
|
||||
|
||||
private function hydrateClinic(Clinic $clinic, array $data): void
|
||||
{
|
||||
if (array_key_exists('name', $data)) $clinic->setName($data['name']);
|
||||
if (array_key_exists('info', $data)) $clinic->setInfo($data['info']);
|
||||
if (array_key_exists('address', $data)) $clinic->setAddress($data['address']);
|
||||
if (array_key_exists('telephone', $data)) $clinic->setTelephone($data['telephone']);
|
||||
if (array_key_exists('working_days', $data)) $clinic->setWorkingDays($data['working_days']);
|
||||
if (array_key_exists('24_7', $data)) $clinic->setIs247((bool) $data['24_7']);
|
||||
if (array_key_exists('latitude', $data)) $clinic->setLatitude((float) $data['latitude']);
|
||||
if (array_key_exists('longitude', $data)) $clinic->setLongitude((float) $data['longitude']);
|
||||
|
||||
// Location
|
||||
if (!empty($data['state']) && is_array($data['state'])) {
|
||||
$clinic->setStateId((int) $data['state'][0]);
|
||||
}
|
||||
if (!empty($data['city']) && is_array($data['city'])) {
|
||||
$clinic->setCityId((int) $data['city'][0]);
|
||||
}
|
||||
|
||||
// Images stored as JSON (from upload response)
|
||||
if (array_key_exists('image_clinic', $data) && is_array($data['image_clinic'])) {
|
||||
$clinic->setImagesClinic($data['image_clinic']);
|
||||
}
|
||||
if (array_key_exists('clinic_logo', $data) && is_array($data['clinic_logo'])) {
|
||||
$clinic->setClinicLogo($data['clinic_logo']);
|
||||
}
|
||||
|
||||
// ManyToMany: doctors
|
||||
if (array_key_exists('doctors', $data) && is_array($data['doctors'])) {
|
||||
$clinic->getDoctors()->clear();
|
||||
foreach ($data['doctors'] as $doctorId) {
|
||||
$doctor = $this->doctorRepo->find((int) $doctorId);
|
||||
if ($doctor !== null) {
|
||||
$clinic->getDoctors()->add($doctor);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ManyToMany: specialties
|
||||
if (array_key_exists('specialties', $data) && is_array($data['specialties'])) {
|
||||
$clinic->getSpecialties()->clear();
|
||||
foreach ($data['specialties'] as $catId) {
|
||||
$cat = $this->categoryRepo->find((int) $catId);
|
||||
if ($cat !== null) {
|
||||
$clinic->getSpecialties()->add($cat);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ManyToMany: services (doctor_services)
|
||||
if (array_key_exists('doctor_services', $data) && is_array($data['doctor_services'])) {
|
||||
$clinic->getServices()->clear();
|
||||
foreach ($data['doctor_services'] as $catId) {
|
||||
$cat = $this->categoryRepo->find((int) $catId);
|
||||
if ($cat !== null) {
|
||||
$clinic->getServices()->add($cat);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ManyToMany: insurances
|
||||
if (array_key_exists('insurance', $data) && is_array($data['insurance'])) {
|
||||
$clinic->getInsurances()->clear();
|
||||
foreach ($data['insurance'] as $catId) {
|
||||
$cat = $this->categoryRepo->find((int) $catId);
|
||||
if ($cat !== null) {
|
||||
$clinic->getInsurances()->add($cat);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function loadLocationData(Clinic $clinic): array
|
||||
{
|
||||
$stateData = [];
|
||||
$cityData = [];
|
||||
|
||||
if ($clinic->getStateId() !== null) {
|
||||
$state = $this->categoryRepo->find($clinic->getStateId());
|
||||
if ($state !== null) {
|
||||
$stateData = ['uuid' => $state->getUuid(), 'id' => (string) $state->getId(), 'name' => $state->getLabel()];
|
||||
}
|
||||
}
|
||||
if ($clinic->getCityId() !== null) {
|
||||
$city = $this->categoryRepo->find($clinic->getCityId());
|
||||
if ($city !== null) {
|
||||
$cityData = [
|
||||
'uuid' => $city->getUuid(),
|
||||
'id' => (string) $city->getId(),
|
||||
'name' => $city->getLabel(),
|
||||
'parent' => $city->getParentId() !== null ? (string) $city->getParentId() : null,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
return [$stateData, $cityData];
|
||||
}
|
||||
|
||||
private function handleFileUpload(Request $request, string $subDir): JsonResponse
|
||||
{
|
||||
$content = $request->getContent();
|
||||
$disposition = $request->headers->get('Content-Disposition', '');
|
||||
preg_match('/filename=["\']?([^"\';\s]+)["\']?/i', $disposition, $m);
|
||||
$filename = $m[1] ?? 'upload.jpg';
|
||||
|
||||
$tmpPath = sys_get_temp_dir() . '/' . uniqid('upload_', true);
|
||||
file_put_contents($tmpPath, $content);
|
||||
|
||||
try {
|
||||
$safeFilename = $this->fileValidator->sanitizeFilename($filename);
|
||||
$mime = $this->fileValidator->detectMimeType($tmpPath);
|
||||
|
||||
$year = date('Y'); $month = date('m');
|
||||
$dir = $this->projectDir . '/public/uploads/' . $subDir . '/' . $year . '-' . $month;
|
||||
if (!is_dir($dir)) mkdir($dir, 0755, true);
|
||||
|
||||
$storedName = uniqid('', true) . '_' . $safeFilename;
|
||||
rename($tmpPath, $dir . '/' . $storedName);
|
||||
|
||||
$url = '/uploads/' . $subDir . '/' . $year . '-' . $month . '/' . $storedName;
|
||||
|
||||
return $this->success([
|
||||
'fid' => time(),
|
||||
'uuid' => Uuid::v4()->toRfc4122(),
|
||||
'url' => $url,
|
||||
'filename' => $safeFilename,
|
||||
'filemime' => $mime,
|
||||
'filesize' => strlen($content),
|
||||
]);
|
||||
} catch (\Throwable $e) {
|
||||
if (file_exists($tmpPath)) unlink($tmpPath);
|
||||
return $this->error(ErrorCodes::ERR_VALIDATION_001, $e->getMessage(), 422);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,212 @@
|
||||
<?php
|
||||
|
||||
namespace App\Clinic\Entity;
|
||||
|
||||
use App\Auth\Entity\User;
|
||||
use App\Category\Entity\Category;
|
||||
use App\Doctor\Entity\Doctor;
|
||||
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: 'clinics')]
|
||||
#[ORM\Index(columns: ['user_id'], name: 'idx_clinics_owner')]
|
||||
#[ORM\Index(columns: ['city_id'], name: 'idx_clinics_city')]
|
||||
#[ORM\Index(columns: ['state_id'], name: 'idx_clinics_state')]
|
||||
class Clinic
|
||||
{
|
||||
#[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: User::class)]
|
||||
#[ORM\JoinColumn(name: 'user_id', referencedColumnName: 'id', nullable: false)]
|
||||
private User $user;
|
||||
|
||||
#[ORM\Column(type: 'string', length: 255, nullable: true)]
|
||||
private ?string $name = null;
|
||||
|
||||
#[ORM\Column(type: 'text', nullable: true)]
|
||||
private ?string $info = 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(name: 'is_24_7', type: 'boolean')]
|
||||
private bool $is247 = false;
|
||||
|
||||
#[ORM\Column(name: 'working_days', type: 'string', length: 255, nullable: true)]
|
||||
private ?string $workingDays = null;
|
||||
|
||||
#[ORM\Column(type: 'float', nullable: true)]
|
||||
private ?float $latitude = null;
|
||||
|
||||
#[ORM\Column(type: 'float', nullable: true)]
|
||||
private ?float $longitude = null;
|
||||
|
||||
#[ORM\Column(name: 'city_id', type: 'integer', nullable: true)]
|
||||
private ?int $cityId = null;
|
||||
|
||||
#[ORM\Column(name: 'state_id', type: 'integer', nullable: true)]
|
||||
private ?int $stateId = null;
|
||||
|
||||
#[ORM\Column(name: 'representation_id', type: 'integer', nullable: true)]
|
||||
private ?int $representationId = null;
|
||||
|
||||
#[ORM\Column(name: 'images_clinic', type: 'json', nullable: true)]
|
||||
private ?array $imagesClinic = null;
|
||||
|
||||
#[ORM\Column(name: 'clinic_logo', type: 'json', nullable: true)]
|
||||
private ?array $clinicLogo = null;
|
||||
|
||||
#[ORM\Column(name: 'created_at', type: 'integer')]
|
||||
private int $createdAt;
|
||||
|
||||
#[ORM\Column(name: 'updated_at', type: 'integer')]
|
||||
private int $updatedAt;
|
||||
|
||||
#[ORM\ManyToMany(targetEntity: Doctor::class)]
|
||||
#[ORM\JoinTable(
|
||||
name: 'clinic_doctors',
|
||||
joinColumns: [new ORM\JoinColumn(name: 'clinic_id', referencedColumnName: 'id', onDelete: 'CASCADE')],
|
||||
inverseJoinColumns: [new ORM\JoinColumn(name: 'doctor_id', referencedColumnName: 'id', onDelete: 'CASCADE')]
|
||||
)]
|
||||
private Collection $doctors;
|
||||
|
||||
#[ORM\ManyToMany(targetEntity: Category::class)]
|
||||
#[ORM\JoinTable(
|
||||
name: 'clinic_specialties',
|
||||
joinColumns: [new ORM\JoinColumn(name: 'clinic_id', referencedColumnName: 'id', onDelete: 'CASCADE')],
|
||||
inverseJoinColumns: [new ORM\JoinColumn(name: 'category_id', referencedColumnName: 'id')]
|
||||
)]
|
||||
private Collection $specialties;
|
||||
|
||||
#[ORM\ManyToMany(targetEntity: Category::class)]
|
||||
#[ORM\JoinTable(
|
||||
name: 'clinic_services',
|
||||
joinColumns: [new ORM\JoinColumn(name: 'clinic_id', referencedColumnName: 'id', onDelete: 'CASCADE')],
|
||||
inverseJoinColumns: [new ORM\JoinColumn(name: 'category_id', referencedColumnName: 'id')]
|
||||
)]
|
||||
private Collection $services;
|
||||
|
||||
#[ORM\ManyToMany(targetEntity: Category::class)]
|
||||
#[ORM\JoinTable(
|
||||
name: 'clinic_insurances',
|
||||
joinColumns: [new ORM\JoinColumn(name: 'clinic_id', referencedColumnName: 'id', onDelete: 'CASCADE')],
|
||||
inverseJoinColumns: [new ORM\JoinColumn(name: 'category_id', referencedColumnName: 'id')]
|
||||
)]
|
||||
private Collection $insurances;
|
||||
|
||||
public function __construct(User $user)
|
||||
{
|
||||
$this->uuid = Uuid::v4()->toRfc4122();
|
||||
$this->user = $user;
|
||||
$this->createdAt = time();
|
||||
$this->updatedAt = time();
|
||||
$this->doctors = new ArrayCollection();
|
||||
$this->specialties = new ArrayCollection();
|
||||
$this->services = new ArrayCollection();
|
||||
$this->insurances = 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 getInfo(): ?string { return $this->info; }
|
||||
public function getAddress(): ?string { return $this->address; }
|
||||
public function getTelephone(): ?string { return $this->telephone; }
|
||||
public function isIs247(): bool { return $this->is247; }
|
||||
public function getWorkingDays(): ?string { return $this->workingDays; }
|
||||
public function getLatitude(): ?float { return $this->latitude; }
|
||||
public function getLongitude(): ?float { return $this->longitude; }
|
||||
public function getCityId(): ?int { return $this->cityId; }
|
||||
public function getStateId(): ?int { return $this->stateId; }
|
||||
public function getRepresentationId(): ?int { return $this->representationId; }
|
||||
public function getImagesClinic(): ?array { return $this->imagesClinic; }
|
||||
public function getClinicLogo(): ?array { return $this->clinicLogo; }
|
||||
public function getDoctors(): Collection { return $this->doctors; }
|
||||
public function getSpecialties(): Collection { return $this->specialties; }
|
||||
public function getServices(): Collection { return $this->services; }
|
||||
public function getInsurances(): Collection { return $this->insurances; }
|
||||
|
||||
public function setName(?string $v): self { $this->name = $v; $this->touch(); return $this; }
|
||||
public function setInfo(?string $v): self { $this->info = $v; $this->touch(); 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 setIs247(bool $v): self { $this->is247 = $v; $this->touch(); return $this; }
|
||||
public function setWorkingDays(?string $v): self { $this->workingDays = $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; }
|
||||
public function setCityId(?int $v): self { $this->cityId = $v; $this->touch(); return $this; }
|
||||
public function setStateId(?int $v): self { $this->stateId = $v; $this->touch(); return $this; }
|
||||
public function setRepresentationId(?int $v): self { $this->representationId = $v; $this->touch(); return $this; }
|
||||
public function setImagesClinic(?array $v): self { $this->imagesClinic = $v; $this->touch(); return $this; }
|
||||
public function setClinicLogo(?array $v): self { $this->clinicLogo = $v; $this->touch(); return $this; }
|
||||
|
||||
private function touch(): void { $this->updatedAt = time(); }
|
||||
|
||||
public function toDetailArray(array $stateData = [], array $cityData = []): array
|
||||
{
|
||||
$formatCat = fn(Category $c) => [
|
||||
'uuid' => $c->getUuid(), 'id' => (string) $c->getId(), 'name' => $c->getLabel(),
|
||||
];
|
||||
$formatCatWithParent = fn(Category $c) => [
|
||||
'uuid' => $c->getUuid(), 'id' => (string) $c->getId(), 'name' => $c->getLabel(),
|
||||
'parent' => $c->getParentId() !== null ? (string) $c->getParentId() : null,
|
||||
];
|
||||
|
||||
return [
|
||||
'id' => (string) $this->id,
|
||||
'uuid' => $this->uuid,
|
||||
'title' => $this->name,
|
||||
'images_clinic' => $this->imagesClinic ?? [],
|
||||
'clinic_logo' => $this->clinicLogo ?? [],
|
||||
'phone_number' => $this->telephone,
|
||||
'caption' => $this->info,
|
||||
'list_bime' => array_map($formatCat, $this->insurances->toArray()),
|
||||
'specialties' => array_map($formatCatWithParent, $this->specialties->toArray()),
|
||||
'services' => array_map($formatCat, $this->services->toArray()),
|
||||
'clinic_specialty' => array_map($formatCatWithParent, $this->specialties->toArray()),
|
||||
'doctors' => $this->doctors->count(),
|
||||
'doctor_list' => null,
|
||||
'city' => $cityData ? [$cityData] : [],
|
||||
'state' => $stateData ? [$stateData] : [],
|
||||
'location' => $this->address,
|
||||
'map' => [
|
||||
'latitude' => $this->latitude !== null ? (string) $this->latitude : null,
|
||||
'longitude' => $this->longitude !== null ? (string) $this->longitude : null,
|
||||
],
|
||||
'24_7' => $this->is247,
|
||||
'field_working_days' => $this->workingDays,
|
||||
];
|
||||
}
|
||||
|
||||
public function toListArray(): array
|
||||
{
|
||||
$formatCat = fn(Category $c) => [
|
||||
'uuid' => $c->getUuid(), 'id' => (string) $c->getId(), 'name' => $c->getLabel(),
|
||||
];
|
||||
|
||||
return [
|
||||
'id' => (string) $this->id,
|
||||
'uuid' => $this->uuid,
|
||||
'title' => $this->name,
|
||||
'images_clinic' => $this->imagesClinic ?? [],
|
||||
'clinic_logo' => $this->clinicLogo ?? [],
|
||||
'phone_number' => $this->telephone,
|
||||
'specialties' => array_map($formatCat, $this->specialties->toArray()),
|
||||
'doctors' => $this->doctors->count(),
|
||||
'24_7' => $this->is247,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
|
||||
namespace App\Clinic\Repository;
|
||||
|
||||
use App\Auth\Entity\User;
|
||||
use App\Clinic\Entity\Clinic;
|
||||
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
|
||||
use Doctrine\ORM\Tools\Pagination\Paginator;
|
||||
use Doctrine\Persistence\ManagerRegistry;
|
||||
|
||||
class ClinicRepository extends ServiceEntityRepository
|
||||
{
|
||||
public function __construct(ManagerRegistry $registry)
|
||||
{
|
||||
parent::__construct($registry, Clinic::class);
|
||||
}
|
||||
|
||||
public function findByUuid(string $uuid): ?Clinic
|
||||
{
|
||||
return $this->findOneBy(['uuid' => $uuid]);
|
||||
}
|
||||
|
||||
public function findByUser(User $user): ?Clinic
|
||||
{
|
||||
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('c')
|
||||
->leftJoin('c.specialties', 's')
|
||||
->distinct();
|
||||
|
||||
if (!empty($filters['state'])) {
|
||||
$qb->andWhere('c.stateId = :state')->setParameter('state', (int) $filters['state']);
|
||||
}
|
||||
if (!empty($filters['city'])) {
|
||||
$qb->andWhere('c.cityId = :city')->setParameter('city', (int) $filters['city']);
|
||||
}
|
||||
if (!empty($filters['specialty'])) {
|
||||
$qb->andWhere('s.id = :specialty')->setParameter('specialty', (int) $filters['specialty']);
|
||||
}
|
||||
|
||||
$qb->orderBy('c.id', $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(Clinic $clinic, bool $flush = true): void
|
||||
{
|
||||
$this->getEntityManager()->persist($clinic);
|
||||
if ($flush) {
|
||||
$this->getEntityManager()->flush();
|
||||
}
|
||||
}
|
||||
|
||||
public function remove(Clinic $clinic, bool $flush = true): void
|
||||
{
|
||||
$this->getEntityManager()->remove($clinic);
|
||||
if ($flush) {
|
||||
$this->getEntityManager()->flush();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user