Skip to content

Commit

Permalink
feat: [ANDROAPP-5244] add TEI owned by org unit (#3794)
Browse files Browse the repository at this point in the history
  • Loading branch information
mmmateos authored Sep 13, 2024
1 parent 85be740 commit b655ebd
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import org.dhis2.usescases.flow.teiFlow.teiFlowRobot
import org.dhis2.usescases.searchTrackEntity.SearchTEActivity
import org.dhis2.usescases.searchte.robot.filterRobot
import org.hisp.dhis.android.core.mockwebserver.ResponseController
import org.junit.Ignore
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
Expand All @@ -38,6 +39,7 @@ class SearchFlowTest : BaseTest() {
}

@Test
@Ignore("Flaky test, will be looked up in ANDROAPP-6478")
fun shouldCreateTEIAndFilterByEnrollment() {
mockWebServerRobot.addResponse(
ResponseController.GET,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ sealed class DashboardModel(
open val orgUnits: List<OrganisationUnit>,
open val teiHeader: String?,
open val avatarPath: String?,
open val ownerOrgUnit: OrganisationUnit?,
) {
open fun getTrackedEntityAttributeValueBySortOrder(sortOrder: Int): String? {
return if (sortOrder <= trackedEntityAttributeValues.size) {
Expand All @@ -37,13 +38,15 @@ data class DashboardEnrollmentModel(
override val orgUnits: List<OrganisationUnit>,
override val teiHeader: String?,
override val avatarPath: String?,
override val ownerOrgUnit: OrganisationUnit?,
) : DashboardModel(
trackedEntityInstance,
trackedEntityAttributeValues,
enrollmentPrograms,
orgUnits,
teiHeader,
avatarPath,
ownerOrgUnit,
) {
fun currentProgram(): Program {
return enrollmentPrograms.first { it.first.uid() == currentEnrollment.program() }.first
Expand All @@ -67,13 +70,15 @@ data class DashboardTEIModel(
override val orgUnits: List<OrganisationUnit>,
override val teiHeader: String?,
override val avatarPath: String?,
override val ownerOrgUnit: OrganisationUnit?,
) : DashboardModel(
trackedEntityInstance,
trackedEntityAttributeValues,
enrollmentPrograms,
orgUnits,
teiHeader,
avatarPath,
ownerOrgUnit,
) {
fun getProgramsWithActiveEnrollment(): List<Program>? {
return enrollmentPrograms.sortedBy { it.first.displayName()?.lowercase() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,17 @@ class DashboardRepositoryImpl(
}
}

private fun getOwnerOrgUnit(teiUid: String): OrganisationUnit? {
val orgUnitId = d2.trackedEntityModule()
.trackedEntityInstances().withProgramOwners()
.uid(teiUid).blockingGet()
?.programOwners()?.first { it.trackedEntityInstance() == teiUid }?.ownerOrgUnit()

return d2.organisationUnitModule().organisationUnits()
.uid(orgUnitId)
.blockingGet()
}

override fun getDashboardModel(): DashboardModel {
return if (programUid.isNullOrEmpty()) {
DashboardTEIModel(
Expand All @@ -384,6 +395,7 @@ class DashboardRepositoryImpl(
getTeiOrgUnits(teiUid, null).blockingFirst(),
getTeiHeader(),
getTeiProfilePath(),
getOwnerOrgUnit(teiUid),
)
} else {
DashboardEnrollmentModel(
Expand All @@ -396,6 +408,7 @@ class DashboardRepositoryImpl(
getTeiOrgUnits(teiUid, programUid).blockingFirst(),
getTeiHeader(),
getTeiProfilePath(),
getOwnerOrgUnit(teiUid),
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,11 @@ class TeiDashboardCardMapper(
item.currentEnrollment.enrollmentDate(),
)
}.also { list ->
if (item.orgUnits.isNotEmpty()) {
addOwnedBy(
list,
item.ownerOrgUnit,
)
if (item.getCurrentOrgUnit() != item.ownerOrgUnit) {
addEnrollIn(
list,
item.getCurrentOrgUnit(),
Expand Down Expand Up @@ -210,6 +214,19 @@ class TeiDashboardCardMapper(
)
}

private fun addOwnedBy(
list: MutableList<AdditionalInfoItem>,
ownedByOrgUnit: OrganisationUnit?,
) {
list.add(
AdditionalInfoItem(
key = resourceManager.getString(R.string.ownedBy),
value = ownedByOrgUnit?.displayName() ?: "-",
isConstantItem = true,
),
)
}

private fun addIncidentDate(
list: MutableList<AdditionalInfoItem>,
incidentDateLabel: String?,
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -894,6 +894,7 @@
<string name="program_description">Program description</string>
<string name="wrong_pin">Wrong pin</string>
<string name="enrolledIn">Enrolled in</string>
<string name="ownedBy">Owned by</string>
<string name="sync_dialog_title_not_synced">Sync needed</string>
<string name="sync_dialog_title_error">Sync error</string>
<string name="sync_dialog_title_synced">Synced</string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ class InfoBarMapperTest {
emptyList<OrganisationUnit>(),
null,
null,
null,
)

return model
Expand Down Expand Up @@ -149,7 +150,7 @@ class InfoBarMapperTest {
.program("Program1Uid")
.build()

private fun setPrograms() = listOf<kotlin.Pair<Program, MetadataIconData>>(
private fun setPrograms() = listOf<Pair<Program, MetadataIconData>>(
Pair(
Program.builder()
.uid("Program1Uid")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,26 @@ class TEIDetailMapperTest {
assertEquals(result.title, model.teiHeader)
assertEquals(result.additionalInfo[0].value, model.trackedEntityAttributes[0].second.value())
assertEquals(result.additionalInfo[1].value, model.trackedEntityAttributes[1].second.value())
assertEquals(result.additionalInfo[3].value, orgUnit().displayName())
}

private fun createFakeModel(): DashboardEnrollmentModel {
@Test
fun shouldShowOwnedAndEnrolledOrgUnit() {
val model = createFakeModel(otherOrgUnit())

val result = mapper.map(
dashboardModel = model,
phoneCallback = {},
emailCallback = {},
programsCallback = {},
onImageClick = {},
)

assertEquals(result.additionalInfo[3].value, otherOrgUnit().displayName())
assertEquals(result.additionalInfo[4].value, model.orgUnits.first().displayName())
}

private fun createFakeModel(ownerOrgUnit: OrganisationUnit = orgUnit()): DashboardEnrollmentModel {
val attributeValues = listOf(
Pair(getTEA("uid1", "First Name"), getTEAValue("Jonah")),
Pair(getTEA("uid2", "Last Name"), getTEAValue("Hill")),
Expand All @@ -64,9 +81,10 @@ class TEIDetailMapperTest {
attributeValues,
emptyList<TrackedEntityAttributeValue>(),
setPrograms(),
emptyList<OrganisationUnit>(),
listOf(orgUnit()),
"header",
"avatarFilepath",
ownerOrgUnit,
)

return model
Expand All @@ -93,6 +111,7 @@ class TEIDetailMapperTest {
.uid("EnrollmentUid")
.status(EnrollmentStatus.COMPLETED)
.program("Program1Uid")
.organisationUnit("orgUnitUid")
.build()

private fun setPrograms() = listOf<Pair<Program, MetadataIconData>>(
Expand All @@ -111,4 +130,14 @@ class TEIDetailMapperTest {
MetadataIconData.defaultIcon(),
),
)

private fun orgUnit() = OrganisationUnit.builder()
.uid("orgUnitUid")
.displayName("orgUnitName")
.build()

private fun otherOrgUnit() = OrganisationUnit.builder()
.uid("otherOrgUnitUid")
.displayName("otherOrgUnitName")
.build()
}

0 comments on commit b655ebd

Please sign in to comment.