Skip to content

Commit

Permalink
fix: [ANDROAPP-6535] refactor use case
Browse files Browse the repository at this point in the history
Signed-off-by: andresmr <[email protected]>
  • Loading branch information
andresmr authored and Balcan committed Dec 23, 2024
1 parent 921a11c commit 44fc4b6
Show file tree
Hide file tree
Showing 13 changed files with 357 additions and 360 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,6 @@ private void setRelationshipsInfo(@NonNull SearchTeiModel searchTeiModel, Progra
relationship,
fromTei.geometry(),
toTei.geometry(),
relationshipType,
direction,
relationshipTEIUid,
RelationshipOwnerType.TEI,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,6 @@ class RelationshipPresenterTest {
private val relationshipMapsRepository: RelationshipMapsRepository = mock()
private val analyticsHelper: AnalyticsHelper = mock()
private val mapRelationshipsToFeatureCollection: MapRelationshipsToFeatureCollection = mock()
private val relationshipConstrain: RelationshipConstraint = mock()
private val relationshipType: RelationshipType = mock {
on { fromConstraint() } doReturn relationshipConstrain
}
private val mapStyleConfiguration: MapStyleConfiguration = mock()
private val relationshipsRepository: RelationshipsRepository = mock()
private val avatarProvider: AvatarProvider = mock()
Expand Down Expand Up @@ -82,23 +78,27 @@ class RelationshipPresenterTest {

@Test
fun `If user has permission should create a new relationship`() {
val relationshipTypeUid = "relationshipTypeUid"
val teiTypeToAdd = "teiTypeToAdd"

whenever(
d2.relationshipModule().relationshipService().hasAccessPermission(relationshipType),
relationshipsRepository.hasWritePermission(relationshipTypeUid),
) doReturn true

presenter.goToAddRelationship("teiType", relationshipType)
presenter.goToAddRelationship(relationshipTypeUid, teiTypeToAdd)

verify(view, times(1)).goToAddRelationship("teiUid", "teiType")
verify(view, times(1)).goToAddRelationship("teiUid", teiTypeToAdd)
verify(view, times(0)).showPermissionError()
}

@Test
fun `If user don't have permission should show an error`() {
val relationshipTypeUid = "relationshipTypeUid"
whenever(
d2.relationshipModule().relationshipService().hasAccessPermission(relationshipType),
relationshipsRepository.hasWritePermission(relationshipTypeUid),
) doReturn false

presenter.goToAddRelationship("teiType", relationshipType)
presenter.goToAddRelationship(relationshipTypeUid, "teiTypeToAdd")

verify(view, times(1)).showPermissionError()
}
Expand Down Expand Up @@ -193,7 +193,7 @@ class RelationshipPresenterTest {
private fun getMockedTei(state: State): TrackedEntityInstance {
return TrackedEntityInstance.builder()
.uid("teiUid")
.state(state)
.aggregatedSyncState(state)
.build()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import org.dhis2.tracker.relationships.model.RelationshipConstraintSide
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.hisp.dhis.android.core.D2
import org.hisp.dhis.android.core.common.State
import org.hisp.dhis.android.core.event.Event
Expand All @@ -26,75 +27,66 @@ class EventRelationshipsRepository(
private val profilePictureProvider: ProfilePictureProvider,
) : RelationshipsRepository(d2, resources) {

override suspend fun getRelationshipTypes(): List<org.dhis2.tracker.relationships.model.RelationshipType> {
override suspend fun getRelationshipTypes(): List<RelationshipSection> {
val event = d2.eventModule().events().uid(eventUid).blockingGet()
val programStageUid = event?.programStage() ?: ""

val relationshipWithEntitySide = d2.relationshipModule().relationshipService()
return d2.relationshipModule().relationshipService()
.getRelationshipTypesForEvents(
programStageUid = programStageUid,
)
).map { relationshipWithEntitySide ->
RelationshipSection(
uid = relationshipWithEntitySide.relationshipType.uid(),
title = getRelationshipTitle(
relationshipWithEntitySide.relationshipType,
relationshipWithEntitySide.entitySide,
),
relationships = emptyList(),
side = when (relationshipWithEntitySide.entitySide) {
RelationshipConstraintType.TO -> RelationshipConstraintSide.TO
RelationshipConstraintType.FROM -> RelationshipConstraintSide.FROM
},
entityToAdd = when (relationshipWithEntitySide.entitySide) {
RelationshipConstraintType.FROM ->
relationshipWithEntitySide.relationshipType.toConstraint()
?.trackedEntityType()?.uid()

val relationships = d2.relationshipModule().relationships().getByItem(
RelationshipItem.builder().event(
RelationshipItemEvent.builder().event(eventUid).build(),
).build(),
)

return relationshipWithEntitySide.map {
val filteredRelationships = relationships.filter { relationship ->
val sameSide = when (it.entitySide) {
RelationshipConstraintType.FROM -> {
relationship.from()?.event()?.event() == eventUid
RelationshipConstraintType.TO ->
relationshipWithEntitySide.relationshipType.fromConstraint()
?.trackedEntityType()?.uid()
}
)
}
}

RelationshipConstraintType.TO -> {
relationship.to()?.event()?.event() == eventUid
}
}
relationship.relationshipType() == it.relationshipType.uid() && sameSide
override suspend fun getRelationshipsGroupedByTypeAndSide(relationshipSection: RelationshipSection): RelationshipSection {
val constraintType = when (relationshipSection.side) {
RelationshipConstraintSide.FROM -> RelationshipConstraintType.FROM
RelationshipConstraintSide.TO -> RelationshipConstraintType.TO
}
val relationshipType = d2.relationshipModule().relationshipTypes()
.withConstraints()
.uid(relationshipSection.uid)
.blockingGet()

val relationships = d2.relationshipModule().relationships()
.getByItem(
RelationshipItem.builder()
.event(
RelationshipItemEvent.builder().event(eventUid).build(),
).relationshipItemType(constraintType)
.build(),
).filter {
it.relationshipType() == relationshipSection.uid
}.mapNotNull { relationship ->
mapToRelationshipModel(
relationship = relationship,
relationshipType = it.relationshipType,
relationshipType = relationshipType,
eventUid = eventUid,
)
}
org.dhis2.tracker.relationships.model.RelationshipType(
uid = it.relationshipType.uid(),
title = getRelationshipTitle(
it.relationshipType,
it.entitySide,
),
relationships = filteredRelationships,
side = when (it.entitySide) {
RelationshipConstraintType.TO -> RelationshipConstraintSide.TO
RelationshipConstraintType.FROM -> RelationshipConstraintSide.FROM
},
entityToAdd = when (it.entitySide) {
RelationshipConstraintType.FROM ->
it.relationshipType.toConstraint()?.trackedEntityType()?.uid()
RelationshipConstraintType.TO ->
it.relationshipType.fromConstraint()?.trackedEntityType()?.uid()
}
)

}


}

override fun createRelationship(
selectedTeiUid: String,
relationshipTypeUid: String,
relationshipSide: RelationshipConstraintSide,
): Relationship {
val (fromUid, toUid) = when (relationshipSide) {
RelationshipConstraintSide.FROM -> Pair(eventUid, selectedTeiUid)
RelationshipConstraintSide.TO -> Pair(selectedTeiUid, eventUid)
}
return RelationshipHelper.eventToTeiRelationship(
fromUid, toUid, relationshipTypeUid
return relationshipSection.copy(
relationships = relationships
)
}

Expand All @@ -118,9 +110,23 @@ class EventRelationshipsRepository(
)
}

override fun createRelationship(
selectedTeiUid: String,
relationshipTypeUid: String,
relationshipSide: RelationshipConstraintSide,
): Relationship {
val (fromUid, toUid) = when (relationshipSide) {
RelationshipConstraintSide.FROM -> Pair(eventUid, selectedTeiUid)
RelationshipConstraintSide.TO -> Pair(selectedTeiUid, eventUid)
}
return RelationshipHelper.eventToTeiRelationship(
fromUid, toUid, relationshipTypeUid
)
}

private fun mapToRelationshipModel(
relationship: Relationship,
relationshipType: RelationshipType,
relationshipType: RelationshipType?,
eventUid: String,
): RelationshipModel? {
val relationshipOwnerUid: String?
Expand Down Expand Up @@ -187,7 +193,6 @@ class EventRelationshipsRepository(
relationship,
fromGeometry,
toGeometry,
relationshipType,
direction,
relationshipOwnerUid,
RelationshipOwnerType.TEI,
Expand Down Expand Up @@ -299,31 +304,31 @@ class EventRelationshipsRepository(
private fun getValues(
direction: RelationshipDirection,
relationshipOwnerUid: String?,
relationshipType: RelationshipType,
relationshipType: RelationshipType?,
relationship: Relationship
) = if (direction == RelationshipDirection.FROM) {
Pair(
getTeiAttributesForRelationship(
relationshipOwnerUid,
relationshipType.fromConstraint(),
relationshipType?.fromConstraint(),
relationship.created(),
),
getEventValuesForRelationship(
eventUid,
relationshipType.toConstraint(),
relationshipType?.toConstraint(),
relationship.created(),
),
)
} else {
Pair(
getEventValuesForRelationship(
eventUid,
relationshipType.fromConstraint(),
relationshipType?.fromConstraint(),
relationship.created(),
),
getTeiAttributesForRelationship(
relationshipOwnerUid,
relationshipType.toConstraint(),
relationshipType?.toConstraint(),
relationship.created(),
),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import org.dhis2.tracker.R
import org.dhis2.tracker.relationships.model.RelationshipConstraintSide
import org.dhis2.tracker.relationships.model.RelationshipModel
import org.dhis2.tracker.relationships.model.RelationshipOwnerType
import org.dhis2.tracker.relationships.model.RelationshipType
import org.dhis2.tracker.relationships.model.RelationshipSection
import org.hisp.dhis.android.core.D2
import org.hisp.dhis.android.core.common.ObjectStyle
import org.hisp.dhis.android.core.event.Event
Expand All @@ -19,6 +19,7 @@ import org.hisp.dhis.android.core.program.ProgramType
import org.hisp.dhis.android.core.relationship.Relationship
import org.hisp.dhis.android.core.relationship.RelationshipConstraint
import org.hisp.dhis.android.core.relationship.RelationshipConstraintType
import org.hisp.dhis.android.core.relationship.RelationshipType
import org.hisp.dhis.android.core.systeminfo.DHISVersion
import org.hisp.dhis.android.core.trackedentity.TrackedEntityInstance
import java.util.Date
Expand All @@ -30,7 +31,10 @@ abstract class RelationshipsRepository(
private val d2: D2,
private val resources: ResourceManager,
) {
abstract suspend fun getRelationshipTypes(): List<RelationshipType>
abstract suspend fun getRelationshipTypes(): List<RelationshipSection>

abstract suspend fun getRelationshipsGroupedByTypeAndSide(relationshipSection: RelationshipSection): RelationshipSection

abstract fun getRelationships(): Flow<List<RelationshipModel>>

abstract fun createRelationship(
Expand Down Expand Up @@ -259,7 +263,7 @@ abstract class RelationshipsRepository(
.blockingGet()

protected fun getRelationshipTitle(
relationshipType: org.hisp.dhis.android.core.relationship.RelationshipType,
relationshipType: RelationshipType,
entitySide: RelationshipConstraintType
): String {
return when (entitySide) {
Expand Down
Loading

0 comments on commit 44fc4b6

Please sign in to comment.