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,180 @@
|
||||
<?php
|
||||
|
||||
namespace App\Secretary\Controller;
|
||||
|
||||
use App\Auth\Entity\User;
|
||||
use App\Auth\Repository\UserRepository;
|
||||
use App\Doctor\Repository\DoctorRepository;
|
||||
use App\Secretary\Entity\DoctorSecretary;
|
||||
use App\Secretary\Repository\DoctorSecretaryRepository;
|
||||
use App\Shared\Constant\ErrorCodes;
|
||||
use App\Shared\Controller\BaseController;
|
||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
|
||||
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 SecretaryController extends BaseController
|
||||
{
|
||||
// TODO: link to subscription plan (Task 15). Basic plan = 1, advanced = 3
|
||||
private const MAX_SECRETARIES = 1;
|
||||
|
||||
public function __construct(
|
||||
private readonly DoctorSecretaryRepository $secretaryRepo,
|
||||
private readonly DoctorRepository $doctorRepo,
|
||||
private readonly UserRepository $userRepo,
|
||||
private readonly UserPasswordHasherInterface $hasher,
|
||||
) {}
|
||||
|
||||
#[Route('/api/v1/secretary', methods: ['POST'])]
|
||||
public function create(Request $request, #[CurrentUser] User $currentUser): JsonResponse
|
||||
{
|
||||
$data = json_decode($request->getContent(), true) ?? [];
|
||||
$doctorUuid = trim($data['doctor_uuid'] ?? '');
|
||||
$mobile = trim($data['mobile_number'] ?? '');
|
||||
|
||||
if (empty($doctorUuid) || empty($mobile)) {
|
||||
return $this->error(ErrorCodes::ERR_VALIDATION_001, 'doctor_uuid و mobile_number الزامی است', 422);
|
||||
}
|
||||
|
||||
$doctor = $this->doctorRepo->findByUuid($doctorUuid);
|
||||
if ($doctor === null) {
|
||||
return $this->error(ErrorCodes::ERR_VALIDATION_002, 'دکتر یافت نشد', 404);
|
||||
}
|
||||
|
||||
// Only the doctor owner or admin can create secretary
|
||||
if ($doctor->getUser()->getId() !== $currentUser->getId() && !$currentUser->hasRole('ROLE_ADMIN')) {
|
||||
return $this->error(ErrorCodes::ERR_AUTH_006, 'دسترسی ممنوع', 403);
|
||||
}
|
||||
|
||||
// Check plan limit
|
||||
$activeCount = $this->secretaryRepo->countActiveByDoctor($doctor);
|
||||
if ($activeCount >= self::MAX_SECRETARIES) {
|
||||
return $this->error(ErrorCodes::ERR_SECRETARY_001, ErrorCodes::message(ErrorCodes::ERR_SECRETARY_001), 422);
|
||||
}
|
||||
|
||||
// Find or create secretary user
|
||||
$secretaryUser = $this->userRepo->findByMobile($mobile);
|
||||
if ($secretaryUser === null) {
|
||||
$secretaryUser = new User($mobile);
|
||||
// Set a temporary password if provided
|
||||
if (!empty($data['password'])) {
|
||||
$hash = $this->hasher->hashPassword($secretaryUser, $data['password']);
|
||||
$secretaryUser->setPasswordHash($hash);
|
||||
}
|
||||
}
|
||||
|
||||
// Assign ROLE_SECRETARY
|
||||
$roles = $secretaryUser->getRoles();
|
||||
if (!in_array('ROLE_SECRETARY', $roles, true)) {
|
||||
$roles[] = 'ROLE_SECRETARY';
|
||||
$secretaryUser->setRoles(array_values(array_unique($roles)));
|
||||
}
|
||||
$this->userRepo->save($secretaryUser);
|
||||
|
||||
// Check duplicate
|
||||
$existing = $this->secretaryRepo->findOneBy(['doctor' => $doctor, 'secretary' => $secretaryUser]);
|
||||
if ($existing !== null) {
|
||||
return $this->error(ErrorCodes::ERR_VALIDATION_001, 'این منشی قبلاً اضافه شده است', 409);
|
||||
}
|
||||
|
||||
$secretary = new DoctorSecretary($doctor, $secretaryUser);
|
||||
|
||||
// Apply custom permissions if provided
|
||||
if (!empty($data['permissions'])) {
|
||||
$secretary->mergePermissions($data['permissions']);
|
||||
}
|
||||
|
||||
$this->secretaryRepo->save($secretary);
|
||||
|
||||
return $this->success(['data' => $secretary->toArray()], 201);
|
||||
}
|
||||
|
||||
#[Route('/api/v1/secretary/{uuid}', methods: ['GET'])]
|
||||
public function show(string $uuid, #[CurrentUser] User $currentUser): JsonResponse
|
||||
{
|
||||
$secretary = $this->secretaryRepo->findByUuid($uuid);
|
||||
if ($secretary === null) {
|
||||
return $this->error(ErrorCodes::ERR_VALIDATION_002, 'منشی یافت نشد', 404);
|
||||
}
|
||||
|
||||
if (!$this->canManage($secretary, $currentUser)) {
|
||||
return $this->error(ErrorCodes::ERR_AUTH_006, 'دسترسی ممنوع', 403);
|
||||
}
|
||||
|
||||
return $this->success(['data' => $secretary->toArray()]);
|
||||
}
|
||||
|
||||
#[Route('/api/v1/secretary/{uuid}', methods: ['PATCH'])]
|
||||
public function update(string $uuid, Request $request, #[CurrentUser] User $currentUser): JsonResponse
|
||||
{
|
||||
$secretary = $this->secretaryRepo->findByUuid($uuid);
|
||||
if ($secretary === null) {
|
||||
return $this->error(ErrorCodes::ERR_VALIDATION_002, 'منشی یافت نشد', 404);
|
||||
}
|
||||
|
||||
if (!$this->canManage($secretary, $currentUser)) {
|
||||
return $this->error(ErrorCodes::ERR_AUTH_006, 'دسترسی ممنوع', 403);
|
||||
}
|
||||
|
||||
$data = json_decode($request->getContent(), true) ?? [];
|
||||
|
||||
if (array_key_exists('active', $data)) {
|
||||
$secretary->setActive((bool) $data['active']);
|
||||
}
|
||||
|
||||
if (!empty($data['permissions'])) {
|
||||
$secretary->mergePermissions($data['permissions']);
|
||||
}
|
||||
|
||||
$this->secretaryRepo->save($secretary);
|
||||
|
||||
return $this->success(['data' => $secretary->toArray()]);
|
||||
}
|
||||
|
||||
#[Route('/api/v1/secretary/{uuid}', methods: ['DELETE'])]
|
||||
public function delete(string $uuid, #[CurrentUser] User $currentUser): JsonResponse
|
||||
{
|
||||
$secretary = $this->secretaryRepo->findByUuid($uuid);
|
||||
if ($secretary === null) {
|
||||
return $this->error(ErrorCodes::ERR_VALIDATION_002, 'منشی یافت نشد', 404);
|
||||
}
|
||||
|
||||
if (!$this->canManage($secretary, $currentUser)) {
|
||||
return $this->error(ErrorCodes::ERR_AUTH_006, 'دسترسی ممنوع', 403);
|
||||
}
|
||||
|
||||
$this->secretaryRepo->remove($secretary);
|
||||
|
||||
return $this->success(['message' => 'منشی با موفقیت حذف شد']);
|
||||
}
|
||||
|
||||
#[Route('/api/v1/secretaries/{doctorUuid}', methods: ['GET'])]
|
||||
public function list(string $doctorUuid, #[CurrentUser] User $currentUser): JsonResponse
|
||||
{
|
||||
$doctor = $this->doctorRepo->findByUuid($doctorUuid);
|
||||
if ($doctor === null) {
|
||||
return $this->error(ErrorCodes::ERR_VALIDATION_002, 'دکتر یافت نشد', 404);
|
||||
}
|
||||
|
||||
if ($doctor->getUser()->getId() !== $currentUser->getId() && !$currentUser->hasRole('ROLE_ADMIN')) {
|
||||
return $this->error(ErrorCodes::ERR_AUTH_006, 'دسترسی ممنوع', 403);
|
||||
}
|
||||
|
||||
$secretaries = array_map(
|
||||
fn(DoctorSecretary $s) => $s->toArray(),
|
||||
$this->secretaryRepo->findByDoctor($doctor)
|
||||
);
|
||||
|
||||
return $this->success(['data' => $secretaries]);
|
||||
}
|
||||
|
||||
private function canManage(DoctorSecretary $secretary, User $user): bool
|
||||
{
|
||||
return $secretary->getDoctor()->getUser()->getId() === $user->getId()
|
||||
|| $user->hasRole('ROLE_ADMIN');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
<?php
|
||||
|
||||
namespace App\Secretary\Entity;
|
||||
|
||||
use App\Auth\Entity\User;
|
||||
use App\Doctor\Entity\Doctor;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
use Symfony\Component\Uid\Uuid;
|
||||
|
||||
#[ORM\Entity]
|
||||
#[ORM\Table(name: 'doctor_secretaries')]
|
||||
#[ORM\UniqueConstraint(name: 'idx_doctor_secretaries_pair', columns: ['doctor_id', 'secretary_id'])]
|
||||
class DoctorSecretary
|
||||
{
|
||||
public const DEFAULT_PERMISSIONS = [
|
||||
'version' => 1,
|
||||
'resources' => [
|
||||
'appointments' => ['view' => true, 'create' => true, 'cancel' => false, 'update_status' => true],
|
||||
'addresses' => ['view' => true, 'create' => false, 'update' => false, 'delete' => false],
|
||||
'clinic_info' => ['view' => true, 'update' => false],
|
||||
'insurances' => ['view' => true, 'create' => false, 'update' => false, 'delete' => false],
|
||||
],
|
||||
];
|
||||
|
||||
#[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)]
|
||||
#[ORM\JoinColumn(name: 'doctor_id', referencedColumnName: 'id', nullable: false, onDelete: 'CASCADE')]
|
||||
private Doctor $doctor;
|
||||
|
||||
#[ORM\ManyToOne(targetEntity: User::class)]
|
||||
#[ORM\JoinColumn(name: 'secretary_id', referencedColumnName: 'id', nullable: false)]
|
||||
private User $secretary;
|
||||
|
||||
#[ORM\Column(name: 'permission', type: 'json', nullable: true)]
|
||||
private ?array $permissions = 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(Doctor $doctor, User $secretary)
|
||||
{
|
||||
$this->uuid = Uuid::v4()->toRfc4122();
|
||||
$this->doctor = $doctor;
|
||||
$this->secretary = $secretary;
|
||||
$this->permissions = self::DEFAULT_PERMISSIONS;
|
||||
$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 getSecretary(): User { return $this->secretary; }
|
||||
public function getPermissions(): array { return $this->permissions ?? self::DEFAULT_PERMISSIONS; }
|
||||
public function isActive(): bool { return $this->active; }
|
||||
public function getCreatedAt(): int { return $this->createdAt; }
|
||||
|
||||
public function setActive(bool $v): self { $this->active = $v; $this->touch(); return $this; }
|
||||
public function setPermissions(array $v): self { $this->permissions = $v; $this->touch(); return $this; }
|
||||
|
||||
/** Deep merge: only provided resources/actions are updated */
|
||||
public function mergePermissions(array $patch): void
|
||||
{
|
||||
$current = $this->getPermissions();
|
||||
|
||||
if (isset($patch['resources']) && is_array($patch['resources'])) {
|
||||
foreach ($patch['resources'] as $resource => $actions) {
|
||||
if (!is_array($actions)) continue;
|
||||
foreach ($actions as $action => $value) {
|
||||
$current['resources'][$resource][$action] = (bool) $value;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (isset($patch['version'])) {
|
||||
$current['version'] = (int) $patch['version'];
|
||||
}
|
||||
|
||||
$this->permissions = $current;
|
||||
$this->touch();
|
||||
}
|
||||
|
||||
private function touch(): void { $this->updatedAt = time(); }
|
||||
|
||||
public function toArray(): array
|
||||
{
|
||||
return [
|
||||
'uuid' => $this->uuid,
|
||||
'user' => [
|
||||
'uuid' => $this->secretary->getUuid(),
|
||||
'realname' => $this->secretary->getRealName(),
|
||||
'mobile' => $this->secretary->getMobileNumber(),
|
||||
'picture' => null,
|
||||
],
|
||||
'doctor' => [
|
||||
'uuid' => $this->doctor->getUuid(),
|
||||
'name' => $this->doctor->getName(),
|
||||
],
|
||||
'active' => $this->active,
|
||||
'permissions' => $this->getPermissions(),
|
||||
'created_at' => $this->createdAt,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
namespace App\Secretary\Repository;
|
||||
|
||||
use App\Doctor\Entity\Doctor;
|
||||
use App\Secretary\Entity\DoctorSecretary;
|
||||
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
|
||||
use Doctrine\Persistence\ManagerRegistry;
|
||||
|
||||
class DoctorSecretaryRepository extends ServiceEntityRepository
|
||||
{
|
||||
public function __construct(ManagerRegistry $registry)
|
||||
{
|
||||
parent::__construct($registry, DoctorSecretary::class);
|
||||
}
|
||||
|
||||
public function findByUuid(string $uuid): ?DoctorSecretary
|
||||
{
|
||||
return $this->findOneBy(['uuid' => $uuid]);
|
||||
}
|
||||
|
||||
public function countActiveByDoctor(Doctor $doctor): int
|
||||
{
|
||||
return (int) $this->createQueryBuilder('s')
|
||||
->select('COUNT(s.id)')
|
||||
->where('s.doctor = :doctor')
|
||||
->andWhere('s.active = true')
|
||||
->setParameter('doctor', $doctor)
|
||||
->getQuery()
|
||||
->getSingleScalarResult();
|
||||
}
|
||||
|
||||
/** @return DoctorSecretary[] */
|
||||
public function findByDoctor(Doctor $doctor): array
|
||||
{
|
||||
return $this->findBy(['doctor' => $doctor], ['createdAt' => 'DESC']);
|
||||
}
|
||||
|
||||
public function save(DoctorSecretary $entity, bool $flush = true): void
|
||||
{
|
||||
$this->getEntityManager()->persist($entity);
|
||||
if ($flush) {
|
||||
$this->getEntityManager()->flush();
|
||||
}
|
||||
}
|
||||
|
||||
public function remove(DoctorSecretary $entity, bool $flush = true): void
|
||||
{
|
||||
$this->getEntityManager()->remove($entity);
|
||||
if ($flush) {
|
||||
$this->getEntityManager()->flush();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace App\Secretary\Security;
|
||||
|
||||
use App\Secretary\Entity\DoctorSecretary;
|
||||
|
||||
class SecretaryPermissionChecker
|
||||
{
|
||||
public function can(DoctorSecretary $secretary, string $resource, string $action): bool
|
||||
{
|
||||
if (!$secretary->isActive()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$permissions = $secretary->getPermissions();
|
||||
|
||||
return (bool) ($permissions['resources'][$resource][$action] ?? false);
|
||||
}
|
||||
|
||||
public function canAll(DoctorSecretary $secretary, string $resource, array $actions): bool
|
||||
{
|
||||
return array_reduce(
|
||||
$actions,
|
||||
fn(bool $carry, string $action) => $carry && $this->can($secretary, $resource, $action),
|
||||
true
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user