feat: replace patient messages tab with pinnable notes
Port the tauri AddNoteModal notes feature into the admin patient case-file,
replacing the mislabeled «پیامها» (SMS log) tab with «یادداشتها».
Backend (src/Patient):
- PatientNote entity + repository (record-scoped, pinned-first ordering)
- CRUD endpoints on PatientController: GET /notes, POST /note,
PATCH /note/{uuid} (edit body + toggle pin), DELETE /note/{uuid}
- author display name captured server-side from the current user
- migration for patient_notes; docs/api/patient.md updated
Frontend (assets/admin):
- NotesTab: compose box, newest/oldest sort, pinned-first list with
accent rail + pin/edit/delete, edit modal, confirm-delete, empty state
- tab key/label/icon messages -> notes; onAddNote deep-links the notes tab
Tests: PatientNoteTest (create/list-order/edit/pin/delete/validation/ownership),
PatientDetailPage notes cases (render/empty/pin/create).
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\PatientNoteRepository $noteRepo,
|
||||
private readonly \App\Patient\Repository\PatientCallRepository $callRepo,
|
||||
private readonly \App\Shared\Service\FileUploadService $fileUpload,
|
||||
private readonly \App\Payment\Repository\PaymentRepository $paymentRepo,
|
||||
@@ -340,6 +341,87 @@ class PatientController extends BaseController
|
||||
return $this->success(['message' => 'پیام حذف شد']);
|
||||
}
|
||||
|
||||
// ── Notes (یادداشتها) ─────────────────────────────────────────────────────
|
||||
//
|
||||
// Personal staff memos on a record; shared with everyone who owns the record.
|
||||
// The author's display name is captured at write time (survives user deletion).
|
||||
|
||||
#[Route('/api/v1/patient/{uuid}/notes', methods: ['GET'])]
|
||||
public function listNotes(string $uuid, #[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);
|
||||
}
|
||||
|
||||
return $this->success(array_map(
|
||||
fn(\App\Patient\Entity\PatientNote $n) => $n->toArray(),
|
||||
$this->noteRepo->findByRecord($record)
|
||||
));
|
||||
}
|
||||
|
||||
#[Route('/api/v1/patient/{uuid}/note', methods: ['POST'])]
|
||||
public function createNote(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) ?? [];
|
||||
$body = trim((string) ($data['body'] ?? ''));
|
||||
if ($body === '') {
|
||||
return $this->error(ErrorCodes::ERR_VALIDATION_001, 'متن یادداشت الزامی است', 422, 'body');
|
||||
}
|
||||
|
||||
$note = new \App\Patient\Entity\PatientNote($record, $body, (bool) ($data['pinned'] ?? false));
|
||||
$note->setAuthor($user, $user->getRealName() ?? $user->getMobileNumber());
|
||||
$this->noteRepo->save($note);
|
||||
|
||||
return $this->success($note->toArray(), 201);
|
||||
}
|
||||
|
||||
#[Route('/api/v1/patient/note/{uuid}', methods: ['PATCH'])]
|
||||
public function updateNote(string $uuid, Request $request, #[CurrentUser] User $user): JsonResponse
|
||||
{
|
||||
[$entityType, $entityId] = $this->resolveEntity($user);
|
||||
$note = $this->noteRepo->findByUuid($uuid);
|
||||
if ($note === null || !$this->ownsRecord($note->getRecord(), $entityType, $entityId)) {
|
||||
return $this->error(ErrorCodes::ERR_NOT_FOUND_001, 'یادداشت یافت نشد', 404);
|
||||
}
|
||||
|
||||
$data = json_decode($request->getContent(), true) ?? [];
|
||||
if (array_key_exists('body', $data)) {
|
||||
$body = trim((string) $data['body']);
|
||||
if ($body === '') {
|
||||
return $this->error(ErrorCodes::ERR_VALIDATION_001, 'متن یادداشت الزامی است', 422, 'body');
|
||||
}
|
||||
$note->setBody($body);
|
||||
}
|
||||
if (array_key_exists('pinned', $data)) {
|
||||
$note->setPinned((bool) $data['pinned']);
|
||||
}
|
||||
$this->noteRepo->save($note);
|
||||
|
||||
return $this->success($note->toArray());
|
||||
}
|
||||
|
||||
#[Route('/api/v1/patient/note/{uuid}', methods: ['DELETE'])]
|
||||
public function deleteNote(string $uuid, #[CurrentUser] User $user): JsonResponse
|
||||
{
|
||||
[$entityType, $entityId] = $this->resolveEntity($user);
|
||||
$note = $this->noteRepo->findByUuid($uuid);
|
||||
if ($note === null || !$this->ownsRecord($note->getRecord(), $entityType, $entityId)) {
|
||||
return $this->error(ErrorCodes::ERR_NOT_FOUND_001, 'یادداشت یافت نشد', 404);
|
||||
}
|
||||
|
||||
$this->noteRepo->remove($note);
|
||||
|
||||
return $this->success(['message' => 'یادداشت حذف شد']);
|
||||
}
|
||||
|
||||
// ── Medical records (پرونده پزشکی) ────────────────────────────────────────
|
||||
|
||||
#[Route('/api/v1/patient/{uuid}/medical-records', methods: ['GET'])]
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
<?php
|
||||
|
||||
namespace App\Patient\Entity;
|
||||
|
||||
use App\Auth\Entity\User;
|
||||
use App\Patient\Repository\PatientNoteRepository;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
use Symfony\Component\Uid\Uuid;
|
||||
|
||||
/**
|
||||
* A personal, pinnable staff note attached to a patient record (the «یادداشتها» tab).
|
||||
* Shared across the staff who own the record; the author's display name is denormalised
|
||||
* so the note survives the author being deleted.
|
||||
*/
|
||||
#[ORM\Entity(repositoryClass: PatientNoteRepository::class)]
|
||||
#[ORM\Table(name: 'patient_notes')]
|
||||
#[ORM\Index(columns: ['record_id'], name: 'idx_patient_notes_record')]
|
||||
class PatientNote
|
||||
{
|
||||
#[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;
|
||||
|
||||
/** متن یادداشت — free-text body. */
|
||||
#[ORM\Column(type: 'text')]
|
||||
private string $body;
|
||||
|
||||
/** یادداشتهای پینشده بالای لیست نشان داده میشوند. */
|
||||
#[ORM\Column(type: 'boolean')]
|
||||
private bool $pinned = false;
|
||||
|
||||
/** The staff user who wrote the note; kept for possible audit, nulled if they are removed. */
|
||||
#[ORM\ManyToOne(targetEntity: User::class)]
|
||||
#[ORM\JoinColumn(name: 'created_by', nullable: true, onDelete: 'SET NULL')]
|
||||
private ?User $createdBy = null;
|
||||
|
||||
/** Denormalised author display name (survives user deletion). */
|
||||
#[ORM\Column(name: 'author_name', type: 'string', length: 120, nullable: true)]
|
||||
private ?string $authorName = null;
|
||||
|
||||
#[ORM\Column(name: 'created_at', type: 'integer')]
|
||||
private int $createdAt;
|
||||
|
||||
#[ORM\Column(name: 'updated_at', type: 'integer', nullable: true)]
|
||||
private ?int $updatedAt = null;
|
||||
|
||||
public function __construct(PatientRecord $record, string $body, bool $pinned = false)
|
||||
{
|
||||
$this->uuid = Uuid::v4()->toRfc4122();
|
||||
$this->record = $record;
|
||||
$this->body = $body;
|
||||
$this->pinned = $pinned;
|
||||
$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 isPinned(): bool { return $this->pinned; }
|
||||
|
||||
public function setBody(string $body): self { $this->body = $body; $this->touch(); return $this; }
|
||||
public function setPinned(bool $pinned): self { $this->pinned = $pinned; $this->touch(); return $this; }
|
||||
|
||||
public function setAuthor(?User $user, ?string $name): self
|
||||
{
|
||||
$this->createdBy = $user;
|
||||
$this->authorName = $name;
|
||||
return $this;
|
||||
}
|
||||
|
||||
private function touch(): void { $this->updatedAt = time(); }
|
||||
|
||||
public function toArray(): array
|
||||
{
|
||||
return [
|
||||
'uuid' => $this->uuid,
|
||||
'body' => $this->body,
|
||||
'pinned' => $this->pinned,
|
||||
'author' => $this->authorName,
|
||||
'created_at' => $this->createdAt,
|
||||
'updated_at' => $this->updatedAt,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace App\Patient\Repository;
|
||||
|
||||
use App\Patient\Entity\PatientNote;
|
||||
use App\Patient\Entity\PatientRecord;
|
||||
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
|
||||
use Doctrine\Persistence\ManagerRegistry;
|
||||
|
||||
class PatientNoteRepository extends ServiceEntityRepository
|
||||
{
|
||||
public function __construct(ManagerRegistry $registry)
|
||||
{
|
||||
parent::__construct($registry, PatientNote::class);
|
||||
}
|
||||
|
||||
public function findByUuid(string $uuid): ?PatientNote
|
||||
{
|
||||
return $this->findOneBy(['uuid' => $uuid]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Notes for a record, pinned first then newest first.
|
||||
*
|
||||
* @return PatientNote[]
|
||||
*/
|
||||
public function findByRecord(PatientRecord $record): array
|
||||
{
|
||||
return $this->createQueryBuilder('n')
|
||||
->where('n.record = :record')
|
||||
->setParameter('record', $record)
|
||||
->orderBy('n.pinned', 'DESC')
|
||||
->addOrderBy('n.id', 'DESC')
|
||||
->getQuery()
|
||||
->getResult();
|
||||
}
|
||||
|
||||
public function save(PatientNote $n): void
|
||||
{
|
||||
$this->getEntityManager()->persist($n);
|
||||
$this->getEntityManager()->flush();
|
||||
}
|
||||
|
||||
public function remove(PatientNote $n): void
|
||||
{
|
||||
$this->getEntityManager()->remove($n);
|
||||
$this->getEntityManager()->flush();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user