Skip to content

Commit de7c7bb

Browse files
authoredFeb 29, 2024··
Merge pull request #28 from d-tree-org/27-questmalawi-core-android-app-sync-resources-with-custom-filters
[Quest/Malawi Core] Android App : Sync Resources with custom filters
2 parents 661190a + decebfc commit de7c7bb

File tree

2 files changed

+103
-8
lines changed

2 files changed

+103
-8
lines changed
 

‎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+
}

0 commit comments

Comments
 (0)