Skip to content

Commit

Permalink
ci: [ANDROAPP-6535] AddRelationship use case
Browse files Browse the repository at this point in the history
Signed-off-by: andresmr <[email protected]>
  • Loading branch information
andresmr committed Dec 12, 2024
1 parent f70cefc commit 88b16fb
Show file tree
Hide file tree
Showing 10 changed files with 223 additions and 155 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,11 @@ class RelationshipFragment : FragmentGlobalAbstract(), RelationshipView {

is RelationshipResult.Success -> {
relationshipSection?.let { relationshipSection ->
presenter.addRelationship(it.teiUidToAddAsRelationship, relationshipSection)
relationShipsViewModel.onAddRelationship(
selectedTeiUid = it.teiUidToAddAsRelationship,
relationshipTypeUid = relationshipSection.relationshipType.uid(),
direction = relationshipSection.direction,
)
}
}
}
Expand Down Expand Up @@ -136,6 +140,8 @@ class RelationshipFragment : FragmentGlobalAbstract(), RelationshipView {
val uiState by relationShipsViewModel.relationshipsUiState.collectAsState()
val relationshipSelectionState by relationShipsViewModel.relationshipSelectionState.collectAsState()
val showDeleteConfirmation by relationShipsViewModel.showDeleteConfirmation.collectAsState()
val snackbarMessage =
relationShipsViewModel.snackbarMessage.collectAsState(initial = null)

when (showMap) {
true -> RelationshipMapScreen(savedInstanceState)
Expand All @@ -157,6 +163,7 @@ class RelationshipFragment : FragmentGlobalAbstract(), RelationshipView {
)
},
onRelationShipSelected = relationShipsViewModel::updateSelectedList,
snackbarMessage = snackbarMessage.value,
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
import org.dhis2.commons.data.ProgramConfigurationRepository;
import org.dhis2.commons.date.DateLabelProvider;
import org.dhis2.commons.date.DateUtils;
import org.dhis2.commons.di.dagger.PerActivity;
import org.dhis2.commons.di.dagger.PerFragment;
import org.dhis2.commons.network.NetworkUtils;
import org.dhis2.commons.resources.D2ErrorUtils;
import org.dhis2.commons.resources.MetadataIconProvider;
import org.dhis2.commons.resources.ResourceManager;
import org.dhis2.commons.viewmodel.DispatcherProvider;
Expand All @@ -20,6 +21,7 @@
import org.dhis2.tracker.relationships.data.EventRelationshipsRepository;
import org.dhis2.tracker.relationships.data.RelationshipsRepository;
import org.dhis2.tracker.relationships.data.TrackerRelationshipsRepository;
import org.dhis2.tracker.relationships.domain.AddRelationship;
import org.dhis2.tracker.relationships.domain.DeleteRelationships;
import org.dhis2.tracker.relationships.domain.GetRelationshipsByType;
import org.dhis2.tracker.relationships.ui.RelationshipsViewModel;
Expand Down Expand Up @@ -150,12 +152,16 @@ TeiAttributesProvider teiAttributesProvider(D2 d2) {
RelationshipsViewModel provideRelationshipsViewModel(
GetRelationshipsByType getRelationshipsByType,
DeleteRelationships deleteRelationships,
DispatcherProvider dispatcherProvider
DispatcherProvider dispatcherProvider,
AddRelationship addRelationship,
D2ErrorUtils d2ErrorUtils
) {
return new RelationshipsViewModel(
dispatcherProvider,
getRelationshipsByType,
deleteRelationships,
dispatcherProvider
addRelationship,
d2ErrorUtils
);
}

Expand Down Expand Up @@ -188,6 +194,15 @@ DeleteRelationships provideDeleteRelationships(
return new DeleteRelationships(relationshipsRepository);
}

@Provides
@PerFragment
AddRelationship provideAddRelationship(
DispatcherProvider dispatcherProvider,
RelationshipsRepository relationshipsRepository
) {
return new AddRelationship(dispatcherProvider, relationshipsRepository);
}

@Provides
@PerFragment
RelationshipsRepository provideRelationshipsRepository(
Expand Down Expand Up @@ -233,4 +248,12 @@ AvatarProvider provideAvatarProvider(
) {
return new AvatarProvider(metadataIconProvider);
}

@Provides
@PerFragment
D2ErrorUtils provideD2ErrorUtils(
NetworkUtils networkUtils
) {
return new D2ErrorUtils(moduleContext, networkUtils);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,16 @@ 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
import org.dhis2.utils.analytics.DELETE_RELATIONSHIP
import org.dhis2.utils.analytics.NEW_RELATIONSHIP
import org.hisp.dhis.android.core.D2
import org.hisp.dhis.android.core.common.State
import org.hisp.dhis.android.core.maintenance.D2Error
import org.hisp.dhis.android.core.relationship.RelationshipHelper
import org.hisp.dhis.android.core.relationship.RelationshipType
import org.hisp.dhis.mobile.ui.designsystem.component.AdditionalInfoItem
import timber.log.Timber

class RelationshipPresenter internal constructor(
private val view: RelationshipView,
Expand Down Expand Up @@ -144,81 +138,9 @@ class RelationshipPresenter internal constructor(
}
}

fun deleteRelationship(relationshipUid: String) {
try {
d2.relationshipModule().relationships().withItems().uid(relationshipUid)
.blockingDelete()
} catch (e: D2Error) {
Timber.d(e)
} finally {
analyticsHelper.setEvent(DELETE_RELATIONSHIP, CLICK, DELETE_RELATIONSHIP)
updateRelationships.onNext(true)
}
}

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

private fun addTeiToTeiRelationship(
teiUid: String,
selectedTei: String,
relationshipSection: RelationshipSection,
) {
try {
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())
} finally {
updateRelationships.onNext(true)
}
}

private fun addEventToTeiRelationship(
eventUid: String,
selectedTei: String,
relationshipTypeUid: String,
) {
try {
val relationship =
RelationshipHelper.eventToTeiRelationship(
eventUid,
selectedTei,
relationshipTypeUid,
)
d2.relationshipModule().relationships().blockingAdd(relationship)
} catch (e: D2Error) {
view.displayMessage(e.errorDescription())
} finally {
updateRelationships.onNext(true)
}
}

fun openDashboard(teiUid: String) {
if (d2.trackedEntityModule()
.trackedEntityInstances().uid(teiUid).blockingGet()!!.state() !=
.trackedEntityInstances().uid(teiUid).blockingGet()?.aggregatedSyncState() !=
State.RELATIONSHIP
) {
if (d2.enrollmentModule().enrollments()
Expand All @@ -239,7 +161,7 @@ class RelationshipPresenter internal constructor(
}
}

fun openEvent(eventUid: String, eventProgramUid: String) {
private fun openEvent(eventUid: String, eventProgramUid: String) {
view.openEventFor(eventUid, eventProgramUid)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import org.dhis2.maps.usecases.MapStyleConfiguration
import org.dhis2.tracker.relationships.data.RelationshipsRepository
import org.dhis2.tracker.ui.AvatarProvider
import org.dhis2.utils.analytics.AnalyticsHelper
import org.dhis2.utils.analytics.CLICK
import org.dhis2.utils.analytics.DELETE_RELATIONSHIP
import org.hisp.dhis.android.core.D2
import org.hisp.dhis.android.core.common.ObjectWithUid
import org.hisp.dhis.android.core.common.State
Expand All @@ -24,7 +22,6 @@ import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.mockito.Mockito
import org.mockito.kotlin.any
import org.mockito.kotlin.doReturn
import org.mockito.kotlin.mock
import org.mockito.kotlin.times
Expand Down Expand Up @@ -106,23 +103,6 @@ class RelationshipPresenterTest {
verify(view, times(1)).showPermissionError()
}

@Test
fun `Should delete relationship`() {
presenter.deleteRelationship(getMockedRelationship().uid()!!)
verify(analyticsHelper).setEvent(DELETE_RELATIONSHIP, CLICK, DELETE_RELATIONSHIP)
}

@Test
fun `Should create a relationship`() {
whenever(
d2.relationshipModule().relationshipTypes().withConstraints().uid("relationshipTypeUid")
.blockingGet(),
) doReturn getMockedRelationshipType(true)
presenter.addRelationship("selectedTei", "relationshipTypeUid")

verify(view, times(0)).displayMessage(any())
}

@Test
fun `Should open dashboard`() {
whenever(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import org.hisp.dhis.android.core.D2
import org.hisp.dhis.android.core.common.State
import org.hisp.dhis.android.core.event.Event
import org.hisp.dhis.android.core.relationship.Relationship
import org.hisp.dhis.android.core.relationship.RelationshipHelper
import org.hisp.dhis.android.core.relationship.RelationshipItem
import org.hisp.dhis.android.core.relationship.RelationshipItemEvent
import org.hisp.dhis.android.core.relationship.RelationshipType
Expand Down Expand Up @@ -57,6 +58,20 @@ class EventRelationshipsRepository(
)
}

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

override fun getRelationships(): Flow<List<RelationshipModel>> {
return flowOf(
d2.relationshipModule().relationships().getByItem(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ 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
import org.hisp.dhis.android.core.relationship.Relationship
import org.hisp.dhis.android.core.relationship.RelationshipConstraint
import org.hisp.dhis.android.core.relationship.RelationshipType
import org.hisp.dhis.android.core.systeminfo.DHISVersion
Expand All @@ -29,7 +31,15 @@ abstract class RelationshipsRepository(
) {
abstract fun getRelationshipTypes(): Flow<List<Pair<RelationshipType, String?>>>
abstract fun getRelationships(): Flow<List<RelationshipModel>>
abstract fun getRelationshipDirectionInfo(relationshipType: RelationshipType): Pair<String, RelationshipDirection>
abstract fun getRelationshipDirectionInfo(
relationshipType: RelationshipType
): Pair<String, RelationshipDirection>

abstract fun createRelationship(
selectedTeiUid: String,
relationshipTypeUid: String,
direction: RelationshipDirection,
): Relationship

protected fun orgUnitInScope(orgUnitUid: String?): Boolean {
return orgUnitUid?.let {
Expand Down Expand Up @@ -235,4 +245,13 @@ abstract class RelationshipsRepository(
.uid(relationshipUid)
.blockingDelete()
}

fun addRelationship(relationship: Relationship): Result<String> {
return try {
val relationshipUid = d2.relationshipModule().relationships().blockingAdd(relationship)
Result.success(relationshipUid)
} catch (error: D2Error) {
Result.failure(error)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import org.dhis2.tracker.relationships.model.RelationshipOwnerType
import org.hisp.dhis.android.core.D2
import org.hisp.dhis.android.core.common.Geometry
import org.hisp.dhis.android.core.common.State
import org.hisp.dhis.android.core.relationship.Relationship
import org.hisp.dhis.android.core.relationship.RelationshipHelper
import org.hisp.dhis.android.core.relationship.RelationshipItem
import org.hisp.dhis.android.core.relationship.RelationshipItemTrackedEntityInstance
import org.hisp.dhis.android.core.relationship.RelationshipType
Expand Down Expand Up @@ -227,6 +229,20 @@ class TrackerRelationshipsRepository(
)
}

override fun createRelationship(
selectedTeiUid: String,
relationshipTypeUid: String,
direction: RelationshipDirection,
): Relationship {
val (fromUid, toUid) = when (direction) {
RelationshipDirection.FROM -> Pair(selectedTeiUid, teiUid)
RelationshipDirection.TO -> Pair(teiUid, selectedTeiUid)
}
return RelationshipHelper.teiToTeiRelationship(
fromUid, toUid, relationshipTypeUid
)
}

override fun getRelationshipDirectionInfo(relationshipType: RelationshipType): Pair<String, RelationshipDirection> {
val teiTypeUid = d2.trackedEntityModule().trackedEntityInstances()
.uid(teiUid)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.dhis2.tracker.relationships.domain

import kotlinx.coroutines.withContext
import org.dhis2.commons.viewmodel.DispatcherProvider
import org.dhis2.tracker.relationships.data.RelationshipsRepository
import org.dhis2.tracker.relationships.model.RelationshipDirection

class AddRelationship(
private val dispatcher: DispatcherProvider,
private val repository: RelationshipsRepository,
) {
suspend operator fun invoke(
selectedTeiUid: String,
relationshipTypeUid: String,
direction: RelationshipDirection,
): Result<String> = withContext(dispatcher.io()) {
val relationship = repository.createRelationship(
selectedTeiUid = selectedTeiUid,
relationshipTypeUid = relationshipTypeUid,
direction = direction,
)
repository.addRelationship(relationship)
}
}
Loading

0 comments on commit 88b16fb

Please sign in to comment.