Skip to content

Commit

Permalink
chore: refactor dataset.go
Browse files Browse the repository at this point in the history
  • Loading branch information
Salaton committed Mar 8, 2023
1 parent 74a97f8 commit 175fc8f
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 236 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ env:
OPENCONCEPTLAB_API_URL: ${{ secrets.OPENCONCEPTLAB_API_URL }}
JWT_KEY: ${{ secrets.JWT_KEY }}
CLOUD_HEALTH_FHIRSTORE_ID: ${{ secrets.CLOUD_HEALTH_FHIRSTORE_ID }}
CLOUD_HEALTH_DATASET_LOCATION: ${{ secrets.CLOUD_HEALTH_DATASET_LOCATION }}
SENTRY_DSN: ${{ secrets.SENTRY_DSN }}
SAVANNAH_ADMIN_EMAIL: ${{ secrets.SAVANNAH_ADMIN_EMAIL }}
AUTHSERVER_ENDPOINT: ${{ secrets.AUTHSERVER_ENDPOINT }}
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/staging_multitenant.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ jobs:
CLOUD_HEALTH_PUBSUB_TOPIC=${{ secrets.CLOUD_HEALTH_PUBSUB_TOPIC }}
CLOUD_HEALTH_DATASET_ID=${{ secrets.CLOUD_HEALTH_DATASET_ID }}
CLOUD_HEALTH_FHIRSTORE_ID=${{ secrets.CLOUD_HEALTH_FHIRSTORE_ID }}
CLOUD_HEALTH_DATASET_LOCATION=${{ secrets.CLOUD_HEALTH_DATASET_LOCATION }}
OPENCONCEPTLAB_TOKEN=${{ secrets.OPENCONCEPTLAB_TOKEN }}
SERVICE_HOST=${{ secrets.SERVICE_HOST }}
OPENCONCEPTLAB_API_URL=${{ secrets.OPENCONCEPTLAB_API_URL }}
Expand Down
16 changes: 15 additions & 1 deletion pkg/clinical/application/common/testutils/testhelpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,34 @@ package testutils

import (
"context"
"log"

"github.com/savannahghi/clinical/pkg/clinical/application/extensions"
"github.com/savannahghi/clinical/pkg/clinical/infrastructure"
fhir "github.com/savannahghi/clinical/pkg/clinical/infrastructure/datastore/cloudhealthcare"
dataset "github.com/savannahghi/clinical/pkg/clinical/infrastructure/datastore/cloudhealthcare/fhirdataset"
"github.com/savannahghi/clinical/pkg/clinical/infrastructure/services/openconceptlab"
"github.com/savannahghi/clinical/pkg/clinical/usecases"
"github.com/savannahghi/serverutils"
"google.golang.org/api/healthcare/v1"
)

// InitializeTestService sets up the structure that will be used by the usecase layer for
// integration tests
func InitializeTestService(ctx context.Context) (usecases.Interactor, error) {
project := serverutils.MustGetEnvVar(serverutils.GoogleCloudProjectIDEnvVarName)
_ = serverutils.MustGetEnvVar("CLOUD_HEALTH_PUBSUB_TOPIC")
datasetID := serverutils.MustGetEnvVar("CLOUD_HEALTH_DATASET_ID")
datasetLocation := serverutils.MustGetEnvVar("CLOUD_HEALTH_DATASET_LOCATION")
fhirStoreID := serverutils.MustGetEnvVar("CLOUD_HEALTH_FHIRSTORE_ID")
hsv, err := healthcare.NewService(ctx)
if err != nil {
log.Panicf("unable to initialize new Google Cloud Healthcare Service: %s", err)
}

repo := dataset.NewFHIRRepository(ctx, hsv, project, datasetID, datasetLocation, fhirStoreID)

baseExtension := extensions.NewBaseExtensionImpl()
repo := dataset.NewFHIRRepository(ctx)
fhir := fhir.NewFHIRStoreImpl(repo)
ocl := openconceptlab.NewServiceOCL()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (

// constants used to configure the Google Cloud Healthcare API
const (
DatasetLocation = "europe-west4"
baseFHIRURL = "https://healthcare.googleapis.com/v1"
defaultTimeoutSeconds = 10
)
Expand All @@ -29,33 +28,30 @@ const (
type Repository struct {
healthcareService *healthcare.Service
projectID, location, datasetID, fhirStoreID string
parent string
datasetName string
fhirStoreName string
}

// NewFHIRRepository initializes a FHIR repository
func NewFHIRRepository(ctx context.Context) *Repository {
project := serverutils.MustGetEnvVar(serverutils.GoogleCloudProjectIDEnvVarName)
_ = serverutils.MustGetEnvVar("CLOUD_HEALTH_PUBSUB_TOPIC")
dataset := serverutils.MustGetEnvVar("CLOUD_HEALTH_DATASET_ID")
fhirStore := serverutils.MustGetEnvVar("CLOUD_HEALTH_FHIRSTORE_ID")
hsv, err := healthcare.NewService(ctx)
if err != nil {
log.Panicf("unable to initialize new Google Cloud Healthcare Service: %s", err)
}
func NewFHIRRepository(ctx context.Context, hsv *healthcare.Service, projectID, datasetID, datasetLocation, fhirStoreID string) *Repository {
return &Repository{
healthcareService: hsv,
projectID: project,
location: DatasetLocation,
datasetID: dataset,
fhirStoreID: fhirStore,
projectID: projectID,
location: datasetLocation,
datasetID: datasetID,
fhirStoreID: fhirStoreID,
parent: fmt.Sprintf("projects/%s/locations/%s", projectID, datasetLocation),
datasetName: fmt.Sprintf("projects/%s/locations/%s/datasets/%s", projectID, datasetLocation, datasetID),
fhirStoreName: fmt.Sprintf("projects/%s/locations/%s/datasets/%s/fhirStores/%s", projectID, datasetLocation, datasetID, fhirStoreID),
}
}

// CreateDataset creates a dataset and returns it's name
func (fr Repository) CreateDataset() (*healthcare.Operation, error) {
fr.checkPreconditions()
datasetsService := fr.healthcareService.Projects.Locations.Datasets
parent := fmt.Sprintf("projects/%s/locations/%s", fr.projectID, fr.location)
resp, err := datasetsService.Create(parent, &healthcare.Dataset{}).DatasetId(fr.datasetID).Do()
resp, err := datasetsService.Create(fr.parent, &healthcare.Dataset{}).DatasetId(fr.datasetID).Do()
if err != nil {
return nil, fmt.Errorf("create Data Set: %w", err)
}
Expand All @@ -66,15 +62,14 @@ func (fr Repository) CreateDataset() (*healthcare.Operation, error) {
func (fr Repository) GetDataset() (*healthcare.Dataset, error) {
fr.checkPreconditions()
datasetsService := fr.healthcareService.Projects.Locations.Datasets
name := fmt.Sprintf("projects/%s/locations/%s/datasets/%s", fr.projectID, fr.location, fr.datasetID)
resp, err := datasetsService.Get(name).Do()
resp, err := datasetsService.Get(fr.datasetName).Do()
if err != nil {
return nil, fmt.Errorf("get Data Set: %w", err)
}
return resp, nil
}

// CreateFHIRStore creates an FHIR store.
// CreateFHIRStore creates a FHIR store.
func (fr Repository) CreateFHIRStore() (*healthcare.FhirStore, error) {
fr.checkPreconditions()
storesService := fr.healthcareService.Projects.Locations.Datasets.FhirStores
Expand All @@ -84,8 +79,7 @@ func (fr Repository) CreateFHIRStore() (*healthcare.FhirStore, error) {
EnableUpdateCreate: true,
Version: "R4",
}
parent := fmt.Sprintf("projects/%s/locations/%s/datasets/%s", fr.projectID, fr.location, fr.datasetID)
resp, err := storesService.Create(parent, store).FhirStoreId(fr.fhirStoreID).Do()
resp, err := storesService.Create(fr.datasetName, store).FhirStoreId(fr.fhirStoreID).Do()
if err != nil {
return nil, fmt.Errorf("create FHIR Store: %w", err)
}
Expand All @@ -96,10 +90,7 @@ func (fr Repository) CreateFHIRStore() (*healthcare.FhirStore, error) {
func (fr Repository) GetFHIRStore() (*healthcare.FhirStore, error) {
fr.checkPreconditions()
storesService := fr.healthcareService.Projects.Locations.Datasets.FhirStores
name := fmt.Sprintf(
"projects/%s/locations/%s/datasets/%s/fhirStores/%s",
fr.projectID, fr.location, fr.datasetID, fr.fhirStoreID)
store, err := storesService.Get(name).Do()
store, err := storesService.Get(fr.fhirStoreName).Do()
if err != nil {
return nil, fmt.Errorf("get FHIR Store: %w", err)
}
Expand Down Expand Up @@ -148,12 +139,7 @@ func (fr Repository) CreateFHIRResource(resourceType string, payload map[string]
return nil, fmt.Errorf("json.Encode: %w", err)
}

parent := fmt.Sprintf(
"projects/%s/locations/%s/datasets/%s/fhirStores/%s",
fr.projectID, fr.location, fr.datasetID, fr.fhirStoreID)
call := fhirService.Create(
parent, resourceType, bytes.NewReader(jsonPayload))

call := fhirService.Create(fr.fhirStoreName, resourceType, bytes.NewReader(jsonPayload))
call.Header().Set("Content-Type", "application/fhir+json;charset=utf-8")
resp, err := call.Do()
if err != nil {
Expand Down Expand Up @@ -183,11 +169,8 @@ func (fr Repository) DeleteFHIRResource(resourceType, fhirResourceID string) ([]
fr.checkPreconditions()

fhirService := fr.healthcareService.Projects.Locations.Datasets.FhirStores.Fhir
name := fmt.Sprintf(
"projects/%s/locations/%s/datasets/%s/fhirStores/%s/fhir/%s/%s",
fr.projectID, fr.location, fr.datasetID, fr.fhirStoreID,
resourceType, fhirResourceID)
resp, err := fhirService.Delete(name).Do()
fhirResource := fmt.Sprintf("%s/fhir/%s/%s", fr.fhirStoreName, resourceType, fhirResourceID)
resp, err := fhirService.Delete(fhirResource).Do()
if err != nil {
return nil, fmt.Errorf("delete: %w", err)
}
Expand Down Expand Up @@ -229,12 +212,8 @@ func (fr Repository) PatchFHIRResource(
if err != nil {
return nil, fmt.Errorf("json.Encode: %w", err)
}
name := fmt.Sprintf(
"projects/%s/locations/%s/datasets/%s/fhirStores/%s/fhir/%s/%s",
fr.projectID, fr.location, fr.datasetID, fr.fhirStoreID,
resourceType, fhirResourceID)

call := fhirService.Patch(name, bytes.NewReader(jsonPayload))
fhirResource := fmt.Sprintf("%s/fhir/%s/%s", fr.fhirStoreName, resourceType, fhirResourceID)
call := fhirService.Patch(fhirResource, bytes.NewReader(jsonPayload))
call.Header().Set("Content-Type", "application/json-patch+json")
resp, err := call.Do()
if err != nil {
Expand Down Expand Up @@ -270,11 +249,8 @@ func (fr Repository) UpdateFHIRResource(
if serverutils.IsDebug() {
log.Printf("FHIR Update payload: %s", string(jsonPayload))
}
name := fmt.Sprintf(
"projects/%s/locations/%s/datasets/%s/fhirStores/%s/fhir/%s/%s",
fr.projectID, fr.location, fr.datasetID, fr.fhirStoreID,
resourceType, fhirResourceID)
call := fhirService.Update(name, bytes.NewReader(jsonPayload))
fhirResource := fmt.Sprintf("%s/fhir/%s/%s", fr.fhirStoreName, resourceType, fhirResourceID)
call := fhirService.Update(fhirResource, bytes.NewReader(jsonPayload))
call.Header().Set("Content-Type", "application/fhir+json;charset=utf-8")
resp, err := call.Do()
if err != nil {
Expand All @@ -298,16 +274,8 @@ func (fr Repository) UpdateFHIRResource(
// patient compartment.
func (fr Repository) GetFHIRPatientAllData(fhirResourceID string) ([]byte, error) {
fhirService := fr.healthcareService.Projects.Locations.Datasets.FhirStores.Fhir
name := fmt.Sprintf(
"projects/%s/locations/%s/datasets/%s/fhirStores/%s/fhir/Patient/%s",
fr.projectID,
fr.location,
fr.datasetID,
fr.fhirStoreID,
fhirResourceID,
)

resp, err := fhirService.PatientEverything(name).Do()
patientResource := fmt.Sprintf("%s/fhir/Patient/%s", fr.fhirStoreName, fhirResourceID)
resp, err := fhirService.PatientEverything(patientResource).Do()
if err != nil {
return nil, fmt.Errorf("PatientAllData: %w", err)
}
Expand All @@ -332,11 +300,8 @@ func (fr Repository) GetFHIRPatientAllData(fhirResourceID string) ([]byte, error
func (fr Repository) GetFHIRResource(resourceType, fhirResourceID string) ([]byte, error) {
fr.checkPreconditions()
fhirService := fr.healthcareService.Projects.Locations.Datasets.FhirStores.Fhir
name := fmt.Sprintf(
"projects/%s/locations/%s/datasets/%s/fhirStores/%s/fhir/%s/%s",
fr.projectID, fr.location, fr.datasetID, fr.fhirStoreID,
resourceType, fhirResourceID)
call := fhirService.Read(name)
fhirResource := fmt.Sprintf("%s/fhir/%s/%s", fr.fhirStoreName, resourceType, fhirResourceID)
call := fhirService.Read(fhirResource)
call.Header().Set("Content-Type", "application/fhir+json;charset=utf-8")
resp, err := call.Do()
if err != nil {
Expand Down

0 comments on commit 175fc8f

Please sign in to comment.