Skip to content

Commit

Permalink
fix: [ANDROAPP-5803] new scheduled event date (#3448)
Browse files Browse the repository at this point in the history
  • Loading branch information
mmmateos authored Dec 26, 2023
1 parent 366249c commit df06bff
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ class TeiDashboardTest : BaseTest() {
clickOnScheduleNew()
clickOnFirstReferralEvent()
clickOnReferralNextButton()
checkEventWasCreated(LAB_MONITORING)
checkEventWasCreatedWithDate(LAB_MONITORING, LAB_MONITORING_SCHEDULE_DATE)
}
}

Expand Down Expand Up @@ -375,6 +375,7 @@ class TeiDashboardTest : BaseTest() {
const val USER = "android"

const val LAB_MONITORING = "Lab monitoring"
const val LAB_MONITORING_SCHEDULE_DATE = "10/9/2019"

const val API_TEI_1_RESPONSE_OK = "mocks/teilist/teilist_1.json"
const val API_TEI_2_RESPONSE_OK = "mocks/teilist/teilist_2.json"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,37 @@ class TeiDashboardRobot : BaseRobot() {
)
}

fun checkEventWasCreatedWithDate(eventName: String, eventDate: String) {
onView(withId(R.id.tei_recycler))
.check(
matches(
allOf(
isDisplayed(),
isNotEmpty(),
atPosition(
1,
hasDescendant(
allOf(
hasSibling(
allOf(
withId(R.id.programStageName),
withText(eventName),
),
),
hasSibling(
allOf(
withId(R.id.event_date),
withText(eventDate),
),
),
),
),
),
),
),
)
}

fun clickOnMenuDeactivate() {
onView(withText(R.string.deactivate)).perform(click())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import org.hisp.dhis.android.core.maintenance.D2Error
import org.hisp.dhis.android.core.organisationunit.OrganisationUnit
import org.hisp.dhis.android.core.program.Program
import org.hisp.dhis.android.core.program.ProgramStage
import java.util.Calendar
import java.util.Date

class EventDetailsRepository(
Expand Down Expand Up @@ -72,7 +71,7 @@ class EventDetailsRepository(
return programStage?.minDaysFromStart() ?: 0
}

fun getStageLastDate(enrollmentUid: String?): Date {
fun getStageLastDate(enrollmentUid: String?): Date? {
val activeEvents =
d2.eventModule().events().byEnrollmentUid().eq(enrollmentUid).byProgramStageUid()
.eq(programStageUid)
Expand All @@ -89,7 +88,7 @@ class EventDetailsRepository(
}
if (scheduleEvents.isNotEmpty()) scheduleDate = scheduleEvents[0].dueDate()

return activeDate ?: (scheduleDate ?: Calendar.getInstance().time)
return activeDate ?: scheduleDate
}

fun hasAccessDataWrite(): Boolean {
Expand All @@ -111,6 +110,11 @@ class EventDetailsRepository(
return enrollment.enrollmentDate()
}

fun getEnrollmentIncidentDate(uid: String?): Date? {
val enrollment = d2.enrollmentModule().enrollments().uid(uid).blockingGet()
return enrollment?.incidentDate()
}

fun getFilteredOrgUnits(date: String?, parentUid: String?): List<OrganisationUnit> {
val organisationUnits = parentUid?.let {
getOrgUnitsByParentUid(it)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import org.dhis2.usescases.eventsWithoutRegistration.eventDetails.models.EventDa
import org.dhis2.usescases.eventsWithoutRegistration.eventDetails.providers.EventDetailResourcesProvider
import org.hisp.dhis.android.core.period.PeriodType
import org.hisp.dhis.android.core.program.ProgramStage
import java.util.Calendar
import java.util.Calendar.DAY_OF_YEAR
import java.util.Date
import java.util.Locale
Expand Down Expand Up @@ -106,27 +105,24 @@ class ConfigureEventReportDate(
}

private fun getNextScheduleDate(): Date {
val isGeneratedEventBasedOnEnrollment =
repository.getProgramStage()?.generatedByEnrollmentDate()

val initialDate = if (isGeneratedEventBasedOnEnrollment == true) {
val enrollmentDate = repository.getEnrollmentDate(enrollmentId)
DateUtils.getInstance().getCalendarByDate(enrollmentDate)
} else {
val date = DateUtils.getInstance().calendar
date.time = repository.getStageLastDate(enrollmentId)
val scheduleDate = repository.getStageLastDate(enrollmentId)?.let {
val lastStageDate = DateUtils.getInstance().getCalendarByDate(it)
lastStageDate.add(DAY_OF_YEAR, getScheduleInterval())
lastStageDate
} ?: run {
val enrollmentDate = with(repository) {
when (getProgramStage()?.generatedByEnrollmentDate()) {
true -> getEnrollmentDate(enrollmentId)
else -> getEnrollmentIncidentDate(enrollmentId)
?: getEnrollmentDate(enrollmentId)
}
}
val date = DateUtils.getInstance().getCalendarByDate(enrollmentDate)
val minDateFromStart = repository.getMinDaysFromStartByProgramStage()
date.add(DAY_OF_YEAR, minDateFromStart)
date
}

if (getScheduleInterval() > 0) {
initialDate.add(Calendar.DAY_OF_YEAR, getScheduleInterval())
}
val minDateFromStart =
repository.getMinDaysFromStartByProgramStage()
if (minDateFromStart > 0) {
initialDate.add(DAY_OF_YEAR, minDateFromStart)
}
return DateUtils.getInstance().getNextPeriod(null, initialDate.time, 0)
return DateUtils.getInstance().getNextPeriod(null, scheduleDate.time, 0)
}

private fun getCurrentDay() = DateUtils.getInstance().today
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ data class EventDate(
val currentDate: Date? = null,
val minDate: Date? = null,
val maxDate: Date? = null,
var scheduleInterval: Int = 0,
val scheduleInterval: Int = 0,
val allowFutureDates: Boolean = true,
val periodType: PeriodType? = null,
)
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,6 @@ class EventDetailsFragment : FragmentGlobalAbstract() {
dialog.setInitialDate(viewModel.eventDate.value.currentDate)
dialog.setMinDate(viewModel.eventDate.value.minDate)
dialog.setMaxDate(viewModel.eventDate.value.maxDate)
dialog.setScheduleInterval(viewModel.eventDate.value.scheduleInterval)
dialog.isFutureDatesAllowed(viewModel.eventDate.value.allowFutureDates)
dialog.setListener(
object : OnDatePickerListener {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,6 @@ class EventDetailsViewModel(
viewModelScope.launch {
configureEventReportDate().collect {
_eventDate.value = it
if (_eventDate.value.scheduleInterval > 0) {
_eventDate.value.scheduleInterval = 0
}
}

configureOrgUnit(eventDate.value.currentDate)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,45 @@ class ConfigureEventReportDateTest {
repository = repository,
periodUtils = periodUtils,
enrollmentId = ENROLLMENT_ID,
scheduleInterval = 6,
)

val lastEventDate = "13/2/2022"
val nextEventDate = "19/2/2022"
whenever(
repository.getStageLastDate(ENROLLMENT_ID),
) doReturn DateUtils.uiDateFormat().parse(lastEventDate)

// When reportDate is invoked
val eventDate = configureEventReportDate.invoke().first()

// Then date should be next period
assert(eventDate.dateValue == nextEventDate)
}

@Test
fun `Get next period when creating first scheduled event generated by enrollment date`() = runBlocking {
// Given the creation of new scheduled event
configureEventReportDate = ConfigureEventReportDate(
creationType = EventCreationType.SCHEDULE,
resourceProvider = resourcesProvider,
repository = repository,
periodUtils = periodUtils,
enrollmentId = ENROLLMENT_ID,
scheduleInterval = 6,
)

val lastEventDate = "13/2/2022"
val nextEventDate = "19/2/2022"
whenever(
repository.getStageLastDate(ENROLLMENT_ID),
) doReturn null
whenever(
repository.getProgramStage()?.generatedByEnrollmentDate(),
) doReturn true
whenever(
repository.getEnrollmentDate(ENROLLMENT_ID),
) doReturn DateUtils.uiDateFormat().parse(lastEventDate)
whenever(
repository.getMinDaysFromStartByProgramStage(),
) doReturn 6
Expand All @@ -137,6 +169,39 @@ class ConfigureEventReportDateTest {
assert(eventDate.dateValue == nextEventDate)
}

@Test
fun `Get next period when creating first scheduled event generated by incident date`() = runBlocking {
// Given the creation of new scheduled event
configureEventReportDate = ConfigureEventReportDate(
creationType = EventCreationType.SCHEDULE,
resourceProvider = resourcesProvider,
repository = repository,
periodUtils = periodUtils,
enrollmentId = ENROLLMENT_ID,
)

val lastEventDate = "13/2/2022"
val nextEventDate = "15/2/2022"
whenever(
repository.getStageLastDate(ENROLLMENT_ID),
) doReturn null
whenever(
repository.getProgramStage()?.generatedByEnrollmentDate(),
) doReturn false
whenever(
repository.getEnrollmentIncidentDate(ENROLLMENT_ID),
) doReturn DateUtils.uiDateFormat().parse(lastEventDate)
whenever(
repository.getMinDaysFromStartByProgramStage(),
) doReturn 2

// When reportDate is invoked
val eventDate = configureEventReportDate.invoke().first()

// Then date should be next period
assert(eventDate.dateValue == nextEventDate)
}

@Test
fun `Should hide field when scheduled`() = runBlocking {
// Given an scheduled event
Expand Down

0 comments on commit df06bff

Please sign in to comment.