feat: implement staff management and subscription system

- Added StaffController for managing clinic staff, including listing, creating, updating, and toggling staff status.
- Created ClinicStaff entity and repository for staff data handling.
- Developed SubscriptionController to manage subscription plans and periods, including trial subscriptions.
- Introduced SubscriptionPlan, SubscriptionPeriod, and ClinicSubscription entities for subscription management.
- Implemented SubscriptionService for handling subscription logic, including trial activation and subscription creation from payments.
- Added necessary repositories for subscription entities to facilitate data access and manipulation.
This commit is contained in:
hamed
2026-06-14 22:10:28 +03:30
parent dcd631f503
commit b0244f28f5
53 changed files with 4434 additions and 40 deletions
+140
View File
@@ -0,0 +1,140 @@
<?php
namespace App\Staff\Controller;
use App\Auth\Entity\User;
use App\Clinic\Repository\ClinicRepository;
use App\Doctor\Repository\DoctorRepository;
use App\Shared\Constant\ErrorCodes;
use App\Shared\Controller\BaseController;
use App\Staff\Entity\ClinicStaff;
use App\Staff\Repository\ClinicStaffRepository;
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;
#[IsGranted('IS_AUTHENTICATED_FULLY')]
class StaffController extends BaseController
{
public function __construct(
private readonly ClinicStaffRepository $staffRepo,
private readonly DoctorRepository $doctorRepo,
private readonly ClinicRepository $clinicRepo,
) {}
#[Route('/api/v1/staff', methods: ['GET'])]
public function list(#[CurrentUser] User $user): JsonResponse
{
[$entityType, $entityId] = $this->resolveEntity($user);
if ($entityId === null) {
return $this->error(ErrorCodes::ERR_FORBIDDEN_001, 'پروفایل یافت نشد', 403);
}
$staff = array_map(
fn(ClinicStaff $s) => $s->toArray(),
$this->staffRepo->findByEntity($entityType, $entityId)
);
return $this->success($staff);
}
#[Route('/api/v1/staff', methods: ['POST'])]
public function create(Request $request, #[CurrentUser] User $user): JsonResponse
{
[$entityType, $entityId] = $this->resolveEntity($user);
if ($entityId === null) {
return $this->error(ErrorCodes::ERR_FORBIDDEN_001, 'پروفایل یافت نشد', 403);
}
$data = json_decode($request->getContent(), true) ?? [];
$fullName = trim($data['full_name'] ?? '');
if ($fullName === '') {
return $this->error(ErrorCodes::ERR_VALIDATION_001, 'full_name الزامی است', 422);
}
$staff = new ClinicStaff($entityType, $entityId, $fullName);
$staff->setPhone($data['phone'] ?? null);
$staff->setJobTitle($data['job_title'] ?? null);
$staff->setAddress($data['address'] ?? null);
$staff->setNationalCode($data['national_code'] ?? null);
$this->staffRepo->save($staff);
return $this->success($staff->toArray(), 201);
}
#[Route('/api/v1/staff/{uuid}', methods: ['PATCH'])]
public function update(string $uuid, Request $request, #[CurrentUser] User $user): JsonResponse
{
$staff = $this->staffRepo->findByUuid($uuid);
if ($staff === null) {
return $this->error(ErrorCodes::ERR_STAFF_NOT_FOUND, ErrorCodes::message(ErrorCodes::ERR_STAFF_NOT_FOUND), 404);
}
if (!$this->ownsStaff($staff, $user)) {
return $this->error(ErrorCodes::ERR_FORBIDDEN_001, ErrorCodes::message(ErrorCodes::ERR_FORBIDDEN_001), 403);
}
$data = json_decode($request->getContent(), true) ?? [];
if (isset($data['full_name']) && trim($data['full_name']) !== '') {
$staff->setFullName(trim($data['full_name']));
}
if (array_key_exists('phone', $data)) { $staff->setPhone($data['phone']); }
if (array_key_exists('job_title', $data)) { $staff->setJobTitle($data['job_title']); }
if (array_key_exists('address', $data)) { $staff->setAddress($data['address']); }
if (array_key_exists('national_code', $data)){ $staff->setNationalCode($data['national_code']); }
$this->staffRepo->save($staff);
return $this->success($staff->toArray());
}
#[Route('/api/v1/staff/{uuid}/toggle', methods: ['PATCH'])]
public function toggle(string $uuid, #[CurrentUser] User $user): JsonResponse
{
$staff = $this->staffRepo->findByUuid($uuid);
if ($staff === null) {
return $this->error(ErrorCodes::ERR_STAFF_NOT_FOUND, ErrorCodes::message(ErrorCodes::ERR_STAFF_NOT_FOUND), 404);
}
if (!$this->ownsStaff($staff, $user)) {
return $this->error(ErrorCodes::ERR_FORBIDDEN_001, ErrorCodes::message(ErrorCodes::ERR_FORBIDDEN_001), 403);
}
$staff->toggleActive();
$this->staffRepo->save($staff);
return $this->success($staff->toArray());
}
private function resolveEntity(User $user): array
{
if ($user->hasRole('ROLE_DOCTOR')) {
$doctor = $this->doctorRepo->findByUser($user);
return $doctor !== null ? ['doctor', $doctor->getId()] : ['doctor', null];
}
if ($user->hasRole('ROLE_CLINIC')) {
$clinic = $this->clinicRepo->findByUser($user);
return $clinic !== null ? ['clinic', $clinic->getId()] : ['clinic', null];
}
return ['unknown', null];
}
private function ownsStaff(ClinicStaff $staff, User $user): bool
{
if ($user->hasRole('ROLE_ADMIN')) {
return true;
}
[$entityType, $entityId] = $this->resolveEntity($user);
return $entityId !== null
&& $staff->getEntityType() === $entityType
&& $staff->getEntityId() === $entityId;
}
}
+105
View File
@@ -0,0 +1,105 @@
<?php
namespace App\Staff\Entity;
use App\Staff\Repository\ClinicStaffRepository;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Uid\Uuid;
#[ORM\Entity(repositoryClass: ClinicStaffRepository::class)]
#[ORM\Table(name: 'clinic_staff')]
#[ORM\Index(columns: ['entity_type', 'entity_id', 'active'], name: 'idx_staff_entity_active')]
class ClinicStaff
{
#[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(name: 'entity_type', type: 'string', length: 10)]
private string $entityType;
#[ORM\Column(name: 'entity_id', type: 'integer')]
private int $entityId;
#[ORM\Column(name: 'full_name', type: 'string', length: 200)]
private string $fullName;
#[ORM\Column(type: 'string', length: 20, nullable: true)]
private ?string $phone = null;
#[ORM\Column(name: 'job_title', type: 'string', length: 100, nullable: true)]
private ?string $jobTitle = null;
#[ORM\Column(type: 'text', nullable: true)]
private ?string $address = null;
#[ORM\Column(name: 'national_code', type: 'string', length: 10, nullable: true)]
private ?string $nationalCode = null;
#[ORM\Column(type: 'boolean')]
private bool $active = true;
#[ORM\Column(name: 'created_at', type: 'integer')]
private int $createdAt;
#[ORM\Column(name: 'updated_at', type: 'integer')]
private int $updatedAt;
public function __construct(string $entityType, int $entityId, string $fullName)
{
$this->uuid = Uuid::v4()->toRfc4122();
$this->entityType = $entityType;
$this->entityId = $entityId;
$this->fullName = $fullName;
$this->createdAt = time();
$this->updatedAt = time();
}
public function getId(): ?int { return $this->id; }
public function getUuid(): string { return $this->uuid; }
public function getEntityType(): string { return $this->entityType; }
public function getEntityId(): int { return $this->entityId; }
public function getFullName(): string { return $this->fullName; }
public function getPhone(): ?string { return $this->phone; }
public function getJobTitle(): ?string { return $this->jobTitle; }
public function getAddress(): ?string { return $this->address; }
public function getNationalCode(): ?string { return $this->nationalCode; }
public function isActive(): bool { return $this->active; }
public function getCreatedAt(): int { return $this->createdAt; }
public function getUpdatedAt(): int { return $this->updatedAt; }
public function setFullName(string $fullName): self { $this->fullName = $fullName; $this->updatedAt = time(); return $this; }
public function setPhone(?string $phone): self { $this->phone = $phone; $this->updatedAt = time(); return $this; }
public function setJobTitle(?string $jobTitle): self { $this->jobTitle = $jobTitle; $this->updatedAt = time(); return $this; }
public function setAddress(?string $address): self { $this->address = $address; $this->updatedAt = time(); return $this; }
public function setNationalCode(?string $code): self { $this->nationalCode = $code; $this->updatedAt = time(); return $this; }
public function setActive(bool $active): self { $this->active = $active; $this->updatedAt = time(); return $this; }
public function toggleActive(): self
{
$this->active = !$this->active;
$this->updatedAt = time();
return $this;
}
public function toArray(): array
{
return [
'uuid' => $this->uuid,
'entity_type' => $this->entityType,
'entity_id' => $this->entityId,
'full_name' => $this->fullName,
'phone' => $this->phone,
'job_title' => $this->jobTitle,
'address' => $this->address,
'national_code' => $this->nationalCode,
'active' => $this->active,
'created_at' => $this->createdAt,
'updated_at' => $this->updatedAt,
];
}
}
@@ -0,0 +1,42 @@
<?php
namespace App\Staff\Repository;
use App\Staff\Entity\ClinicStaff;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
class ClinicStaffRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, ClinicStaff::class);
}
public function findByUuid(string $uuid): ?ClinicStaff
{
return $this->findOneBy(['uuid' => $uuid]);
}
public function findByEntity(string $entityType, int $entityId, bool $activeOnly = false): array
{
$qb = $this->createQueryBuilder('s')
->where('s.entityType = :type')
->andWhere('s.entityId = :id')
->setParameter('type', $entityType)
->setParameter('id', $entityId)
->orderBy('s.fullName', 'ASC');
if ($activeOnly) {
$qb->andWhere('s.active = true');
}
return $qb->getQuery()->getResult();
}
public function save(ClinicStaff $staff): void
{
$this->getEntityManager()->persist($staff);
$this->getEntityManager()->flush();
}
}