Skip to content

Commit

Permalink
feat: get patient everything
Browse files Browse the repository at this point in the history
Signed-off-by: Kathurima Kimathi <kathurimakimathi415@gmail.com>
  • Loading branch information
KathurimaKimathi committed Mar 13, 2024
1 parent 789c60f commit 238717c
Show file tree
Hide file tree
Showing 11 changed files with 553 additions and 21 deletions.
21 changes: 19 additions & 2 deletions pkg/clinical/infrastructure/datastore/cloudhealthcare/fhir.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ type Dataset interface {
UpdateFHIRResource(resourceType, fhirResourceID string, payload map[string]interface{}, resource interface{}) error
SearchFHIRResource(resourceType string, params map[string]interface{}, tenant dto.TenantIdentifiers, pagination dto.Pagination) (*domain.PagedFHIRResource, error)

GetFHIRPatientAllData(fhirResourceID string) ([]byte, error)
GetFHIRPatientAllData(fhirResourceID string, params map[string]interface{}) ([]byte, error)
}

// StoreImpl represents the FHIR infrastructure implementation
Expand Down Expand Up @@ -1251,7 +1251,7 @@ func (fh StoreImpl) GetFHIRPatient(_ context.Context, id string) (*domain.FHIRPa

// DeleteFHIRPatient deletes the FHIRPatient identified by the supplied ID
func (fh StoreImpl) DeleteFHIRPatient(_ context.Context, id string) (bool, error) {
patientEverythingBs, err := fh.Dataset.GetFHIRPatientAllData(id)
patientEverythingBs, err := fh.Dataset.GetFHIRPatientAllData(id, nil)
if err != nil {
return false, fmt.Errorf("unable to get patient's compartment: %w", err)
}
Expand Down Expand Up @@ -1893,3 +1893,20 @@ func (fh StoreImpl) CreateFHIRDiagnosticReport(_ context.Context, input *domain.

return resource, nil
}

// GetFHIRPatientEverything is used to retrieve all patient related information
func (fh StoreImpl) GetFHIRPatientEverything(ctx context.Context, id string, params map[string]interface{}) (map[string]interface{}, error) {
patientEverythingBs, err := fh.Dataset.GetFHIRPatientAllData(id, params)
if err != nil {
return nil, fmt.Errorf("unable to get patient's compartment: %w", err)
}

var patientEverything map[string]interface{}

err = json.Unmarshal(patientEverythingBs, &patientEverything)
if err != nil {
return nil, fmt.Errorf("unable to unmarshal patient everything")
}

return patientEverything, nil
}
154 changes: 143 additions & 11 deletions pkg/clinical/infrastructure/datastore/cloudhealthcare/fhir_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1241,7 +1241,7 @@ func TestStoreImpl_DeleteFHIRPatient(t *testing.T) {
fh := FHIR.NewFHIRStoreImpl(dataset)

if tt.name == "happy case: delete all patient data" {
dataset.MockGetFHIRPatientAllDataFn = func(fhirResourceID string) ([]byte, error) {
dataset.MockGetFHIRPatientAllDataFn = func(fhirResourceID string, params map[string]interface{}) ([]byte, error) {
data := map[string]interface{}{
"entry": []map[string]interface{}{
{
Expand Down Expand Up @@ -1323,13 +1323,13 @@ func TestStoreImpl_DeleteFHIRPatient(t *testing.T) {
}

if tt.name == "sad case: all patient data error" {
dataset.MockGetFHIRPatientAllDataFn = func(fhirResourceID string) ([]byte, error) {
dataset.MockGetFHIRPatientAllDataFn = func(fhirResourceID string, params map[string]interface{}) ([]byte, error) {
return nil, fmt.Errorf("failed to get data")
}
}

if tt.name == "sad case: all patient data invalid entry" {
dataset.MockGetFHIRPatientAllDataFn = func(fhirResourceID string) ([]byte, error) {
dataset.MockGetFHIRPatientAllDataFn = func(fhirResourceID string, params map[string]interface{}) ([]byte, error) {
data := map[string]interface{}{
"entry": "invalid",
}
Expand All @@ -1344,7 +1344,7 @@ func TestStoreImpl_DeleteFHIRPatient(t *testing.T) {
}

if tt.name == "sad case: all patient data invalid entry type" {
dataset.MockGetFHIRPatientAllDataFn = func(fhirResourceID string) ([]byte, error) {
dataset.MockGetFHIRPatientAllDataFn = func(fhirResourceID string, params map[string]interface{}) ([]byte, error) {
data := map[string]interface{}{
"entry": []map[int]string{
{
Expand All @@ -1363,7 +1363,7 @@ func TestStoreImpl_DeleteFHIRPatient(t *testing.T) {
}

if tt.name == "sad case: all patient data entry invalid resource type" {
dataset.MockGetFHIRPatientAllDataFn = func(fhirResourceID string) ([]byte, error) {
dataset.MockGetFHIRPatientAllDataFn = func(fhirResourceID string, params map[string]interface{}) ([]byte, error) {
data := map[string]interface{}{
"entry": []map[string]interface{}{

Expand All @@ -1383,7 +1383,7 @@ func TestStoreImpl_DeleteFHIRPatient(t *testing.T) {
}

if tt.name == "sad case: error deleting medication request" {
dataset.MockGetFHIRPatientAllDataFn = func(fhirResourceID string) ([]byte, error) {
dataset.MockGetFHIRPatientAllDataFn = func(fhirResourceID string, params map[string]interface{}) ([]byte, error) {
data := map[string]interface{}{
"entry": []map[string]interface{}{
{
Expand Down Expand Up @@ -1412,7 +1412,7 @@ func TestStoreImpl_DeleteFHIRPatient(t *testing.T) {
}

if tt.name == "sad case: error deleting other types" {
dataset.MockGetFHIRPatientAllDataFn = func(fhirResourceID string) ([]byte, error) {
dataset.MockGetFHIRPatientAllDataFn = func(fhirResourceID string, params map[string]interface{}) ([]byte, error) {
data := map[string]interface{}{
"entry": []map[string]interface{}{
{
Expand Down Expand Up @@ -1441,7 +1441,7 @@ func TestStoreImpl_DeleteFHIRPatient(t *testing.T) {
}

if tt.name == "sad case: error deleting patient" {
dataset.MockGetFHIRPatientAllDataFn = func(fhirResourceID string) ([]byte, error) {
dataset.MockGetFHIRPatientAllDataFn = func(fhirResourceID string, params map[string]interface{}) ([]byte, error) {
data := map[string]interface{}{
"entry": []map[string]interface{}{
{
Expand Down Expand Up @@ -1470,7 +1470,7 @@ func TestStoreImpl_DeleteFHIRPatient(t *testing.T) {
}

if tt.name == "sad case: error deleting observation" {
dataset.MockGetFHIRPatientAllDataFn = func(fhirResourceID string) ([]byte, error) {
dataset.MockGetFHIRPatientAllDataFn = func(fhirResourceID string, params map[string]interface{}) ([]byte, error) {
data := map[string]interface{}{
"entry": []map[string]interface{}{
{
Expand Down Expand Up @@ -1499,7 +1499,7 @@ func TestStoreImpl_DeleteFHIRPatient(t *testing.T) {
}

if tt.name == "sad case: error deleting encounters" {
dataset.MockGetFHIRPatientAllDataFn = func(fhirResourceID string) ([]byte, error) {
dataset.MockGetFHIRPatientAllDataFn = func(fhirResourceID string, params map[string]interface{}) ([]byte, error) {
data := map[string]interface{}{
"entry": []map[string]interface{}{

Expand Down Expand Up @@ -1529,7 +1529,7 @@ func TestStoreImpl_DeleteFHIRPatient(t *testing.T) {
}

if tt.name == "sad case: error deleting episode of care" {
dataset.MockGetFHIRPatientAllDataFn = func(fhirResourceID string) ([]byte, error) {
dataset.MockGetFHIRPatientAllDataFn = func(fhirResourceID string, params map[string]interface{}) ([]byte, error) {
data := map[string]interface{}{
"entry": []map[string]interface{}{
{
Expand Down Expand Up @@ -4977,3 +4977,135 @@ func TestStoreImpl_SearchFHIRRiskAssessment(t *testing.T) {
})
}
}

func TestStoreImpl_GetFHIRPatientEverything(t *testing.T) {
type args struct {
ctx context.Context
id string
params map[string]interface{}
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "Happy case: fetch patient everything",
args: args{
ctx: context.Background(),
id: "1",
params: map[string]interface{}{
"_count": 10,
},
},
wantErr: false,
},
{
name: "Sad case: unable to fetch patient everything",
args: args{
ctx: context.Background(),
id: "1",
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
dataset := fakeDataset.NewFakeFHIRRepositoryMock()
fh := FHIR.NewFHIRStoreImpl(dataset)

if tt.name == "Happy case: fetch patient everything" {
dataset.MockGetFHIRPatientAllDataFn = func(fhirResourceID string, params map[string]interface{}) ([]byte, error) {
data := map[string]interface{}{
"entry": []map[string]interface{}{
{
"resource": map[string]interface{}{
"resourceType": "EpisodeOfCare",
"id": gofakeit.UUID(),
},
},
{
"resource": map[string]interface{}{
"resourceType": "Observation",
"id": gofakeit.UUID(),
},
},
{
"resource": map[string]interface{}{
"resourceType": "AllergyIntolerance",
"id": gofakeit.UUID(),
},
},
{
"resource": map[string]interface{}{
"resourceType": "ServiceRequest",
"id": gofakeit.UUID(),
},
},
{
"resource": map[string]interface{}{
"resourceType": "MedicationRequest",
"id": gofakeit.UUID(),
},
},
{
"resource": map[string]interface{}{
"resourceType": "Condition",
"id": gofakeit.UUID(),
},
},
{
"resource": map[string]interface{}{
"resourceType": "Encounter",
"id": gofakeit.UUID(),
},
},
{
"resource": map[string]interface{}{
"resourceType": "Composition",
"id": gofakeit.UUID(),
},
},
{
"resource": map[string]interface{}{
"resourceType": "MedicationStatement",
"id": gofakeit.UUID(),
},
},
{
"resource": map[string]interface{}{
"resourceType": "Medication",
"id": gofakeit.UUID(),
},
},
{
"resource": map[string]interface{}{
"resourceType": "Patient",
"id": gofakeit.UUID(),
},
},
},
}

bs, err := json.Marshal(data)
if err != nil {
return nil, err
}

return bs, err
}
}
if tt.name == "Sad case: unable to fetch patient everything" {
dataset.MockGetFHIRPatientAllDataFn = func(fhirResourceID string, params map[string]interface{}) ([]byte, error) {
return nil, fmt.Errorf("an error occurred")
}
}

_, err := fh.GetFHIRPatientEverything(tt.args.ctx, tt.args.id, tt.args.params)
if (err != nil) != tt.wantErr {
t.Errorf("StoreImpl.GetFHIRPatientEverything() error = %v, wantErr %v", err, tt.wantErr)
return
}
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -353,13 +353,21 @@ func (fr Repository) UpdateFHIRResource(

// GetFHIRPatientAllData gets all resources associated with a particular
// patient compartment.
func (fr Repository) GetFHIRPatientAllData(fhirResourceID string) ([]byte, error) {
func (fr Repository) GetFHIRPatientAllData(fhirResourceID string, params map[string]interface{}) ([]byte, error) {
fhirService := fr.healthcareService.Projects.Locations.Datasets.FhirStores.Fhir
patientResource := fmt.Sprintf("%s/fhir/Patient/%s", fr.fhirStoreName, fhirResourceID)

resp, err := fhirService.PatientEverything(patientResource).Do()
patientEverythingCall := fhirService.PatientEverything(patientResource)

if params != nil {
if count, ok := params["_count"].(int); ok {
patientEverythingCall.Count(int64(count))
}
}

resp, err := patientEverythingCall.Do()
if err != nil {
return nil, fmt.Errorf("PatientAllData: %w", err)
return nil, fmt.Errorf("patientEverything: %w", err)
}

defer func() {
Expand Down
Loading

0 comments on commit 238717c

Please sign in to comment.