feat(patients): phase D — call-center tab (patient call log)
Add a record-scoped PatientCall entity (subject, summary, outcome
success/missed, called_at, personnel) with its repository and three
owner-gated endpoints on PatientController:
GET /patient/{uuid}/calls — call log, newest first, optional ?outcome
POST /patient/{uuid}/call — log a call (subject required)
DELETE /patient/call/{uuid} — delete an entry
Wire the previously-placeholder "کال سنتر" tab as a CallCenterTab: a register
form (date/time/subject/summary + success/missed toggle, personnel taken from
the logged-in user) beside a filterable call history (all / success / missed).
With this every patient-detail tab is now backed by a real endpoint, so the
generic Placeholder is no longer reachable. PatientCallTest covers create/list/
delete, the outcome filter, the invalid-outcome fallback, and ownership scoping.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -53,6 +53,7 @@ class PatientController extends BaseController
|
||||
private readonly \App\Patient\Repository\PatientAttachmentRepository $attachmentRepo,
|
||||
private readonly \App\Patient\Repository\PatientMedicalRecordRepository $medicalRepo,
|
||||
private readonly \App\Patient\Repository\PatientMessageRepository $messageRepo,
|
||||
private readonly \App\Patient\Repository\PatientCallRepository $callRepo,
|
||||
private readonly \App\Shared\Service\FileUploadService $fileUpload,
|
||||
private readonly \App\Payment\Repository\PaymentRepository $paymentRepo,
|
||||
private readonly \App\Settlement\Repository\WalletTransactionRepository $walletRepo,
|
||||
@@ -133,6 +134,78 @@ class PatientController extends BaseController
|
||||
return $this->paginated($txns, $this->walletRepo->countByUser($patient), $page, $limit);
|
||||
}
|
||||
|
||||
// ── Call center (کال سنتر) ─────────────────────────────────────────────────
|
||||
|
||||
/** List the patient's call log (newest first, optional ?outcome=success|missed). */
|
||||
#[Route('/api/v1/patient/{uuid}/calls', methods: ['GET'])]
|
||||
public function listCalls(string $uuid, Request $request, #[CurrentUser] User $user): JsonResponse
|
||||
{
|
||||
[$entityType, $entityId] = $this->resolveEntity($user);
|
||||
$record = $this->recordRepo->findByUuid($uuid);
|
||||
if ($record === null || !$this->ownsRecord($record, $entityType, $entityId)) {
|
||||
return $this->error(ErrorCodes::ERR_PATIENT_NOT_FOUND, ErrorCodes::message(ErrorCodes::ERR_PATIENT_NOT_FOUND), 404);
|
||||
}
|
||||
|
||||
$outcome = $request->query->get('outcome');
|
||||
$outcome = in_array($outcome, ['success', 'missed'], true) ? $outcome : null;
|
||||
|
||||
return $this->success(array_map(
|
||||
fn(\App\Patient\Entity\PatientCall $c) => $c->toArray(),
|
||||
$this->callRepo->findByRecord($record, $outcome)
|
||||
));
|
||||
}
|
||||
|
||||
/** Log a new call. `subject` required; `outcome` defaults to success; `personnel`/`summary`/`called_at` optional. */
|
||||
#[Route('/api/v1/patient/{uuid}/call', methods: ['POST'])]
|
||||
public function createCall(string $uuid, Request $request, #[CurrentUser] User $user): JsonResponse
|
||||
{
|
||||
[$entityType, $entityId] = $this->resolveEntity($user);
|
||||
$record = $this->recordRepo->findByUuid($uuid);
|
||||
if ($record === null || !$this->ownsRecord($record, $entityType, $entityId)) {
|
||||
return $this->error(ErrorCodes::ERR_PATIENT_NOT_FOUND, ErrorCodes::message(ErrorCodes::ERR_PATIENT_NOT_FOUND), 404);
|
||||
}
|
||||
|
||||
$data = json_decode($request->getContent(), true) ?? [];
|
||||
$subject = trim((string) ($data['subject'] ?? ''));
|
||||
if ($subject === '') {
|
||||
return $this->error(ErrorCodes::ERR_VALIDATION_001, 'موضوع تماس الزامی است', 422, 'subject');
|
||||
}
|
||||
|
||||
$outcome = (string) ($data['outcome'] ?? 'success');
|
||||
if (!in_array($outcome, ['success', 'missed'], true)) {
|
||||
$outcome = 'success';
|
||||
}
|
||||
$calledAt = isset($data['called_at']) ? (int) $data['called_at'] : null;
|
||||
|
||||
$call = new \App\Patient\Entity\PatientCall($record, $subject, $outcome, $calledAt);
|
||||
$summary = trim((string) ($data['summary'] ?? ''));
|
||||
if ($summary !== '') {
|
||||
$call->setSummary($summary);
|
||||
}
|
||||
$personnel = trim((string) ($data['personnel'] ?? ''));
|
||||
if ($personnel !== '') {
|
||||
$call->setPersonnel($personnel);
|
||||
}
|
||||
$this->callRepo->save($call);
|
||||
|
||||
return $this->success($call->toArray(), 201);
|
||||
}
|
||||
|
||||
/** Delete a call log entry. Owner-scoped; otherwise 404. */
|
||||
#[Route('/api/v1/patient/call/{uuid}', methods: ['DELETE'])]
|
||||
public function deleteCall(string $uuid, #[CurrentUser] User $user): JsonResponse
|
||||
{
|
||||
[$entityType, $entityId] = $this->resolveEntity($user);
|
||||
$call = $this->callRepo->findByUuid($uuid);
|
||||
if ($call === null || !$this->ownsRecord($call->getRecord(), $entityType, $entityId)) {
|
||||
return $this->error(ErrorCodes::ERR_PATIENT_NOT_FOUND, ErrorCodes::message(ErrorCodes::ERR_PATIENT_NOT_FOUND), 404);
|
||||
}
|
||||
|
||||
$this->callRepo->remove($call);
|
||||
|
||||
return $this->success(['deleted' => true]);
|
||||
}
|
||||
|
||||
// ── Messages (پیامها) ─────────────────────────────────────────────────────
|
||||
|
||||
#[Route('/api/v1/patient/{uuid}/messages', methods: ['GET'])]
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
<?php
|
||||
|
||||
namespace App\Patient\Entity;
|
||||
|
||||
use App\Patient\Repository\PatientCallRepository;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
use Symfony\Component\Uid\Uuid;
|
||||
|
||||
/** A logged phone call with a patient (the «کال سنتر» tab). */
|
||||
#[ORM\Entity(repositoryClass: PatientCallRepository::class)]
|
||||
#[ORM\Table(name: 'patient_calls')]
|
||||
#[ORM\Index(columns: ['record_id'], name: 'idx_patient_calls_record')]
|
||||
class PatientCall
|
||||
{
|
||||
#[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: PatientRecord::class)]
|
||||
#[ORM\JoinColumn(name: 'record_id', nullable: false, onDelete: 'CASCADE')]
|
||||
private PatientRecord $record;
|
||||
|
||||
/** موضوع تماس — short subject (e.g. «پیگیری نوبت»). */
|
||||
#[ORM\Column(type: 'string', length: 255)]
|
||||
private string $subject;
|
||||
|
||||
/** خلاصه تماس — free-text call summary. */
|
||||
#[ORM\Column(type: 'text', nullable: true)]
|
||||
private ?string $summary = null;
|
||||
|
||||
/** Call outcome: success | missed. */
|
||||
#[ORM\Column(type: 'string', length: 10)]
|
||||
private string $outcome = 'success';
|
||||
|
||||
/** When the call happened (Unix seconds); may differ from createdAt. */
|
||||
#[ORM\Column(name: 'called_at', type: 'integer')]
|
||||
private int $calledAt;
|
||||
|
||||
/** Display name of the staff member who logged the call. */
|
||||
#[ORM\Column(type: 'string', length: 120, nullable: true)]
|
||||
private ?string $personnel = null;
|
||||
|
||||
#[ORM\Column(name: 'created_at', type: 'integer')]
|
||||
private int $createdAt;
|
||||
|
||||
public function __construct(PatientRecord $record, string $subject, string $outcome = 'success', ?int $calledAt = null)
|
||||
{
|
||||
$this->uuid = Uuid::v4()->toRfc4122();
|
||||
$this->record = $record;
|
||||
$this->subject = $subject;
|
||||
$this->outcome = $outcome;
|
||||
$this->calledAt = $calledAt ?? time();
|
||||
$this->createdAt = time();
|
||||
}
|
||||
|
||||
public function getId(): ?int { return $this->id; }
|
||||
public function getUuid(): string { return $this->uuid; }
|
||||
public function getRecord(): PatientRecord { return $this->record; }
|
||||
|
||||
public function setSummary(?string $s): self { $this->summary = $s; return $this; }
|
||||
public function setPersonnel(?string $p): self { $this->personnel = $p; return $this; }
|
||||
|
||||
public function toArray(): array
|
||||
{
|
||||
return [
|
||||
'uuid' => $this->uuid,
|
||||
'subject' => $this->subject,
|
||||
'summary' => $this->summary,
|
||||
'outcome' => $this->outcome,
|
||||
'called_at' => $this->calledAt,
|
||||
'personnel' => $this->personnel,
|
||||
'created_at' => $this->createdAt,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
namespace App\Patient\Repository;
|
||||
|
||||
use App\Patient\Entity\PatientCall;
|
||||
use App\Patient\Entity\PatientRecord;
|
||||
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
|
||||
use Doctrine\Persistence\ManagerRegistry;
|
||||
|
||||
class PatientCallRepository extends ServiceEntityRepository
|
||||
{
|
||||
public function __construct(ManagerRegistry $registry)
|
||||
{
|
||||
parent::__construct($registry, PatientCall::class);
|
||||
}
|
||||
|
||||
public function findByUuid(string $uuid): ?PatientCall
|
||||
{
|
||||
return $this->findOneBy(['uuid' => $uuid]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Newest-first call log for a record, optionally filtered by outcome.
|
||||
*
|
||||
* @return PatientCall[]
|
||||
*/
|
||||
public function findByRecord(PatientRecord $record, ?string $outcome = null): array
|
||||
{
|
||||
$qb = $this->createQueryBuilder('c')
|
||||
->where('c.record = :record')
|
||||
->setParameter('record', $record)
|
||||
->orderBy('c.calledAt', 'DESC')
|
||||
->addOrderBy('c.id', 'DESC');
|
||||
|
||||
if ($outcome !== null) {
|
||||
$qb->andWhere('c.outcome = :outcome')->setParameter('outcome', $outcome);
|
||||
}
|
||||
|
||||
return $qb->getQuery()->getResult();
|
||||
}
|
||||
|
||||
public function save(PatientCall $c): void
|
||||
{
|
||||
$this->getEntityManager()->persist($c);
|
||||
$this->getEntityManager()->flush();
|
||||
}
|
||||
|
||||
public function remove(PatientCall $c): void
|
||||
{
|
||||
$this->getEntityManager()->remove($c);
|
||||
$this->getEntityManager()->flush();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user