From ffe299a2cd9fd2babe7327cfd86b05fce7bfb7df Mon Sep 17 00:00:00 2001 From: andresmr Date: Tue, 10 Dec 2024 13:00:04 +0100 Subject: [PATCH] fix: [ANDROAPP-6535] Get TEI relationship title checking before the program and then the TEIType Signed-off-by: andresmr --- .../relationships/RelationshipFragment.kt | 2 +- .../data/RelationshipsRepository.kt | 1 - .../data/TrackerRelationshipsRepository.kt | 22 +++- .../domain/GetRelationshipsByType.kt | 9 +- .../model/RelationshipSection.kt | 4 +- .../relationships/ui/RelationshipsScreen.kt | 4 +- .../TrackerRelationshipsRepositoryTest.kt | 105 ++++++++++++++++++ .../domain/GetRelationshipsByTypeTest.kt | 4 +- 8 files changed, 133 insertions(+), 18 deletions(-) create mode 100644 tracker/src/test/kotlin/org/dhis2/tracker/relationships/data/TrackerRelationshipsRepositoryTest.kt diff --git a/app/src/main/java/org/dhis2/usescases/teiDashboard/dashboardfragments/relationships/RelationshipFragment.kt b/app/src/main/java/org/dhis2/usescases/teiDashboard/dashboardfragments/relationships/RelationshipFragment.kt index a2799ef335..c5a7e9936d 100644 --- a/app/src/main/java/org/dhis2/usescases/teiDashboard/dashboardfragments/relationships/RelationshipFragment.kt +++ b/app/src/main/java/org/dhis2/usescases/teiDashboard/dashboardfragments/relationships/RelationshipFragment.kt @@ -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, diff --git a/tracker/src/main/kotlin/org/dhis2/tracker/relationships/data/RelationshipsRepository.kt b/tracker/src/main/kotlin/org/dhis2/tracker/relationships/data/RelationshipsRepository.kt index b1ba7ed66e..da3288be82 100644 --- a/tracker/src/main/kotlin/org/dhis2/tracker/relationships/data/RelationshipsRepository.kt +++ b/tracker/src/main/kotlin/org/dhis2/tracker/relationships/data/RelationshipsRepository.kt @@ -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 diff --git a/tracker/src/main/kotlin/org/dhis2/tracker/relationships/data/TrackerRelationshipsRepository.kt b/tracker/src/main/kotlin/org/dhis2/tracker/relationships/data/TrackerRelationshipsRepository.kt index 9b5a5d3b1a..6f2efc0df7 100644 --- a/tracker/src/main/kotlin/org/dhis2/tracker/relationships/data/TrackerRelationshipsRepository.kt +++ b/tracker/src/main/kotlin/org/dhis2/tracker/relationships/data/TrackerRelationshipsRepository.kt @@ -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() @@ -44,7 +44,7 @@ class TrackerRelationshipsRepository( else -> null } - Pair(relationshipType, secondaryTeTypeUid) + Pair(relationshipType, creationTEITypeUid) } ) } @@ -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() ?: "" } diff --git a/tracker/src/main/kotlin/org/dhis2/tracker/relationships/domain/GetRelationshipsByType.kt b/tracker/src/main/kotlin/org/dhis2/tracker/relationships/domain/GetRelationshipsByType.kt index b3d281527a..2c38a3780b 100644 --- a/tracker/src/main/kotlin/org/dhis2/tracker/relationships/domain/GetRelationshipsByType.kt +++ b/tracker/src/main/kotlin/org/dhis2/tracker/relationships/domain/GetRelationshipsByType.kt @@ -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 @@ -25,7 +24,7 @@ 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 { @@ -33,10 +32,12 @@ class GetRelationshipsByType( } RelationshipSection( - title = relationshipsRepository.getRelationshipTitle(relationshipType), + title = relationshipsRepository.getRelationshipTitle( + relationshipType = relationshipType, + ), relationshipType = relationshipType, relationships = filteredRelationships.map { mapToRelationshipItem(it) }, - teiTypeUid = teiTypeUid + creationTEITypeUid = creationTEITypeUid ) } } diff --git a/tracker/src/main/kotlin/org/dhis2/tracker/relationships/model/RelationshipSection.kt b/tracker/src/main/kotlin/org/dhis2/tracker/relationships/model/RelationshipSection.kt index 7eb553f4f6..711fd816b8 100644 --- a/tracker/src/main/kotlin/org/dhis2/tracker/relationships/model/RelationshipSection.kt +++ b/tracker/src/main/kotlin/org/dhis2/tracker/relationships/model/RelationshipSection.kt @@ -5,8 +5,8 @@ import org.hisp.dhis.android.core.relationship.RelationshipType data class RelationshipSection( val title: String, val relationships: List, - val teiTypeUid: String?, + val creationTEITypeUid: String?, val relationshipType: RelationshipType, ) { - fun canAddRelationship(): Boolean = teiTypeUid != null + fun canAddRelationship(): Boolean = creationTEITypeUid != null } diff --git a/tracker/src/main/kotlin/org/dhis2/tracker/relationships/ui/RelationshipsScreen.kt b/tracker/src/main/kotlin/org/dhis2/tracker/relationships/ui/RelationshipsScreen.kt index 51dcad9f70..3f308d828d 100644 --- a/tracker/src/main/kotlin/org/dhis2/tracker/relationships/ui/RelationshipsScreen.kt +++ b/tracker/src/main/kotlin/org/dhis2/tracker/relationships/ui/RelationshipsScreen.kt @@ -330,7 +330,7 @@ fun RelationShipScreenPreview() { lastUpdated = "Yesterday", ) ), - teiTypeUid = null, + creationTEITypeUid = null, ), RelationshipSection( title = "Empty relation ship", @@ -339,7 +339,7 @@ fun RelationShipScreenPreview() { .displayName("Empty relation ship") .build(), relationships = emptyList(), - teiTypeUid = "teiTypeUid", + creationTEITypeUid = "teiTypeUid", ) ) ) diff --git a/tracker/src/test/kotlin/org/dhis2/tracker/relationships/data/TrackerRelationshipsRepositoryTest.kt b/tracker/src/test/kotlin/org/dhis2/tracker/relationships/data/TrackerRelationshipsRepositoryTest.kt new file mode 100644 index 0000000000..1c518790cc --- /dev/null +++ b/tracker/src/test/kotlin/org/dhis2/tracker/relationships/data/TrackerRelationshipsRepositoryTest.kt @@ -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) + } +} \ No newline at end of file diff --git a/tracker/src/test/kotlin/org/dhis2/tracker/relationships/domain/GetRelationshipsByTypeTest.kt b/tracker/src/test/kotlin/org/dhis2/tracker/relationships/domain/GetRelationshipsByTypeTest.kt index d3e69639f7..d5b5f1f3d9 100644 --- a/tracker/src/test/kotlin/org/dhis2/tracker/relationships/domain/GetRelationshipsByTypeTest.kt +++ b/tracker/src/test/kotlin/org/dhis2/tracker/relationships/domain/GetRelationshipsByTypeTest.kt @@ -171,7 +171,7 @@ class GetRelationshipsByTypeTest { lastUpdated = "5 days ago" ) ), - teiTypeUid = "teiType1" + creationTEITypeUid = "teiType1" ), RelationshipSection( title = "Relationship 2", @@ -193,7 +193,7 @@ class GetRelationshipsByTypeTest { lastUpdated = "5 days ago" ) ), - teiTypeUid = "teiType2" + creationTEITypeUid = "teiType2" ) ) } \ No newline at end of file