fix: sync patient updated_at when visit is created or updated

This commit is contained in:
SajjadMahmoody 2026-04-29 00:49:08 +03:30
parent 40edd93fa5
commit d19191a320
5 changed files with 49 additions and 18 deletions

View File

@ -13,7 +13,6 @@ class Visit extends Model
{
use HasUserStamps;
protected $touches = ['patientRecord'];
protected $fillable = [
'patient',
'visit_date',

View File

@ -0,0 +1,30 @@
<?php
declare(strict_types=1);
namespace App\Observers;
use App\Models\Visit;
class VisitObserver
{
public function created(Visit $visit): void
{
$this->touchPatient($visit);
}
public function updated(Visit $visit): void
{
$this->touchPatient($visit);
}
private function touchPatient(Visit $visit): void
{
$patient = $visit->patientRecord;
if (! $patient) {
return;
}
$patient->updated_at = now();
$patient->save();
}
}

View File

@ -18,6 +18,7 @@
use App\Observers\PatientIllnessObserver;
use App\Observers\PatientObserver;
use App\Observers\SyncObserver;
use App\Observers\VisitObserver;
use App\Filament\Widgets\VisitStatsWidget;
use Filament\Actions\DeleteAction;
use Filament\Actions\DeleteBulkAction;
@ -59,6 +60,7 @@ public function boot(): void
Prescription::observe($observer);
SurgeryAppointment::observe($observer);
Visit::observe($observer);
Visit::observe(VisitObserver::class);
User::observe($observer);
Role::observe($observer);

View File

@ -9,39 +9,39 @@ echo =============================================
echo Fix Patient updated_at - Sync Repair
echo =============================================
echo.
echo این اسکریپت زمان ویرایش بیماران رو در صف sync
echo قرار میده تا دفعه بعد sync درست منتقل بشه.
echo This script queues the correct updated_at for all patients
echo in sync_log so the next sync pushes them to the peer system.
echo.
if not exist "!PHP!" (
echo [ERROR] php.exe پیدا نشد: !PHP!
echo [ERROR] php.exe not found: !PHP!
pause & exit /b 1
)
if not exist "!SCRIPTS!\fix_patient_updated_at.php" (
echo [ERROR] fix_patient_updated_at.php پیدا نشد.
echo [ERROR] fix_patient_updated_at.php not found.
pause & exit /b 1
)
echo [1/2] Dry run - بررسی بیماران...
echo [1/2] Dry run - listing patients...
echo.
"!PHP!" "!SCRIPTS!\fix_patient_updated_at.php"
echo.
set /p CONFIRM="آیا اعمال کنم؟ (y/n): "
set /p CONFIRM="Apply changes? (y/n): "
if /i "!CONFIRM!" neq "y" (
echo لغو شد.
echo Cancelled.
pause & exit /b 0
)
echo.
echo [2/2] در حال نوشتن در sync_log...
echo [2/2] Writing to sync_log...
echo.
"!PHP!" "!SCRIPTS!\fix_patient_updated_at.php" --apply
echo.
echo =============================================
echo Done! حالا از صفحه همگام سازی sync بزن.
echo Done! Run sync from the Sync page.
echo =============================================
echo.
pause

View File

@ -15,8 +15,8 @@
echo "\n=== Fix Patient updated_at in sync_log ===\n";
echo $dryRun
? "Mode: DRY RUN — برای اعمال واقعی با --apply اجرا کن\n\n"
: "Mode: APPLY — در حال نوشتن در sync_log...\n\n";
? "Mode: DRY RUN — run with --apply to write to sync_log\n\n"
: "Mode: APPLY — writing entries to sync_log...\n\n";
$patients = DB::table('patients')->get(['id', 'first_name', 'last_name', 'updated_at']);
@ -25,13 +25,13 @@
foreach ($patients as $patient) {
if (empty($patient->updated_at)) {
echo " [SKIP] patient#{$patient->id} ({$patient->first_name} {$patient->last_name}) — updated_at خالیه\n";
echo " [SKIP] patient#{$patient->id} ({$patient->first_name} {$patient->last_name}) — updated_at is empty\n";
continue;
}
echo " [" . ($dryRun ? 'DRY' : 'OK ') . "] patient#{$patient->id}"
. " ({$patient->first_name} {$patient->last_name})"
. " updated_at: {$patient->updated_at}\n";
. " => updated_at: {$patient->updated_at}\n";
if (! $dryRun) {
DB::table('sync_log')->insert([
@ -49,13 +49,13 @@
}
echo "\n─────────────────────────────────\n";
echo "تعداد بیماران: {$count}\n";
echo "Total patients: {$count}\n";
if ($dryRun) {
echo "\nبرای اعمال واقعی اجرا کن:\n php scripts/fix_patient_updated_at.php --apply\n";
echo "\nTo apply, run:\n php scripts/fix_patient_updated_at.php --apply\n";
} else {
echo "\n{$count} entry در sync_log ایجاد شد.\n";
echo "حالا از صفحه همگام‌سازی، sync رو اجرا کن تا لپ‌تاپ دکتر به‌روز بشه.\n";
echo "\n[OK] {$count} entries written to sync_log.\n";
echo "Now run sync from the Sync page to push changes to the peer system.\n";
}
echo "\nDone!\n";