Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Quest/Malawi Core] Android App : Sync Resources with custom filters #28

Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,12 @@ constructor(
val configurationRegistry: ConfigurationRegistry
) : RegisterDao, PatientDao {

private val code = defaultRepository.sharedPreferencesHelper.organisationCode()

fun isValidPatient(patient: Patient): Boolean =
patient.active &&
!patient.hasDeceased() &&
patient.hasName() &&
patient.hasGender() &&
patient.meta.tag.none { it.code.equals(HAPI_MDM_TAG, true) } &&
patient.belongsTo(code)
patient.meta.tag.none { it.code.equals(HAPI_MDM_TAG, true) }

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

fun SharedPreferencesHelper.organisationCode() =
read(ResourceType.Organization.name, null)?.filter { it.isDigit() } ?: ""
fun SharedPreferencesHelper.locationCode() =
read(ResourceType.Location.name, null)?.filter { it.isDigit() } ?: ""

infix fun Patient.belongsTo(code: String) =
meta.tag.any {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,23 @@ import com.google.android.fhir.sync.SyncJobStatus
import java.lang.ref.WeakReference
import javax.inject.Inject
import javax.inject.Singleton
import org.hl7.fhir.r4.model.Appointment
import org.hl7.fhir.r4.model.CarePlan
import org.hl7.fhir.r4.model.Encounter
import org.hl7.fhir.r4.model.ListResource
import org.hl7.fhir.r4.model.Observation
import org.hl7.fhir.r4.model.Parameters
import org.hl7.fhir.r4.model.Patient
import org.hl7.fhir.r4.model.RelatedPerson
import org.hl7.fhir.r4.model.ResourceType
import org.hl7.fhir.r4.model.SearchParameter
import org.smartregister.fhircore.engine.R
import org.smartregister.fhircore.engine.configuration.ConfigurationRegistry
import org.smartregister.fhircore.engine.configuration.FhirConfiguration
import org.smartregister.fhircore.engine.configuration.app.AppConfigClassification
import org.smartregister.fhircore.engine.configuration.app.ApplicationConfiguration
import org.smartregister.fhircore.engine.configuration.app.ConfigService
import org.smartregister.fhircore.engine.data.local.register.dao.locationCode
import org.smartregister.fhircore.engine.data.remote.model.response.UserInfo
import org.smartregister.fhircore.engine.util.SharedPreferencesHelper
import org.smartregister.fhircore.engine.util.USER_INFO_SHARED_PREFERENCE_KEY
Expand Down Expand Up @@ -138,14 +147,103 @@ constructor(
// e.g. [(Patient, {organization=105})] to [(Patient, {organization=105, _count=100})]
val updatedPair = pair.second.toMutableMap().apply { put(sp.code, expressionValue) }
val index = pairs.indexOfFirst { it.first == resourceType }
resourceType.filterBasedOnPerResourceType(pairs)
pairs.set(index, Pair(resourceType, updatedPair))
}
}
}
}

Timber.i("SYNC CONFIG $pairs")
val syncConfigParams = sharedPreferencesHelper.filterByResourceLocation(pairs)
Timber.i("SYNC CONFIG $syncConfigParams")

return mapOf(*pairs.toTypedArray())
return mapOf(*syncConfigParams.toTypedArray())
}
}

private fun ResourceType.filterBasedOnPerResourceType(
pairs: MutableList<Pair<ResourceType, Map<String, String>>>
) =
when (this) {
ResourceType.RelatedPerson ->
pairs.addParam(resourceType = this, param = RelatedPerson.SP_ACTIVE, value = true.toString())
ResourceType.Patient ->
pairs.addParam(resourceType = this, param = Patient.SP_ACTIVE, value = true.toString())
ResourceType.CarePlan ->
pairs.addParam(
resourceType = this,
param = CarePlan.SP_STATUS,
value = CarePlan.CarePlanStatus.ACTIVE.toString().lowercase()
)
ResourceType.Observation ->
pairs.addParam(
resourceType = this,
param = Observation.SP_STATUS,
value = Observation.ObservationStatus.FINAL.toString().lowercase()
)

// ResourceType.Task ->
// pairs.addParam(
// resourceType = this,
// param = Task.SP_STATUS,
// value =
// String.format(
// "%s,%s",
// Task.TaskStatus.FAILED.toString().lowercase(),
// Task.TaskStatus.INPROGRESS.toString().lowercase()
// )
// )

ResourceType.Appointment ->
pairs.addParam(
resourceType = this,
param = Appointment.SP_STATUS,
value = Appointment.AppointmentStatus.BOOKED.toString().lowercase()
)
ResourceType.Encounter ->
pairs.addParam(
resourceType = this,
param = Encounter.SP_STATUS,
value = Encounter.EncounterStatus.INPROGRESS.toString().lowercase()
)
ResourceType.List ->
pairs.addParam(
resourceType = this,
param = ListResource.SP_STATUS,
value = ListResource.ListStatus.CURRENT.toString().lowercase()
)
else -> Unit
}

private fun SharedPreferencesHelper.filterByResourceLocation(
pairs: MutableList<Pair<ResourceType, Map<String, String>>>,
): MutableList<Pair<ResourceType, Map<String, String>>> {

val resourcesTemp = mutableListOf<Pair<ResourceType, Map<String, String>>>()
val results = mutableListOf<Pair<ResourceType, Map<String, String>>>()
resourcesTemp.addAll(pairs)

val locationSystem = context.getString(R.string.sync_strategy_location_system)
val locationTag = "$locationSystem|${locationCode()}"

resourcesTemp.forEach {
val resourceType = it.first
if (resourceType != ResourceType.Practitioner &&
resourceType != ResourceType.Questionnaire &&
resourceType != ResourceType.StructureMap
) {
val tags = mutableMapOf("_tag" to locationTag)
it.second.entries.forEach { entry -> tags[entry.key] = entry.value }
results.add(Pair(resourceType, tags))
} else results.add(it)
}
return results
}

private fun MutableList<Pair<ResourceType, Map<String, String>>>.addParam(
resourceType: ResourceType,
param: String,
value: String,
) {
add(Pair(resourceType, mapOf(param to value)))
}
Loading