feat: add insurance and location management

- Introduced InsuranceType enum for insurance categorization.
- Created InsuranceRepository for managing insurance entities.
- Developed LocationController for handling provinces and cities, including CRUD operations.
- Implemented City and Province entities with necessary fields and relationships.
- Added CityRepository and ProvinceRepository for database interactions.
- Established Specialty management with SpecialtyController, including CRUD operations.
- Created Specialty and Tag entities with appropriate fields and relationships.
- Implemented TagController for managing tags, including CRUD operations.
- Added TagRepository for database interactions with tags.
This commit is contained in:
hamed
2026-06-10 14:22:26 +03:30
parent 4b8504df91
commit 5066fcbd91
36 changed files with 2937 additions and 1241 deletions
+27 -149
View File
@@ -2,164 +2,42 @@
namespace App\Category\Controller;
use App\Category\Entity\Category;
use App\Category\Repository\CategoryRepository;
use App\Category\Service\CategoryService;
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\IsGranted;
use Symfony\Component\Uid\Uuid;
/**
* Legacy stub — all endpoints migrated to domain-specific controllers:
* Provinces/Cities → App\Location\Controller\LocationController
* Specialties → App\Specialty\Controller\SpecialtyController
* DoctorServices → App\DoctorService\Controller\DoctorServiceController
* Insurances → App\Insurance\Controller\InsuranceController
* Tags → App\Tag\Controller\TagController
*/
class CategoryController extends BaseController
{
public function __construct(
private readonly CategoryRepository $repository,
private readonly CategoryService $service,
private readonly FileValidatorService $fileValidator,
private readonly string $projectDir,
) {}
#[Route('/api/v1/categorys/tag', methods: ['GET'])]
public function listTags(): JsonResponse
#[Route('/api/v1/categorys/{bundle}', methods: ['GET'])]
public function legacyList(string $bundle): JsonResponse
{
return $this->success(['data' => $this->service->listByBundle('tag')]);
}
$map = [
'state' => '/api/v1/provinces',
'city' => '/api/v1/cities',
'specially_doctor' => '/api/v1/specialties',
'doctor_services' => '/api/v1/doctor-services',
'insurance_type' => '/api/v1/insurances?type=basic',
'supplementary_insurance' => '/api/v1/insurances?type=supplementary',
'tag' => '/api/v1/tags',
];
#[Route('/api/v1/categorys/supplementary_insurance', methods: ['GET'])]
public function listSupplementaryInsurance(): JsonResponse
{
return $this->success(['data' => $this->service->listByBundle('supplementary_insurance')]);
}
#[Route('/api/v1/categorys/insurance_type', methods: ['GET'])]
public function listInsuranceType(): JsonResponse
{
return $this->success(['data' => $this->service->listByBundle('insurance_type')]);
}
#[Route('/api/v1/categorys/state', methods: ['GET'])]
public function listStates(): JsonResponse
{
return $this->success(['data' => $this->service->listByBundle('state')]);
}
#[Route('/api/v1/categorys/city', methods: ['GET'])]
public function listCities(Request $request): JsonResponse
{
$stateId = $request->query->get('state_id');
$parentId = $stateId !== null ? (int) $stateId : null;
return $this->success(['data' => $this->service->listByBundle('city', $parentId)]);
}
#[Route('/api/v1/categorys/specially_doctor', methods: ['GET'])]
public function listSpecialties(): JsonResponse
{
return $this->success(['data' => $this->service->listByBundle('specially_doctor')]);
}
#[Route('/api/v1/categorys/doctor_services', methods: ['GET'])]
public function listDoctorServices(): JsonResponse
{
return $this->success(['data' => $this->service->listByBundle('doctor_services')]);
}
#[Route('/api/v1/category', methods: ['POST'])]
#[IsGranted('ROLE_ADMIN')]
public function create(Request $request): JsonResponse
{
$data = json_decode($request->getContent(), true) ?? [];
$bundle = trim($data['bundle'] ?? '');
$label = trim($data['label'] ?? '');
if (!in_array($bundle, Category::BUNDLES, true)) {
return $this->error(ErrorCodes::ERR_VALIDATION_001, 'bundle نامعتبر است', 422, 'bundle');
$newUrl = $map[$bundle] ?? null;
if ($newUrl === null) {
return $this->error('ERR_GONE', 'این endpoint حذف شده است', 410);
}
if ($label === '') {
return $this->error(ErrorCodes::ERR_VALIDATION_001, 'label الزامی است', 422, 'label');
}
$category = $this->service->create($bundle, $label, $data);
return $this->success(['data' => $category->toArray()], 201);
}
#[Route('/api/v1/category/{id}', methods: ['PATCH'])]
#[IsGranted('ROLE_ADMIN')]
public function update(int $id, Request $request): JsonResponse
{
$category = $this->repository->find($id);
if ($category === null) {
return $this->error(ErrorCodes::ERR_VALIDATION_002, 'دسته‌بندی یافت نشد', 404);
}
$data = json_decode($request->getContent(), true) ?? [];
if (isset($data['bundle']) && !in_array($data['bundle'], Category::BUNDLES, true)) {
return $this->error(ErrorCodes::ERR_VALIDATION_001, 'bundle نامعتبر است', 422, 'bundle');
}
$category = $this->service->update($category, $data);
return $this->success(['data' => $category->toArray()]);
}
#[Route('/api/v1/category/{id}', methods: ['DELETE'])]
#[IsGranted('ROLE_ADMIN')]
public function delete(int $id): JsonResponse
{
$category = $this->repository->find($id);
if ($category === null) {
return $this->error(ErrorCodes::ERR_VALIDATION_002, 'دسته‌بندی یافت نشد', 404);
}
$this->service->delete($category);
return $this->success(['message' => 'دسته‌بندی با موفقیت حذف شد']);
}
#[Route('/api/v1/admin/category/upload-logo', methods: ['POST'])]
#[IsGranted('ROLE_ADMIN')]
public function uploadLogo(Request $request): JsonResponse
{
$content = $request->getContent();
$disposition = $request->headers->get('Content-Disposition', '');
preg_match('/filename=["\']?([^"\';\s]+)["\']?/i', $disposition, $m);
$filename = $m[1] ?? 'logo.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/categories/logo/' . $year . '-' . $month;
if (!is_dir($dir)) mkdir($dir, 0755, true);
$storedName = uniqid('', true) . '_' . $safeFilename;
rename($tmpPath, $dir . '/' . $storedName);
$url = '/uploads/categories/logo/' . $year . '-' . $month . '/' . $storedName;
return $this->success([
'url' => $url,
'uuid' => Uuid::v4()->toRfc4122(),
'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);
}
return $this->error(
'ERR_MOVED',
sprintf('این endpoint منتقل شده. لطفاً از %s استفاده کنید.', $newUrl),
301
);
}
}
-165
View File
@@ -1,165 +0,0 @@
<?php
namespace App\Category\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Uid\Uuid;
#[ORM\Entity]
#[ORM\Table(name: 'categories')]
#[ORM\Index(columns: ['bundle'], name: 'idx_categories_bundle')]
#[ORM\Index(columns: ['parent_id'], name: 'idx_categories_parent')]
#[ORM\Index(columns: ['status', 'bundle'], name: 'idx_categories_status')]
class Category
{
public const BUNDLES = [
'state', 'city', 'specially_doctor', 'doctor_services',
'insurance_type', 'supplementary_insurance', 'tag',
];
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
private ?int $id = null;
#[ORM\Column(type: 'string', length: 36, unique: true)]
private string $uuid;
#[ORM\Column(type: 'string', length: 32)]
private string $bundle;
#[ORM\Column(type: 'string', length: 255, nullable: true)]
private ?string $label = null;
#[ORM\Column(type: 'smallint')]
private int $status = 1;
#[ORM\Column(name: 'parent_id', type: 'integer', nullable: true)]
private ?int $parentId = null;
#[ORM\Column(type: 'integer')]
private int $weight = 0;
#[ORM\Column(name: 'logo_id', type: 'integer', nullable: true)]
private ?int $logoId = null;
#[ORM\Column(type: 'string', length: 255, nullable: true)]
private ?string $title = null;
#[ORM\Column(name: 'representation_id', type: 'integer', nullable: true)]
private ?int $representationId = null;
// City-specific fields
#[ORM\Column(name: 'contact_phone', type: 'string', length: 255, nullable: true)]
private ?string $contactPhone = null;
#[ORM\Column(type: 'string', length: 255, nullable: true)]
private ?string $email = null;
#[ORM\Column(type: 'text', nullable: true)]
private ?string $description = null;
#[ORM\Column(type: 'string', length: 255, nullable: true)]
private ?string $slogan = null;
#[ORM\Column(type: 'string', length: 255, nullable: true)]
private ?string $domain = null;
#[ORM\Column(type: 'string', length: 255, nullable: true)]
private ?string $keywords = null;
#[ORM\Column(name: 'footer_description', type: 'text', nullable: true)]
private ?string $footerDescription = null;
#[ORM\Column(name: 'footer_disclaimer', type: 'text', nullable: true)]
private ?string $footerDisclaimer = null;
#[ORM\Column(name: 'social_media', type: 'json', nullable: true)]
private ?array $socialMedia = null;
#[ORM\Column(name: 'logo_url', type: 'string', length: 500, nullable: true)]
private ?string $logoUrl = null;
public function __construct(string $bundle, string $label)
{
$this->uuid = Uuid::v4()->toRfc4122();
$this->bundle = $bundle;
$this->label = $label;
}
public function getId(): ?int { return $this->id; }
public function getUuid(): string { return $this->uuid; }
public function getBundle(): string { return $this->bundle; }
public function getLabel(): ?string { return $this->label; }
public function getStatus(): int { return $this->status; }
public function getParentId(): ?int { return $this->parentId; }
public function getWeight(): int { return $this->weight; }
public function getLogoId(): ?int { return $this->logoId; }
public function getTitle(): ?string { return $this->title; }
public function getRepresentationId(): ?int { return $this->representationId; }
public function getContactPhone(): ?string { return $this->contactPhone; }
public function getEmail(): ?string { return $this->email; }
public function getDescription(): ?string { return $this->description; }
public function getSlogan(): ?string { return $this->slogan; }
public function getDomain(): ?string { return $this->domain; }
public function getKeywords(): ?string { return $this->keywords; }
public function getFooterDescription(): ?string { return $this->footerDescription; }
public function getFooterDisclaimer(): ?string { return $this->footerDisclaimer; }
public function getSocialMedia(): ?array { return $this->socialMedia; }
public function getLogoUrl(): ?string { return $this->logoUrl; }
public function setBundle(string $bundle): self { $this->bundle = $bundle; return $this; }
public function setLabel(?string $label): self { $this->label = $label; return $this; }
public function setStatus(int $status): self { $this->status = $status; return $this; }
public function setParentId(?int $id): self { $this->parentId = $id; return $this; }
public function setWeight(int $weight): self { $this->weight = $weight; return $this; }
public function setLogoId(?int $id): self { $this->logoId = $id; return $this; }
public function setTitle(?string $title): self { $this->title = $title; return $this; }
public function setRepresentationId(?int $id): self { $this->representationId = $id; return $this; }
public function setContactPhone(?string $v): self { $this->contactPhone = $v; return $this; }
public function setEmail(?string $v): self { $this->email = $v; return $this; }
public function setDescription(?string $v): self { $this->description = $v; return $this; }
public function setSlogan(?string $v): self { $this->slogan = $v; return $this; }
public function setDomain(?string $v): self { $this->domain = $v; return $this; }
public function setKeywords(?string $v): self { $this->keywords = $v; return $this; }
public function setFooterDescription(?string $v): self { $this->footerDescription = $v; return $this; }
public function setFooterDisclaimer(?string $v): self { $this->footerDisclaimer = $v; return $this; }
public function setSocialMedia(?array $v): self { $this->socialMedia = $v; return $this; }
public function setLogoUrl(?string $v): self { $this->logoUrl = $v; return $this; }
public function toArray(): array
{
$data = [
'id' => $this->id,
'uuid' => $this->uuid,
'bundle' => $this->bundle,
'label' => $this->label,
'status' => $this->status,
'weight' => $this->weight,
];
if ($this->parentId !== null) {
$data['parent_id'] = $this->parentId;
}
if ($this->title !== null) {
$data['title'] = $this->title;
}
if (in_array($this->bundle, ['insurance_type', 'supplementary_insurance'], true)) {
$data['logo_url'] = $this->logoUrl;
}
if ($this->bundle === 'city') {
$data['representation_id'] = $this->representationId;
$data['contact_phone'] = $this->contactPhone;
$data['email'] = $this->email;
$data['description'] = $this->description;
$data['slogan'] = $this->slogan;
$data['domain'] = $this->domain;
$data['keywords'] = $this->keywords;
$data['footer_description'] = $this->footerDescription;
$data['social_media'] = $this->socialMedia;
}
return $data;
}
}
@@ -1,48 +0,0 @@
<?php
namespace App\Category\Repository;
use App\Category\Entity\Category;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
class CategoryRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Category::class);
}
/** @return Category[] */
public function findByBundle(string $bundle, ?int $parentId = null): array
{
$qb = $this->createQueryBuilder('c')
->where('c.bundle = :bundle')
->andWhere('c.status = 1')
->setParameter('bundle', $bundle)
->orderBy('c.weight', 'ASC')
->addOrderBy('c.label', 'ASC');
if ($parentId !== null) {
$qb->andWhere('c.parentId = :parentId')->setParameter('parentId', $parentId);
}
return $qb->getQuery()->getResult();
}
public function save(Category $category, bool $flush = true): void
{
$this->getEntityManager()->persist($category);
if ($flush) {
$this->getEntityManager()->flush();
}
}
public function remove(Category $category, bool $flush = true): void
{
$this->getEntityManager()->remove($category);
if ($flush) {
$this->getEntityManager()->flush();
}
}
}
-99
View File
@@ -1,99 +0,0 @@
<?php
namespace App\Category\Service;
use App\Category\Entity\Category;
use App\Category\Repository\CategoryRepository;
use Psr\Cache\CacheItemPoolInterface;
class CategoryService
{
private const TTL = 3600;
public function __construct(
private readonly CategoryRepository $repository,
private readonly CacheItemPoolInterface $cache,
) {}
/** @return array[] */
public function listByBundle(string $bundle, ?int $parentId = null): array
{
$cacheKey = 'cat_' . $bundle . ($parentId !== null ? '_p' . $parentId : '');
$item = $this->cache->getItem($cacheKey);
if ($item->isHit()) {
return $item->get();
}
$rows = array_map(fn(Category $c) => $c->toArray(), $this->repository->findByBundle($bundle, $parentId));
$item->set($rows)->expiresAfter(self::TTL);
$this->cache->save($item);
return $rows;
}
public function create(string $bundle, string $label, array $extra = []): Category
{
$category = new Category($bundle, $label);
$this->applyExtra($category, $extra);
$this->repository->save($category);
$this->invalidate($bundle);
return $category;
}
public function update(Category $category, array $data): Category
{
$bundle = $data['bundle'] ?? $category->getBundle();
if (isset($data['label'])) $category->setLabel($data['label']);
if (isset($data['status'])) $category->setStatus((int) $data['status']);
if (isset($data['weight'])) $category->setWeight((int) $data['weight']);
if (isset($data['title'])) $category->setTitle($data['title']);
if (isset($data['bundle'])) $category->setBundle($data['bundle']);
if (array_key_exists('parent_id', $data)) $category->setParentId($data['parent_id']);
$this->applyExtra($category, $data);
$this->repository->save($category);
$this->invalidate($bundle);
$this->invalidate($category->getBundle());
return $category;
}
public function delete(Category $category): void
{
$bundle = $category->getBundle();
$this->repository->remove($category);
$this->invalidate($bundle);
}
private function applyExtra(Category $category, array $data): void
{
if (array_key_exists('parent_id', $data)) $category->setParentId($data['parent_id']);
if (array_key_exists('weight', $data)) $category->setWeight((int) $data['weight']);
if (array_key_exists('logo_id', $data)) $category->setLogoId($data['logo_id']);
if (array_key_exists('title', $data)) $category->setTitle($data['title']);
if (array_key_exists('contact_phone', $data)) $category->setContactPhone($data['contact_phone']);
if (array_key_exists('email', $data)) $category->setEmail($data['email']);
if (array_key_exists('description', $data)) $category->setDescription($data['description']);
if (array_key_exists('slogan', $data)) $category->setSlogan($data['slogan']);
if (array_key_exists('domain', $data)) $category->setDomain($data['domain']);
if (array_key_exists('keywords', $data)) $category->setKeywords($data['keywords']);
if (array_key_exists('footer_description', $data)) $category->setFooterDescription($data['footer_description']);
if (array_key_exists('footer_disclaimer', $data)) $category->setFooterDisclaimer($data['footer_disclaimer']);
if (array_key_exists('social_media', $data)) $category->setSocialMedia($data['social_media']);
if (array_key_exists('representation_id', $data)) $category->setRepresentationId($data['representation_id'] !== null ? (int) $data['representation_id'] : null);
if (array_key_exists('logo_url', $data)) $category->setLogoUrl($data['logo_url']);
}
private function invalidate(string $bundle): void
{
$this->cache->deleteItem('cat_' . $bundle);
// Also delete any parent-filtered variants
foreach (range(1, 50) as $id) {
$this->cache->deleteItem('cat_' . $bundle . '_p' . $id);
}
}
}