feat(validation): enforce naming rules for doctors and clinics to prevent placeholders
This commit is contained in:
@@ -6,6 +6,7 @@ use App\Auth\Entity\User;
|
||||
use App\Doctor\Entity\Doctor;
|
||||
use App\DoctorService\Entity\DoctorService;
|
||||
use App\Insurance\Entity\Insurance;
|
||||
use App\Shared\Util\DisplayName;
|
||||
use App\Specialty\Entity\Specialty;
|
||||
use Doctrine\Common\Collections\ArrayCollection;
|
||||
use Doctrine\Common\Collections\Collection;
|
||||
@@ -166,7 +167,8 @@ class Clinic
|
||||
public function getServices(): Collection { return $this->services; }
|
||||
public function getInsurances(): Collection { return $this->insurances; }
|
||||
|
||||
public function setName(?string $v): self { $this->name = $v; $this->touch(); return $this; }
|
||||
// null مجاز است (کلینیک تازهساخته هنوز نام ندارد)؛ ولی نام آلوده رد میشود.
|
||||
public function setName(?string $v): self { if ($v !== null) DisplayName::assertReal($v); $this->name = $v; $this->touch(); return $this; }
|
||||
public function setInfo(?string $v): self { $this->info = $v; $this->touch(); return $this; }
|
||||
public function setAddress(?string $v): self { $this->address = $v; $this->touch(); return $this; }
|
||||
public function setTelephone(?string $v): self { $this->telephone = $v; $this->touch(); return $this; }
|
||||
|
||||
@@ -10,12 +10,15 @@ use App\ClinicInvitation\Repository\ClinicDoctorInvitationRepository;
|
||||
use App\Doctor\Entity\Doctor;
|
||||
use App\Doctor\Repository\DoctorRepository;
|
||||
use App\Shared\Exception\AppException;
|
||||
use App\Shared\Util\DisplayName;
|
||||
use App\Sms\Service\SmsService;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
|
||||
|
||||
class ClinicInvitationService
|
||||
{
|
||||
private const UNNAMED_DOCTOR = 'پزشک دعوتشده';
|
||||
|
||||
public function __construct(
|
||||
private readonly ClinicDoctorInvitationRepository $repo,
|
||||
private readonly DoctorRepository $doctorRepo,
|
||||
@@ -161,7 +164,9 @@ class ClinicInvitationService
|
||||
}
|
||||
|
||||
$mobile = $inv->getMobile();
|
||||
$name = $inv->getInvitedName() ?: $mobile;
|
||||
// شمارهٔ موبایل نامِ پزشک نیست. دعوت بدون نام قبلاً موبایل را بهعنوان نام
|
||||
// مینشاند و همان رکورد در نتایج عمومی و <title> صفحات سایت منتشر میشد.
|
||||
$name = $this->resolveInvitedName($inv->getInvitedName());
|
||||
|
||||
$user = $this->userRepo->findOneBy(['mobileNumber' => $mobile]);
|
||||
if ($user === null) {
|
||||
@@ -173,7 +178,7 @@ class ClinicInvitationService
|
||||
|
||||
$doctor = $this->doctorRepo->findOneBy(['user' => $user]);
|
||||
if ($doctor === null) {
|
||||
$doctor = new Doctor($user, $user->getRealName() ?: $name);
|
||||
$doctor = new Doctor($user, $this->resolveInvitedName($user->getRealName()));
|
||||
$doctor->setMobileNumber($mobile);
|
||||
$doctor->setOwnerStatus('unclaimed');
|
||||
$this->em->persist($doctor);
|
||||
@@ -182,6 +187,16 @@ class ClinicInvitationService
|
||||
return $doctor;
|
||||
}
|
||||
|
||||
/**
|
||||
* نام پزشکِ دعوتشده. کلینیک اغلب فقط شمارهٔ موبایل را دارد، پس نام واقعی هنوز
|
||||
* ناشناخته است — یک برچسب خنثی مینشیند تا وقتی خود پزشک پروفایلش را claim کند.
|
||||
* هرگز موبایل یا مقدار آزمایشی برنمیگرداند.
|
||||
*/
|
||||
private function resolveInvitedName(?string $candidate): string
|
||||
{
|
||||
return DisplayName::isPlaceholder($candidate) ? self::UNNAMED_DOCTOR : $candidate;
|
||||
}
|
||||
|
||||
/**
|
||||
* برای کاربری که هنوز رمز عبور ندارد یک رمز تولید میکند تا بتواند وارد شود.
|
||||
* رمز کاربران موجود هرگز بازنویسی نمیشود.
|
||||
|
||||
@@ -7,6 +7,7 @@ use App\Auth\Entity\User;
|
||||
use App\DoctorService\Entity\DoctorService;
|
||||
use App\Location\Entity\City;
|
||||
use App\Location\Entity\Province;
|
||||
use App\Shared\Util\DisplayName;
|
||||
use App\Specialty\Entity\Specialty;
|
||||
use Doctrine\Common\Collections\ArrayCollection;
|
||||
use Doctrine\Common\Collections\Collection;
|
||||
@@ -142,6 +143,8 @@ class Doctor
|
||||
|
||||
public function __construct(User $user, string $name)
|
||||
{
|
||||
DisplayName::assertReal($name);
|
||||
|
||||
$this->uuid = Uuid::v4()->toRfc4122();
|
||||
$this->user = $user;
|
||||
$this->name = $name;
|
||||
@@ -273,6 +276,7 @@ class Doctor
|
||||
|
||||
public function setName(string $v): self
|
||||
{
|
||||
DisplayName::assertReal($v);
|
||||
$this->name = $v;
|
||||
return $this;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,157 @@
|
||||
<?php
|
||||
|
||||
namespace App\Shared\Command;
|
||||
|
||||
use App\Clinic\Entity\Clinic;
|
||||
use App\Doctor\Entity\Doctor;
|
||||
use App\Shared\Util\DisplayName;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Component\Console\Attribute\AsCommand;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||
|
||||
/**
|
||||
* پزشکان/کلینیکهایی که نام واقعی ندارند (شمارهتلفن، «test») در نتایج عمومی و در
|
||||
* <title> صفحات سایت منتشر میشوند. این فرمان آنها را پیدا و دستهبندی میکند.
|
||||
*
|
||||
* گزارشمحور است: بدون --force هیچ چیزی نوشته نمیشود.
|
||||
*
|
||||
* php bin/console app:audit-polluted-records # فقط گزارش
|
||||
* php bin/console app:audit-polluted-records --force # اعمال تغییرات
|
||||
*
|
||||
* حذف انجام نمیدهد. رکورد دارای رابطهٔ واقعی (نوبت/پرداخت) هرگز خودکار دست نمیخورد؛
|
||||
* برای بقیه فقط انتشار عمومی را میبندد (active=false / is_active=false) تا تصمیمِ
|
||||
* حذف با تیم بماند و برگشتپذیر باشد.
|
||||
*/
|
||||
#[AsCommand(
|
||||
name: 'app:audit-polluted-records',
|
||||
description: 'Find doctors/clinics whose name is a phone number or a placeholder; optionally unpublish them',
|
||||
)]
|
||||
class AuditPollutedRecordsCommand extends Command
|
||||
{
|
||||
public function __construct(private readonly EntityManagerInterface $em)
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
protected function configure(): void
|
||||
{
|
||||
$this->addOption('force', null, InputOption::VALUE_NONE, 'Apply changes. Without it nothing is written.');
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
$io = new SymfonyStyle($input, $output);
|
||||
$force = (bool) $input->getOption('force');
|
||||
|
||||
$doctors = $this->auditDoctors($io);
|
||||
$clinics = $this->auditClinics($io);
|
||||
|
||||
$unpublishable = array_merge(
|
||||
array_filter($doctors, fn(array $r) => !$r['hasRelations'] && $r['published']),
|
||||
array_filter($clinics, fn(array $r) => !$r['hasRelations'] && $r['published']),
|
||||
);
|
||||
|
||||
if ($doctors === [] && $clinics === []) {
|
||||
$io->success('هیچ رکورد آلودهای یافت نشد.');
|
||||
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
|
||||
if (!$force) {
|
||||
$io->warning(sprintf(
|
||||
'حالت گزارش. %d رکورد قابل خارجکردن از انتشار است. برای اعمال: --force',
|
||||
count($unpublishable)
|
||||
));
|
||||
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
|
||||
foreach ($unpublishable as $row) {
|
||||
$row['entity'] instanceof Doctor
|
||||
? $row['entity']->setActiveDoctorAppointment(false)
|
||||
: $row['entity']->setIsActive(false);
|
||||
}
|
||||
$this->em->flush();
|
||||
|
||||
$io->success(sprintf('%d رکورد از انتشار عمومی خارج شد (حذف نشد).', count($unpublishable)));
|
||||
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
|
||||
/** @return array<int, array{entity: Doctor, hasRelations: bool, published: bool}> */
|
||||
private function auditDoctors(SymfonyStyle $io): array
|
||||
{
|
||||
$rows = [];
|
||||
$table = [];
|
||||
|
||||
/** @var Doctor $doctor */
|
||||
foreach ($this->em->getRepository(Doctor::class)->findAll() as $doctor) {
|
||||
if (!DisplayName::isPlaceholder($doctor->getName())) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$appointments = (int) $this->em->createQuery(
|
||||
'SELECT COUNT(a.id) FROM App\Appointment\Entity\Appointment a WHERE a.doctor = :d'
|
||||
)->setParameter('d', $doctor)->getSingleScalarResult();
|
||||
|
||||
$rows[] = [
|
||||
'entity' => $doctor,
|
||||
'hasRelations' => $appointments > 0,
|
||||
'published' => $doctor->isActiveDoctorAppointment(),
|
||||
];
|
||||
$table[] = [
|
||||
$doctor->getId(),
|
||||
$doctor->getName(),
|
||||
$doctor->getOwnerStatus(),
|
||||
$appointments,
|
||||
$doctor->isActiveDoctorAppointment() ? 'بله' : 'خیر',
|
||||
];
|
||||
}
|
||||
|
||||
if ($table !== []) {
|
||||
$io->section(sprintf('پزشکان با نام نامعتبر (%d)', count($table)));
|
||||
$io->table(['id', 'name', 'owner_status', 'نوبتها', 'منتشر شده'], $table);
|
||||
}
|
||||
|
||||
return $rows;
|
||||
}
|
||||
|
||||
/** @return array<int, array{entity: Clinic, hasRelations: bool, published: bool}> */
|
||||
private function auditClinics(SymfonyStyle $io): array
|
||||
{
|
||||
$rows = [];
|
||||
$table = [];
|
||||
|
||||
/** @var Clinic $clinic */
|
||||
foreach ($this->em->getRepository(Clinic::class)->findAll() as $clinic) {
|
||||
if (!DisplayName::isPlaceholder($clinic->getName())) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$doctorCount = $clinic->getDoctors()->count();
|
||||
|
||||
$rows[] = [
|
||||
'entity' => $clinic,
|
||||
'hasRelations' => $doctorCount > 0,
|
||||
'published' => $clinic->isActive(),
|
||||
];
|
||||
$table[] = [
|
||||
$clinic->getId(),
|
||||
$clinic->getName(),
|
||||
$doctorCount,
|
||||
$clinic->isActive() ? 'بله' : 'خیر',
|
||||
];
|
||||
}
|
||||
|
||||
if ($table !== []) {
|
||||
$io->section(sprintf('کلینیکها با نام نامعتبر (%d)', count($table)));
|
||||
$io->table(['id', 'name', 'پزشکان', 'فعال'], $table);
|
||||
}
|
||||
|
||||
return $rows;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
namespace App\Shared\Util;
|
||||
|
||||
use App\Shared\Constant\ErrorCodes;
|
||||
use App\Shared\Exception\AppException;
|
||||
|
||||
/**
|
||||
* نام نمایشی پزشک/کلینیک در نتایج عمومی و در <title> صفحات سایت رندر میشود.
|
||||
* شمارهتلفن یا «test» نام واقعی نیست و صفحه را بیارزش میکند.
|
||||
*
|
||||
* این قواعد باید با nobat724_front/lib/entityQuality.js یکی بماند؛ سایت عمومی
|
||||
* همین رکوردها را noindex میکند. آنجا ماسک است، اینجا جلوگیری از تولید.
|
||||
*/
|
||||
final class DisplayName
|
||||
{
|
||||
private const PHONE_LIKE = '/^0?9\d{9}$/';
|
||||
|
||||
private const PLACEHOLDERS = ['test', 'تست', '-', '—', 'null', 'undefined'];
|
||||
|
||||
private const MIN_LENGTH = 2;
|
||||
|
||||
/** نامی که نباید در نتایج عمومی منتشر شود. */
|
||||
public static function isPlaceholder(?string $name): bool
|
||||
{
|
||||
$trimmed = trim((string) $name);
|
||||
if ($trimmed === '' || mb_strlen($trimmed) < self::MIN_LENGTH) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (preg_match(self::PHONE_LIKE, str_replace([' ', '-', ''], '', $trimmed)) === 1) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return in_array(mb_strtolower($trimmed), self::PLACEHOLDERS, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws AppException وقتی نام واقعی نباشد — ExceptionSubscriber آن را به 422 تبدیل میکند
|
||||
*/
|
||||
public static function assertReal(?string $name): void
|
||||
{
|
||||
if (self::isPlaceholder($name)) {
|
||||
throw new AppException(
|
||||
ErrorCodes::ERR_VALIDATION_001,
|
||||
'نام معتبر نیست؛ شماره تلفن یا مقدار آزمایشی بهعنوان نام پذیرفته نمیشود',
|
||||
422
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user