const fs = require('fs'); const path = 'C:/Users/CSH12/Desktop/Hospital_Api/all-applications-api/app/Services/PharmacyWebhookController.php'; let code = fs.readFileSync(path, 'utf8'); const target4 = ` if ($prescriptionId) { try { $latestPrescription = \\App\\Entities\\PatientHealth::find($prescriptionId); if ($latestPrescription) { $values = is_array($latestPrescription->values) ? $latestPrescription->values : (array)$latestPrescription->values; foreach ($values as &$medicine) { if (is_object($medicine)) { $medicine->dispensing_status = 'dispensed'; $medicine->dispensing_id = $dispensingId; $medicine->dispensed_at = $occurredAt; } elseif (is_array($medicine)) { $medicine['dispensing_status'] = 'dispensed'; $medicine['dispensing_id'] = $dispensingId; $medicine['dispensed_at'] = $occurredAt; } } $latestPrescription->values = array_values($values); $latestPrescription->save(); } else { \\Illuminate\\Support\\Facades\\Log::info("[PharmacyWebhook] No recent prescription found to update for patient: " . $subjectRef); } } catch (\\Exception $e) { \\Illuminate\\Support\\Facades\\Log::error("[PharmacyWebhook] Error updating prescription by ID: " . $e->getMessage()); } } elseif ($subjectRef) { try { // Find patient ID from users table using full name match via DB to bypass any scopes $patient = \\Illuminate\\Support\\Facades\\DB::table('users') ->whereRaw("LOWER(CONCAT(first_name, ' ', last_name)) = ?", [strtolower(trim($subjectRef))]) ->orWhere('id', $subjectRef) ->first(); $patientIdToSearch = $patient ? $patient->id : $subjectRef; // Try to find the latest prescription for this patient ID $latestPrescription = \\App\\Entities\\PatientHealth::where('patient_id', $patientIdToSearch) ->latest() ->first(); if ($latestPrescription) {`; const replace4 = ` $latestPrescription = null; if ($prescriptionId && is_numeric($prescriptionId)) { try { $latestPrescription = \\App\\Entities\\PatientHealth::find($prescriptionId); } catch (\\Exception $e) { \\Illuminate\\Support\\Facades\\Log::error("[PharmacyWebhook] Error updating prescription by ID: " . $e->getMessage()); } } if (!$latestPrescription && $subjectRef) { try { // Find patient ID from users table using full name match via DB to bypass any scopes $patient = \\Illuminate\\Support\\Facades\\DB::table('users') ->whereRaw("LOWER(CONCAT(first_name, ' ', last_name)) = ?", [strtolower(trim($subjectRef))]) ->orWhere('id', $subjectRef) ->first(); $patientIdToSearch = $patient ? $patient->id : $subjectRef; // Try to find the latest prescription for this patient ID $latestPrescription = \\App\\Entities\\PatientHealth::where('patient_id', $patientIdToSearch) ->latest() ->first(); } catch (\\Exception $e) { \\Illuminate\\Support\\Facades\\Log::error("[PharmacyWebhook] Error updating prescription by SubjectRef: " . $e->getMessage()); } } if ($latestPrescription) {`; code = code.replace(target4, replace4); fs.writeFileSync(path, code); console.log("Successfully patched PharmacyWebhookController to fallback correctly");