diff --git a/migrations/Version20260717113519.php b/migrations/Version20260717113519.php new file mode 100644 index 00000000..2526f715 --- /dev/null +++ b/migrations/Version20260717113519.php @@ -0,0 +1,31 @@ +addSql('CREATE TABLE session_audit_logs (id INT AUTO_INCREMENT NOT NULL, uuid VARCHAR(36) NOT NULL, field VARCHAR(40) NOT NULL, operation VARCHAR(10) NOT NULL, old_value LONGTEXT DEFAULT NULL, new_value LONGTEXT DEFAULT NULL, actor_user_id INT DEFAULT NULL, actor_name VARCHAR(191) DEFAULT NULL, note VARCHAR(191) DEFAULT NULL, created_at INT NOT NULL, session_id INT NOT NULL, UNIQUE INDEX UNIQ_36B92303D17F50A6 (uuid), INDEX IDX_36B92303613FECDF (session_id), INDEX idx_session_audit_session (session_id, created_at), PRIMARY KEY (id)) DEFAULT CHARACTER SET utf8mb4'); + $this->addSql('ALTER TABLE session_audit_logs ADD CONSTRAINT FK_36B92303613FECDF FOREIGN KEY (session_id) REFERENCES patient_sessions (id) ON DELETE CASCADE'); + } + + public function down(Schema $schema): void + { + $this->addSql('ALTER TABLE session_audit_logs DROP FOREIGN KEY FK_36B92303613FECDF'); + $this->addSql('DROP TABLE session_audit_logs'); + } +} diff --git a/src/Patient/Entity/SessionAuditLog.php b/src/Patient/Entity/SessionAuditLog.php new file mode 100644 index 00000000..063d25c1 --- /dev/null +++ b/src/Patient/Entity/SessionAuditLog.php @@ -0,0 +1,87 @@ +uuid = Uuid::v4()->toRfc4122(); + $this->session = $session; + $this->field = $field; + $this->operation = $operation; + $this->createdAt = time(); + } + + public function setActor(?int $userId, ?string $name): self { $this->actorUserId = $userId; $this->actorName = $name; return $this; } + public function setValues(?string $old, ?string $new): self { $this->oldValue = $old; $this->newValue = $new; return $this; } + public function setNote(?string $note): self { $this->note = $note; return $this; } + + public function getUuid(): string { return $this->uuid; } + + public function toArray(): array + { + return [ + 'uuid' => $this->uuid, + 'field' => $this->field, + 'operation' => $this->operation, + 'old_value' => $this->oldValue, + 'new_value' => $this->newValue, + 'actor_name' => $this->actorName, + 'note' => $this->note, + 'created_at' => $this->createdAt, + ]; + } +} diff --git a/src/Patient/Repository/SessionAuditLogRepository.php b/src/Patient/Repository/SessionAuditLogRepository.php new file mode 100644 index 00000000..d99d8992 --- /dev/null +++ b/src/Patient/Repository/SessionAuditLogRepository.php @@ -0,0 +1,31 @@ +getEntityManager()->persist($e); + if ($flush) { + $this->getEntityManager()->flush(); + } + } + + /** تاریخچه‌ی یک مراجعه (جدید → قدیم)، آرایه‌ای. */ + public function findBySessionUuid(string $sessionUuid): array + { + return $this->createQueryBuilder('l') + ->select('l.field AS field', 'l.operation AS operation', 'l.oldValue AS old_value', 'l.newValue AS new_value', 'l.actorName AS actor_name', 'l.note AS note', 'l.createdAt AS created_at') + ->join('l.session', 's') + ->where('s.uuid = :uuid')->setParameter('uuid', $sessionUuid) + ->orderBy('l.createdAt', 'DESC')->addOrderBy('l.id', 'DESC') + ->getQuery()->getArrayResult(); + } +}