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:
hamed
2026-07-13 16:01:16 +03:30
co-authored by Claude Opus 4.8
parent 61981a45d0
commit 665a210ef8
8 changed files with 474 additions and 2 deletions
@@ -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'])]