Skip to content

Commit

Permalink
Save remote resources as non local changes(#49)
Browse files Browse the repository at this point in the history
* Save config resources as remote resources

To avoid being marked as LocalChange

* Exclude resources from practitionerDetails endpoint

Not to be marked as local change
  • Loading branch information
LZRS authored Mar 20, 2024
1 parent 34799b5 commit bc87133
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,13 @@ constructor(
}
}

suspend fun saveRemote(vararg resource: Resource) {
return withContext(dispatcherProvider.io()) {
resource.forEach { it.generateMissingId() }
fhirEngine.createRemote(*resource)
}
}

suspend fun create(addResourceTags: Boolean = true, vararg resource: Resource): List<String> {
return withContext(dispatcherProvider.io()) {
resource.onEach {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,20 +111,21 @@ constructor(
}

val composition = data.resource as Composition
defaultRepository.save(composition)

composition.section
.groupBy { it.focus.reference.split("/")[0] }
.entries
.filter { it.key == ResourceType.Binary.name || it.key == ResourceType.Parameters.name }
.forEach { entry: Map.Entry<String, List<Composition.SectionComponent>> ->
val ids = entry.value.joinToString(",") { it.focus.extractId() }
val rPath = entry.key + "?${Composition.SP_RES_ID}=$ids"
fhirResourceDataSource.loadData(rPath).entry.forEach {
defaultRepository.save(it.resource)

val compositionBinaries =
composition.section
.groupBy { it.focus.reference.split("/")[0] }
.entries
.filter { it.key == ResourceType.Binary.name || it.key == ResourceType.Parameters.name }
.flatMap { entry: Map.Entry<String, List<Composition.SectionComponent>> ->
val ids = entry.value.joinToString(",") { it.focus.extractId() }
val rPath = entry.key + "?${Composition.SP_RES_ID}=$ids"
fhirResourceDataSource.loadData(rPath).entry.map { it.resource }
}
}
.toTypedArray()

val remoteResources = compositionBinaries + arrayOf(composition)
defaultRepository.saveRemote(*remoteResources)
loadConfigurations(context)
_showProgressBar.postValue(false)
} catch (unknownHostException: UnknownHostException) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import javax.inject.Inject
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import org.hl7.fhir.r4.model.Bundle
import org.hl7.fhir.r4.model.Resource
import org.hl7.fhir.r4.model.ResourceType
import org.jetbrains.annotations.TestOnly
import org.smartregister.fhircore.engine.auth.AccountAuthenticator
Expand Down Expand Up @@ -248,20 +249,24 @@ constructor(
val locations = practitionerDetails.fhirPractitionerDetails?.locations ?: listOf()
val locationHierarchies =
practitionerDetails.fhirPractitionerDetails?.locationHierarchyList ?: listOf()

val careTeamIds =
defaultRepository.create(false, *careTeams.toTypedArray()).map { it.extractLogicalIdUuid() }
sharedPreferences.write(ResourceType.CareTeam.name, careTeamIds)

val organizationIds =
defaultRepository.create(false, *organizations.toTypedArray()).map {
it.extractLogicalIdUuid()
}
sharedPreferences.write(ResourceType.Organization.name, organizationIds)

val locationIds =
defaultRepository.create(false, *locations.toTypedArray()).map { it.extractLogicalIdUuid() }
sharedPreferences.write(ResourceType.Location.name, locationIds)
val groups = practitionerDetails.fhirPractitionerDetails?.groups ?: emptyList()
val practitionerRoles =
practitionerDetails.fhirPractitionerDetails?.practitionerRoles ?: emptyList()
val practitioners = practitionerDetails.fhirPractitionerDetails?.practitioners ?: emptyList()

val remoteResources =
careTeams.toTypedArray<Resource>() +
organizations.toTypedArray() +
locations.toTypedArray() +
groups.toTypedArray() +
practitionerRoles.toTypedArray() +
practitioners.toTypedArray()
defaultRepository.saveRemote(*remoteResources)
with(sharedPreferences) {
write(ResourceType.CareTeam.name, careTeams.map { it.id.extractLogicalIdUuid() })
write(ResourceType.Organization.name, organizations.map { it.id.extractLogicalIdUuid() })
write(ResourceType.Location.name, locations.map { it.id.extractLogicalIdUuid() })
}

sharedPreferences.write(
SharedPreferenceKey.PRACTITIONER_LOCATION_HIERARCHIES.name,
Expand Down

0 comments on commit bc87133

Please sign in to comment.