Phase 3 of the tenant-marking series. The same concept was written four ways, and the Doctrine filter arriving in phase 4 keys on the field name — so the tables using a different spelling would have been skipped silently, which is exactly the leak this work exists to prevent. - discount_rules: owner_type/owner_id renamed to entity_type/entity_id. Pure rename, no data moves. - doctor_secretaries: owner_type plus a nullable clinic_id replaced by the shared pair. The environment now comes from the clinic argument alone, so the inconsistent combination (owner_type='clinic', clinic_id=NULL) can no longer be constructed, and the redundant constructor parameter is gone. - user_active_context: added db_type, so resolving an environment is one lookup instead of "try clinics, then try doctors". Filled from the type already present in available_contexts. - entity_type is VARCHAR(10) in all twenty tenant tables; four of them were 20. Behaviour change, the only one in this series: the doctor_secretaries unique key went from (doctor_id, secretary_id, owner_type) to (doctor_id, secretary_id, entity_type, entity_id). With clinic_id outside the key, one secretary could not be assigned to the same doctor in two clinics — the second row collided on owner_type='clinic'. The duplicate check in SecretaryController had the same blind spot and would have rejected the request before the database saw it; both are fixed together. Correcting an assumption from the phase-3 plan: mobile_verification_otp.entity_type really is a tenant pair. NotificationMobileController validates the target against ['doctor','clinic'] and stores that entity's id, so the column was normalised with the rest rather than treated as unrelated. TenantOwnedTrait gained assignTenantPair() for callers that resolved the pair as scalars and hold no entity — building an EntityContext from scalars would produce one where isClinic() is true but ->clinic is null, breaking consumers silently. tests/ApiTestCase::createUser now retries on a duplicate mobile. db_test is never reset and already holds ~38k users, so the 9-digit random draw collided often enough to fail unrelated tests a few percent of runs. Tests: 830 passing. PHPStan reports no new errors on the changed files. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
212 lines
7.6 KiB
PHP
212 lines
7.6 KiB
PHP
<?php
|
|
|
|
namespace App\Secretary\Repository;
|
|
|
|
use App\Auth\Entity\User;
|
|
use App\Clinic\Entity\Clinic;
|
|
use App\Doctor\Entity\Doctor;
|
|
use App\Secretary\Entity\DoctorSecretary;
|
|
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
|
|
use Doctrine\Persistence\ManagerRegistry;
|
|
|
|
class DoctorSecretaryRepository extends ServiceEntityRepository
|
|
{
|
|
public function __construct(ManagerRegistry $registry)
|
|
{
|
|
parent::__construct($registry, DoctorSecretary::class);
|
|
}
|
|
|
|
public function findByUuid(string $uuid): ?DoctorSecretary
|
|
{
|
|
return $this->findOneBy(['uuid' => $uuid]);
|
|
}
|
|
|
|
public function countActiveByDoctor(Doctor $doctor): int
|
|
{
|
|
return (int) $this->createQueryBuilder('s')
|
|
->select('COUNT(s.id)')
|
|
->where('s.doctor = :doctor')
|
|
->andWhere('s.active = true')
|
|
->setParameter('doctor', $doctor)
|
|
->getQuery()
|
|
->getSingleScalarResult();
|
|
}
|
|
|
|
/** @return DoctorSecretary[] */
|
|
public function findByDoctor(Doctor $doctor): array
|
|
{
|
|
return $this->findBy(['doctor' => $doctor], ['createdAt' => 'DESC']);
|
|
}
|
|
|
|
/** Base query with secretary/doctor/clinic fetch-joined for toArray() (no N+1). */
|
|
private function listWithRelations(): \Doctrine\ORM\QueryBuilder
|
|
{
|
|
return $this->createQueryBuilder('s')
|
|
->addSelect('sec', 'doc', 'cl')
|
|
->leftJoin('s.secretary', 'sec')
|
|
->leftJoin('s.doctor', 'doc')
|
|
->leftJoin('s.clinic', 'cl')
|
|
->orderBy('s.createdAt', 'DESC');
|
|
}
|
|
|
|
/** منشی ها مطب شخصی یک دکتر (owner_type='doctor') */
|
|
public function findByDoctorScope(Doctor $doctor): array
|
|
{
|
|
return $this->listWithRelations()
|
|
->where('s.doctor = :doctor')
|
|
->andWhere('s.entityType = :type')
|
|
->setParameter('doctor', $doctor)
|
|
->setParameter('type', DoctorSecretary::OWNER_DOCTOR)
|
|
->getQuery()
|
|
->getResult();
|
|
}
|
|
|
|
/** رابطه منشی در scope مطب شخصی */
|
|
public function findActiveBySecretaryForDoctor(User $user, Doctor $doctor): ?DoctorSecretary
|
|
{
|
|
return $this->findOneBy([
|
|
'secretary' => $user,
|
|
'doctor' => $doctor,
|
|
'entityType' => DoctorSecretary::OWNER_DOCTOR,
|
|
'active' => true,
|
|
]);
|
|
}
|
|
|
|
/** اولین رابطه فعال منشی در یک کلینیک (برای چک دسترسی کلی) */
|
|
public function findActiveBySecretaryForClinic(User $user, Clinic $clinic): ?DoctorSecretary
|
|
{
|
|
return $this->createQueryBuilder('s')
|
|
->where('s.secretary = :user')
|
|
->andWhere('s.clinic = :clinic')
|
|
->andWhere('s.entityType = :type')
|
|
->andWhere('s.active = true')
|
|
->setParameter('user', $user)
|
|
->setParameter('clinic', $clinic)
|
|
->setParameter('type', DoctorSecretary::OWNER_CLINIC)
|
|
->setMaxResults(1)
|
|
->getQuery()
|
|
->getOneOrNullResult();
|
|
}
|
|
|
|
/** رابطه فعالِ owner=clinic برای یک (منشی، کلینیک، پزشک) مشخص */
|
|
public function findActiveClinicRow(User $user, Clinic $clinic, Doctor $doctor): ?DoctorSecretary
|
|
{
|
|
return $this->findOneBy([
|
|
'secretary' => $user,
|
|
'clinic' => $clinic,
|
|
'doctor' => $doctor,
|
|
'entityType' => DoctorSecretary::OWNER_CLINIC,
|
|
'active' => true,
|
|
]);
|
|
}
|
|
|
|
/** همه دکترهای کلینیک که این منشی به آنها متصل است */
|
|
public function findDoctorsBySecretaryInClinic(User $user, Clinic $clinic): array
|
|
{
|
|
return $this->getEntityManager()->createQueryBuilder()
|
|
->select('d')
|
|
->from(Doctor::class, 'd')
|
|
->join(DoctorSecretary::class, 's', 'WITH', 's.doctor = d')
|
|
->where('s.secretary = :user')
|
|
->andWhere('s.clinic = :clinic')
|
|
->andWhere('s.entityType = :type')
|
|
->andWhere('s.active = true')
|
|
->setParameter('user', $user)
|
|
->setParameter('clinic', $clinic)
|
|
->setParameter('type', DoctorSecretary::OWNER_CLINIC)
|
|
->getQuery()
|
|
->getResult();
|
|
}
|
|
|
|
/** همه روابط فعال یک منشی — برای auth context builder */
|
|
public function findAllActiveBySecretary(User $user): array
|
|
{
|
|
return $this->findBy(['secretary' => $user, 'active' => true]);
|
|
}
|
|
|
|
/** اولین رابطه فعال — backward compat برای کدهایی که هنوز migrate نشدهاند */
|
|
public function findActiveBySecretary(User $user): ?DoctorSecretary
|
|
{
|
|
return $this->findOneBy(['secretary' => $user, 'active' => true]);
|
|
}
|
|
|
|
public function isDoctorInClinic(Doctor $doctor, Clinic $clinic): bool
|
|
{
|
|
return $clinic->getDoctors()->contains($doctor);
|
|
}
|
|
|
|
/** روابط owner_type='clinic' یک منشی مشخص در یک کلینیک (فعال و غیرفعال، برای sync) */
|
|
public function findByClinicAndSecretary(Clinic $clinic, User $secretary): array
|
|
{
|
|
return $this->createQueryBuilder('s')
|
|
->addSelect('doc')
|
|
->join('s.doctor', 'doc')
|
|
->where('s.clinic = :clinic')
|
|
->andWhere('s.secretary = :secretary')
|
|
->andWhere('s.entityType = :type')
|
|
->setParameter('clinic', $clinic)
|
|
->setParameter('secretary', $secretary)
|
|
->setParameter('type', DoctorSecretary::OWNER_CLINIC)
|
|
->getQuery()
|
|
->getResult();
|
|
}
|
|
|
|
/** همه منشی ها کلینیک (owner_type='clinic') */
|
|
public function findByClinic(Clinic $clinic): array
|
|
{
|
|
return $this->listWithRelations()
|
|
->where('s.clinic = :clinic')
|
|
->andWhere('s.entityType = :type')
|
|
->setParameter('clinic', $clinic)
|
|
->setParameter('type', DoctorSecretary::OWNER_CLINIC)
|
|
->getQuery()
|
|
->getResult();
|
|
}
|
|
|
|
/**
|
|
* رابطههای فعالی که سهم درآمد نوبت آنلاین برایشان روشن است — برای کلینیک همهٔ
|
|
* منشیهای همان کلینیک، برای مطب شخصی منشیهای همان پزشک.
|
|
*
|
|
* @return DoctorSecretary[]
|
|
*/
|
|
public function findOnlineShareRows(Doctor $doctor, ?Clinic $clinic): array
|
|
{
|
|
$qb = $this->createQueryBuilder('s')
|
|
->addSelect('sec')
|
|
->join('s.secretary', 'sec')
|
|
->where('s.active = true')
|
|
->andWhere('s.onlineShareEnabled = true')
|
|
->andWhere('s.onlineSharePercent > 0');
|
|
|
|
if ($clinic !== null) {
|
|
$qb->andWhere('s.clinic = :clinic')
|
|
->andWhere('s.entityType = :type')
|
|
->setParameter('clinic', $clinic)
|
|
->setParameter('type', DoctorSecretary::OWNER_CLINIC);
|
|
} else {
|
|
$qb->andWhere('s.doctor = :doctor')
|
|
->andWhere('s.entityType = :type')
|
|
->setParameter('doctor', $doctor)
|
|
->setParameter('type', DoctorSecretary::OWNER_DOCTOR);
|
|
}
|
|
|
|
return $qb->getQuery()->getResult();
|
|
}
|
|
|
|
public function save(DoctorSecretary $entity, bool $flush = true): void
|
|
{
|
|
$this->getEntityManager()->persist($entity);
|
|
if ($flush) {
|
|
$this->getEntityManager()->flush();
|
|
}
|
|
}
|
|
|
|
public function remove(DoctorSecretary $entity, bool $flush = true): void
|
|
{
|
|
$this->getEntityManager()->remove($entity);
|
|
if ($flush) {
|
|
$this->getEntityManager()->flush();
|
|
}
|
|
}
|
|
}
|