@@ -34,42 +34,45 @@ class DatasetInstancesRepositoryImpl @Inject constructor(
3434 Log .d(TAG , " Fetching dataset instances for dataset: $datasetId " )
3535 return withContext(Dispatchers .IO ) {
3636 try {
37- // Get user's data capture org units
38- Log .d( TAG , " Fetching user org units " )
39- val userOrgUnits = d2.organisationUnitModule().organisationUnits()
37+ Log .d( TAG , " Fetching scoped org units and dataset attachments for $datasetId " )
38+
39+ val scopedOrgUnits = d2.organisationUnitModule().organisationUnits()
4040 .byOrganisationUnitScope(OrganisationUnit .Scope .SCOPE_DATA_CAPTURE )
4141 .get().await()
4242
43- Log .d(TAG , " Found ${userOrgUnits.size} org units" )
44-
45- val userOrgUnitUid = userOrgUnits.firstOrNull()?.uid()
46- if (userOrgUnitUid == null ) {
47- Log .e(TAG , " No organization unit found for user" )
43+ if (scopedOrgUnits.isEmpty()) {
44+ Log .e(TAG , " No organization units found in data capture scope" )
4845 return @withContext emptyList()
4946 }
5047
51- Log .d(TAG , " Using org unit: $userOrgUnitUid " )
48+ val attachedOrgUnits = d2.organisationUnitModule().organisationUnits()
49+ .byDataSetUids(listOf (datasetId))
50+ .get().await()
5251
53- // Get dataset instances
52+ val scopedIds = scopedOrgUnits.map { it.uid() }.toSet()
53+ val attachedIds = attachedOrgUnits.map { it.uid() }.toSet()
54+ val relevantOrgUnitIds = scopedIds.intersect(attachedIds).ifEmpty { scopedIds }
5455
55- val instance = d2.dataSetModule()
56+ Log .d(
57+ TAG ,
58+ " Scoped=${scopedIds.size} , Attached=${attachedIds.size} , Relevant=${relevantOrgUnitIds.size} "
59+ )
60+
61+ // Get dataset instances across all relevant org units
62+ val instanceCount = d2.dataSetModule()
5663 .dataSetInstances()
5764 .byDataSetUid().eq(datasetId)
58- .byOrganisationUnitUid().eq(userOrgUnitUid )
65+ .byOrganisationUnitUid().` in `(relevantOrgUnitIds.toList() )
5966 .blockingCount()
6067
61-
62-
63-
6468 val instances = d2.dataSetModule()
6569 .dataSetInstances()
6670 .byDataSetUid().eq(datasetId)
67- .byOrganisationUnitUid().eq(userOrgUnitUid )
71+ .byOrganisationUnitUid().` in `(relevantOrgUnitIds.toList() )
6872 .get().await()
6973
70- Log .d(TAG , " Found $instance instances for dataset" )
71-
72- Log .d(TAG , " Found ${instances.size} instances for dataset" )
74+ Log .d(TAG , " Found $instanceCount instances for dataset" )
75+ Log .d(TAG , " Loaded ${instances.size} instances for dataset" )
7376
7477
7578 val sdkInstances = instances.map { instance ->
@@ -207,27 +210,42 @@ class DatasetInstancesRepositoryImpl @Inject constructor(
207210 }
208211 }
209212
210- override suspend fun getDatasetInstanceCount (datasetId : String ): Int {
213+ override suspend fun getDatasetInstanceCount (datasetId : String , orgUnitIds : Set < String > ? ): Int {
211214 return withContext(Dispatchers .IO ) {
212215 try {
213216 Log .d(TAG , " Getting instance count for dataset: $datasetId " )
214-
215- // Get user's data capture org units
216- val userOrgUnits = d2.organisationUnitModule().organisationUnits()
217+
218+ // Get scoped org units
219+ val scopedOrgUnits = d2.organisationUnitModule().organisationUnits()
217220 .byOrganisationUnitScope(OrganisationUnit .Scope .SCOPE_DATA_CAPTURE )
218221 .get().await()
219222
220- val userOrgUnitUid = userOrgUnits.firstOrNull()?.uid()
221- if (userOrgUnitUid == null ) {
222- Log .e(TAG , " No organization unit found for user" )
223+ if (scopedOrgUnits.isEmpty()) {
224+ Log .e(TAG , " No organization units found in data capture scope" )
225+ return @withContext 0
226+ }
227+
228+ val scopedIds = scopedOrgUnits.map { it.uid() }.toSet()
229+ val relevantOrgUnitIds = if (orgUnitIds != null ) {
230+ orgUnitIds.intersect(scopedIds)
231+ } else {
232+ val attachedOrgUnits = d2.organisationUnitModule().organisationUnits()
233+ .byDataSetUids(listOf (datasetId))
234+ .get().await()
235+ val attachedIds = attachedOrgUnits.map { it.uid() }.toSet()
236+ scopedIds.intersect(attachedIds).ifEmpty { scopedIds }
237+ }
238+
239+ if (relevantOrgUnitIds.isEmpty()) {
240+ Log .d(TAG , " No relevant org units for dataset $datasetId under current filter" )
223241 return @withContext 0
224242 }
225243
226- // Count dataset instances
244+ // Count dataset instances across all relevant org units
227245 val count = d2.dataSetModule()
228246 .dataSetInstances()
229247 .byDataSetUid().eq(datasetId)
230- .byOrganisationUnitUid().eq(userOrgUnitUid )
248+ .byOrganisationUnitUid().` in `(relevantOrgUnitIds.toList() )
231249 .blockingCount()
232250
233251 Log .d(TAG , " Found $count instances for dataset $datasetId " )
@@ -447,8 +465,24 @@ class DatasetInstancesRepositoryImpl @Inject constructor(
447465 com.ash.simpledataentry.domain.model.ProgramType .TRACKER -> {
448466 withContext(Dispatchers .IO ) {
449467 try {
468+ val scopedOrgUnits = d2.organisationUnitModule().organisationUnits()
469+ .byOrganisationUnitScope(OrganisationUnit .Scope .SCOPE_DATA_CAPTURE )
470+ .get().await()
471+ if (scopedOrgUnits.isEmpty()) {
472+ return @withContext 0
473+ }
474+
475+ val attachedOrgUnits = d2.organisationUnitModule().organisationUnits()
476+ .byProgramUids(listOf (programId))
477+ .get().await()
478+
479+ val scopedIds = scopedOrgUnits.map { it.uid() }.toSet()
480+ val attachedIds = attachedOrgUnits.map { it.uid() }.toSet()
481+ val relevantOrgUnitIds = scopedIds.intersect(attachedIds).ifEmpty { scopedIds }
482+
450483 d2.trackedEntityModule().trackedEntityInstances()
451484 .byProgramUids(listOf (programId))
485+ .byOrganisationUnitUid().`in `(relevantOrgUnitIds.toList())
452486 .blockingCount()
453487 } catch (e: Exception ) {
454488 Log .e(TAG , " Failed to get tracker enrollment count for program $programId " , e)
@@ -459,8 +493,24 @@ class DatasetInstancesRepositoryImpl @Inject constructor(
459493 com.ash.simpledataentry.domain.model.ProgramType .EVENT -> {
460494 withContext(Dispatchers .IO ) {
461495 try {
496+ val scopedOrgUnits = d2.organisationUnitModule().organisationUnits()
497+ .byOrganisationUnitScope(OrganisationUnit .Scope .SCOPE_DATA_CAPTURE )
498+ .get().await()
499+ if (scopedOrgUnits.isEmpty()) {
500+ return @withContext 0
501+ }
502+
503+ val attachedOrgUnits = d2.organisationUnitModule().organisationUnits()
504+ .byProgramUids(listOf (programId))
505+ .get().await()
506+
507+ val scopedIds = scopedOrgUnits.map { it.uid() }.toSet()
508+ val attachedIds = attachedOrgUnits.map { it.uid() }.toSet()
509+ val relevantOrgUnitIds = scopedIds.intersect(attachedIds).ifEmpty { scopedIds }
510+
462511 d2.eventModule().events()
463512 .byProgramUid().eq(programId)
513+ .byOrganisationUnitUid().`in `(relevantOrgUnitIds.toList())
464514 .blockingCount()
465515 } catch (e: Exception ) {
466516 Log .e(TAG , " Failed to get event count for program $programId " , e)
0 commit comments