Skip to content

Commit

Permalink
fix: [ANDROAPP-6535] get direction from relationship type
Browse files Browse the repository at this point in the history
Signed-off-by: andresmr <[email protected]>
  • Loading branch information
andresmr committed Dec 11, 2024
1 parent ffe299a commit f70cefc
Show file tree
Hide file tree
Showing 10 changed files with 120 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import org.dhis2.maps.managers.RelationshipMapManager
import org.dhis2.maps.views.LocationIcon
import org.dhis2.maps.views.MapScreen
import org.dhis2.maps.views.OnMapClickListener
import org.dhis2.tracker.relationships.model.RelationshipSection
import org.dhis2.tracker.relationships.model.RelationshipTopBarIconState
import org.dhis2.tracker.relationships.ui.DeleteRelationshipsConfirmation
import org.dhis2.tracker.relationships.ui.RelationShipsScreen
Expand All @@ -53,7 +54,6 @@ import org.dhis2.usescases.eventsWithoutRegistration.eventCapture.EventCaptureAc
import org.dhis2.usescases.general.FragmentGlobalAbstract
import org.dhis2.usescases.teiDashboard.TeiDashboardMobileActivity
import org.dhis2.utils.OnDialogClickListener
import org.hisp.dhis.android.core.relationship.RelationshipType
import org.hisp.dhis.mobile.ui.designsystem.component.AdditionalInfoItem
import org.hisp.dhis.mobile.ui.designsystem.component.IconButton
import org.hisp.dhis.mobile.ui.designsystem.component.IconButtonStyle
Expand Down Expand Up @@ -81,7 +81,7 @@ class RelationshipFragment : FragmentGlobalAbstract(), RelationshipView {
@Inject
lateinit var relationShipsViewModel: RelationshipsViewModel

private var relationshipType: RelationshipType? = null
private var relationshipSection: RelationshipSection? = null
private var relationshipMapManager: RelationshipMapManager? = null
private lateinit var mapButtonObservable: MapButtonObservable

Expand All @@ -92,7 +92,9 @@ class RelationshipFragment : FragmentGlobalAbstract(), RelationshipView {
}

is RelationshipResult.Success -> {
presenter.addRelationship(it.teiUidToAddAsRelationship, relationshipType!!.uid())
relationshipSection?.let { relationshipSection ->
presenter.addRelationship(it.teiUidToAddAsRelationship, relationshipSection)
}
}
}
}
Expand Down Expand Up @@ -143,7 +145,7 @@ class RelationshipFragment : FragmentGlobalAbstract(), RelationshipView {
onCreateRelationshipClick = {
it.creationTEITypeUid?.let { teiTypeUid ->
goToRelationShip(
relationshipTypeModel = it.relationshipType,
relationshipSection = it,
teiTypeUid = teiTypeUid,
)
}
Expand Down Expand Up @@ -402,9 +404,9 @@ class RelationshipFragment : FragmentGlobalAbstract(), RelationshipView {
)
}

private fun goToRelationShip(relationshipTypeModel: RelationshipType, teiTypeUid: String) {
relationshipType = relationshipTypeModel
presenter.goToAddRelationship(teiTypeUid, relationshipType!!)
private fun goToRelationShip(relationshipSection: RelationshipSection, teiTypeUid: String) {
this.relationshipSection = relationshipSection
presenter.goToAddRelationship(teiTypeUid, relationshipSection.relationshipType)
}

override fun showPermissionError() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ import org.dhis2.maps.layer.basemaps.BaseMapStyle
import org.dhis2.maps.model.MapItemModel
import org.dhis2.maps.usecases.MapStyleConfiguration
import org.dhis2.tracker.relationships.data.RelationshipsRepository
import org.dhis2.tracker.relationships.model.RelationshipDirection
import org.dhis2.tracker.relationships.model.RelationshipModel
import org.dhis2.tracker.relationships.model.RelationshipOwnerType
import org.dhis2.tracker.relationships.model.RelationshipSection
import org.dhis2.tracker.ui.AvatarProvider
import org.dhis2.utils.analytics.AnalyticsHelper
import org.dhis2.utils.analytics.CLICK
Expand Down Expand Up @@ -154,22 +156,38 @@ class RelationshipPresenter internal constructor(
}
}

fun addRelationship(selectedTei: String, relationshipTypeUid: String) {
fun addRelationship(selectedTei: String, relationshipSection: RelationshipSection) {
if (teiUid != null) {
addTeiToTeiRelationship(teiUid, selectedTei, relationshipTypeUid)
addTeiToTeiRelationship(teiUid, selectedTei, relationshipSection)
} else if (eventUid != null) {
addEventToTeiRelationship(eventUid, selectedTei, relationshipTypeUid)
addEventToTeiRelationship(
eventUid,
selectedTei,
relationshipSection.relationshipType.uid(),
)
}
}

private fun addTeiToTeiRelationship(
teiUid: String,
selectedTei: String,
relationshipTypeUid: String,
relationshipSection: RelationshipSection,
) {
try {
val relationship =
RelationshipHelper.teiToTeiRelationship(teiUid, selectedTei, relationshipTypeUid)
val relationshipTypeUid = relationshipSection.relationshipType.uid()
val relationship = when (relationshipSection.direction) {
RelationshipDirection.FROM -> RelationshipHelper.teiToTeiRelationship(
selectedTei,
teiUid,
relationshipTypeUid,
)

RelationshipDirection.TO -> RelationshipHelper.teiToTeiRelationship(
teiUid,
selectedTei,
relationshipTypeUid,
)
}
d2.relationshipModule().relationships().blockingAdd(relationship)
} catch (e: D2Error) {
view.displayMessage(e.errorDescription())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,21 +154,24 @@ class EventRelationshipsRepository(
)
}

override fun getRelationshipTitle(relationshipType: RelationshipType): String {
override fun getRelationshipDirectionInfo(relationshipType: RelationshipType): Pair<String, RelationshipDirection> {
val event = d2.eventModule().events().uid(eventUid).blockingGet()
val programStageUid = event?.programStage() ?: ""
return when (programStageUid) {
relationshipType.fromConstraint()?.programStage()?.uid() -> {
relationshipType.fromToName() ?: relationshipType.displayName() ?: ""
}
relationshipType.fromConstraint()?.programStage()?.uid() -> Pair(
relationshipType.fromToName() ?: relationshipType.displayName() ?: "",
RelationshipDirection.TO,
)

relationshipType.toConstraint()?.program()?.uid() -> {
relationshipType.toFromName() ?: relationshipType.displayName() ?: ""
}
relationshipType.toConstraint()?.program()?.uid() -> Pair(
relationshipType.toFromName() ?: relationshipType.displayName() ?: "",
RelationshipDirection.FROM,
)

else -> {
relationshipType.displayName() ?: ""
}
else -> Pair(
relationshipType.displayName() ?: "",
RelationshipDirection.FROM,
)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import org.dhis2.bindings.userFriendlyValue
import org.dhis2.commons.date.toUi
import org.dhis2.commons.resources.ResourceManager
import org.dhis2.tracker.R
import org.dhis2.tracker.relationships.model.RelationshipDirection
import org.dhis2.tracker.relationships.model.RelationshipModel
import org.dhis2.tracker.relationships.model.RelationshipOwnerType
import org.hisp.dhis.android.core.D2
Expand All @@ -28,7 +29,7 @@ abstract class RelationshipsRepository(
) {
abstract fun getRelationshipTypes(): Flow<List<Pair<RelationshipType, String?>>>
abstract fun getRelationships(): Flow<List<RelationshipModel>>
abstract fun getRelationshipTitle(relationshipType: RelationshipType): String
abstract fun getRelationshipDirectionInfo(relationshipType: RelationshipType): Pair<String, RelationshipDirection>

protected fun orgUnitInScope(orgUnitUid: String?): Boolean {
return orgUnitUid?.let {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,32 +227,45 @@ class TrackerRelationshipsRepository(
)
}

override fun getRelationshipTitle(relationshipType: RelationshipType): String {
override fun getRelationshipDirectionInfo(relationshipType: RelationshipType): Pair<String, RelationshipDirection> {
val teiTypeUid = d2.trackedEntityModule().trackedEntityInstances()
.uid(teiUid)
.blockingGet()?.trackedEntityType()
val teiProgramUid = d2.enrollmentModule().enrollments()
.uid(enrollmentUid).blockingGet()?.program()

val fromConstraint = relationshipType.fromConstraint()
val toConstraint = relationshipType.toConstraint()

val fromProgramUid = fromConstraint?.program()?.uid()
val toProgramUid = toConstraint?.program()?.uid()
val fromTeiTypeUid = fromConstraint?.trackedEntityType()?.uid()
val toTeiTypeUid = toConstraint?.trackedEntityType()?.uid()

return when {
teiProgramUid == relationshipType.fromConstraint()?.program()?.uid() -> {
relationshipType.fromToName() ?: relationshipType.displayName() ?: ""
}
teiProgramUid == fromProgramUid -> Pair(
relationshipType.fromToName() ?: relationshipType.displayName() ?: "",
RelationshipDirection.TO
)

teiProgramUid == relationshipType.toConstraint()?.program()?.uid() -> {
relationshipType.toFromName() ?: relationshipType.displayName() ?: ""
}
teiProgramUid == toProgramUid -> Pair(
relationshipType.toFromName() ?: relationshipType.displayName() ?: "",
RelationshipDirection.FROM
)

teiTypeUid == relationshipType.fromConstraint()?.trackedEntityType()?.uid() -> {
relationshipType.fromToName() ?: relationshipType.displayName() ?: ""
}
teiTypeUid == fromTeiTypeUid -> Pair(
relationshipType.fromToName() ?: relationshipType.displayName() ?: "",
RelationshipDirection.TO
)

teiTypeUid == relationshipType.toConstraint()?.trackedEntityType()?.uid() -> {
relationshipType.toFromName() ?: relationshipType.displayName() ?: ""
}
teiTypeUid == toTeiTypeUid -> Pair(
relationshipType.toFromName() ?: relationshipType.displayName() ?: "",
RelationshipDirection.FROM
)

else -> {
relationshipType.displayName() ?: ""
}
else -> Pair(
relationshipType.displayName() ?: "", RelationshipDirection.FROM
)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,16 @@ class GetRelationshipsByType(
it.relationshipType.uid() == relationshipType.uid()
}

val (title, direction) = relationshipsRepository.getRelationshipDirectionInfo(
relationshipType = relationshipType,
)

RelationshipSection(
title = relationshipsRepository.getRelationshipTitle(
relationshipType = relationshipType,
),
title = title,
relationshipType = relationshipType,
relationships = filteredRelationships.map { mapToRelationshipItem(it) },
creationTEITypeUid = creationTEITypeUid
creationTEITypeUid = creationTEITypeUid,
direction = direction,
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ data class RelationshipSection(
val relationships: List<RelationshipItem>,
val creationTEITypeUid: String?,
val relationshipType: RelationshipType,
val direction: RelationshipDirection,
) {
fun canAddRelationship(): Boolean = creationTEITypeUid != null
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import org.dhis2.tracker.R
import org.dhis2.tracker.relationships.model.ListSelectionState
import org.dhis2.tracker.relationships.model.RelationshipDirection
import org.dhis2.tracker.relationships.model.RelationshipItem
import org.dhis2.tracker.relationships.model.RelationshipOwnerType
import org.dhis2.tracker.relationships.model.RelationshipSection
Expand Down Expand Up @@ -288,10 +289,6 @@ fun RelationShipScreenPreview() {
data = listOf(
RelationshipSection(
title = "Relationship type",
relationshipType = RelationshipType.builder()
.uid("")
.displayName("Relationship type")
.build(),
relationships = listOf(
RelationshipItem(
uid = "uidA",
Expand Down Expand Up @@ -331,15 +328,21 @@ fun RelationShipScreenPreview() {
)
),
creationTEITypeUid = null,
relationshipType = RelationshipType.builder()
.uid("")
.displayName("Relationship type")
.build(),
direction = RelationshipDirection.TO,
),
RelationshipSection(
title = "Empty relation ship",
relationships = emptyList(),
creationTEITypeUid = "teiTypeUid",
relationshipType = RelationshipType.builder()
.uid("")
.displayName("Empty relation ship")
.build(),
relationships = emptyList(),
creationTEITypeUid = "teiTypeUid",
direction = RelationshipDirection.TO,
)
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@ 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.enrollment.Enrollment
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
Expand All @@ -24,26 +21,17 @@ class TrackerRelationshipsRepositoryTest {
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()
private val d2: D2 = mock(defaultAnswer = Mockito.RETURNS_DEEP_STUBS)
private val enrollment: Enrollment = 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()
d2.enrollmentModule()
.enrollments()
.uid("enrollmentUid")
.blockingGet()
) doReturn enrollment

trackerRelationshipsRepository = TrackerRelationshipsRepository(
d2 = d2,
Expand Down Expand Up @@ -71,11 +59,11 @@ class TrackerRelationshipsRepositoryTest {
}

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

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

@Test
Expand All @@ -95,11 +83,11 @@ class TrackerRelationshipsRepositoryTest {
}

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

val title = trackerRelationshipsRepository.getRelationshipTitle(relationshipType)
val title = trackerRelationshipsRepository.getRelationshipDirectionInfo(relationshipType)

//Then the title should be the one from the To constraint
assert(title == relationshipName)
assert(title.first == relationshipName)
}
}
Loading

0 comments on commit f70cefc

Please sign in to comment.