Skip to content

Commit

Permalink
Replace patient tags on enrollment
Browse files Browse the repository at this point in the history
  • Loading branch information
toddkazakov committed Oct 1, 2024
1 parent 3ab94e1 commit 71771fd
Show file tree
Hide file tree
Showing 11 changed files with 363 additions and 108 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ require (
github.com/oapi-codegen/runtime v1.1.1
github.com/onsi/ginkgo/v2 v2.19.0
github.com/onsi/gomega v1.33.1
github.com/tidepool-org/clinic/client v0.0.0-20240926112325-657da308fce2
github.com/tidepool-org/clinic/client v0.0.0-20241001100615-be3961ba3c20
github.com/tidepool-org/clinic/redox_models v0.0.0-20240802193352-3f912afe2109
github.com/tidepool-org/go-common v0.12.2-0.20240612192926-de6d5c5a742c
github.com/tidepool-org/hydrophone/client v0.0.0-20240613035211-756659d74c0d
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ github.com/tidepool-org/clinic/client v0.0.0-20240802193352-3f912afe2109 h1:AKup
github.com/tidepool-org/clinic/client v0.0.0-20240802193352-3f912afe2109/go.mod h1:7BpAdFdGJNB3aw/xvCz5XnWjSWRoUtWIX4xcMc4Bsko=
github.com/tidepool-org/clinic/client v0.0.0-20240926112325-657da308fce2 h1:fTIgILcOpTCE64kWKQul2ofb0O8h8Mf6ZocA7iTVfgg=
github.com/tidepool-org/clinic/client v0.0.0-20240926112325-657da308fce2/go.mod h1:7BpAdFdGJNB3aw/xvCz5XnWjSWRoUtWIX4xcMc4Bsko=
github.com/tidepool-org/clinic/client v0.0.0-20241001100615-be3961ba3c20 h1:7ePwwA5T6SsHDmfaq9cwx7H/kHGV9hpqKGMDNNj+nCA=
github.com/tidepool-org/clinic/client v0.0.0-20241001100615-be3961ba3c20/go.mod h1:7BpAdFdGJNB3aw/xvCz5XnWjSWRoUtWIX4xcMc4Bsko=
github.com/tidepool-org/clinic/redox_models v0.0.0-20240802193352-3f912afe2109 h1:NVsWq93dgv1mQ/ELrJW1lv4pwMQaoSO0BLaK4qHZ8Xc=
github.com/tidepool-org/clinic/redox_models v0.0.0-20240802193352-3f912afe2109/go.mod h1:bQ9DZxk015RhmGG1tR6jRScP9KxyHvS8tzPbVtr82DE=
github.com/tidepool-org/go-common v0.12.2-0.20240612192926-de6d5c5a742c h1:hJZyiHNGeqyLA/5p60/0H9CZtJi4fAuzOuyQF0TpF7E=
Expand Down
106 changes: 74 additions & 32 deletions redox/processor_neworder.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,17 @@ func (o *newOrderProcessor) handleEnableSummaryReports(ctx context.Context, para

patient := (*params.Match.Patients)[0]
o.logger.Infow("successfully matched clinic and patient", "order", params.Order.Meta, "clinicId", params.Match.Clinic.Id, "patientId", patient.Id)

tags, err := o.createTagsForPatient(ctx, params.Order, params.Match)
if err != nil {
return err
}
if tags != nil {
err = o.replacePatientTags(ctx, params.Match, tags)
if err != nil {
return err
}
}
if err := o.handleSuccessfulPatientMatch(ctx, params); err != nil {
return err
}
Expand Down Expand Up @@ -249,44 +260,24 @@ func (o *newOrderProcessor) handleCreateAccount(ctx context.Context, order model
}

func (o *newOrderProcessor) createTagsForPatient(ctx context.Context, order models.NewOrder, match clinics.EHRMatchResponse) (*clinics.PatientTagIds, error) {
separator := o.getPatientTagsSeparator(match)
codes := o.getPatientTagCodes(match)
if len(codes) == 0 {
tagNames := o.getTagNamesFromOrder(order, match)
if tagNames == nil {
return nil, nil
}

tagNames := make([]string, 0)
if order.Order.ClinicalInfo == nil {
for _, info := range *order.Order.ClinicalInfo {
if info.Code != nil && info.Value != nil {
if _, ok := codes[*info.Code]; ok {
if separator != nil || *separator == "" {
tagNames = append(tagNames, strings.TrimSpace(*info.Value))
} else {
for _, tag := range strings.Split(*info.Value, *separator) {
tagNames = append(tagNames, strings.TrimSpace(tag))
}
}
}
}
}
}

existingTags := o.getExistingTags(match.Clinic)
for _, tagName := range tagNames {
_, exists := existingTags[tagName]
if !exists {
if _, ok := existingTags[tagName]; !ok {
createTag := clinics.CreatePatientTagJSONRequestBody{
Name: tagName,
}
resp, err := o.clinics.CreatePatientTagWithResponse(ctx, *match.Clinic.Id, createTag)
if err != nil {
return nil, err
}
if resp.StatusCode() != http.StatusOK || resp.StatusCode() != http.StatusCreated{
return nil, fmt.Errorf("unexpected status code %v when creating tagName %s", resp.StatusCode(), tagName)
}
createTag := clinics.CreatePatientTagJSONRequestBody{
Name: tagName,
}
resp, err := o.clinics.CreatePatientTagWithResponse(ctx, *match.Clinic.Id, createTag)
if err != nil {
return nil, err
}
if resp.StatusCode() != http.StatusOK && resp.StatusCode() != http.StatusCreated {
return nil, fmt.Errorf("unexpected status code %v when creating tagName %s", resp.StatusCode(), tagName)
}
}
}
Expand All @@ -304,7 +295,7 @@ func (o *newOrderProcessor) createTagsForPatient(ctx context.Context, order mode
for _, tagName := range tagNames {
patientTag, ok := existingTags[tagName]
if !ok {
// The tag should have been created. If it was deleted in the mean time returning an error will result in a retry
// The tag should have been created. If it was deleted in the meantime returning an error will result in a retry
return nil, fmt.Errorf("patient tag doesn't exist")
}
patientTagIds = append(patientTagIds, *patientTag.Id)
Expand All @@ -313,6 +304,30 @@ func (o *newOrderProcessor) createTagsForPatient(ctx context.Context, order mode
return &patientTagIds, nil
}

func (o *newOrderProcessor) replacePatientTags(ctx context.Context, match clinics.EHRMatchResponse, tagIds *clinics.PatientTagIds) error {
if tagIds == nil {
return nil
}

patient := (*match.Patients)[0]
update := clinics.UpdatePatientJSONRequestBody{
Email: patient.Email,
BirthDate: patient.BirthDate,
FullName: patient.FullName,
Mrn: patient.Mrn,
TargetDevices: patient.TargetDevices,
Tags: tagIds,
}
resp, err := o.clinics.UpdatePatientWithResponse(ctx, *match.Clinic.Id, *patient.Id, update)
if err != nil {
return err
}
if resp.StatusCode() != http.StatusOK {
return fmt.Errorf("unexpected status code %v replacing tags for patient %s", resp.StatusCode(), *patient.Id)
}
return nil
}

func (o *newOrderProcessor) getPatientTagCodes(match clinics.EHRMatchResponse) map[string]struct{} {
result := make(map[string]struct{})
if match.Settings.Tags.Codes != nil {
Expand All @@ -327,6 +342,33 @@ func (o *newOrderProcessor) getPatientTagsSeparator(match clinics.EHRMatchRespon
return match.Settings.Tags.Separator
}

func (o *newOrderProcessor) getTagNamesFromOrder(order models.NewOrder, match clinics.EHRMatchResponse) []string {
separator := o.getPatientTagsSeparator(match)
codes := o.getPatientTagCodes(match)
if len(codes) == 0 {
return nil
}

tagNames := make([]string, 0)
if order.Order.ClinicalInfo != nil {
for _, info := range *order.Order.ClinicalInfo {
if info.Code != nil && info.Value != nil {
if _, ok := codes[*info.Code]; ok {
if separator == nil || *separator == "" {
tagNames = append(tagNames, strings.TrimSpace(*info.Value))
} else {
for _, tag := range strings.Split(*info.Value, *separator) {
tagNames = append(tagNames, strings.TrimSpace(tag))
}
}
}
}
}
}

return tagNames
}

func (o *newOrderProcessor) getExistingTags(clinic clinics.Clinic) map[string]clinics.PatientTag {
tags := make(map[string]clinics.PatientTag)
if clinic.PatientTags != nil {
Expand Down
Loading

0 comments on commit 71771fd

Please sign in to comment.