Skip to content

Commit

Permalink
refactor: move fhirdataset into cloudhealthcare
Browse files Browse the repository at this point in the history
-  Created a folder called `cloudhealthcare` in the infrastructure layer. The folder will hold all implementations that are
specific to google cloud healthcare API.
  • Loading branch information
Salaton committed Jan 28, 2023
1 parent bb879ee commit 44c77a8
Show file tree
Hide file tree
Showing 22 changed files with 207 additions and 101 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ jobs:

- name: Install Go dependencies
run: |
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.37.1
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.50.1
go get -u github.com/kisielk/errcheck
go get -u golang.org/x/lint/golint
go install honnef.co/go/tools/cmd/staticcheck@2021.1.2
Expand Down
6 changes: 3 additions & 3 deletions pkg/clinical/application/common/testutils/testhelpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (

"github.com/savannahghi/clinical/pkg/clinical/application/extensions"
"github.com/savannahghi/clinical/pkg/clinical/infrastructure"
"github.com/savannahghi/clinical/pkg/clinical/infrastructure/datastore/fhir"
dataset "github.com/savannahghi/clinical/pkg/clinical/infrastructure/datastore/fhirdataset"
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/firebasetools"
Expand All @@ -21,7 +21,7 @@ func InitializeTestService(ctx context.Context) (usecases.Interactor, error) {
fhir := fhir.NewFHIRStoreImpl(repo)
ocl := openconceptlab.NewServiceOCL()

infrastructure := infrastructure.NewInfrastructureInteractor(baseExtension, repo, fhir, ocl)
infrastructure := infrastructure.NewInfrastructureInteractor(baseExtension, fhir, ocl)

usecases := usecases.NewUsecasesInteractor(infrastructure)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (
"github.com/savannahghi/clinical/pkg/clinical/application/extensions"
"github.com/savannahghi/clinical/pkg/clinical/domain"
"github.com/savannahghi/clinical/pkg/clinical/infrastructure"
"github.com/savannahghi/clinical/pkg/clinical/infrastructure/datastore/fhir"
dataset "github.com/savannahghi/clinical/pkg/clinical/infrastructure/datastore/fhirdataset"
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/presentation/interactor"
"github.com/savannahghi/converterandformatter"
Expand Down Expand Up @@ -82,7 +82,7 @@ func InitializeTestInfrastructure(ctx context.Context) (infrastructure.Infrastru
fhir := fhir.NewFHIRStoreImpl(repo)

ocl := openconceptlab.NewServiceOCL()
return infrastructure.NewInfrastructureInteractor(baseExtension, repo, fhir, ocl), nil
return infrastructure.NewInfrastructureInteractor(baseExtension, fhir, ocl), nil
}

// func simplePatientRegistration() (*domain.SimplePatientRegistrationInput, error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"time"

Expand All @@ -12,7 +14,7 @@ import (
"github.com/savannahghi/clinical/pkg/clinical/application/common/helpers"
"github.com/savannahghi/clinical/pkg/clinical/application/utils"
"github.com/savannahghi/clinical/pkg/clinical/domain"
dataset "github.com/savannahghi/clinical/pkg/clinical/infrastructure/datastore/fhirdataset"
dataset "github.com/savannahghi/clinical/pkg/clinical/infrastructure/datastore/cloudhealthcare/fhirdataset"
"github.com/savannahghi/clinical/pkg/clinical/repository"
"github.com/savannahghi/converterandformatter"
"github.com/savannahghi/scalarutils"
Expand Down Expand Up @@ -1794,3 +1796,34 @@ func (fh *StoreImpl) SearchFHIRMedicationStatement(ctx context.Context, params m
}
return &output, nil
}

// CreateFHIRResource creates a FHIR resource
func (fh *StoreImpl) CreateFHIRResource(resourceType string, payload map[string]interface{}) ([]byte, error) {
return fh.Dataset.CreateFHIRResource(resourceType, payload)
}

// PatchFHIRResource patches a FHIR resource
func (fh *StoreImpl) PatchFHIRResource(resourceType, fhirResourceID string, payload []map[string]interface{}) ([]byte, error) {
return fh.Dataset.PatchFHIRResource(resourceType, fhirResourceID, payload)
}

// UpdateFHIRResource updates a FHIR resource
func (fh *StoreImpl) UpdateFHIRResource(resourceType, fhirResourceID string, payload map[string]interface{}) ([]byte, error) {
return fh.Dataset.UpdateFHIRResource(resourceType, fhirResourceID, payload)
}

// POSTRequest is used to manually compose POST requests to the FHIR service
func (fh *StoreImpl) POSTRequest(resourceName string, path string, params url.Values, body io.Reader) ([]byte, error) {
return fh.Dataset.POSTRequest(resourceName, path, params, body)
}

// GetFHIRResource gets a FHIR resource.
func (fh *StoreImpl) GetFHIRResource(resourceType, fhirResourceID string) ([]byte, error) {
return fh.Dataset.GetFHIRResource(resourceType, fhirResourceID)
}

// FHIRHeaders composes suitable FHIR headers, with authentication and content
// type already set
func (fh *StoreImpl) FHIRHeaders() (http.Header, error) {
return fh.Dataset.FHIRHeaders()
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import (
"github.com/brianvoe/gofakeit"
"github.com/google/uuid"
"github.com/savannahghi/clinical/pkg/clinical/domain"
FHIR "github.com/savannahghi/clinical/pkg/clinical/infrastructure/datastore/fhir"
FHIR "github.com/savannahghi/clinical/pkg/clinical/infrastructure/datastore/cloudhealthcare"
"github.com/savannahghi/scalarutils"
"github.com/segmentio/ksuid"

fakeDataset "github.com/savannahghi/clinical/pkg/clinical/infrastructure/datastore/fhirdataset/mock"
fakeDataset "github.com/savannahghi/clinical/pkg/clinical/infrastructure/datastore/cloudhealthcare/fhirdataset/mock"
)

func TestStoreImpl_CreateEpisodeOfCare_Unittest(t *testing.T) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"strings"
"testing"

dataset "github.com/savannahghi/clinical/pkg/clinical/infrastructure/datastore/fhirdataset"
dataset "github.com/savannahghi/clinical/pkg/clinical/infrastructure/datastore/cloudhealthcare/fhirdataset"
"github.com/tj/assert"
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package mock

import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -67,6 +68,11 @@ type FHIRMock struct {
MockSearchFHIRMedicationStatementFn func(ctx context.Context, params map[string]interface{}) (*domain.FHIRMedicationStatementRelayConnection, error)
MockFHIRHeadersFn func() (http.Header, error)
MockGetBearerTokenFn func() (string, error)

MockCreateFHIRResourceFn func(resourceType string, payload map[string]interface{}) ([]byte, error)
MockPatchFHIRResourceFn func(resourceType, fhirResourceID string, payload []map[string]interface{}) ([]byte, error)
MockUpdateFHIRResourceFn func(resourceType, fhirResourceID string, payload map[string]interface{}) ([]byte, error)
MockGetFHIRResourceFn func(resourceType, fhirResourceID string) ([]byte, error)
}

// NewFHIRMock initializes a new instance of FHIR mock
Expand Down Expand Up @@ -145,7 +151,17 @@ func NewFHIRMock() *FHIRMock {
return &domain.FHIROrganizationRelayConnection{}, nil
},
MockPOSTRequestFn: func(resourceName string, path string, params url.Values, body io.Reader) ([]byte, error) {
return []byte("m"), nil
m := map[string]string{
"resourceType": "Bundle",
"type": "searchset",
"total": "10",
"link": "test",
}
bs, err := json.Marshal(m)
if err != nil {
return nil, fmt.Errorf("unable to marshal map to JSON: %w", err)
}
return bs, nil
},
MockSearchEpisodesByParamFn: func(ctx context.Context, searchParams url.Values) ([]*domain.FHIREpisodeOfCare, error) {
return []*domain.FHIREpisodeOfCare{}, nil
Expand Down Expand Up @@ -386,6 +402,55 @@ func NewFHIRMock() *FHIRMock {
MockGetBearerTokenFn: func() (string, error) {
return fmt.Sprintf("Bearer %s", uuid.NewString()), nil
},
MockCreateFHIRResourceFn: func(resourceType string, payload map[string]interface{}) ([]byte, error) {
m := map[string]interface{}{
"key": "value",
}
bs, err := json.Marshal(m)
if err != nil {
return nil, fmt.Errorf("unable to marshal map to JSON: %w", err)
}
return bs, nil
},
MockPatchFHIRResourceFn: func(resourceType, fhirResourceID string, payload []map[string]interface{}) ([]byte, error) {
m := map[string]interface{}{
"key": "value",
}
bs, err := json.Marshal(m)
if err != nil {
return nil, fmt.Errorf("unable to marshal map to JSON: %w", err)
}
return bs, nil
},
MockUpdateFHIRResourceFn: func(resourceType, fhirResourceID string, payload map[string]interface{}) ([]byte, error) {
m := map[string]interface{}{
"key": "value",
}
bs, err := json.Marshal(m)
if err != nil {
return nil, fmt.Errorf("unable to marshal map to JSON: %w", err)
}
return bs, nil
},
MockGetFHIRResourceFn: func(resourceType, fhirResourceID string) ([]byte, error) {
n := map[string]interface{}{"given": []string{"John"}, "family": []string{"Doe"}}
p := map[string]interface{}{
"resourceType": "Patient/",
"id": "test-UUID",
"name": []map[string]interface{}{n},
}
m := map[string]interface{}{
"resourceType": "Patient/",
"status": "active",
"id": "test-UUID",
"patientRecord": p,
}
bs, err := json.Marshal(m)
if err != nil {
return nil, fmt.Errorf("unable to marshal map to JSON: %w", err)
}
return bs, nil
},
}
}

Expand Down Expand Up @@ -629,3 +694,23 @@ func (fh *FHIRMock) CreateFHIRMedication(ctx context.Context, input domain.FHIRM
func (fh *FHIRMock) FHIRHeaders() (http.Header, error) {
return fh.MockFHIRHeadersFn()
}

// CreateFHIRResource mocks the implementation for creating a fhir resource
func (fh *FHIRMock) CreateFHIRResource(resourceType string, payload map[string]interface{}) ([]byte, error) {
return fh.MockCreateFHIRResourceFn(resourceType, payload)
}

// PatchFHIRResource mocks the implementation for patching a fhir resource
func (fh *FHIRMock) PatchFHIRResource(resourceType, fhirResourceID string, payload []map[string]interface{}) ([]byte, error) {
return fh.MockPatchFHIRResourceFn(resourceType, fhirResourceID, payload)
}

// UpdateFHIRResource mocks the implementation for updating a FHIR resource
func (fh *FHIRMock) UpdateFHIRResource(resourceType, fhirResourceID string, payload map[string]interface{}) ([]byte, error) {
return fh.MockUpdateFHIRResourceFn(resourceType, fhirResourceID, payload)
}

// GetFHIRResource mocks the implementation of getting a FHIR resource
func (fh *FHIRMock) GetFHIRResource(resourceType, fhirResourceID string) ([]byte, error) {
return fh.MockGetFHIRResourceFn(resourceType, fhirResourceID)
}
4 changes: 0 additions & 4 deletions pkg/clinical/infrastructure/infrastructure.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@ package infrastructure
import (
"github.com/savannahghi/clinical/pkg/clinical/application/common"
"github.com/savannahghi/clinical/pkg/clinical/application/extensions"
dataset "github.com/savannahghi/clinical/pkg/clinical/infrastructure/datastore/fhirdataset"
"github.com/savannahghi/clinical/pkg/clinical/infrastructure/services/mycarehub"
"github.com/savannahghi/clinical/pkg/clinical/infrastructure/services/openconceptlab"
"github.com/savannahghi/clinical/pkg/clinical/repository"
)

// Infrastructure ...
type Infrastructure struct {
FHIRRepo dataset.FHIRRepository
FHIR repository.FHIR
OpenConceptLab openconceptlab.ServiceOCL
BaseExtension extensions.BaseExtension
Expand All @@ -21,15 +19,13 @@ type Infrastructure struct {
// NewInfrastructureInteractor initializes a new Infrastructure
func NewInfrastructureInteractor(
ext extensions.BaseExtension,
fhirRepository dataset.FHIRRepository,
fhir repository.FHIR,
openconceptlab openconceptlab.ServiceOCL,
) Infrastructure {
myCareHubClient := common.NewInterServiceClient("mycarehub", ext)
mycarehub := mycarehub.NewServiceMyCareHub(myCareHubClient, ext)

return Infrastructure{
fhirRepository,
fhir,
openconceptlab,
ext,
Expand Down
6 changes: 3 additions & 3 deletions pkg/clinical/infrastructure/services/pubsub/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"cloud.google.com/go/pubsub"
"github.com/savannahghi/clinical/pkg/clinical/application/extensions"
"github.com/savannahghi/clinical/pkg/clinical/infrastructure"
fhir "github.com/savannahghi/clinical/pkg/clinical/infrastructure/datastore/fhir"
dataset "github.com/savannahghi/clinical/pkg/clinical/infrastructure/datastore/fhirdataset"
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"
pubsubmessaging "github.com/savannahghi/clinical/pkg/clinical/infrastructure/services/pubsub"
"github.com/savannahghi/clinical/pkg/clinical/usecases"
Expand Down Expand Up @@ -41,7 +41,7 @@ func InitializeTestPubSub(t *testing.T) (*pubsubmessaging.ServicePubSubMessaging
fhir := fhir.NewFHIRStoreImpl(repo)
ocl := openconceptlab.NewServiceOCL()

infrastructure := infrastructure.NewInfrastructureInteractor(baseExtension, repo, fhir, ocl)
infrastructure := infrastructure.NewInfrastructureInteractor(baseExtension, fhir, ocl)
usecases := usecases.NewUsecasesInteractor(infrastructure)
oclUseCase := usecases.UseCasesOCL
pubSub, err := pubsubmessaging.NewServicePubSubMessaging(
Expand Down
6 changes: 3 additions & 3 deletions pkg/clinical/presentation/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import (
"github.com/savannahghi/authutils"
"github.com/savannahghi/clinical/pkg/clinical/application/extensions"
"github.com/savannahghi/clinical/pkg/clinical/infrastructure"
fhir "github.com/savannahghi/clinical/pkg/clinical/infrastructure/datastore/fhir"
dataset "github.com/savannahghi/clinical/pkg/clinical/infrastructure/datastore/fhirdataset"
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"
pubsubmessaging "github.com/savannahghi/clinical/pkg/clinical/infrastructure/services/pubsub"
"github.com/savannahghi/clinical/pkg/clinical/presentation/graph"
Expand Down Expand Up @@ -120,7 +120,7 @@ func Router(ctx context.Context) (*mux.Router, error) {
fhir := fhir.NewFHIRStoreImpl(repo)
ocl := openconceptlab.NewServiceOCL()

infrastructure := infrastructure.NewInfrastructureInteractor(baseExtension, repo, fhir, ocl)
infrastructure := infrastructure.NewInfrastructureInteractor(baseExtension, fhir, ocl)
usecases := usecases.NewUsecasesInteractor(infrastructure)
h := rest.NewPresentationHandlers(usecases)

Expand Down
9 changes: 9 additions & 0 deletions pkg/clinical/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package repository

import (
"context"
"io"
"net/http"
"net/url"

"github.com/savannahghi/clinical/pkg/clinical/domain"
Expand Down Expand Up @@ -60,4 +62,11 @@ type FHIR interface {
CreateFHIRMedicationStatement(ctx context.Context, input domain.FHIRMedicationStatementInput) (*domain.FHIRMedicationStatementRelayPayload, error)
CreateFHIRMedication(ctx context.Context, input domain.FHIRMedicationInput) (*domain.FHIRMedicationRelayPayload, error)
SearchFHIRMedicationStatement(ctx context.Context, params map[string]interface{}) (*domain.FHIRMedicationStatementRelayConnection, error)

CreateFHIRResource(resourceType string, payload map[string]interface{}) ([]byte, error)
PatchFHIRResource(resourceType, fhirResourceID string, payload []map[string]interface{}) ([]byte, error)
UpdateFHIRResource(resourceType, fhirResourceID string, payload map[string]interface{}) ([]byte, error)
POSTRequest(resourceName string, path string, params url.Values, body io.Reader) ([]byte, error)
GetFHIRResource(resourceType, fhirResourceID string) ([]byte, error)
FHIRHeaders() (http.Header, error)
}
6 changes: 3 additions & 3 deletions pkg/clinical/usecases/clinical/fhir.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ func (c *UseCasesClinicalImpl) SearchEpisodesByParam(ctx context.Context, search
// - `path` is a sub-path e.g `_search` under a resource
// - `params` should be query params, sent as `url.Values`
func (c *UseCasesClinicalImpl) POSTRequest(resourceName string, path string, params url.Values, body io.Reader) ([]byte, error) {
return c.infrastructure.FHIRRepo.POSTRequest(resourceName, path, params, body)
return c.infrastructure.FHIR.POSTRequest(resourceName, path, params, body)
}

// OpenEpisodes returns the IDs of a patient's open episodes
Expand All @@ -241,7 +241,7 @@ func (c *UseCasesClinicalImpl) HasOpenEpisode(
// FHIRHeaders composes suitable FHIR headers, with authentication and content
// type already set
func (c *UseCasesClinicalImpl) FHIRHeaders() (http.Header, error) {
return c.infrastructure.FHIRRepo.FHIRHeaders()
return c.infrastructure.FHIR.FHIRHeaders()
}

// CreateFHIREncounter creates a FHIREncounter instance
Expand Down Expand Up @@ -334,7 +334,7 @@ func (c *UseCasesClinicalImpl) UpgradeEpisode(ctx context.Context, input domain.
return nil, fmt.Errorf("unable to turn episode of care input into a map: %v", err)
}

_, err = c.infrastructure.FHIRRepo.UpdateFHIRResource(
_, err = c.infrastructure.FHIR.UpdateFHIRResource(
"EpisodeOfCare", *episode.ID, payload)
if err != nil {
utils.ReportErrorToSentry(err)
Expand Down

0 comments on commit 44c77a8

Please sign in to comment.