Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion xealth/xealth.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
Expand Down