-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: port more clinical implementation and improve dependency injec…
…tion Implement OCL as a service Improve FHIR repository dependency injection Signed-off-by: Otieno Calvine <nyarangaotieno@gmail.com>
- Loading branch information
Showing
13 changed files
with
353 additions
and
458 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,102 +1,57 @@ | ||
package fhir | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
|
||
"github.com/savannahghi/serverutils" | ||
"google.golang.org/api/healthcare/v1" | ||
) | ||
|
||
// constants used to configure the Google Cloud Healthcare API | ||
const ( | ||
DatasetLocation = "europe-west4" | ||
) | ||
|
||
// CreateDataset creates a cloud healthcare dataset and returns it's name | ||
func (s Service) CreateDataset() (*healthcare.Operation, error) { | ||
s.checkPreconditions() | ||
datasetsService := s.healthcareService.Projects.Locations.Datasets | ||
parent := fmt.Sprintf("projects/%s/locations/%s", s.projectID, s.location) | ||
resp, err := datasetsService.Create(parent, &healthcare.Dataset{}).DatasetId(s.datasetID).Do() | ||
if err != nil { | ||
return nil, fmt.Errorf("create Data Set: %v", err) | ||
} | ||
return resp, nil | ||
// Repository accesses and updates patient data that is stored on Healthcare | ||
// FHIR repository | ||
type Repository struct { | ||
Dataset DatasetExtension | ||
} | ||
|
||
// GetDataset gets a cloud healthcare dataset. | ||
func (s Service) GetDataset() (*healthcare.Dataset, error) { | ||
s.checkPreconditions() | ||
datasetsService := s.healthcareService.Projects.Locations.Datasets | ||
name := fmt.Sprintf("projects/%s/locations/%s/datasets/%s", s.projectID, s.location, s.datasetID) | ||
resp, err := datasetsService.Get(name).Do() | ||
if err != nil { | ||
return nil, fmt.Errorf("get Data Set: %v", err) | ||
// NewFHIRRepository initializes a FHIR repository | ||
func NewFHIRRepository( | ||
dataset DatasetExtension, | ||
) *Repository { | ||
return &Repository{ | ||
Dataset: dataset, | ||
} | ||
return resp, nil | ||
} | ||
|
||
// Service is an implementation of the healthcare fhir repository | ||
// It is implementation agnostic i.e logic should be handled using | ||
// the preferred datastore/database | ||
type Service struct { | ||
healthcareService *healthcare.Service | ||
projectID, location, datasetID, fhirStoreID string | ||
// CreateFHIRResource creates an FHIR resource. | ||
// | ||
// The payload should be the result of marshalling a resource to JSON | ||
func (fr Repository) CreateFHIRResource(resourceType string, payload map[string]interface{}) ([]byte, error) { | ||
return nil, nil | ||
} | ||
|
||
// NewService initializes a Google Cloud Healthcare API service | ||
func NewService() *Service { | ||
projectID := serverutils.MustGetEnvVar(serverutils.GoogleCloudProjectIDEnvVarName) | ||
_ = serverutils.MustGetEnvVar("CLOUD_HEALTH_PUBSUB_TOPIC") | ||
datasetID := serverutils.MustGetEnvVar("CLOUD_HEALTH_DATASET_ID") | ||
fhirStoreID := serverutils.MustGetEnvVar("CLOUD_HEALTH_FHIRSTORE_ID") | ||
|
||
ctx := context.Background() | ||
hsv, err := healthcare.NewService(ctx) | ||
if err != nil { | ||
log.Panicf("unable to initialize new Google Cloud Healthcare Service: %s", err) | ||
} | ||
service := &Service{ | ||
healthcareService: hsv, | ||
projectID: projectID, | ||
location: DatasetLocation, | ||
datasetID: datasetID, | ||
fhirStoreID: fhirStoreID, | ||
} | ||
|
||
// ensure that the dataset exists | ||
_, err = service.GetDataset() | ||
if err != nil { | ||
_, err = service.CreateDataset() | ||
if err != nil { | ||
log.Printf( | ||
"Unable to get or create dataset with projectID %s, "+ | ||
"location %s, datasetID %s; got error %s", | ||
service.projectID, service.location, service.datasetID, err, | ||
) | ||
} | ||
} | ||
// DeleteFHIRResource deletes an FHIR resource. | ||
func (fr Repository) DeleteFHIRResource(resourceType, fhirResourceID string) ([]byte, error) { | ||
return nil, nil | ||
} | ||
|
||
// ensure that the FHIR store exists | ||
_, err = service.GetFHIRStore() | ||
if err != nil { | ||
_, err = service.CreateFHIRStore() | ||
if err != nil { | ||
log.Printf( | ||
"Unable to get or create FHIR store with projectID %s, "+ | ||
"location %s, datasetID %s, fhirStoreID %s; got error %s", | ||
service.projectID, service.location, service.datasetID, service.fhirStoreID, err, | ||
) | ||
} | ||
} | ||
// PatchFHIRResource patches a FHIR resource. | ||
// The payload is a JSON patch document that follows guidance on Patch from the | ||
// FHIR standard. | ||
// See: | ||
// payload := []map[string]interface{}{ | ||
// { | ||
// "op": "replace", | ||
// "path": "/active", | ||
// "value": active, | ||
// }, | ||
// } | ||
// See: https://www.hl7.org/fhir/http.html#patch | ||
func (fr Repository) PatchFHIRResource( | ||
resourceType, fhirResourceID string, payload []map[string]interface{}) ([]byte, error) { | ||
return nil, nil | ||
} | ||
|
||
return service | ||
// UpdateFHIRResource updates the entire contents of a resource. | ||
func (fr Repository) UpdateFHIRResource( | ||
resourceType, fhirResourceID string, payload map[string]interface{}) ([]byte, error) { | ||
return nil, nil | ||
} | ||
|
||
func (s Service) checkPreconditions() { | ||
if s.healthcareService == nil { | ||
log.Panicf("cloudhealth.Service *healthcare.Service is nil") | ||
} | ||
// GetFHIRPatientEverything gets all resources associated with a particular | ||
// patient compartment. | ||
func (fr Repository) GetFHIRPatientEverything(fhirResourceID string) ([]byte, error) { | ||
return nil, nil | ||
} |
Oops, something went wrong.