Improved FHIR handling

This commit is contained in:
2026-02-20 17:17:26 -05:00
parent 63cb2b7b1c
commit f21d2039cb
16 changed files with 1188 additions and 131 deletions

View File

@@ -32,21 +32,25 @@ func (s *Store) UpsertObservation(o *models.Observation) (string, error) {
if err == nil {
_, err = s.db.Exec(`
UPDATE observations SET
patient_fhir_id = ?,
status = ?,
category = ?,
code_text = ?,
code_system = ?,
code_code = ?,
effective_date = ?,
value_quantity = ?,
value_unit = ?,
value_string = ?,
synced_at = ?
patient_fhir_id = ?,
status = ?,
category = ?,
code_text = ?,
code_system = ?,
code_code = ?,
effective_date = ?,
value_quantity = ?,
value_unit = ?,
value_string = ?,
interpretation = ?,
ref_range_low = ?,
ref_range_high = ?,
synced_at = ?
WHERE id = ?`,
o.PatientFHIRID, o.Status, o.Category,
o.CodeText, o.CodeSystem, o.CodeCode,
o.EffectiveDate, o.ValueQuantity, o.ValueUnit, o.ValueString,
o.Interpretation, o.ReferenceRangeLow, o.ReferenceRangeHigh,
now, existingID,
)
if err != nil {
@@ -60,11 +64,13 @@ func (s *Store) UpsertObservation(o *models.Observation) (string, error) {
INSERT INTO observations (
id, fhir_id, ehr_url, patient_fhir_id, status, category,
code_text, code_system, code_code, effective_date,
value_quantity, value_unit, value_string, synced_at
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
value_quantity, value_unit, value_string, interpretation,
ref_range_low, ref_range_high, synced_at
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
id, o.FHIRID, o.EHRURL, o.PatientFHIRID, o.Status, o.Category,
o.CodeText, o.CodeSystem, o.CodeCode, o.EffectiveDate,
o.ValueQuantity, o.ValueUnit, o.ValueString, now,
o.ValueQuantity, o.ValueUnit, o.ValueString, o.Interpretation,
o.ReferenceRangeLow, o.ReferenceRangeHigh, now,
)
if err != nil {
return "", fmt.Errorf("db: insert observation fhir_id=%s: %w", o.FHIRID, err)
@@ -77,7 +83,8 @@ func (s *Store) ListObservations(patientFHIRID, ehrURL string) ([]models.Observa
rows, err := s.db.Query(`
SELECT id, fhir_id, ehr_url, patient_fhir_id, status, category,
code_text, code_system, code_code, effective_date,
value_quantity, value_unit, value_string, synced_at
value_quantity, value_unit, value_string, interpretation,
ref_range_low, ref_range_high, synced_at
FROM observations
WHERE patient_fhir_id = ? AND ehr_url = ?
ORDER BY effective_date DESC`,
@@ -94,7 +101,8 @@ func (s *Store) ListObservations(patientFHIRID, ehrURL string) ([]models.Observa
if err := rows.Scan(
&o.ID, &o.FHIRID, &o.EHRURL, &o.PatientFHIRID, &o.Status, &o.Category,
&o.CodeText, &o.CodeSystem, &o.CodeCode, &o.EffectiveDate,
&o.ValueQuantity, &o.ValueUnit, &o.ValueString, &o.SyncedAt,
&o.ValueQuantity, &o.ValueUnit, &o.ValueString, &o.Interpretation,
&o.ReferenceRangeLow, &o.ReferenceRangeHigh, &o.SyncedAt,
); err != nil {
return nil, fmt.Errorf("db: scan observation: %w", err)
}
@@ -289,6 +297,181 @@ func (s *Store) ListDocumentReferences(patientFHIRID, ehrURL string) ([]models.D
return out, rows.Err()
}
// ---------------------------------------------------------------------------
// MedicationRequest
// ---------------------------------------------------------------------------
// UpsertMedicationRequest inserts or updates a MedicationRequest record keyed on (fhir_id, ehr_url).
func (s *Store) UpsertMedicationRequest(m *models.MedicationRequest) (string, error) {
now := time.Now().UTC()
var existingID string
err := s.db.QueryRow(
`SELECT id FROM medication_requests WHERE fhir_id = ? AND ehr_url = ?`,
m.FHIRID, m.EHRURL,
).Scan(&existingID)
if err == nil {
_, err = s.db.Exec(`
UPDATE medication_requests SET
patient_fhir_id = ?,
status = ?,
intent = ?,
med_code_text = ?,
med_code_system = ?,
med_code_code = ?,
authored_on = ?,
requester_display = ?,
dosage_text = ?,
synced_at = ?
WHERE id = ?`,
m.PatientFHIRID, m.Status, m.Intent,
m.MedCodeText, m.MedCodeSystem, m.MedCodeCode,
m.AuthoredOn, m.RequesterDisplay, m.DosageText,
now, existingID,
)
if err != nil {
return "", fmt.Errorf("db: update medication_request %s: %w", existingID, err)
}
return existingID, nil
}
id := uuid.NewString()
_, err = s.db.Exec(`
INSERT INTO medication_requests (
id, fhir_id, ehr_url, patient_fhir_id,
status, intent, med_code_text, med_code_system, med_code_code,
authored_on, requester_display, dosage_text, synced_at
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
id, m.FHIRID, m.EHRURL, m.PatientFHIRID,
m.Status, m.Intent, m.MedCodeText, m.MedCodeSystem, m.MedCodeCode,
m.AuthoredOn, m.RequesterDisplay, m.DosageText, now,
)
if err != nil {
return "", fmt.Errorf("db: insert medication_request fhir_id=%s: %w", m.FHIRID, err)
}
return id, nil
}
// ListMedicationRequests returns all MedicationRequests for the given patient, newest first.
func (s *Store) ListMedicationRequests(patientFHIRID, ehrURL string) ([]models.MedicationRequest, error) {
rows, err := s.db.Query(`
SELECT id, fhir_id, ehr_url, patient_fhir_id,
status, intent, med_code_text, med_code_system, med_code_code,
authored_on, requester_display, dosage_text, synced_at
FROM medication_requests
WHERE patient_fhir_id = ? AND ehr_url = ?
ORDER BY authored_on DESC`,
patientFHIRID, ehrURL,
)
if err != nil {
return nil, fmt.Errorf("db: list medication_requests: %w", err)
}
defer rows.Close()
var out []models.MedicationRequest
for rows.Next() {
var m models.MedicationRequest
if err := rows.Scan(
&m.ID, &m.FHIRID, &m.EHRURL, &m.PatientFHIRID,
&m.Status, &m.Intent, &m.MedCodeText, &m.MedCodeSystem, &m.MedCodeCode,
&m.AuthoredOn, &m.RequesterDisplay, &m.DosageText, &m.SyncedAt,
); err != nil {
return nil, fmt.Errorf("db: scan medication_request: %w", err)
}
out = append(out, m)
}
return out, rows.Err()
}
// ---------------------------------------------------------------------------
// AllergyIntolerance
// ---------------------------------------------------------------------------
// UpsertAllergyIntolerance inserts or updates an AllergyIntolerance record keyed on (fhir_id, ehr_url).
func (s *Store) UpsertAllergyIntolerance(a *models.AllergyIntolerance) (string, error) {
now := time.Now().UTC()
var existingID string
err := s.db.QueryRow(
`SELECT id FROM allergy_intolerances WHERE fhir_id = ? AND ehr_url = ?`,
a.FHIRID, a.EHRURL,
).Scan(&existingID)
if err == nil {
_, err = s.db.Exec(`
UPDATE allergy_intolerances SET
patient_fhir_id = ?,
clinical_status = ?,
verification_status = ?,
type = ?,
category = ?,
criticality = ?,
code_text = ?,
code_system = ?,
code_code = ?,
recorded_date = ?,
synced_at = ?
WHERE id = ?`,
a.PatientFHIRID, a.ClinicalStatus, a.VerificationStatus,
a.Type, a.Category, a.Criticality,
a.CodeText, a.CodeSystem, a.CodeCode,
a.RecordedDate, now, existingID,
)
if err != nil {
return "", fmt.Errorf("db: update allergy_intolerance %s: %w", existingID, err)
}
return existingID, nil
}
id := uuid.NewString()
_, err = s.db.Exec(`
INSERT INTO allergy_intolerances (
id, fhir_id, ehr_url, patient_fhir_id,
clinical_status, verification_status, type, category, criticality,
code_text, code_system, code_code, recorded_date, synced_at
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
id, a.FHIRID, a.EHRURL, a.PatientFHIRID,
a.ClinicalStatus, a.VerificationStatus, a.Type, a.Category, a.Criticality,
a.CodeText, a.CodeSystem, a.CodeCode, a.RecordedDate, now,
)
if err != nil {
return "", fmt.Errorf("db: insert allergy_intolerance fhir_id=%s: %w", a.FHIRID, err)
}
return id, nil
}
// ListAllergyIntolerances returns all AllergyIntolerances for the given patient, newest first.
func (s *Store) ListAllergyIntolerances(patientFHIRID, ehrURL string) ([]models.AllergyIntolerance, error) {
rows, err := s.db.Query(`
SELECT id, fhir_id, ehr_url, patient_fhir_id,
clinical_status, verification_status, type, category, criticality,
code_text, code_system, code_code, recorded_date, synced_at
FROM allergy_intolerances
WHERE patient_fhir_id = ? AND ehr_url = ?
ORDER BY recorded_date DESC`,
patientFHIRID, ehrURL,
)
if err != nil {
return nil, fmt.Errorf("db: list allergy_intolerances: %w", err)
}
defer rows.Close()
var out []models.AllergyIntolerance
for rows.Next() {
var a models.AllergyIntolerance
if err := rows.Scan(
&a.ID, &a.FHIRID, &a.EHRURL, &a.PatientFHIRID,
&a.ClinicalStatus, &a.VerificationStatus, &a.Type, &a.Category, &a.Criticality,
&a.CodeText, &a.CodeSystem, &a.CodeCode, &a.RecordedDate, &a.SyncedAt,
); err != nil {
return nil, fmt.Errorf("db: scan allergy_intolerance: %w", err)
}
out = append(out, a)
}
return out, rows.Err()
}
// ---------------------------------------------------------------------------
// PatientSync
// ---------------------------------------------------------------------------

View File

@@ -193,6 +193,52 @@ var migrations = []migration{
CREATE INDEX IF NOT EXISTS idx_patient_syncs_patient ON patient_syncs(patient_fhir_id, ehr_url);
`,
},
{
version: 4,
sql: `
ALTER TABLE observations ADD COLUMN interpretation TEXT NOT NULL DEFAULT '';
ALTER TABLE observations ADD COLUMN ref_range_low REAL;
ALTER TABLE observations ADD COLUMN ref_range_high REAL;
CREATE TABLE IF NOT EXISTS medication_requests (
id TEXT PRIMARY KEY,
fhir_id TEXT NOT NULL,
ehr_url TEXT NOT NULL,
patient_fhir_id TEXT NOT NULL,
status TEXT NOT NULL DEFAULT '',
intent TEXT NOT NULL DEFAULT '',
med_code_text TEXT NOT NULL DEFAULT '',
med_code_system TEXT NOT NULL DEFAULT '',
med_code_code TEXT NOT NULL DEFAULT '',
authored_on TEXT NOT NULL DEFAULT '',
requester_display TEXT NOT NULL DEFAULT '',
dosage_text TEXT NOT NULL DEFAULT '',
synced_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
UNIQUE(fhir_id, ehr_url)
);
CREATE TABLE IF NOT EXISTS allergy_intolerances (
id TEXT PRIMARY KEY,
fhir_id TEXT NOT NULL,
ehr_url TEXT NOT NULL,
patient_fhir_id TEXT NOT NULL,
clinical_status TEXT NOT NULL DEFAULT '',
verification_status TEXT NOT NULL DEFAULT '',
type TEXT NOT NULL DEFAULT '',
category TEXT NOT NULL DEFAULT '',
criticality TEXT NOT NULL DEFAULT '',
code_text TEXT NOT NULL DEFAULT '',
code_system TEXT NOT NULL DEFAULT '',
code_code TEXT NOT NULL DEFAULT '',
recorded_date TEXT NOT NULL DEFAULT '',
synced_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
UNIQUE(fhir_id, ehr_url)
);
CREATE INDEX IF NOT EXISTS idx_medication_requests_patient ON medication_requests(patient_fhir_id, ehr_url);
CREATE INDEX IF NOT EXISTS idx_allergy_intolerances_patient ON allergy_intolerances(patient_fhir_id, ehr_url);
`,
},
// Future migrations: append new entries here with incrementing version numbers.
// Example:
// {
@@ -349,6 +395,36 @@ func (s *Store) GetUserByID(id string) (*models.User, error) {
return u, nil
}
// ListUsersByRole retrieves all users with the given role and originating EHR URL.
func (s *Store) ListUsersByRole(role models.Role, ehrURL string) ([]models.User, error) {
rows, err := s.db.Query(`
SELECT id, fhir_resource_type, fhir_id, ehr_url, role,
first_name, middle_name, last_name, dob, gender, email,
created_at, updated_at
FROM users WHERE role = ? AND ehr_url = ?
ORDER BY last_name ASC, first_name ASC`,
string(role), ehrURL,
)
if err != nil {
return nil, fmt.Errorf("db: list users by role: %w", err)
}
defer rows.Close()
var users []models.User
for rows.Next() {
var u models.User
if err := rows.Scan(
&u.ID, &u.FHIRResourceType, &u.FHIRID, &u.EHRURL, &u.Role,
&u.FirstName, &u.MiddleName, &u.LastName, &u.DOB, &u.Gender, &u.Email,
&u.CreatedAt, &u.UpdatedAt,
); err != nil {
return nil, fmt.Errorf("db: scan user: %w", err)
}
users = append(users, u)
}
return users, rows.Err()
}
// ---------------------------------------------------------------------------
// Session operations
// ---------------------------------------------------------------------------

View File

@@ -145,6 +145,45 @@ func TestUpsertUser_TenantIsolation(t *testing.T) {
}
}
func TestListUsersByRole(t *testing.T) {
store := newTestStore(t)
ehrURL := "https://ehr.example.com/fhir"
users := []*models.User{
{FHIRID: "p1", EHRURL: ehrURL, Role: models.RolePatient, FirstName: "Zoe", LastName: "Adams"},
{FHIRID: "p2", EHRURL: ehrURL, Role: models.RolePatient, FirstName: "Alice", LastName: "Adams"},
{FHIRID: "d1", EHRURL: ehrURL, Role: models.RolePractitioner, FirstName: "Dr.", LastName: "House"},
{FHIRID: "p3", EHRURL: ehrURL, Role: models.RolePatient, FirstName: "Charlie", LastName: "Brown"},
}
for _, u := range users {
u.FHIRResourceType = "Patient"
if u.Role == models.RolePractitioner {
u.FHIRResourceType = "Practitioner"
}
if _, err := store.UpsertUser(u); err != nil {
t.Fatalf("failed to upsert user %s: %v", u.FHIRID, err)
}
}
got, err := store.ListUsersByRole(models.RolePatient, ehrURL)
if err != nil {
t.Fatalf("ListUsersByRole: %v", err)
}
if len(got) != 3 {
t.Errorf("got %d patients, want 3", len(got))
}
// Verify ordering: Adams, Alice -> Adams, Zoe -> Brown, Charlie
expected := []string{"Alice", "Zoe", "Charlie"}
for i, name := range expected {
if got[i].FirstName != name {
t.Errorf("at index %d: got FirstName %q, want %q", i, got[i].FirstName, name)
}
}
}
// ---------------------------------------------------------------------------
// Session tests
// ---------------------------------------------------------------------------