Skip to content

Commit 6acb345

Browse files
committed
clean up tracing
1 parent 54ca6fb commit 6acb345

File tree

6 files changed

+123
-16
lines changed

6 files changed

+123
-16
lines changed

android/engine/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ dependencies {
133133
implementation 'androidx.fragment:fragment-ktx:1.5.5'
134134
implementation 'io.jsonwebtoken:jjwt:0.9.1'
135135
implementation 'androidx.security:security-crypto:1.1.0-alpha03'
136-
implementation 'org.smartregister:fhir-common-utils:0.0.6-SNAPSHOT'
136+
implementation 'org.smartregister:fhir-common-utils:0.0.8-SNAPSHOT'
137137
implementation 'com.squareup.okhttp3:logging-interceptor:4.9.1'
138138
implementation 'com.squareup.okhttp3:okhttp:4.9.1'
139139
implementation "androidx.cardview:cardview:1.0.0"

android/engine/src/main/java/org/smartregister/fhircore/engine/data/local/register/dao/HivRegisterDao.kt

+3-6
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,12 @@ constructor(
7979
val configurationRegistry: ConfigurationRegistry
8080
) : RegisterDao, PatientDao {
8181

82-
private val code = defaultRepository.sharedPreferencesHelper.organisationCode()
83-
8482
fun isValidPatient(patient: Patient): Boolean =
8583
patient.active &&
8684
!patient.hasDeceased() &&
8785
patient.hasName() &&
8886
patient.hasGender() &&
89-
patient.meta.tag.none { it.code.equals(HAPI_MDM_TAG, true) } &&
90-
patient.belongsTo(code)
87+
patient.meta.tag.none { it.code.equals(HAPI_MDM_TAG, true) }
9188

9289
fun hivPatientIdentifier(patient: Patient): String =
9390
// would either be an ART or HCC number
@@ -446,8 +443,8 @@ suspend fun DefaultRepository.isPatientPregnant(patient: Patient) =
446443
suspend fun DefaultRepository.isPatientBreastfeeding(patient: Patient) =
447444
patientConditions(patient.logicalId).activelyBreastfeeding()
448445

449-
fun SharedPreferencesHelper.organisationCode() =
450-
read(ResourceType.Organization.name, null)?.filter { it.isDigit() } ?: ""
446+
fun SharedPreferencesHelper.locationCode() =
447+
read(ResourceType.Location.name, null)?.filter { it.isDigit() } ?: ""
451448

452449
infix fun Patient.belongsTo(code: String) =
453450
meta.tag.any {

android/engine/src/main/java/org/smartregister/fhircore/engine/sync/SyncListenerManager.kt

+100-2
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,23 @@ import com.google.android.fhir.sync.SyncJobStatus
2323
import java.lang.ref.WeakReference
2424
import javax.inject.Inject
2525
import javax.inject.Singleton
26+
import org.hl7.fhir.r4.model.Appointment
27+
import org.hl7.fhir.r4.model.CarePlan
28+
import org.hl7.fhir.r4.model.Encounter
29+
import org.hl7.fhir.r4.model.ListResource
30+
import org.hl7.fhir.r4.model.Observation
2631
import org.hl7.fhir.r4.model.Parameters
32+
import org.hl7.fhir.r4.model.Patient
33+
import org.hl7.fhir.r4.model.RelatedPerson
2734
import org.hl7.fhir.r4.model.ResourceType
2835
import org.hl7.fhir.r4.model.SearchParameter
36+
import org.smartregister.fhircore.engine.R
2937
import org.smartregister.fhircore.engine.configuration.ConfigurationRegistry
3038
import org.smartregister.fhircore.engine.configuration.FhirConfiguration
3139
import org.smartregister.fhircore.engine.configuration.app.AppConfigClassification
3240
import org.smartregister.fhircore.engine.configuration.app.ApplicationConfiguration
3341
import org.smartregister.fhircore.engine.configuration.app.ConfigService
42+
import org.smartregister.fhircore.engine.data.local.register.dao.locationCode
3443
import org.smartregister.fhircore.engine.data.remote.model.response.UserInfo
3544
import org.smartregister.fhircore.engine.util.SharedPreferencesHelper
3645
import org.smartregister.fhircore.engine.util.USER_INFO_SHARED_PREFERENCE_KEY
@@ -138,14 +147,103 @@ constructor(
138147
// e.g. [(Patient, {organization=105})] to [(Patient, {organization=105, _count=100})]
139148
val updatedPair = pair.second.toMutableMap().apply { put(sp.code, expressionValue) }
140149
val index = pairs.indexOfFirst { it.first == resourceType }
150+
resourceType.filterBasedOnPerResourceType(pairs)
141151
pairs.set(index, Pair(resourceType, updatedPair))
142152
}
143153
}
144154
}
145155
}
146156

147-
Timber.i("SYNC CONFIG $pairs")
157+
val syncConfigParams = sharedPreferencesHelper.filterByResourceLocation(pairs)
158+
Timber.i("SYNC CONFIG $syncConfigParams")
148159

149-
return mapOf(*pairs.toTypedArray())
160+
return mapOf(*syncConfigParams.toTypedArray())
150161
}
151162
}
163+
164+
private fun ResourceType.filterBasedOnPerResourceType(
165+
pairs: MutableList<Pair<ResourceType, Map<String, String>>>
166+
) =
167+
when (this) {
168+
ResourceType.RelatedPerson ->
169+
pairs.addParam(resourceType = this, param = RelatedPerson.SP_ACTIVE, value = true.toString())
170+
ResourceType.Patient ->
171+
pairs.addParam(resourceType = this, param = Patient.SP_ACTIVE, value = true.toString())
172+
ResourceType.CarePlan ->
173+
pairs.addParam(
174+
resourceType = this,
175+
param = CarePlan.SP_STATUS,
176+
value = CarePlan.CarePlanStatus.ACTIVE.toString().lowercase()
177+
)
178+
ResourceType.Observation ->
179+
pairs.addParam(
180+
resourceType = this,
181+
param = Observation.SP_STATUS,
182+
value = Observation.ObservationStatus.FINAL.toString().lowercase()
183+
)
184+
185+
// ResourceType.Task ->
186+
// pairs.addParam(
187+
// resourceType = this,
188+
// param = Task.SP_STATUS,
189+
// value =
190+
// String.format(
191+
// "%s,%s",
192+
// Task.TaskStatus.FAILED.toString().lowercase(),
193+
// Task.TaskStatus.INPROGRESS.toString().lowercase()
194+
// )
195+
// )
196+
197+
ResourceType.Appointment ->
198+
pairs.addParam(
199+
resourceType = this,
200+
param = Appointment.SP_STATUS,
201+
value = Appointment.AppointmentStatus.BOOKED.toString().lowercase()
202+
)
203+
ResourceType.Encounter ->
204+
pairs.addParam(
205+
resourceType = this,
206+
param = Encounter.SP_STATUS,
207+
value = Encounter.EncounterStatus.INPROGRESS.toString().lowercase()
208+
)
209+
ResourceType.List ->
210+
pairs.addParam(
211+
resourceType = this,
212+
param = ListResource.SP_STATUS,
213+
value = ListResource.ListStatus.CURRENT.toString().lowercase()
214+
)
215+
else -> Unit
216+
}
217+
218+
private fun SharedPreferencesHelper.filterByResourceLocation(
219+
pairs: MutableList<Pair<ResourceType, Map<String, String>>>,
220+
): MutableList<Pair<ResourceType, Map<String, String>>> {
221+
222+
val resourcesTemp = mutableListOf<Pair<ResourceType, Map<String, String>>>()
223+
val results = mutableListOf<Pair<ResourceType, Map<String, String>>>()
224+
resourcesTemp.addAll(pairs)
225+
226+
val locationSystem = context.getString(R.string.sync_strategy_location_system)
227+
val locationTag = "$locationSystem|${locationCode()}"
228+
229+
resourcesTemp.forEach {
230+
val resourceType = it.first
231+
if (resourceType != ResourceType.Practitioner &&
232+
resourceType != ResourceType.Questionnaire &&
233+
resourceType != ResourceType.StructureMap
234+
) {
235+
val tags = mutableMapOf("_tag" to locationTag)
236+
it.second.entries.forEach { entry -> tags[entry.key] = entry.value }
237+
results.add(Pair(resourceType, tags))
238+
} else results.add(it)
239+
}
240+
return results
241+
}
242+
243+
private fun MutableList<Pair<ResourceType, Map<String, String>>>.addParam(
244+
resourceType: ResourceType,
245+
param: String,
246+
value: String,
247+
) {
248+
add(Pair(resourceType, mapOf(param to value)))
249+
}

android/engine/src/main/java/org/smartregister/fhircore/engine/ui/login/LoginViewModel.kt

+7-1
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,13 @@ constructor(
263263
)
264264
sharedPreferences.write(
265265
key = SharedPreferenceKey.PRACTITIONER_ID.name,
266-
value = practitionerDetails.fhirPractitionerDetails?.practitionerId.valueToString()
266+
value =
267+
practitionerDetails
268+
.fhirPractitionerDetails
269+
?.practitioners
270+
?.firstOrNull()
271+
?.id
272+
?.extractLogicalIdUuid()
267273
)
268274

269275
sharedPreferences.write(SharedPreferenceKey.PRACTITIONER_DETAILS.name, practitionerDetails)

android/engine/src/test/java/org/smartregister/fhircore/engine/ui/questionnaire/QuestionnaireViewModelTest.kt

+11-5
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,9 @@ import org.smartregister.fhircore.engine.util.TracingHelpers
9898
import org.smartregister.fhircore.engine.util.USER_INFO_SHARED_PREFERENCE_KEY
9999
import org.smartregister.fhircore.engine.util.extension.addTags
100100
import org.smartregister.fhircore.engine.util.extension.asReference
101+
import org.smartregister.fhircore.engine.util.extension.extractLogicalIdUuid
101102
import org.smartregister.fhircore.engine.util.extension.loadResource
102103
import org.smartregister.fhircore.engine.util.extension.retainMetadata
103-
import org.smartregister.fhircore.engine.util.extension.valueToString
104104
import org.smartregister.model.practitioner.FhirPractitionerDetails
105105
import org.smartregister.model.practitioner.PractitionerDetails
106106

@@ -148,7 +148,12 @@ class QuestionnaireViewModelTest : RobolectricTest() {
148148
)
149149
sharedPreferencesHelper.write(
150150
SharedPreferenceKey.PRACTITIONER_ID.name,
151-
practitionerDetails().fhirPractitionerDetails.practitionerId.valueToString()
151+
practitionerDetails()
152+
.fhirPractitionerDetails
153+
?.practitioners
154+
?.firstOrNull()
155+
?.id
156+
?.extractLogicalIdUuid()
152157
)
153158

154159
defaultRepo =
@@ -1172,7 +1177,7 @@ class QuestionnaireViewModelTest : RobolectricTest() {
11721177
fhirPractitionerDetails =
11731178
FhirPractitionerDetails().apply {
11741179
id = "12345"
1175-
practitionerId = StringType("12345")
1180+
practitioners?.firstOrNull()?.id = StringType("12345").toString()
11761181
}
11771182
}
11781183
}
@@ -1183,7 +1188,7 @@ class QuestionnaireViewModelTest : RobolectricTest() {
11831188

11841189
questionnaireViewModel.appendPractitionerInfo(patient)
11851190

1186-
Assert.assertEquals("Practitioner/12345", patient.generalPractitioner[0].reference)
1191+
Assert.assertEquals(null, patient.generalPractitioner.firstOrNull())
11871192
}
11881193

11891194
@Test
@@ -1203,7 +1208,8 @@ class QuestionnaireViewModelTest : RobolectricTest() {
12031208
fun testAddPractitionerInfoShouldSetIndividualPractitionerReferenceToEncounterResource() {
12041209
val encounter = Encounter().apply { this.id = "123456" }
12051210
questionnaireViewModel.appendPractitionerInfo(encounter)
1206-
Assert.assertEquals("Practitioner/12345", encounter.participant[0].individual.reference)
1211+
// Assert.assertEquals("Practitioner/12345", encounter.participant[0].individual.reference)
1212+
Assert.assertEquals(null, encounter.participant.firstOrNull()?.individual?.reference)
12071213
}
12081214

12091215
@Test

android/quest/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ dependencies {
212212
implementation(project(":engine"))
213213
implementation 'androidx.ui:ui-foundation:0.1.0-dev14'
214214
implementation('org.smartregister:p2p-lib:0.3.0-SNAPSHOT')
215-
implementation 'org.smartregister:fhir-common-utils:0.0.6-SNAPSHOT'
215+
implementation 'org.smartregister:fhir-common-utils:0.0.8-SNAPSHOT'
216216
implementation deps.accompanist.swiperefresh
217217

218218
implementation 'com.github.anrwatchdog:anrwatchdog:1.4.0'

0 commit comments

Comments
 (0)