mirror of
https://github.com/AmanTahiliani/FHIR-Sandbox.git
synced 2026-08-07 19:56:17 -04:00
Improved FHIR handling
This commit is contained in:
@@ -3,18 +3,24 @@ package handlers
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/AmanTahiliani/FHIR-Sandbox/app/fhir"
|
||||
"github.com/AmanTahiliani/FHIR-Sandbox/app/middleware"
|
||||
)
|
||||
|
||||
// HandleSync performs a live FHIR pull for Observations, Conditions, and
|
||||
// DocumentReferences for the session's patient, upserts all results into the
|
||||
// database, records a PatientSync event, then redirects back to GET /dashboard.
|
||||
// HandleSync performs a live FHIR pull for Observations, Conditions, DocumentReferences,
|
||||
// MedicationRequests, and AllergyIntolerances for the session's patient, upserts all
|
||||
// results into the database, records a PatientSync event, then redirects back to
|
||||
// GET /dashboard?synced=true.
|
||||
//
|
||||
// POST /dashboard/sync
|
||||
// Incremental sync: If a previous sync exists, only fetches resources updated since
|
||||
// the last sync time (using FHIR _lastUpdated parameter).
|
||||
//
|
||||
// GET /dashboard/sync (auto-sync on first dashboard load)
|
||||
// POST /dashboard/sync (manual sync from dashboard UI)
|
||||
func (h *Handler) HandleSync(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
if r.Method != http.MethodGet && r.Method != http.MethodPost {
|
||||
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
@@ -27,12 +33,29 @@ func (h *Handler) HandleSync(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
ehrURL := sess.EHRURL
|
||||
patientID := sess.PatientFHIRID
|
||||
|
||||
// Allow overriding the patient context via a query parameter.
|
||||
if overrideID := r.URL.Query().Get("patient_id"); overrideID != "" {
|
||||
patientID = overrideID
|
||||
}
|
||||
|
||||
// Determine if this is an incremental sync
|
||||
latestSync, err := h.store.LatestSync(patientID, ehrURL)
|
||||
if err != nil {
|
||||
log.Printf("handlers: sync LatestSync Patient/%s: %v", patientID, err)
|
||||
}
|
||||
|
||||
var sinceTime string
|
||||
if latestSync != nil {
|
||||
sinceTime = latestSync.SyncedAt.Format(time.RFC3339)
|
||||
}
|
||||
|
||||
client := fhir.NewClient(ehrURL, sess.AccessToken)
|
||||
|
||||
// -----------------------------------------------------------------
|
||||
// Fetch Observations
|
||||
// -----------------------------------------------------------------
|
||||
rawObs, err := client.GetObservations(patientID)
|
||||
rawObs, err := client.GetObservations(patientID, sinceTime)
|
||||
if err != nil {
|
||||
log.Printf("handlers: sync GetObservations for Patient/%s: %v", patientID, err)
|
||||
// Non-fatal; continue with whatever we got.
|
||||
@@ -51,7 +74,7 @@ func (h *Handler) HandleSync(w http.ResponseWriter, r *http.Request) {
|
||||
// -----------------------------------------------------------------
|
||||
// Fetch Conditions
|
||||
// -----------------------------------------------------------------
|
||||
rawConds, err := client.GetConditions(patientID)
|
||||
rawConds, err := client.GetConditions(patientID, sinceTime)
|
||||
if err != nil {
|
||||
log.Printf("handlers: sync GetConditions for Patient/%s: %v", patientID, err)
|
||||
}
|
||||
@@ -69,7 +92,7 @@ func (h *Handler) HandleSync(w http.ResponseWriter, r *http.Request) {
|
||||
// -----------------------------------------------------------------
|
||||
// Fetch DocumentReferences
|
||||
// -----------------------------------------------------------------
|
||||
rawDocs, err := client.GetDocumentReferences(patientID)
|
||||
rawDocs, err := client.GetDocumentReferences(patientID, sinceTime)
|
||||
if err != nil {
|
||||
log.Printf("handlers: sync GetDocumentReferences for Patient/%s: %v", patientID, err)
|
||||
}
|
||||
@@ -84,6 +107,42 @@ func (h *Handler) HandleSync(w http.ResponseWriter, r *http.Request) {
|
||||
docCount++
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------
|
||||
// Fetch MedicationRequests
|
||||
// -----------------------------------------------------------------
|
||||
rawMeds, err := client.GetMedicationRequests(patientID, sinceTime)
|
||||
if err != nil {
|
||||
log.Printf("handlers: sync GetMedicationRequests for Patient/%s: %v", patientID, err)
|
||||
}
|
||||
|
||||
medCount := 0
|
||||
for i := range rawMeds {
|
||||
m := fhir.ExtractMedicationRequest(&rawMeds[i], patientID, ehrURL)
|
||||
if _, err := h.store.UpsertMedicationRequest(m); err != nil {
|
||||
log.Printf("handlers: sync UpsertMedicationRequest fhir_id=%s: %v", m.FHIRID, err)
|
||||
continue
|
||||
}
|
||||
medCount++
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------
|
||||
// Fetch AllergyIntolerances
|
||||
// -----------------------------------------------------------------
|
||||
rawAllergies, err := client.GetAllergyIntolerances(patientID, sinceTime)
|
||||
if err != nil {
|
||||
log.Printf("handlers: sync GetAllergyIntolerances for Patient/%s: %v", patientID, err)
|
||||
}
|
||||
|
||||
allergyCount := 0
|
||||
for i := range rawAllergies {
|
||||
m := fhir.ExtractAllergyIntolerance(&rawAllergies[i], patientID, ehrURL)
|
||||
if _, err := h.store.UpsertAllergyIntolerance(m); err != nil {
|
||||
log.Printf("handlers: sync UpsertAllergyIntolerance fhir_id=%s: %v", m.FHIRID, err)
|
||||
continue
|
||||
}
|
||||
allergyCount++
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------
|
||||
// Record the sync event
|
||||
// -----------------------------------------------------------------
|
||||
@@ -91,8 +150,12 @@ func (h *Handler) HandleSync(w http.ResponseWriter, r *http.Request) {
|
||||
log.Printf("handlers: sync RecordSync Patient/%s: %v", patientID, err)
|
||||
}
|
||||
|
||||
log.Printf("handlers: sync complete for Patient/%s — obs=%d cond=%d docs=%d",
|
||||
patientID, obsCount, condCount, docCount)
|
||||
log.Printf("handlers: sync complete for Patient/%s — obs=%d cond=%d docs=%d med=%d allergy=%d",
|
||||
patientID, obsCount, condCount, docCount, medCount, allergyCount)
|
||||
|
||||
http.Redirect(w, r, "/dashboard", http.StatusSeeOther)
|
||||
dashboardURL := "/dashboard?synced=true"
|
||||
if overrideID := r.URL.Query().Get("patient_id"); overrideID != "" {
|
||||
dashboardURL += "&patient_id=" + overrideID
|
||||
}
|
||||
http.Redirect(w, r, dashboardURL, http.StatusSeeOther)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user