From 695d4bf0baee208737e185adbe19bc3629406ac8 Mon Sep 17 00:00:00 2001 From: Todd Kazakov Date: Wed, 3 Sep 2025 15:54:04 +0300 Subject: [PATCH 1/2] Populate the email of custodial accounts from subscription orders if one isn't set already --- xealth/xealth.go | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/xealth/xealth.go b/xealth/xealth.go index c1071933..826c7f11 100644 --- a/xealth/xealth.go +++ b/xealth/xealth.go @@ -437,8 +437,9 @@ func (d *defaultHandler) handleNewOrder(ctx context.Context, documentId string) return fmt.Errorf("unable to create subscription update: %w", err) } + var create *patients.Patient if match.Patient == nil { - create, err := GetPatientCreateFromOrder(match, preorderData) + create, err = GetPatientCreateFromOrder(match, preorderData) if err != nil { return fmt.Errorf("unable to create patient create: %w", err) } @@ -466,6 +467,18 @@ func (d *defaultHandler) handleNewOrder(ctx context.Context, documentId string) } } + // Update the email of custodial patients if it's missing + if match.Patient != nil && match.Patient.IsCustodial() && match.Patient.Email == nil && create != nil && create.Email != nil { + match.Patient.Email = create.Email + if _, err := d.patients.Update(ctx, patients.PatientUpdate{ + ClinicId: match.Patient.ClinicId.Hex(), + UserId: *match.Patient.UserId, + Patient: *match.Patient, + }); err != nil { + return fmt.Errorf("unable to update patient: %w", err) + } + } + err = d.patients.UpdateEHRSubscription(ctx, match.Clinic.Id.Hex(), *match.Patient.UserId, *update) if err != nil { return fmt.Errorf("unable to update ehr subscription: %w", err) From 9b37ac94512749a57238029de5373d8aafa9ed0f Mon Sep 17 00:00:00 2001 From: Todd Kazakov Date: Thu, 11 Sep 2025 12:06:58 +0300 Subject: [PATCH 2/2] Log an error message if duplicate email address exists --- xealth/xealth.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/xealth/xealth.go b/xealth/xealth.go index 826c7f11..94c7d5c6 100644 --- a/xealth/xealth.go +++ b/xealth/xealth.go @@ -475,7 +475,11 @@ func (d *defaultHandler) handleNewOrder(ctx context.Context, documentId string) UserId: *match.Patient.UserId, Patient: *match.Patient, }); err != nil { - return fmt.Errorf("unable to update patient: %w", err) + if errors.Is(err, patients.ErrDuplicateEmail) { + d.logger.Warnw("custodial account email is already taken", "clinicId", match.Patient.ClinicId.Hex(), "userId", *match.Patient.UserId) + } else { + return fmt.Errorf("unable to update patient: %w", err) + } } }