fix : SurgeryAppointment

This commit is contained in:
SajjadMahmoody 2026-04-19 00:57:40 +03:30
parent a02631a3fc
commit e807773403
2 changed files with 27 additions and 60 deletions

View File

@ -662,11 +662,20 @@ public static function table(Table $table): Table
'surgeryDateFull' => $surgeryDateTime->toDateTimeString(), 'surgeryDateFull' => $surgeryDateTime->toDateTimeString(),
]; ];
$appointment = SurgeryAppointment::updateOrCreate(
['patient_id' => $record->id],
[
'surgery_date' => $surgeryDateTime,
'surgery_center_id' => $data['surgery_center_id'],
]
);
try { try {
$response = \Illuminate\Support\Facades\Http::timeout(30) $response = \Illuminate\Support\Facades\Http::timeout(30)
->withHeader('X-API-Key', $apiKey) ->withHeader('X-API-Key', $apiKey)
->asJson() ->asJson()
->post($apiUrl . '/surgery-reminder', $smsPayload); ->post($apiUrl . '/surgery-reminder', array_merge($smsPayload, [
'appointmentId' => $appointment->id,
]));
if (! ($response->json('success') ?? false)) { if (! ($response->json('success') ?? false)) {
\Filament\Notifications\Notification::make() \Filament\Notifications\Notification::make()
@ -689,24 +698,6 @@ public static function table(Table $table): Table
return; return;
} }
$appointment = SurgeryAppointment::updateOrCreate(
['patient_id' => $record->id],
[
'surgery_date' => $surgeryDateTime,
'surgery_center_id' => $data['surgery_center_id'],
]
);
try {
\Illuminate\Support\Facades\Http::timeout(30)
->withHeader('X-API-Key', $apiKey)
->asJson()
->post($apiUrl . '/surgery-reminder', array_merge($smsPayload, [
'appointmentId' => $appointment->id,
]));
} catch (\Throwable) {
}
\Filament\Notifications\Notification::make() \Filament\Notifications\Notification::make()
->title(__('patients.actions.appointment_saved')) ->title(__('patients.actions.appointment_saved'))
->body(__('patients.actions.sms_sent')) ->body(__('patients.actions.sms_sent'))

View File

@ -30,30 +30,30 @@ protected function mutateFormDataBeforeCreate(array $data): array
return $data; return $data;
} }
protected function beforeCreate(): void protected function afterCreate(): void
{ {
$data = $this->form->getState(); $data = $this->form->getState();
$dateStr = $data['surgery_date_date'] ?? ''; $dateStr = $data['surgery_date_date'] ?? $this->record->surgery_date?->format('Y-m-d') ?? '';
$timeStr = $data['surgery_date_time'] ?? '09:00'; $timeStr = $data['surgery_date_time'] ?? $this->record->surgery_date?->format('H:i') ?? '09:00';
$surgeryDateTime = \Carbon\Carbon::parse($dateStr . ' ' . $timeStr); $surgeryDateTime = \Carbon\Carbon::parse($dateStr . ' ' . $timeStr);
$jalaliDate = Jalalian::fromCarbon($surgeryDateTime)->format('Y/m/d'); $jalaliDate = Jalalian::fromCarbon($surgeryDateTime)->format('Y/m/d');
$patient = Patient::find($data['patient_id'] ?? null); $patient = Patient::find($data['patient_id'] ?? $this->record->patient_id);
$apiKey = \App\Models\OsurgInitial::val('sms_api_key', config('sms.api_key')); $apiKey = \App\Models\OsurgInitial::val('sms_api_key', config('sms.api_key'));
$apiUrl = \App\Models\OsurgInitial::val('sms_api_url', config('sms.api_url')); $apiUrl = \App\Models\OsurgInitial::val('sms_api_url', config('sms.api_url'));
$smsPayload = [
'phoneNumber' => $patient?->hand_phone ?? '',
'patientName' => $patient?->full_name ?? '',
'surgeryDate' => $jalaliDate,
'patientId' => $patient?->id,
'surgeryDateFull' => $surgeryDateTime->toDateTimeString(),
];
try { try {
$response = Http::timeout(30) $response = Http::timeout(30)
->withHeader('X-API-Key', $apiKey) ->withHeader('X-API-Key', $apiKey)
->asJson() ->asJson()
->post($apiUrl . '/surgery-reminder', $smsPayload); ->post($apiUrl . '/surgery-reminder', [
'phoneNumber' => $patient?->hand_phone ?? '',
'patientName' => $patient?->full_name ?? '',
'surgeryDate' => $jalaliDate,
'appointmentId' => $this->record->id,
'patientId' => $patient?->id,
'surgeryDateFull' => $surgeryDateTime->toDateTimeString(),
]);
if (! ($response->json('success') ?? false)) { if (! ($response->json('success') ?? false)) {
Notification::make() Notification::make()
@ -62,13 +62,13 @@ protected function beforeCreate(): void
->danger() ->danger()
->send(); ->send();
$this->halt();
return; return;
} }
} catch (\Throwable $e) { } catch (\Throwable $e) {
Log::warning('Surgery appointment SMS failed', [ Log::warning('Surgery appointment SMS failed', [
'patient_id' => $patient?->id, 'patient_id' => $patient?->id,
'error' => $e->getMessage(), 'appointment_id' => $this->record->id,
'error' => $e->getMessage(),
]); ]);
Notification::make() Notification::make()
@ -77,33 +77,9 @@ protected function beforeCreate(): void
->danger() ->danger()
->send(); ->send();
$this->halt();
return; return;
} }
$this->smsApiKey = $apiKey;
$this->smsApiUrl = $apiUrl;
$this->smsSentPayload = $smsPayload;
}
public string $smsApiKey = '';
public string $smsApiUrl = '';
public array $smsSentPayload = [];
protected function afterCreate(): void
{
if (! empty($this->smsSentPayload) && $this->record) {
try {
Http::timeout(30)
->withHeader('X-API-Key', $this->smsApiKey)
->asJson()
->post($this->smsApiUrl . '/surgery-reminder', array_merge($this->smsSentPayload, [
'appointmentId' => $this->record->id,
]));
} catch (\Throwable) {
}
}
Notification::make() Notification::make()
->title(__('patients.actions.appointment_saved')) ->title(__('patients.actions.appointment_saved'))
->body(__('patients.actions.sms_sent')) ->body(__('patients.actions.sms_sent'))