Skip to content

Commit

Permalink
fix: [ANDROAPP-6493] take incident date into account for scheduling e…
Browse files Browse the repository at this point in the history
…vents (#3871)

* fix: [ANDROAPP-6493] take incident date into account for scheduling and creating events

* fix: [ANDROAPP-6493] migrate code to tracker module, add repository

* fix: [ANDROAPP-6493] Adapt unit tests to new usage

* fix: [ANDROAPP-6493] Add tests that take into account incident date

* fix: [ANDROAPP-6493] add tests for min days form start and standard interval days

* fix: [ANDROAPP-6493] refactor setEventDate logic to improve readability

* fix: [ANDROAPP-6493] add test for period generation, clean up code

* fix: [ANDROAPP-6493] get next available period and not current one

* fix: [ANDROAPP-6493] take incident date into account for scheduling and creating events

* fix: [ANDROAPP-6493] migrate code to tracker module, add repository

* fix: [ANDROAPP-6493] Adapt unit tests to new usage

* fix: [ANDROAPP-6493] Add tests that take into account incident date

* fix: [ANDROAPP-6493] add tests for min days form start and standard interval days

* fix: [ANDROAPP-6493] refactor setEventDate logic to improve readability

* fix: [ANDROAPP-6493] add test for period generation, clean up code

* fix: [ANDROAPP-6493] get next available period and not current one

* fix: [ANDROAPP-6493] adapt unit test

* fix: [ANDROAPP-6645] dont take into account min days from start or standard interval days for new events

* fix: [ANDROAPP-6645] adapt unit tests

* fix: [ANDROAPP-6493] adapt unit tests

* fix: [ANDROAPP-6493] modify unit test

* fix: [ANDROAPP-6493] ensure suggested date for enrollments is current date

* fix: [ANDROAPP-6493] ensure suggested date for enrollments is current date

* fix: [ANDROAPP-6493] ktlint

* fix: [ANDROAPP-6493] remove failing test

* fix: [ANDROAPP-6493] ktlint

* fix: [ANDROAPP-6493] fix test
  • Loading branch information
xavimolloy authored Nov 14, 2024
1 parent 126ef89 commit 27ff180
Show file tree
Hide file tree
Showing 19 changed files with 364 additions and 174 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class ConfigureEventReportDate(
label = getLabel(),
dateValue = getDateValue(selectedDate),
currentDate = getDate(selectedDate),
minDate = getMinDate(),
minDate = getMinDate(selectedDate),
maxDate = getMaxDate(),
scheduleInterval = getScheduleInterval(),
allowFutureDates = getAllowFutureDates(),
Expand Down Expand Up @@ -68,8 +68,7 @@ class ConfigureEventReportDate(
private fun getDate(selectedDate: Date?) = when {
selectedDate != null -> selectedDate
repository.getEvent() != null -> repository.getEvent()?.eventDate()
periodType != null -> getDateBasedOnPeriodType()
creationType == SCHEDULE -> getNextScheduleDate()
periodType != null || creationType == SCHEDULE -> getNextScheduleDate()
else -> getCurrentDay()
}

Expand All @@ -84,14 +83,15 @@ class ConfigureEventReportDate(

private fun getProgramStage(): ProgramStage? = repository.getProgramStage()

private fun getDateBasedOnPeriodType(): Date {
private fun getDateBasedOnPeriodType(startDate: Date?): Date {
val initialDate = startDate ?: DateUtils.getInstance().today
val calendar = DateUtils.getInstance().calendar
calendar.time = initialDate
getProgramStage()?.hideDueDate()?.let { hideDueDate ->
if (creationType == SCHEDULE && hideDueDate) {
return if (periodType != null) {
DateUtils.getInstance().today
calendar.time
} else {
val calendar = DateUtils.getInstance().calendar
calendar.add(DAY_OF_YEAR, getScheduleInterval())
DateUtils.getInstance().getNextPeriod(
null,
calendar.time,
Expand All @@ -100,16 +100,15 @@ class ConfigureEventReportDate(
}
}
}

return DateUtils.getInstance()
.getNextPeriod(
periodType,
DateUtils.getInstance().today,
initialDate,
if (creationType != SCHEDULE) 0 else 1,
)
}

private fun getNextScheduleDate(): Date {
fun getNextScheduleDate(): Date {
val scheduleDate = repository.getStageLastDate(enrollmentId)?.let {
val lastStageDate = DateUtils.getInstance().getCalendarByDate(it)
lastStageDate.add(DAY_OF_YEAR, getScheduleInterval())
Expand All @@ -125,27 +124,30 @@ class ConfigureEventReportDate(
val date = DateUtils.getInstance().getCalendarByDate(enrollmentDate)
val minDateFromStart = repository.getMinDaysFromStartByProgramStage()
date.add(DAY_OF_YEAR, minDateFromStart)
date
periodType?.let {
return getDateBasedOnPeriodType(date.time)
}
return date.time
}
return DateUtils.getInstance().getNextPeriod(null, scheduleDate.time, 0)
return DateUtils.getInstance().getNextPeriod(periodType, scheduleDate.time, if (periodType != null) 1 else 0)
}

private fun getCurrentDay() = DateUtils.getInstance().today
private fun getCurrentDay() = DateUtils.getInstance().getStartOfDay(Date())

private fun getMinDate(): Date? {
private fun getMinDate(initialDate: Date?): Date? {
repository.getProgram()?.let { program ->
if (periodType == null) {
if (program.expiryPeriodType() != null) {
val expiryDays = program.expiryDays() ?: 0
return DateUtils.getInstance().expDate(
null,
initialDate,
expiryDays,
program.expiryPeriodType(),
)
}
} else {
var minDate = DateUtils.getInstance().expDate(
null,
initialDate,
program.expiryDays() ?: 0,
periodType,
)
Expand Down Expand Up @@ -186,7 +188,7 @@ class ConfigureEventReportDate(
when (creationType) {
ADDNEW,
DEFAULT,
-> DateUtils.getInstance().today
-> DateUtils.getInstance().getStartOfDay(Date())

else -> null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ import org.dhis2.commons.viewmodel.DispatcherProvider
import org.dhis2.maps.usecases.MapStyleConfiguration
import org.dhis2.maps.utils.DhisMapUtils
import org.dhis2.tracker.data.ProfilePictureProvider
import org.dhis2.tracker.events.CreateEventUseCase
import org.dhis2.tracker.events.CreateEventUseCaseRepository
import org.dhis2.usescases.events.EventInfoProvider
import org.dhis2.usescases.programEventDetail.eventList.ui.mapper.EventCardMapper
import org.dhis2.usescases.programEventDetail.usecase.CreateEventUseCase
import org.dhis2.utils.customviews.navigationbar.NavigationPageConfigurator
import org.hisp.dhis.android.core.D2

Expand Down Expand Up @@ -181,10 +182,18 @@ class ProgramEventDetailModule(
@PerActivity
fun provideCreateEventUseCase(
dispatcher: DispatcherProvider,
d2: D2,
dateUtils: DateUtils,
repository: CreateEventUseCaseRepository,
) = CreateEventUseCase(
dispatcher = dispatcher,
repository = repository,
)

@Provides
@PerActivity
fun provideCreateEventUseCaseRepository(
d2: D2,
dateUtils: DateUtils,
) = CreateEventUseCaseRepository(
d2 = d2,
dateUtils = dateUtils,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import org.dhis2.commons.viewmodel.DispatcherProvider
import org.dhis2.maps.layer.basemaps.BaseMapStyle
import org.dhis2.maps.usecases.MapStyleConfiguration
import org.dhis2.tracker.NavigationBarUIState
import org.dhis2.usescases.programEventDetail.usecase.CreateEventUseCase
import org.dhis2.tracker.events.CreateEventUseCase
import org.dhis2.utils.customviews.navigationbar.NavigationPage
import org.dhis2.utils.customviews.navigationbar.NavigationPageConfigurator
import org.hisp.dhis.mobile.ui.designsystem.component.navigationBar.NavigationBarItem
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import androidx.lifecycle.ViewModelProvider
import org.dhis2.commons.resources.ResourceManager
import org.dhis2.commons.viewmodel.DispatcherProvider
import org.dhis2.maps.usecases.MapStyleConfiguration
import org.dhis2.usescases.programEventDetail.usecase.CreateEventUseCase
import org.dhis2.tracker.events.CreateEventUseCase
import org.dhis2.utils.customviews.navigationbar.NavigationPageConfigurator

@Suppress("UNCHECKED_CAST")
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import org.dhis2.commons.resources.MetadataIconProvider
import org.dhis2.commons.schedulers.SchedulerProvider
import org.dhis2.commons.viewmodel.DispatcherProvider
import org.dhis2.form.data.RulesUtilsProvider
import org.dhis2.usescases.programEventDetail.usecase.CreateEventUseCase
import org.dhis2.tracker.events.CreateEventUseCase
import org.dhis2.tracker.events.CreateEventUseCaseRepository
import org.hisp.dhis.android.core.D2

@PerActivity
Expand Down Expand Up @@ -73,9 +74,15 @@ class ProgramStageSelectionModule(
@PerActivity
fun provideCreateEventUseCase(
dispatcherProvider: DispatcherProvider,
repository: CreateEventUseCaseRepository,
) = CreateEventUseCase(dispatcherProvider, repository)

@Provides
@PerActivity
fun provideCreateEventUseCaseRepository(
d2: D2,
dateUtils: DateUtils,
) = CreateEventUseCase(dispatcherProvider, d2, dateUtils)
) = CreateEventUseCaseRepository(d2, dateUtils)

@Provides
@PerActivity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import org.dhis2.commons.schedulers.SchedulerProvider
import org.dhis2.commons.viewmodel.DispatcherProvider
import org.dhis2.form.data.RulesUtilsProvider
import org.dhis2.form.model.EventMode
import org.dhis2.usescases.programEventDetail.usecase.CreateEventUseCase
import org.dhis2.tracker.events.CreateEventUseCase
import org.dhis2.utils.Result
import org.hisp.dhis.android.core.program.ProgramStage
import org.hisp.dhis.mobile.ui.designsystem.theme.SurfaceColor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ public Observable<Pair<String, String>> saveToEnroll(@NonNull String teiType,
.organisationUnit(orgUnit)
.build())
.map(enrollmentUid -> {
d2.enrollmentModule().enrollments().uid(enrollmentUid).setEnrollmentDate(DateUtils.getInstance().getToday());
d2.enrollmentModule().enrollments().uid(enrollmentUid).setEnrollmentDate(DateUtils.getInstance().getStartOfDay(new Date()));
d2.enrollmentModule().enrollments().uid(enrollmentUid).setFollowUp(false);
return Pair.create(enrollmentUid, uid);
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ import org.dhis2.data.forms.dataentry.SearchTEIRepositoryImpl
import org.dhis2.form.data.FormValueStore
import org.dhis2.form.data.OptionsRepository
import org.dhis2.mobileProgramRules.RuleEngineHelper
import org.dhis2.usescases.programEventDetail.usecase.CreateEventUseCase
import org.dhis2.tracker.events.CreateEventUseCase
import org.dhis2.tracker.events.CreateEventUseCaseRepository
import org.dhis2.usescases.teiDashboard.DashboardRepository
import org.dhis2.usescases.teiDashboard.dashboardfragments.teidata.teievents.ui.mapper.TEIEventCardMapper
import org.dhis2.usescases.teiDashboard.domain.GetNewEventCreationTypeOptions
Expand Down Expand Up @@ -171,10 +172,17 @@ class TEIDataModule(
@Provides
fun provideCreateEventUseCase(
dispatcherProvider: DispatcherProvider,
d2: D2,
dateUtils: DateUtils,
repository: CreateEventUseCaseRepository,
) = CreateEventUseCase(
dispatcher = dispatcherProvider,
repository = repository,
)

@Provides
fun provideCreateEventUseCaseRepository(
d2: D2,
dateUtils: DateUtils,
) = CreateEventUseCaseRepository(
d2 = d2,
dateUtils = dateUtils,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ import org.dhis2.form.data.OptionsRepository
import org.dhis2.form.data.RulesUtilsProviderImpl
import org.dhis2.form.model.EventMode
import org.dhis2.mobileProgramRules.RuleEngineHelper
import org.dhis2.tracker.events.CreateEventUseCase
import org.dhis2.usescases.eventsWithoutRegistration.eventCapture.EventCaptureActivity
import org.dhis2.usescases.eventsWithoutRegistration.eventCapture.EventCaptureActivity.Companion.getActivityBundle
import org.dhis2.usescases.eventsWithoutRegistration.eventInitial.EventInitialActivity
import org.dhis2.usescases.programEventDetail.usecase.CreateEventUseCase
import org.dhis2.usescases.programStageSelection.ProgramStageSelectionActivity
import org.dhis2.usescases.teiDashboard.DashboardRepository
import org.dhis2.usescases.teiDashboard.dashboardfragments.teidata.TeiDataIdlingResourceSingleton.decrement
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,9 @@ class SchedulingViewModel(
resourceManager = resourceManager,
eventResourcesProvider = eventResourcesProvider,
)

private fun loadProgramStage(event: Event? = null) {
viewModelScope.launch {
val selectedDate = event?.dueDate()
val selectedDate = event?.dueDate() ?: configureEventReportDate.getNextScheduleDate()
configureEventReportDate(selectedDate = selectedDate).collect {
_eventDate.value = it
}
Expand Down Expand Up @@ -290,7 +289,7 @@ class SchedulingViewModel(
val programUid = event.program() ?: return@launch

d2.eventModule().events().uid(launchMode.eventUid).run {
setEventDate(dateUtils.today)
setEventDate(dateUtils.getStartOfDay(Date()))
setStatus(EventStatus.ACTIVE)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,30 +65,6 @@ class ConfigureEventReportDateTest {
assert(eventDate.label == EVENT_DATE)
}

@Test
fun `Should return current day when new event`() = runBlocking {
// Given the creation of new event
whenever(repository.getEvent()) doReturn null
whenever(programStage.displayEventLabel()) doReturn null

configureEventReportDate = ConfigureEventReportDate(
resourceProvider = resourcesProvider,
repository = repository,
periodUtils = periodUtils,
)
val currentDay =
DateUtils.uiDateFormat().format(DateUtils.getInstance().today)

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

// Then report date should be active
assert(eventDate.active)
// Then reportDate should be the current day
assert(eventDate.dateValue == currentDay)
assert(eventDate.label == NEXT_EVENT)
}

@Test
fun `Should return tomorrow when new daily event`() = runBlocking {
// Given the creation of new event
Expand All @@ -99,8 +75,16 @@ class ConfigureEventReportDateTest {
repository = repository,
periodType = periodType,
periodUtils = periodUtils,
enrollmentId = ENROLLMENT_ID,
)
val today = "15/02/2022"

whenever(
repository.getEnrollmentDate(ENROLLMENT_ID),
) doReturn DateUtils.getInstance().getStartOfDay(DateUtils.uiDateFormat().parse(today))
whenever(
repository.getEnrollmentIncidentDate(ENROLLMENT_ID),
) doReturn DateUtils.getInstance().getStartOfDay(DateUtils.uiDateFormat().parse(today))
val tomorrow = "16/02/2022"

whenever(
Expand Down Expand Up @@ -156,6 +140,7 @@ class ConfigureEventReportDateTest {
whenever(
repository.getStageLastDate(ENROLLMENT_ID),
) doReturn null

whenever(
repository.getProgramStage()?.generatedByEnrollmentDate(),
) doReturn true
Expand Down
Loading

0 comments on commit 27ff180

Please sign in to comment.