Files
clinicpro/src/Appointment/Entity/Appointment.php
T
hamedandClaude Opus 4.8 05fde81320 fix(orm): declare repositoryClass on all entities with a custom repository
On prod (opcache.preload + prod container), getRepository(Entity::class)
returned Doctrine's default repository instead of the custom one when the
entity's #[ORM\Entity] had no repositoryClass — so custom finders like
DoctorSecretaryRepository::findAllActiveBySecretary threw BadMethodCallException,
making /oauth/userinfo return 500 after login. Declare repositoryClass explicitly
on all 25 affected entities.

Also add app:create-admin command (create/promote a ROLE_ADMIN user by mobile).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-28 16:19:07 +03:30

201 lines
8.3 KiB
PHP

<?php
namespace App\Appointment\Entity;
use App\Auth\Entity\User;
use App\Doctor\Entity\Doctor;
use Doctrine\ORM\Mapping as ORM;
use App\Appointment\Repository\AppointmentRepository;
use Symfony\Component\Uid\Uuid;
#[ORM\Entity(repositoryClass: AppointmentRepository::class)]
#[ORM\Table(name: 'appointments')]
#[ORM\Index(columns: ['doctor_id', 'slot_start'], name: 'idx_appointments_doctor_slot')]
#[ORM\Index(columns: ['user_id', 'status'], name: 'idx_appointments_user_status')]
class Appointment
{
// Status machine: pending → confirmed → completed
// ↘ cancelled_by_doctor / cancelled_by_user
// pending → expired (cron)
// confirmed → no_show
public const STATUS_PENDING = 'pending';
public const STATUS_CONFIRMED = 'confirmed';
public const STATUS_COMPLETED = 'completed';
public const STATUS_CANCELLED_BY_DOCTOR = 'cancelled_by_doctor';
public const STATUS_CANCELLED_BY_USER = 'cancelled_by_user';
public const STATUS_EXPIRED = 'expired';
public const STATUS_NO_SHOW = 'no_show';
public const PAYMENT_TTL = 900; // 15 minutes to pay before a pending booking expires
public const ALLOWED_TRANSITIONS = [
self::STATUS_PENDING => [self::STATUS_CONFIRMED, self::STATUS_CANCELLED_BY_DOCTOR, self::STATUS_CANCELLED_BY_USER, self::STATUS_EXPIRED],
self::STATUS_CONFIRMED => [self::STATUS_COMPLETED, self::STATUS_CANCELLED_BY_DOCTOR, self::STATUS_CANCELLED_BY_USER, self::STATUS_NO_SHOW],
];
// Optimistic locking
#[ORM\Version]
#[ORM\Column(type: 'integer')]
private int $version = 1;
#[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: Doctor::class)]
#[ORM\JoinColumn(name: 'doctor_id', referencedColumnName: 'id', nullable: false, onDelete: 'RESTRICT')]
private Doctor $doctor;
#[ORM\ManyToOne(targetEntity: User::class)]
#[ORM\JoinColumn(name: 'user_id', referencedColumnName: 'id', nullable: false, onDelete: 'RESTRICT')]
private User $user;
#[ORM\Column(name: 'slot_start', type: 'integer')]
private int $slotStart;
#[ORM\Column(name: 'slot_end', type: 'integer')]
private int $slotEnd;
#[ORM\Column(type: 'string', length: 30)]
private string $status = self::STATUS_PENDING;
#[ORM\Column(type: 'string', length: 255, nullable: true)]
private ?string $note = null;
#[ORM\Column(name: 'expires_at', type: 'integer', nullable: true)]
private ?int $expiresAt = null;
#[ORM\Column(name: 'patient_name', type: 'string', length: 150, nullable: true)]
private ?string $patientName = null;
#[ORM\Column(name: 'patient_mobile', type: 'string', length: 20, nullable: true)]
private ?string $patientMobile = null;
#[ORM\Column(name: 'patient_national_code', type: 'string', length: 20, nullable: true)]
private ?string $patientNationalCode = null;
#[ORM\Column(name: 'patient_gender', type: 'string', length: 10, nullable: true)]
private ?string $patientGender = null;
#[ORM\Column(name: 'patient_reason', type: 'text', nullable: true)]
private ?string $patientReason = null;
#[ORM\Column(name: 'address_id', type: 'integer', nullable: true)]
private ?int $addressId = null;
#[ORM\Column(name: 'booking_representation_id', type: 'integer', nullable: true)]
private ?int $bookingRepresentationId = null;
#[ORM\Column(name: 'created_at', type: 'integer')]
private int $createdAt;
#[ORM\Column(name: 'updated_at', type: 'integer')]
private int $updatedAt;
public function __construct(Doctor $doctor, User $user, int $slotStart, int $slotEnd)
{
$this->uuid = Uuid::v4()->toRfc4122();
$this->doctor = $doctor;
$this->user = $user;
$this->slotStart = $slotStart;
$this->slotEnd = $slotEnd;
$this->createdAt = time();
$this->updatedAt = time();
}
public function getId(): ?int { return $this->id; }
public function getUuid(): string { return $this->uuid; }
public function getDoctor(): Doctor { return $this->doctor; }
public function getUser(): User { return $this->user; }
public function getSlotStart(): int { return $this->slotStart; }
public function getSlotEnd(): int { return $this->slotEnd; }
public function getStatus(): string { return $this->status; }
public function getNote(): ?string { return $this->note; }
public function getVersion(): int { return $this->version; }
public function getExpiresAt(): ?int { return $this->expiresAt; }
public function getPatientName(): ?string { return $this->patientName; }
public function getPatientMobile(): ?string { return $this->patientMobile; }
public function getPatientNationalCode(): ?string { return $this->patientNationalCode; }
public function getPatientGender(): ?string { return $this->patientGender; }
public function getPatientReason(): ?string { return $this->patientReason; }
public function getAddressId(): ?int { return $this->addressId; }
public function getBookingRepresentationId(): ?int { return $this->bookingRepresentationId; }
public function setNote(?string $v): self { $this->note = $v; return $this; }
public function setBookingRepresentationId(?int $v): self { $this->bookingRepresentationId = $v; return $this; }
public function setAddressId(?int $v): self { $this->addressId = $v; return $this; }
public function setPatientName(?string $v): self { $this->patientName = $v; return $this; }
public function setPatientMobile(?string $v): self { $this->patientMobile = $v; return $this; }
public function setPatientNationalCode(?string $v): self { $this->patientNationalCode = $v; return $this; }
public function setPatientGender(?string $v): self { $this->patientGender = $v; return $this; }
public function setPatientReason(?string $v): self { $this->patientReason = $v; return $this; }
public function markPendingWithTtl(int $ttl): self
{
$this->expiresAt = time() + $ttl;
$this->updatedAt = time();
return $this;
}
public function canTransitionTo(string $newStatus): bool
{
return in_array($newStatus, self::ALLOWED_TRANSITIONS[$this->status] ?? [], true);
}
public function transitionTo(string $newStatus): self
{
if (!$this->canTransitionTo($newStatus)) {
throw new \LogicException(sprintf(
'Cannot transition appointment from "%s" to "%s"',
$this->status, $newStatus
));
}
$this->status = $newStatus;
$this->updatedAt = time();
if ($newStatus !== self::STATUS_PENDING) {
$this->expiresAt = null;
}
return $this;
}
public function toArray(): array
{
$firstAddress = $this->doctor->getAddresses()->first() ?: null;
return [
'uuid' => $this->uuid,
'doctor' => [
'uuid' => $this->doctor->getUuid(),
'name' => $this->doctor->getName(),
'specialties' => array_map(
fn($s) => ['uuid' => $s->getUuid(), 'name' => $s->getName()],
$this->doctor->getSpecialties()->toArray()
),
],
'address' => $firstAddress?->toArray(),
'address_id' => $this->addressId,
'user' => [
'uuid' => $this->user->getUuid(),
'mobile' => $this->user->getMobileNumber(),
],
'slot_start' => $this->slotStart,
'slot_end' => $this->slotEnd,
'status' => $this->status,
'note' => $this->note,
'expires_at' => $this->expiresAt,
'patient_name' => $this->patientName,
'patient_mobile' => $this->patientMobile,
'patient_national_code' => $this->patientNationalCode,
'patient_gender' => $this->patientGender,
'patient_reason' => $this->patientReason,
'version' => $this->version,
'created_at' => $this->createdAt,
'updated_at' => $this->updatedAt,
];
}
}