@@ -6,12 +6,15 @@ import dagger.Lazy
66import dagger.assisted.Assisted
77import dagger.assisted.AssistedFactory
88import dagger.assisted.AssistedInject
9+ import io.reactivex.Completable
910import io.reactivex.ObservableTransformer
1011import io.reactivex.Scheduler
1112import org.simple.clinic.facility.Facility
1213import org.simple.clinic.medicalhistory.MedicalHistoryRepository
1314import org.simple.clinic.medicalhistory.OngoingMedicalHistoryEntry
1415import org.simple.clinic.patient.PatientRepository
16+ import org.simple.clinic.patientattribute.BMIReading
17+ import org.simple.clinic.patientattribute.PatientAttributeRepository
1518import org.simple.clinic.sync.DataSync
1619import org.simple.clinic.user.User
1720import org.simple.clinic.util.scheduler.SchedulersProvider
@@ -23,6 +26,7 @@ class NewMedicalHistoryEffectHandler @AssistedInject constructor(
2326 private val schedulersProvider : SchedulersProvider ,
2427 private val patientRepository : PatientRepository ,
2528 private val medicalHistoryRepository : MedicalHistoryRepository ,
29+ private val patientAttributeRepository : PatientAttributeRepository ,
2630 private val dataSync : DataSync ,
2731 private val currentUser : Lazy <User >,
2832 private val currentFacility : Lazy <Facility >,
@@ -57,30 +61,44 @@ class NewMedicalHistoryEffectHandler @AssistedInject constructor(
5761 val loggedInUser = currentUser.get()
5862 val facility = currentFacility.get()
5963 val ongoingMedicalHistoryEntry = registerPatientEffect.ongoingMedicalHistoryEntry
64+ val bmiReading = registerPatientEffect.bmiReading
6065
61- RegisterPatientData (loggedInUser, facility, ongoingMedicalHistoryEntry)
66+ RegisterPatientData (loggedInUser, facility, ongoingMedicalHistoryEntry, bmiReading )
6267 }
63- .map { (user, facility, ongoingMedicalHistoryEntry) ->
68+ .map { registerPatientData ->
6469 patientRepository
6570 .saveOngoingEntryAsPatient(
6671 patientEntry = patientRepository.ongoingEntry(),
67- loggedInUser = user,
68- facility = facility,
72+ loggedInUser = registerPatientData. user,
73+ facility = registerPatientData. facility,
6974 patientUuid = uuidGenerator.v4(),
7075 addressUuid = uuidGenerator.v4(),
7176 supplyUuidForBpPassport = uuidGenerator::v4,
7277 supplyUuidForAlternativeId = uuidGenerator::v4,
7378 supplyUuidForPhoneNumber = uuidGenerator::v4,
7479 dateOfBirthFormatter = dateOfBirthFormatter
75- ) to ongoingMedicalHistoryEntry
80+ ) to registerPatientData
7681 }
77- .flatMapSingle { (registeredPatient, ongoingMedicalHistoryEntry) ->
82+ .flatMapSingle { (registeredPatient, registeredPatientData) ->
83+
84+ val saveBmi = Completable .fromAction {
85+ registeredPatientData.bmiReading?.let {
86+ patientAttributeRepository.save(
87+ bmiReading = it,
88+ patientUuid = registeredPatient.patientUuid,
89+ loggedInUserUuid = registeredPatientData.user.uuid,
90+ uuid = uuidGenerator.v4(),
91+ )
92+ }
93+ }
94+
7895 medicalHistoryRepository
7996 .save(
8097 uuid = uuidGenerator.v4(),
8198 patientUuid = registeredPatient.patientUuid,
82- historyEntry = ongoingMedicalHistoryEntry
99+ historyEntry = registeredPatientData. ongoingMedicalHistoryEntry
83100 )
101+ .andThen(saveBmi)
84102 .toSingleDefault(PatientRegistered (registeredPatient.patientUuid))
85103 }
86104 }
@@ -114,6 +132,7 @@ class NewMedicalHistoryEffectHandler @AssistedInject constructor(
114132 private data class RegisterPatientData (
115133 val user : User ,
116134 val facility : Facility ,
117- val ongoingMedicalHistoryEntry : OngoingMedicalHistoryEntry
135+ val ongoingMedicalHistoryEntry : OngoingMedicalHistoryEntry ,
136+ val bmiReading : BMIReading ?
118137 )
119138}
0 commit comments