table('patient_master')->where('id', $patientId)->update([ 'abha_verified_status' => 'LINKED', 'abdm_linked_at' => $now, ]); $existing = $db->table('record_links')->where('care_context_reference', 'REG-P261010000' . $patientId)->get(1)->getRowArray(); if (empty($existing)) { $db->table('record_links')->insert([ 'abha_id' => $abha, 'care_context_reference' => 'REG-P261010000' . $patientId, 'link_status' => 'linked', 'linked_at' => $now, 'created_at' => $now, 'updated_at' => $now, ]); } else { $db->table('record_links')->where('id', $existing['id'])->update([ 'link_status' => 'linked', 'linked_at' => $now, 'updated_at' => $now, ]); } CLI::write("Recorded link for Patient {$patientId} in DB.", 'green'); } CLI::write("Inspecting ABHA: {$abha} (Patient ID: {$patientId})...", 'yellow'); $_SERVER['REQUEST_METHOD'] = 'GET'; $_GET['abha_address'] = $abha; $_GET['patient_id'] = (string) $patientId; $gateway = new \App\Controllers\AbdmGateway(); $request = \Config\Services::request(); $response = \Config\Services::response(); $logger = service('logger'); $gateway->initController($request, $response, $logger); $res = $gateway->hipPatientCareContexts(); $body = $res->getBody(); $data = json_decode($body, true); if (empty($data['ok'])) { CLI::error('Error: ' . ($data['error_text'] ?? 'Unknown error')); return; } $contexts = $data['care_contexts'] ?? []; CLI::write("Total Care Contexts: " . count($contexts), 'green'); CLI::newLine(); $table = []; foreach ($contexts as $cc) { $table[] = [ 'Reference' => $cc['ref'], 'HI Type' => $cc['hi_type'], 'FHIR' => $cc['is_fhir_ready'] ? 'Ready' : 'Draft', 'Status' => $cc['is_linked'] ? 'LINKED' : 'UNLINKED', 'Display' => $cc['display'], ]; } CLI::table($table, ['Reference', 'HI Type', 'FHIR', 'Status', 'Display']); $linksRes = $gateway->hipPatientLinks(); $linksData = json_decode($linksRes->getBody(), true); $linkedContexts = $linksData['care_contexts'] ?? []; if (! empty($linkedContexts)) { CLI::newLine(); CLI::write("HIP Linked Records ({$linksData['total_linked']}):", 'green'); $linkTable = []; foreach ($linkedContexts as $lc) { $linkTable[] = [ 'Reference' => $lc['referenceNumber'] ?? '', 'Display' => $lc['display'] ?? '', 'Status' => strtoupper($lc['status'] ?? 'linked'), 'Linked At' => $lc['linked_at'] ?? 'N/A', 'Source' => $lc['source'] ?? 'bridge', ]; } CLI::table($linkTable, ['Reference', 'Display', 'Status', 'Linked At', 'Source']); } if (CLI::getOption('hiu')) { CLI::newLine(); CLI::write("=== RECENT HIU CONSENTS ===", 'yellow'); if ($db->tableExists('abdm_hiu_consents')) { $cons = $db->table('abdm_hiu_consents') ->orderBy('id', 'DESC') ->limit(10) ->get() ->getResultArray(); $conTable = []; foreach ($cons as $c) { $conTable[] = [ 'ID' => $c['id'], 'Pat' => $c['patient_id'] ?? 'N/A', 'ReqID' => substr((string)($c['consent_request_id'] ?? ''), 0, 18), 'ArtID' => substr((string)($c['consent_id'] ?? ''), 0, 18), 'Status' => $c['status'] ?? '', 'Created' => $c['created_at'] ?? '', ]; } CLI::table($conTable, ['ID', 'Pat', 'ReqID', 'ArtID', 'Status', 'Created']); } CLI::newLine(); CLI::write("=== RECENT HIU DOCUMENTS ===", 'yellow'); if ($db->tableExists('abdm_hiu_documents')) { $docs = $db->table('abdm_hiu_documents') ->orderBy('id', 'DESC') ->limit(15) ->get() ->getResultArray(); $docTable = []; foreach ($docs as $d) { $docTable[] = [ 'ID' => $d['id'], 'Pat' => $d['patient_id'] ?? 'N/A', 'Ref' => substr((string)($d['care_context_reference'] ?? ''), 0, 22), 'Title' => substr((string)($d['document_title'] ?? ''), 0, 22), 'Org' => substr((string)($d['organization_name'] ?? 'N/A'), 0, 20), 'ArtID' => substr((string)($d['consent_artifact_id'] ?? 'N/A'), 0, 16), 'Date' => substr((string)($d['document_date'] ?? $d['created_at'] ?? ''), 0, 10), ]; } CLI::table($docTable, ['ID', 'Pat', 'Ref', 'Title', 'Org', 'ArtID', 'Date']); } } } }