@@ -23,14 +23,23 @@ import com.google.android.fhir.sync.SyncJobStatus
23
23
import java.lang.ref.WeakReference
24
24
import javax.inject.Inject
25
25
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
26
31
import org.hl7.fhir.r4.model.Parameters
32
+ import org.hl7.fhir.r4.model.Patient
33
+ import org.hl7.fhir.r4.model.RelatedPerson
27
34
import org.hl7.fhir.r4.model.ResourceType
28
35
import org.hl7.fhir.r4.model.SearchParameter
36
+ import org.smartregister.fhircore.engine.R
29
37
import org.smartregister.fhircore.engine.configuration.ConfigurationRegistry
30
38
import org.smartregister.fhircore.engine.configuration.FhirConfiguration
31
39
import org.smartregister.fhircore.engine.configuration.app.AppConfigClassification
32
40
import org.smartregister.fhircore.engine.configuration.app.ApplicationConfiguration
33
41
import org.smartregister.fhircore.engine.configuration.app.ConfigService
42
+ import org.smartregister.fhircore.engine.data.local.register.dao.locationCode
34
43
import org.smartregister.fhircore.engine.data.remote.model.response.UserInfo
35
44
import org.smartregister.fhircore.engine.util.SharedPreferencesHelper
36
45
import org.smartregister.fhircore.engine.util.USER_INFO_SHARED_PREFERENCE_KEY
@@ -138,14 +147,103 @@ constructor(
138
147
// e.g. [(Patient, {organization=105})] to [(Patient, {organization=105, _count=100})]
139
148
val updatedPair = pair.second.toMutableMap().apply { put(sp.code, expressionValue) }
140
149
val index = pairs.indexOfFirst { it.first == resourceType }
150
+ resourceType.filterBasedOnPerResourceType(pairs)
141
151
pairs.set(index, Pair (resourceType, updatedPair))
142
152
}
143
153
}
144
154
}
145
155
}
146
156
147
- Timber .i(" SYNC CONFIG $pairs " )
157
+ val syncConfigParams = sharedPreferencesHelper.filterByResourceLocation(pairs)
158
+ Timber .i(" SYNC CONFIG $syncConfigParams " )
148
159
149
- return mapOf (* pairs .toTypedArray())
160
+ return mapOf (* syncConfigParams .toTypedArray())
150
161
}
151
162
}
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