diff --git a/app/Filament/Resources/PatientResource.php b/app/Filament/Resources/PatientResource.php index b8470a6..c669c24 100644 --- a/app/Filament/Resources/PatientResource.php +++ b/app/Filament/Resources/PatientResource.php @@ -662,11 +662,20 @@ public static function table(Table $table): Table 'surgeryDateFull' => $surgeryDateTime->toDateTimeString(), ]; + $appointment = SurgeryAppointment::updateOrCreate( + ['patient_id' => $record->id], + [ + 'surgery_date' => $surgeryDateTime, + 'surgery_center_id' => $data['surgery_center_id'], + ] + ); try { $response = \Illuminate\Support\Facades\Http::timeout(30) ->withHeader('X-API-Key', $apiKey) ->asJson() - ->post($apiUrl . '/surgery-reminder', $smsPayload); + ->post($apiUrl . '/surgery-reminder', array_merge($smsPayload, [ + 'appointmentId' => $appointment->id, + ])); if (! ($response->json('success') ?? false)) { \Filament\Notifications\Notification::make() @@ -689,24 +698,6 @@ public static function table(Table $table): Table 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() ->title(__('patients.actions.appointment_saved')) ->body(__('patients.actions.sms_sent')) diff --git a/app/Filament/Resources/SurgeryAppointmentResource/Pages/CreateSurgeryAppointment.php b/app/Filament/Resources/SurgeryAppointmentResource/Pages/CreateSurgeryAppointment.php index e9e492b..dc4d600 100644 --- a/app/Filament/Resources/SurgeryAppointmentResource/Pages/CreateSurgeryAppointment.php +++ b/app/Filament/Resources/SurgeryAppointmentResource/Pages/CreateSurgeryAppointment.php @@ -30,30 +30,30 @@ protected function mutateFormDataBeforeCreate(array $data): array return $data; } - protected function beforeCreate(): void + protected function afterCreate(): void { $data = $this->form->getState(); - $dateStr = $data['surgery_date_date'] ?? ''; - $timeStr = $data['surgery_date_time'] ?? '09:00'; + $dateStr = $data['surgery_date_date'] ?? $this->record->surgery_date?->format('Y-m-d') ?? ''; + $timeStr = $data['surgery_date_time'] ?? $this->record->surgery_date?->format('H:i') ?? '09:00'; $surgeryDateTime = \Carbon\Carbon::parse($dateStr . ' ' . $timeStr); $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')); - $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(), - ]; + $apiKey = \App\Models\OsurgInitial::val('sms_api_key', config('sms.api_key')); + $apiUrl = \App\Models\OsurgInitial::val('sms_api_url', config('sms.api_url')); try { $response = Http::timeout(30) ->withHeader('X-API-Key', $apiKey) ->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)) { Notification::make() @@ -62,13 +62,13 @@ protected function beforeCreate(): void ->danger() ->send(); - $this->halt(); return; } } catch (\Throwable $e) { Log::warning('Surgery appointment SMS failed', [ - 'patient_id' => $patient?->id, - 'error' => $e->getMessage(), + 'patient_id' => $patient?->id, + 'appointment_id' => $this->record->id, + 'error' => $e->getMessage(), ]); Notification::make() @@ -77,33 +77,9 @@ protected function beforeCreate(): void ->danger() ->send(); - $this->halt(); 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() ->title(__('patients.actions.appointment_saved')) ->body(__('patients.actions.sms_sent'))