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 target2 = `} elseif ($subjectRef) { try { // Try to find the latest prescription for this patient ref $latestPrescription = \\App\\Entities\\PatientHealth::where('patient_id', $subjectRef) ->whereIn('slug', ['prescription', 'patient-prescription', 'medication-prescription']) ->latest() ->first();`; const replace2 = `} elseif ($subjectRef) { try { // Find patient ID from users table using full name match $patient = \\App\\Entities\\User::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();`; code = code.replace(target2, replace2); fs.writeFileSync(path, code); console.log("Successfully patched PharmacyWebhookController fallback logic");