diff --git a/xealth/xealth.go b/xealth/xealth.go index c1071933..94c7d5c6 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,22 @@ 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 { + 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) + } + } + } + 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)