Skip to content

Commit

Permalink
Allow creating and associating patient tags with Redox create account…
Browse files Browse the repository at this point in the history
… orders
  • Loading branch information
toddkazakov committed Sep 26, 2024
1 parent ba27541 commit 3ab94e1
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 2 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-20240802193352-3f912afe2109
github.com/tidepool-org/clinic/client v0.0.0-20240926112325-657da308fce2
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 @@ -107,6 +107,8 @@ github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsT
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/tidepool-org/clinic/client v0.0.0-20240802193352-3f912afe2109 h1:AKup7wHFkLcWv13GaKfKQcWjYSvUYilUPcbloP0JXX4=
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/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
96 changes: 96 additions & 0 deletions redox/processor_neworder.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,12 @@ func (o *newOrderProcessor) handleCreateAccount(ctx context.Context, order model
}
}

createPatient.Tags, err = o.createTagsForPatient(ctx, order, match)
if err != nil {
o.logger.Errorw("unexpected error when creating tags for patient", "order", order.Meta, "error", err)
return err
}

resp, err := o.clinics.CreatePatientAccountWithResponse(ctx, *match.Clinic.Id, createPatient)
if err != nil {
// Retry in case of unexpected failure
Expand All @@ -242,6 +248,96 @@ func (o *newOrderProcessor) handleCreateAccount(ctx context.Context, order model
return o.handleAccountCreationSuccess(ctx, order, match)
}

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 {
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)
}
}
}
}

resp, err := o.clinics.GetClinicWithResponse(ctx, *match.Clinic.Id)
if err != nil {
return nil, err
}
if resp.StatusCode() != http.StatusOK {
return nil, fmt.Errorf("unexpected status code %vwhen fetching clinic with id %s", resp.StatusCode(), *match.Clinic.Id)
}

existingTags = o.getExistingTags(*resp.JSON200)
patientTagIds := clinics.PatientTagIds{}
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
return nil, fmt.Errorf("patient tag doesn't exist")
}
patientTagIds = append(patientTagIds, *patientTag.Id)
}

return &patientTagIds, nil
}

func (o *newOrderProcessor) getPatientTagCodes(match clinics.EHRMatchResponse) map[string]struct{} {
result := make(map[string]struct{})
if match.Settings.Tags.Codes != nil {
for _, code := range *match.Settings.Tags.Codes {
result[code] = struct{}{}
}
}
return result
}

func (o *newOrderProcessor) getPatientTagsSeparator(match clinics.EHRMatchResponse) *string {
return match.Settings.Tags.Separator
}

func (o *newOrderProcessor) getExistingTags(clinic clinics.Clinic) map[string]clinics.PatientTag {
tags := make(map[string]clinics.PatientTag)
if clinic.PatientTags != nil {
for _, tag := range *clinic.PatientTags {
tags[tag.Name] = tag
}
}

return tags
}

func (o *newOrderProcessor) emailExists(email string) (bool, error) {
user, err := o.shorelineClient.GetUser(email, o.shorelineClient.TokenProvide())
if user != nil && len(user.UserID) > 0 {
Expand Down
26 changes: 26 additions & 0 deletions vendor/github.com/tidepool-org/clinic/client/types.go

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

2 changes: 1 addition & 1 deletion vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ github.com/pierrec/lz4/v4/internal/xxh32
# github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475
## explicit
github.com/rcrowley/go-metrics
# github.com/tidepool-org/clinic/client v0.0.0-20240802193352-3f912afe2109
# github.com/tidepool-org/clinic/client v0.0.0-20240926112325-657da308fce2
## explicit; go 1.22
github.com/tidepool-org/clinic/client
# github.com/tidepool-org/clinic/redox_models v0.0.0-20240802193352-3f912afe2109
Expand Down

0 comments on commit 3ab94e1

Please sign in to comment.