Skip to content

Commit

Permalink
refactor: add observation when referring a patient
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 19, 2024
1 parent 8b4f9b9 commit 46dd102
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 33 deletions.
2 changes: 1 addition & 1 deletion pkg/clinical/presentation/graph/clinical.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -224,5 +224,5 @@ extend type Mutation {
getEncounterAssociatedResources(encounterID: String!): EncounterAssociatedResourceOutput!

# Referral
referPatient(input: ReferralInput!): ServiceRequest!
referPatient(input: ReferralInput!, count: Int): ServiceRequest!
}
4 changes: 2 additions & 2 deletions pkg/clinical/presentation/graph/clinical.resolvers.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 21 additions & 15 deletions pkg/clinical/presentation/graph/generated/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 26 additions & 14 deletions pkg/clinical/usecases/clinical/encounter.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,29 @@ func (c *UseCasesClinicalImpl) GetEncounterAssociatedResources(ctx context.Conte
return nil, err
}

resources, err := c.mapFHIREncounterDataToEncounterAssociatedDTO(encounterAllData)
if err != nil {
return nil, err
}

output := &dto.EncounterAssociatedResourceOutput{}
if len(resources.RiskAssessment) > 0 {
output.RiskAssessment = resources.RiskAssessment[0]
}

if len(resources.Consent) > 0 {
output.Consent = resources.Consent[0]
}

if len(resources.Observation) > 0 {
output.Observation = resources.Observation[0]
}

return output, nil
}

// mapFHIREncounterDataToEncounterAssociatedDTO is a helper function to map out resources associated with an encounter to an output model designed to suit the business requirement
func (*UseCasesClinicalImpl) mapFHIREncounterDataToEncounterAssociatedDTO(encounterAllData *domain.PagedFHIRResource) (*dto.EncounterAssociatedResources, error) {
result := dto.EncounterAssociatedResources{}

for _, encounterData := range encounterAllData.Resources {
Expand Down Expand Up @@ -280,21 +303,10 @@ func (c *UseCasesClinicalImpl) GetEncounterAssociatedResources(ctx context.Conte
TimeRecorded: string(*observation.EffectiveInstant),
Note: observationNote,
})
default:
continue
}
}

output := &dto.EncounterAssociatedResourceOutput{}
if len(result.RiskAssessment) > 0 {
output.RiskAssessment = result.RiskAssessment[0]
}

if len(result.Consent) > 0 {
output.Consent = result.Consent[0]
}

if len(result.Observation) > 0 {
output.Observation = result.Observation[0]
}

return output, nil
return &result, nil
}
41 changes: 41 additions & 0 deletions pkg/clinical/usecases/clinical/servicerequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
func (c *UseCasesClinicalImpl) ReferPatient(
ctx context.Context,
input *dto.ReferralInput,
count int,
) (*dto.ServiceRequest, error) {
err := input.Validate()
if err != nil {
Expand All @@ -32,12 +33,50 @@ func (c *UseCasesClinicalImpl) ReferPatient(
return nil, fmt.Errorf("cannot record a referral in a finished encounter")
}

identifiers, err := c.infrastructure.BaseExtension.GetTenantIdentifiers(ctx)
if err != nil {
return nil, err
}

includedResources := []string{
"Observation:encounter",
}

searchParams := map[string]interface{}{
"_id": input.EncounterID,
"_sort": "_lastUpdated",
"_revinclude": includedResources,
}

encounterAllData, err := c.infrastructure.FHIR.SearchFHIREncounterAllData(ctx, searchParams, *identifiers, dto.Pagination{
First: &count,
})
if err != nil {
return nil, err
}

output, err := c.mapFHIREncounterDataToEncounterAssociatedDTO(encounterAllData)
if err != nil {
return nil, err
}

patientID := encounter.Resource.Subject.ID
patientReference := fmt.Sprintf("Patient/%s", *patientID)

encounterReference := fmt.Sprintf("Encounter/%s", *encounter.Resource.ID)
startTime := scalarutils.DateTime(time.Now().Format("2006-01-02T15:04:05+03:00"))

reasonReference := []*domain.FHIRReferenceInput{}

for _, observation := range output.Observation {
observationReference := fmt.Sprintf("%s/%s", "Observation", observation.ID)
reasonReference = append(reasonReference, &domain.FHIRReferenceInput{
ID: &observation.ID,
Reference: &observationReference,
Display: observation.Name,
})
}

serviceRequest := domain.FHIRServiceRequestInput{
Status: domain.ServiceRequestStatusActive,
Intent: domain.ServiceRequestIntentOrder,
Expand All @@ -58,6 +97,8 @@ func (c *UseCasesClinicalImpl) ReferPatient(
Text: (*scalarutils.Markdown)(&input.ReferralNote),
},
},
ReasonReference: reasonReference,
// SupportingInfo: supportingInfo, -- TODO: Add diagnostic report
}

tags, err := c.GetTenantMetaTags(ctx)
Expand Down
27 changes: 26 additions & 1 deletion pkg/clinical/usecases/clinical/servicerequest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func TestUseCasesClinicalImpl_ReferPatient(t *testing.T) {
type args struct {
ctx context.Context
input *dto.ReferralInput
count int
}
tests := []struct {
name string
Expand All @@ -40,6 +41,7 @@ func TestUseCasesClinicalImpl_ReferPatient(t *testing.T) {
Facility: "",
ReferralNote: "",
},
count: 4,
},
wantErr: false,
},
Expand All @@ -52,6 +54,7 @@ func TestUseCasesClinicalImpl_ReferPatient(t *testing.T) {
ReferralType: "DIAGNOSTICS",
Tests: []string{"VIA"},
},
count: 4,
},
wantErr: true,
},
Expand All @@ -64,6 +67,7 @@ func TestUseCasesClinicalImpl_ReferPatient(t *testing.T) {
ReferralType: "DIAGNOSTICS",
Tests: []string{"VIA"},
},
count: 4,
},
wantErr: true,
},
Expand All @@ -76,6 +80,7 @@ func TestUseCasesClinicalImpl_ReferPatient(t *testing.T) {
ReferralType: "DIAGNOSTICS",
Tests: []string{"VIA"},
},
count: 4,
},
wantErr: true,
},
Expand All @@ -88,6 +93,7 @@ func TestUseCasesClinicalImpl_ReferPatient(t *testing.T) {
ReferralType: "DIAGNOSTICS",
Tests: []string{"VIA"},
},
count: 4,
},
wantErr: true,
},
Expand All @@ -99,6 +105,20 @@ func TestUseCasesClinicalImpl_ReferPatient(t *testing.T) {
ReferralType: "DIAGNOSTICS",
Tests: []string{"VIA"},
},
count: 4,
},
wantErr: true,
},
{
name: "Sad Case: unable to get fhir encounter data",
args: args{
ctx: context.Background(),
input: &dto.ReferralInput{
EncounterID: gofakeit.UUID(),
ReferralType: "DIAGNOSTICS",
Tests: []string{"VIA"},
},
count: 4,
},
wantErr: true,
},
Expand Down Expand Up @@ -143,8 +163,13 @@ func TestUseCasesClinicalImpl_ReferPatient(t *testing.T) {
return nil, fmt.Errorf("failed to record service request")
}
}
if tt.name == "Sad Case: unable to get fhir encounter data" {
fakeFHIR.MockSearchFHIREncounterAllDataFn = func(_ context.Context, params map[string]interface{}, tenant dto.TenantIdentifiers, pagination dto.Pagination) (*domain.PagedFHIRResource, error) {
return nil, fmt.Errorf("unable to get fhir encounter data")
}
}

got, err := c.ReferPatient(tt.args.ctx, tt.args.input)
got, err := c.ReferPatient(tt.args.ctx, tt.args.input, tt.args.count)
if (err != nil) != tt.wantErr {
t.Errorf("UseCasesClinicalImpl.ReferPatient() error = %v, wantErr %v", err, tt.wantErr)
return
Expand Down

0 comments on commit 46dd102

Please sign in to comment.