feat: initialize Dr-Panel project with patient file number support
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Observers;
|
||||
|
||||
use App\Models\PatientIllness;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
class PatientIllnessObserver
|
||||
{
|
||||
public function saved(PatientIllness $patientIllness): void
|
||||
{
|
||||
Cache::forget('illness_options_1');
|
||||
Cache::forget('illness_options_2');
|
||||
}
|
||||
|
||||
public function deleted(PatientIllness $patientIllness): void
|
||||
{
|
||||
Cache::forget('illness_options_1');
|
||||
Cache::forget('illness_options_2');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Observers;
|
||||
|
||||
use App\Models\Patient;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
|
||||
class PatientObserver
|
||||
{
|
||||
private const FILE_FIELDS = ['photos_before', 'photos_after', 'videos', 'audio_files'];
|
||||
|
||||
public function creating(Patient $patient): void
|
||||
{
|
||||
if (empty($patient->file_number)) {
|
||||
$max = DB::table('patients')->max('file_number');
|
||||
$patient->file_number = max(100, ($max ?? 99) + 1);
|
||||
}
|
||||
}
|
||||
|
||||
public function updating(Patient $patient): void
|
||||
{
|
||||
foreach (self::FILE_FIELDS as $field) {
|
||||
$rawOld = $patient->getRawOriginal($field);
|
||||
$oldFiles = ! empty($rawOld) ? (json_decode($rawOld, true) ?? []) : [];
|
||||
$newFiles = array_values((array) ($patient->getAttribute($field) ?? []));
|
||||
$removed = array_diff($oldFiles, $newFiles);
|
||||
|
||||
foreach ($removed as $path) {
|
||||
if (! empty(trim((string) $path))) {
|
||||
Storage::disk('public')->delete($path);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function deleting(Patient $patient): void
|
||||
{
|
||||
foreach (self::FILE_FIELDS as $field) {
|
||||
$files = $patient->getAttribute($field) ?? [];
|
||||
|
||||
foreach ((array) $files as $path) {
|
||||
Storage::disk('public')->delete($path);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Observers;
|
||||
|
||||
use App\Models\SyncLog;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class SyncObserver
|
||||
{
|
||||
public function created(Model $model): void
|
||||
{
|
||||
if (app()->bound('sync.applying')) {
|
||||
return;
|
||||
}
|
||||
|
||||
SyncLog::create([
|
||||
'datetime' => now(),
|
||||
'ip' => request()->ip() ?? '127.0.0.1',
|
||||
'user' => auth()->user()?->name ?? 'system',
|
||||
'table_name' => $model->getTable(),
|
||||
'action' => 'created',
|
||||
'record_id' => $model->getKey(),
|
||||
'changed_data' => $model->getAttributes(),
|
||||
]);
|
||||
}
|
||||
|
||||
public function updated(Model $model): void
|
||||
{
|
||||
if (app()->bound('sync.applying')) {
|
||||
return;
|
||||
}
|
||||
|
||||
$dirty = $model->getDirty();
|
||||
if (empty($dirty)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$changedData = [];
|
||||
foreach ($dirty as $field => $rawValue) {
|
||||
$changedData[$field] = $rawValue instanceof \BackedEnum
|
||||
? $rawValue->value
|
||||
: (is_object($rawValue) ? (string) $rawValue : $rawValue);
|
||||
}
|
||||
|
||||
SyncLog::create([
|
||||
'datetime' => now(),
|
||||
'ip' => request()->ip() ?? '127.0.0.1',
|
||||
'user' => auth()->user()?->name ?? 'system',
|
||||
'table_name' => $model->getTable(),
|
||||
'action' => 'updated',
|
||||
'record_id' => $model->getKey(),
|
||||
'changed_data' => $changedData,
|
||||
]);
|
||||
}
|
||||
|
||||
public function deleted(Model $model): void
|
||||
{
|
||||
if (app()->bound('sync.applying')) {
|
||||
return;
|
||||
}
|
||||
|
||||
SyncLog::create([
|
||||
'datetime' => now(),
|
||||
'ip' => request()->ip() ?? '127.0.0.1',
|
||||
'user' => auth()->user()?->name ?? 'system',
|
||||
'table_name' => $model->getTable(),
|
||||
'action' => 'deleted',
|
||||
'record_id' => $model->getKey(),
|
||||
'changed_data' => null,
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user