feat(patients): phase B4 — messages (پیام‌ها)

Add a patient message/communication log: a new PatientMessage entity
(record-scoped, CASCADE) + repository, and owner-scoped endpoints
(GET messages, POST message, DELETE message) with a validated channel
(sms/note/call/email). Wire the "پیام‌ها" tab in PatientDetailPage
(send box + list + delete). PHPUnit covers create/list/delete + ownership
+ validation; Vitest covers the tab. API docs updated.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
hamed
2026-07-13 15:35:10 +03:30
co-authored by Claude Opus 4.8
parent 976c4f0c0a
commit 5fb4c52246
8 changed files with 358 additions and 1 deletions
@@ -52,10 +52,67 @@ class PatientController extends BaseController
private readonly \App\Tag\Repository\TenantTagRepository $tenantTagRepo,
private readonly \App\Patient\Repository\PatientAttachmentRepository $attachmentRepo,
private readonly \App\Patient\Repository\PatientMedicalRecordRepository $medicalRepo,
private readonly \App\Patient\Repository\PatientMessageRepository $messageRepo,
private readonly \App\Shared\Service\FileUploadService $fileUpload,
private readonly LoggerInterface $logger,
) {}
// ── Messages (پیام‌ها) ─────────────────────────────────────────────────────
#[Route('/api/v1/patient/{uuid}/messages', methods: ['GET'])]
public function listMessages(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\PatientMessage $m) => $m->toArray(),
$this->messageRepo->findByRecord($record)
));
}
#[Route('/api/v1/patient/{uuid}/message', methods: ['POST'])]
public function createMessage(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');
}
$channel = (string) ($data['channel'] ?? 'sms');
if (!in_array($channel, ['sms', 'note', 'call', 'email'], true)) {
$channel = 'sms';
}
$message = new \App\Patient\Entity\PatientMessage($record, $body, $channel);
$this->messageRepo->save($message);
return $this->success($message->toArray(), 201);
}
#[Route('/api/v1/patient/message/{uuid}', methods: ['DELETE'])]
public function deleteMessage(string $uuid, #[CurrentUser] User $user): JsonResponse
{
[$entityType, $entityId] = $this->resolveEntity($user);
$message = $this->messageRepo->findByUuid($uuid);
if ($message === null || !$this->ownsRecord($message->getRecord(), $entityType, $entityId)) {
return $this->error(ErrorCodes::ERR_NOT_FOUND_001, 'پیام یافت نشد', 404);
}
$this->messageRepo->remove($message);
return $this->success(['message' => 'پیام حذف شد']);
}
// ── Medical records (پرونده پزشکی) ────────────────────────────────────────
#[Route('/api/v1/patient/{uuid}/medical-records', methods: ['GET'])]
+59
View File
@@ -0,0 +1,59 @@
<?php
namespace App\Patient\Entity;
use App\Patient\Repository\PatientMessageRepository;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Uid\Uuid;
/** A logged message/communication with a patient (the «پیام‌ها» tab). */
#[ORM\Entity(repositoryClass: PatientMessageRepository::class)]
#[ORM\Table(name: 'patient_messages')]
#[ORM\Index(columns: ['record_id'], name: 'idx_patient_messages_record')]
class PatientMessage
{
#[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;
#[ORM\Column(type: 'text')]
private string $body;
/** Channel: sms | note | call | email … */
#[ORM\Column(type: 'string', length: 20)]
private string $channel = 'sms';
#[ORM\Column(name: 'created_at', type: 'integer')]
private int $createdAt;
public function __construct(PatientRecord $record, string $body, string $channel = 'sms')
{
$this->uuid = Uuid::v4()->toRfc4122();
$this->record = $record;
$this->body = $body;
$this->channel = $channel;
$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 toArray(): array
{
return [
'uuid' => $this->uuid,
'body' => $this->body,
'channel' => $this->channel,
'created_at' => $this->createdAt,
];
}
}
@@ -0,0 +1,44 @@
<?php
namespace App\Patient\Repository;
use App\Patient\Entity\PatientMessage;
use App\Patient\Entity\PatientRecord;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
class PatientMessageRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, PatientMessage::class);
}
public function findByUuid(string $uuid): ?PatientMessage
{
return $this->findOneBy(['uuid' => $uuid]);
}
/** @return PatientMessage[] */
public function findByRecord(PatientRecord $record): array
{
return $this->createQueryBuilder('m')
->where('m.record = :record')
->setParameter('record', $record)
->orderBy('m.id', 'DESC')
->getQuery()
->getResult();
}
public function save(PatientMessage $m): void
{
$this->getEntityManager()->persist($m);
$this->getEntityManager()->flush();
}
public function remove(PatientMessage $m): void
{
$this->getEntityManager()->remove($m);
$this->getEntityManager()->flush();
}
}