Skip to content

Commit

Permalink
fix: [ANDROAPP-6535] Get TEI relationship title checking before the p…
Browse files Browse the repository at this point in the history
…rogram and then the TEIType

Signed-off-by: andresmr <[email protected]>
  • Loading branch information
andresmr committed Dec 10, 2024
1 parent 5465d6d commit ffe299a
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ class RelationshipFragment : FragmentGlobalAbstract(), RelationshipView {
uiState = uiState,
relationshipSelectionState = relationshipSelectionState,
onCreateRelationshipClick = {
it.teiTypeUid?.let { teiTypeUid ->
it.creationTEITypeUid?.let { teiTypeUid ->
goToRelationShip(
relationshipTypeModel = it.relationshipType,
teiTypeUid = teiTypeUid,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import org.dhis2.tracker.relationships.model.RelationshipOwnerType
import org.hisp.dhis.android.core.D2
import org.hisp.dhis.android.core.common.ObjectStyle
import org.hisp.dhis.android.core.event.Event
import org.hisp.dhis.android.core.maintenance.D2Error
import org.hisp.dhis.android.core.organisationunit.OrganisationUnit
import org.hisp.dhis.android.core.program.ProgramStage
import org.hisp.dhis.android.core.program.ProgramType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class TrackerRelationshipsRepository(
.withConstraints()
.byAvailableForTrackedEntityInstance(teiUid)
.blockingGet().map { relationshipType ->
val secondaryTeTypeUid = when {
val creationTEITypeUid = when {
relationshipType.fromConstraint()?.trackedEntityType()
?.uid() == teTypeUid ->
relationshipType.toConstraint()?.trackedEntityType()?.uid()
Expand All @@ -44,7 +44,7 @@ class TrackerRelationshipsRepository(

else -> null
}
Pair(relationshipType, secondaryTeTypeUid)
Pair(relationshipType, creationTEITypeUid)
}
)
}
Expand Down Expand Up @@ -228,15 +228,25 @@ class TrackerRelationshipsRepository(
}

override fun getRelationshipTitle(relationshipType: RelationshipType): String {
val teTypeUid = d2.trackedEntityModule().trackedEntityInstances()
val teiTypeUid = d2.trackedEntityModule().trackedEntityInstances()
.uid(teiUid)
.blockingGet()?.trackedEntityType()
return when (teTypeUid) {
relationshipType.fromConstraint()?.trackedEntityType()?.uid() -> {
val teiProgramUid = d2.enrollmentModule().enrollments()
.uid(enrollmentUid).blockingGet()?.program()
return when {
teiProgramUid == relationshipType.fromConstraint()?.program()?.uid() -> {
relationshipType.fromToName() ?: relationshipType.displayName() ?: ""
}

teiProgramUid == relationshipType.toConstraint()?.program()?.uid() -> {
relationshipType.toFromName() ?: relationshipType.displayName() ?: ""
}

teiTypeUid == relationshipType.fromConstraint()?.trackedEntityType()?.uid() -> {
relationshipType.fromToName() ?: relationshipType.displayName() ?: ""
}

relationshipType.toConstraint()?.trackedEntityType()?.uid() -> {
teiTypeUid == relationshipType.toConstraint()?.trackedEntityType()?.uid() -> {
relationshipType.toFromName() ?: relationshipType.displayName() ?: ""
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.dhis2.tracker.relationships.domain

import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.combine
import org.dhis2.commons.date.DateLabelProvider
import org.dhis2.tracker.relationships.data.RelationshipsRepository
Expand All @@ -25,18 +24,20 @@ class GetRelationshipsByType(
) { types, relationships ->
types.map { type ->
val relationshipType = type.first
val teiTypeUid = type.second
val creationTEITypeUid = type.second // TEI type uid of new relationship

// Filter relationships once based on relationshipType
val filteredRelationships = relationships.filter {
it.relationshipType.uid() == relationshipType.uid()
}

RelationshipSection(
title = relationshipsRepository.getRelationshipTitle(relationshipType),
title = relationshipsRepository.getRelationshipTitle(
relationshipType = relationshipType,
),
relationshipType = relationshipType,
relationships = filteredRelationships.map { mapToRelationshipItem(it) },
teiTypeUid = teiTypeUid
creationTEITypeUid = creationTEITypeUid
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import org.hisp.dhis.android.core.relationship.RelationshipType
data class RelationshipSection(
val title: String,
val relationships: List<RelationshipItem>,
val teiTypeUid: String?,
val creationTEITypeUid: String?,
val relationshipType: RelationshipType,
) {
fun canAddRelationship(): Boolean = teiTypeUid != null
fun canAddRelationship(): Boolean = creationTEITypeUid != null
}
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ fun RelationShipScreenPreview() {
lastUpdated = "Yesterday",
)
),
teiTypeUid = null,
creationTEITypeUid = null,
),
RelationshipSection(
title = "Empty relation ship",
Expand All @@ -339,7 +339,7 @@ fun RelationShipScreenPreview() {
.displayName("Empty relation ship")
.build(),
relationships = emptyList(),
teiTypeUid = "teiTypeUid",
creationTEITypeUid = "teiTypeUid",
)
)
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package org.dhis2.tracker.relationships.data

import org.dhis2.commons.resources.ResourceManager
import org.dhis2.tracker.data.ProfilePictureProvider
import org.hisp.dhis.android.core.D2
import org.hisp.dhis.android.core.common.ObjectWithUid
import org.hisp.dhis.android.core.enrollment.EnrollmentCollectionRepository
import org.hisp.dhis.android.core.enrollment.EnrollmentModule
import org.hisp.dhis.android.core.relationship.RelationshipConstraint
import org.hisp.dhis.android.core.relationship.RelationshipType
import org.hisp.dhis.android.core.trackedentity.TrackedEntityInstanceCollectionRepository
import org.hisp.dhis.android.core.trackedentity.TrackedEntityModule
import org.junit.Before
import org.junit.Test
import org.mockito.Mockito
import org.mockito.kotlin.doReturn
import org.mockito.kotlin.mock
import org.mockito.kotlin.whenever

class TrackerRelationshipsRepositoryTest {

private lateinit var trackerRelationshipsRepository: TrackerRelationshipsRepository

private val resources: ResourceManager = mock()
private val profilePictureProvider: ProfilePictureProvider = mock()

private val d2: D2 = Mockito.mock(D2::class.java, Mockito.RETURNS_DEEP_STUBS)
private val trackedEntityModule: TrackedEntityModule = mock()
private val trackedEntityInstances: TrackedEntityInstanceCollectionRepository = mock()
private val enrollmentModule: EnrollmentModule = mock()
private val enrollment: EnrollmentCollectionRepository = mock()

@Before
fun setup() {
whenever(d2.trackedEntityModule()) doReturn trackedEntityModule
whenever(trackedEntityModule.trackedEntityInstances()) doReturn trackedEntityInstances
whenever(trackedEntityInstances.uid("teiUid")) doReturn mock()
whenever(trackedEntityInstances.uid("teiUid").blockingGet()) doReturn mock()
whenever(
trackedEntityInstances.uid("teiUid").blockingGet()?.trackedEntityType()
) doReturn "trackedEntityTypeUid"

whenever(d2.enrollmentModule()) doReturn enrollmentModule
whenever(enrollmentModule.enrollments()) doReturn enrollment
whenever(enrollment.uid("enrollmentUid")) doReturn mock()
whenever(enrollment.uid("enrollmentUid").blockingGet()) doReturn mock()

trackerRelationshipsRepository = TrackerRelationshipsRepository(
d2 = d2,
resources = resources,
teiUid = "teiUid",
enrollmentUid = "enrollmentUid",
profilePictureProvider = profilePictureProvider
)
}

@Test
fun shouldShowFromToNameRelationshipTitle() {
val relationshipName = "FromRelationship name"

val program: ObjectWithUid = mock {
on { uid() } doReturn "programUid_1"
}
val fromConstraint: RelationshipConstraint = mock {
on { program() } doReturn program
}
//Given a TEI with relationship type in From constraint and related to a program
val relationshipType: RelationshipType = mock {
on { fromConstraint() } doReturn fromConstraint
on { fromToName() } doReturn relationshipName
}

//When getting the relationship title
whenever(enrollment.uid("enrollmentUid").blockingGet()?.program()) doReturn "programUid_1"
val title = trackerRelationshipsRepository.getRelationshipTitle(relationshipType)

//Then the title should be the one from the From constraint
assert(title == relationshipName)
}

@Test
fun shouldShowToFromNameRelationshipTitle() {
val relationshipName = "ToRelationship name"

val program: ObjectWithUid = mock {
on { uid() } doReturn "programUid_2"
}
val toConstraint: RelationshipConstraint = mock {
on { program() } doReturn program
}
//Given a TEI with relationship type in To constraint and related to a program
val relationshipType: RelationshipType = mock {
on { toConstraint() } doReturn toConstraint
on { toFromName() } doReturn relationshipName
}

//When getting the relationship title
whenever(enrollment.uid("enrollmentUid").blockingGet()?.program()) doReturn "programUid_2"

val title = trackerRelationshipsRepository.getRelationshipTitle(relationshipType)

//Then the title should be the one from the To constraint
assert(title == relationshipName)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ class GetRelationshipsByTypeTest {
lastUpdated = "5 days ago"
)
),
teiTypeUid = "teiType1"
creationTEITypeUid = "teiType1"
),
RelationshipSection(
title = "Relationship 2",
Expand All @@ -193,7 +193,7 @@ class GetRelationshipsByTypeTest {
lastUpdated = "5 days ago"
)
),
teiTypeUid = "teiType2"
creationTEITypeUid = "teiType2"
)
)
}

0 comments on commit ffe299a

Please sign in to comment.