Skip to content

Commit

Permalink
setup: base infrastructure to search for a document resource (#424)
Browse files Browse the repository at this point in the history
Signed-off-by: Kathurima Kimathi <kathurimakimathi415@gmail.com>
  • Loading branch information
KathurimaKimathi committed Apr 11, 2024
1 parent c5d62b4 commit 3cac70f
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 1 deletion.
19 changes: 18 additions & 1 deletion pkg/clinical/domain/document_reference.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package domain

import "github.com/savannahghi/scalarutils"
import (
"github.com/savannahghi/scalarutils"
)

// FHIRDocumentReference represents a reference to a document of any kind for any purpose.
// It provides metadata about the document so that the document can be discovered and managed.
Expand Down Expand Up @@ -90,3 +92,18 @@ type FHIRDocumentReferenceInput struct {
Content []FHIRDocumentReferenceContent `json:"content,omitempty"`
Context *FHIRDocumentReferenceContext `json:"context,omitempty"`
}

// FHIRQuestionnaireResponseRelayPayload is used to return a single instance of document response
type FHIRDocumentReferenceRelayPayload struct {
Resource *FHIRDocumentReference `json:"resource,omitempty"`
}

// PagedFHIRDocumentReference is an FHIR document's paginated model data class
type PagedFHIRDocumentReference struct {
DocumentReferences []FHIRDocumentReference
HasNextPage bool
NextCursor string
HasPreviousPage bool
PreviousCursor string
TotalCount int
}
35 changes: 35 additions & 0 deletions pkg/clinical/infrastructure/datastore/cloudhealthcare/fhir.go
Original file line number Diff line number Diff line change
Expand Up @@ -2106,3 +2106,38 @@ func (fh StoreImpl) PatchFHIRServiceRequest(ctx context.Context, id string, inpu
Resource: resource,
}, nil
}

// SearchFHIRDocumentReference is used to search for FHIR document reference resource using the client provided parameters. Some of these parameters include (but not limited) to `categories`, 'subject` etc.`
func (fh StoreImpl) SearchFHIRDocumentReference(ctx context.Context, searchParams map[string]interface{}, tenant dto.TenantIdentifiers, pagination dto.Pagination) (*domain.PagedFHIRDocumentReference, error) {
documentReferences, err := fh.Dataset.SearchFHIRResource(documentReferenceResourceType, searchParams, tenant, pagination)
if err != nil {
return nil, err
}

documentReferenceOutput := domain.PagedFHIRDocumentReference{
DocumentReferences: []domain.FHIRDocumentReference{},
HasNextPage: documentReferences.HasNextPage,
NextCursor: documentReferences.NextCursor,
HasPreviousPage: documentReferences.HasPreviousPage,
PreviousCursor: documentReferences.PreviousCursor,
TotalCount: documentReferences.TotalCount,
}

for _, reference := range documentReferences.Resources {
var documentReference domain.FHIRDocumentReference

resourceBs, err := json.Marshal(reference)
if err != nil {
return nil, fmt.Errorf("unable to marshal resource to JSON: %w", err)
}

err = json.Unmarshal(resourceBs, &documentReference)
if err != nil {
return nil, fmt.Errorf("unable to unmarshal resource: %w", err)
}

documentReferenceOutput.DocumentReferences = append(documentReferenceOutput.DocumentReferences, documentReference)
}

return &documentReferenceOutput, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -5370,3 +5370,60 @@ func TestStoreImpl_CreateFHIRDocumentReference(t *testing.T) {
})
}
}

func TestStoreImpl_SearchFHIRDocumentReference(t *testing.T) {
type args struct {
ctx context.Context
searchParams map[string]interface{}
tenant dto.TenantIdentifiers
pagination dto.Pagination
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "Happy case: search for document reference",
args: args{
ctx: context.Background(),
searchParams: map[string]interface{}{
"related": fmt.Sprintf("ServiceRequest/%s", gofakeit.UUID()),
},
tenant: dto.TenantIdentifiers{},
pagination: dto.Pagination{},
},
wantErr: false,
},
{
name: "Sad case: unable to search for document reference",
args: args{
ctx: context.Background(),
searchParams: map[string]interface{}{
"related": fmt.Sprintf("ServiceRequest/%s", gofakeit.UUID()),
},
tenant: dto.TenantIdentifiers{},
pagination: dto.Pagination{},
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
dataset := fakeDataset.NewFakeFHIRRepositoryMock()
fh := FHIR.NewFHIRStoreImpl(dataset)

if tt.name == "Sad case: unable to search for document reference" {
dataset.MockSearchFHIRResourceFn = func(resourceType string, params map[string]interface{}, tenant dto.TenantIdentifiers, pagination dto.Pagination) (*domain.PagedFHIRResource, error) {
return nil, fmt.Errorf("an error occurred")
}
}

_, err := fh.SearchFHIRDocumentReference(tt.args.ctx, tt.args.searchParams, tt.args.tenant, tt.args.pagination)
if (err != nil) != tt.wantErr {
t.Errorf("StoreImpl.SearchFHIRDocumentReference() error = %v, wantErr %v", err, tt.wantErr)
return
}
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ type FHIRMock struct {
MockCreateFHIRSubscriptionFn func(_ context.Context, subscription *domain.FHIRSubscriptionInput) (*domain.FHIRSubscription, error)
MockPatchFHIRServiceRequestFn func(ctx context.Context, id string, input domain.FHIRServiceRequestInput) (*domain.FHIRServiceRequestRelayPayload, error)
MockCreateFHIRDocumentReferenceFn func(ctx context.Context, documentReference *domain.FHIRDocumentReferenceInput) (*domain.FHIRDocumentReference, error)
MockSearchFHIRDocumentReferenceFn func(ctx context.Context, searchParams map[string]interface{}, tenant dto.TenantIdentifiers, pagination dto.Pagination) (*domain.PagedFHIRDocumentReference, error)
}

// NewFHIRMock initializes a new instance of FHIR mock
Expand Down Expand Up @@ -2331,6 +2332,29 @@ func NewFHIRMock() *FHIRMock {
Context: &domain.FHIRDocumentReferenceContext{},
}, nil
},
MockSearchFHIRDocumentReferenceFn: func(ctx context.Context, searchParams map[string]interface{}, tenant dto.TenantIdentifiers, pagination dto.Pagination) (*domain.PagedFHIRDocumentReference, error) {
resourceID := gofakeit.UUID()
return &domain.PagedFHIRDocumentReference{
DocumentReferences: []domain.FHIRDocumentReference{
{
ID: resourceID,
Context: &domain.FHIRDocumentReferenceContext{
Related: []*domain.FHIRReference{
{
ID: new(string),
Reference: new(string),
},
},
},
},
},
HasNextPage: false,
NextCursor: "",
HasPreviousPage: false,
PreviousCursor: "",
TotalCount: 0,
}, nil
},
}
}

Expand Down Expand Up @@ -2708,3 +2732,8 @@ func (fh *FHIRMock) CreateFHIRDocumentReference(ctx context.Context, documentRef
func (fh *FHIRMock) PatchFHIRServiceRequest(ctx context.Context, id string, input domain.FHIRServiceRequestInput) (*domain.FHIRServiceRequestRelayPayload, error) {
return fh.MockPatchFHIRServiceRequestFn(ctx, id, input)
}

// SearchFHIRDocumentReference mocks the implementation of searching document reference using a related resource
func (fh *FHIRMock) SearchFHIRDocumentReference(ctx context.Context, searchParams map[string]interface{}, tenant dto.TenantIdentifiers, pagination dto.Pagination) (*domain.PagedFHIRDocumentReference, error) {
return fh.MockSearchFHIRDocumentReferenceFn(ctx, searchParams, tenant, pagination)
}
1 change: 1 addition & 0 deletions pkg/clinical/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,5 @@ type FHIRSubscription interface {
// FHIRDocumentReference interface contains the method signatures for the various action to be performed against the FHIR Document Reference resource
type FHIRDocumentReference interface {
CreateFHIRDocumentReference(ctx context.Context, input *domain.FHIRDocumentReferenceInput) (*domain.FHIRDocumentReference, error)
SearchFHIRDocumentReference(ctx context.Context, searchParams map[string]interface{}, tenant dto.TenantIdentifiers, pagination dto.Pagination) (*domain.PagedFHIRDocumentReference, error)
}

0 comments on commit 3cac70f

Please sign in to comment.