Skip to content

Commit

Permalink
Add a new function in RulesFactory to calculate day difference betwee…
Browse files Browse the repository at this point in the history
…n inputDate and today (#2989)

* Refactor logging

Log HTTP body on debug mode

Signed-off-by: Elly Kitoto <[email protected]>

* Add daysPassed method in RulesFactory to calculate difference of days

* Add test for daysPassed in RulesFactoryTest

* update test

---------

Signed-off-by: Elly Kitoto <[email protected]>
Co-authored-by: Elly Kitoto <[email protected]>
  • Loading branch information
qaziabubakar-vd and ellykits authored Jan 19, 2024
1 parent c2feda4 commit 8409166
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ import org.smartregister.fhircore.engine.domain.model.ServiceMemberIcon
import org.smartregister.fhircore.engine.domain.model.ServiceStatus
import org.smartregister.fhircore.engine.util.DispatcherProvider
import org.smartregister.fhircore.engine.util.SharedPreferenceKey
import org.smartregister.fhircore.engine.util.extension.SDF_DD_MMM_YYYY
import org.smartregister.fhircore.engine.util.extension.SDF_E_MMM_DD_YYYY
import org.smartregister.fhircore.engine.util.extension.daysPassed
import org.smartregister.fhircore.engine.util.extension.extractAge
import org.smartregister.fhircore.engine.util.extension.extractGender
import org.smartregister.fhircore.engine.util.extension.extractLogicalIdUuid
Expand Down Expand Up @@ -293,6 +295,13 @@ constructor(
*/
fun prettifyDate(inputDate: Date): String = inputDate.prettifyDate()

/**
* This function takes [inputDate] and returns a difference (for examples 15, 30 etc) between
* inputDate and the currentDate
*/
fun daysPassed(inputDate: String, pattern: String = SDF_DD_MMM_YYYY): String =
inputDate.parseDate(pattern)?.daysPassed().toString()

/**
* This function takes [inputDateString] like 2022-7-1 and returns a difference (for examples 7
* hours ago, 2 days ago, 5 months ago, 3 years ago etc) [inputDateString] can give given as
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ import org.smartregister.fhircore.engine.domain.model.RuleConfig
import org.smartregister.fhircore.engine.robolectric.RobolectricTest
import org.smartregister.fhircore.engine.rule.CoroutineTestRule
import org.smartregister.fhircore.engine.util.DispatcherProvider
import org.smartregister.fhircore.engine.util.extension.SDF_YYYY_MM_DD
import org.smartregister.fhircore.engine.util.fhirpath.FhirPathDataExtractor

@HiltAndroidTest
Expand Down Expand Up @@ -848,6 +849,15 @@ class RulesFactoryTest : RobolectricTest() {
Assert.assertEquals("", expected)
}

@Test
fun testDaysPassed() {
val daysAgo = 14
val inputDateString = LocalDate.now().minusDays(daysAgo).toString()
val daysPassedResult =
rulesFactory.RulesEngineService().daysPassed(inputDateString, SDF_YYYY_MM_DD)
Assert.assertEquals("14", daysPassedResult)
}

@Test
fun extractAge() {
val dateFormatter: DateTimeFormatter? = DateTimeFormat.forPattern("yyyy-MM-dd")
Expand Down

0 comments on commit 8409166

Please sign in to comment.