Skip to content

Commit

Permalink
Add patient address attributes (#1224)
Browse files Browse the repository at this point in the history
* 1219 - Quest - Patient attributes and Address attributes

* update PatientItem telecom as list of string

* revert some changes to fix failing tests

* update as per reviewer comments, and fixes for coverage
  • Loading branch information
AbdulWahabMemon authored May 16, 2022
1 parent cad25be commit 8ac11e7
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,33 @@ fun Patient.extractAddress(): String {
}
}

fun Patient.extractAddressDistrict(): String {
return with(addressFirstRep) { this.district ?: "" }
}

fun Patient.extractAddressState(): String {
return with(addressFirstRep) { this.state ?: "" }
}

fun Patient.extractAddressText(): String {
return with(addressFirstRep) { this.text ?: "" }
}

fun Patient.extractTelecom(): List<String>? {
if (!hasTelecom()) return null
return telecom.map { it.value }
}

fun Patient.extractGeneralPractitionerReference(): String {
if (!hasGeneralPractitioner()) return ""
return with(generalPractitionerFirstRep) { this.reference }
}

fun Patient.extractManagingOrganizationReference(): String {
if (!hasManagingOrganization()) return ""
return with(managingOrganization) { this.reference }
}

fun Patient.extractDeathDate() =
if (this.hasDeceasedDateTimeType()) deceasedDateTimeType?.value else null

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,59 @@ class PatientExtensionTest : RobolectricTest() {
Assert.assertEquals("12 B, Gulshan, Karimabad Sindh", patient.extractAddress())
}

@Test
fun testExtractAddressAttributes() {
val patient =
Patient().apply {
addAddress().apply {
this.addLine("12 B")
this.addLine("Gulshan")
this.district = "Karimabad"
this.state = "Sindh"
this.text = "home location Karachi"
}
}

Assert.assertEquals("12 B, Gulshan, Karimabad Sindh", patient.extractAddress())
Assert.assertEquals("Karimabad", patient.extractAddressDistrict())
Assert.assertEquals("Sindh", patient.extractAddressState())
Assert.assertEquals("home location Karachi", patient.extractAddressText())
}

@Test
fun testExtractTelecomShouldReturnTelecom() {
val patient =
Patient().apply {
addTelecom().apply { this.value = "+1234" }
addTelecom().apply { this.value = "+5678" }
}
val expectedList: List<String> = listOf("+1234", "+5678")
Assert.assertEquals(expectedList, patient.extractTelecom())
if (!patient.hasManagingOrganization()) {
Assert.assertEquals("", patient.extractManagingOrganizationReference())
}
if (!patient.hasGeneralPractitioner()) {
Assert.assertEquals("", patient.extractGeneralPractitionerReference())
}
}

@Test
fun testExtractGeneralPractitionerShouldReturnReference() {
val patient =
Patient().apply { addGeneralPractitioner().apply { this.reference = "practitioner/1234" } }
Assert.assertEquals("practitioner/1234", patient.extractGeneralPractitionerReference())
if (!patient.hasTelecom()) {
Assert.assertEquals(null, patient.extractTelecom())
}
}

@Test
fun testExtractManagingOrganizationShouldReturnReference() {
val patient =
Patient().apply { managingOrganization.apply { this.reference = "reference/1234" } }
Assert.assertEquals("reference/1234", patient.extractManagingOrganizationReference())
}

@Test
fun testIsFamilyHeadShouldReturnTrueWithTagFamily() {
val patient = Patient().apply { meta.addTag().display = "FamiLy" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,12 @@ data class PatientItem(
val name: String = "",
val gender: String = "",
val age: String = "",
val address: String = "",
var additionalData: List<AdditionalData>? = null
val displayAddress: String = "",
var additionalData: List<AdditionalData>? = null,
val telecom: List<String>? = null,
val address: AddressData? = null,
val generalPractitionerReference: String = "",
val managingOrganizationReference: String = ""
)

@Stable
Expand All @@ -41,6 +45,14 @@ data class AdditionalData(
val properties: Properties? = null
)

@Stable
data class AddressData(
val district: String = "",
val state: String = "",
val text: String = "",
val fullAddress: String = ""
)

fun PatientItem.genderFull(): String {
return when (gender) {
"M" -> "Male"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,16 @@ import javax.inject.Inject
import org.hl7.fhir.r4.model.Patient
import org.smartregister.fhircore.engine.data.domain.util.DomainMapper
import org.smartregister.fhircore.engine.util.extension.extractAddress
import org.smartregister.fhircore.engine.util.extension.extractAddressDistrict
import org.smartregister.fhircore.engine.util.extension.extractAddressState
import org.smartregister.fhircore.engine.util.extension.extractAddressText
import org.smartregister.fhircore.engine.util.extension.extractAge
import org.smartregister.fhircore.engine.util.extension.extractGender
import org.smartregister.fhircore.engine.util.extension.extractGeneralPractitionerReference
import org.smartregister.fhircore.engine.util.extension.extractManagingOrganizationReference
import org.smartregister.fhircore.engine.util.extension.extractName
import org.smartregister.fhircore.engine.util.extension.extractTelecom
import org.smartregister.fhircore.quest.data.patient.model.AddressData
import org.smartregister.fhircore.quest.data.patient.model.PatientItem

class PatientItemMapper @Inject constructor(@ApplicationContext val context: Context) :
Expand All @@ -41,7 +48,17 @@ class PatientItemMapper @Inject constructor(@ApplicationContext val context: Con
name = name,
gender = gender.toString(),
age = age,
address = dto.extractAddress()
displayAddress = dto.extractAddress(),
address =
AddressData(
dto.extractAddressDistrict(),
dto.extractAddressState(),
dto.extractAddressText(),
dto.extractAddress()
),
telecom = dto.extractTelecom(),
generalPractitionerReference = dto.extractGeneralPractitionerReference(),
managingOrganizationReference = dto.extractManagingOrganizationReference()
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,15 @@ fun dummyPatientPagingList(): LazyPagingItems<PatientItem> {
name = "John Doe",
gender = "M",
age = "27y",
address = "Nairobi"
displayAddress = "Nairobi"
),
PatientItem(
id = "my-test-id2",
identifier = "10002",
name = "Jane Doe",
gender = "F",
age = "20y",
address = "Nairobi"
displayAddress = "Nairobi"
)
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,27 +45,42 @@ class PatientMapperTest : RobolectricTest() {
@Test
fun testMapToDomainModel() {

val dto = buildPatient("123456", "123456", "Doe", "John", 12)
val dto = buildPatient("123456", "123456", "Doe", "John", 12, listOf("+12345678"))
val patientItem = patientItemMapper.mapToDomainModel(dto)
with(patientItem) {
assertEquals("12y", age)
assertEquals("John Doe", name)
assertEquals("123456", id)
assertEquals("123456", identifier)
assertEquals("Dist 1 City 1", address)
assertEquals("Dist 1 City 1 State 1", displayAddress)
assertEquals("+12345678", telecom!![0])
assertEquals("practitioner/1234", generalPractitionerReference)
assertEquals("reference/5678", managingOrganizationReference)
assertEquals("State 1", address!!.state)
assertEquals("Dist 1", address!!.district)
assertEquals("Location 1", address!!.text)
assertEquals("Dist 1 City 1 State 1", address!!.fullAddress)
}
}

@Test
fun testMapToDomainModelWithoutIdentifier() {
val dto = buildPatient("123456", null, "Doe", "John", 12)
val dto = buildPatient("123456", null, "Doe", "John", 12, listOf("+1234", "+5678"))
val patientItem = patientItemMapper.mapToDomainModel(dto)
with(patientItem) {
assertEquals("12y", age)
assertEquals("John Doe", name)
assertEquals("123456", id)
assertEquals("", identifier)
assertEquals("Dist 1 City 1", address)
assertEquals("+1234", telecom!![0])
assertEquals("+5678", telecom!![1])
assertEquals("practitioner/1234", generalPractitionerReference)
assertEquals("reference/5678", managingOrganizationReference)
assertEquals("Dist 1 City 1 State 1", displayAddress)
assertEquals("State 1", address!!.state)
assertEquals("Dist 1", address!!.district)
assertEquals("Location 1", address!!.text)
assertEquals("Dist 1 City 1 State 1", address!!.fullAddress)
}
}

Expand All @@ -74,7 +89,8 @@ class PatientMapperTest : RobolectricTest() {
identifier: String?,
family: String,
given: String,
age: Int
age: Int,
telephones: List<String>
): Patient {
return Patient().apply {
this.id = id
Expand All @@ -88,7 +104,14 @@ class PatientMapperTest : RobolectricTest() {
this.addAddress().apply {
district = "Dist 1"
city = "City 1"
state = "State 1"
text = "Location 1"
}

telephones.forEach { telephoneItem -> this.addTelecom().apply { value = telephoneItem } }

this.generalPractitionerFirstRep.apply { reference = "practitioner/1234" }
this.managingOrganization.apply { reference = "reference/5678" }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class PatientRowTest : RobolectricTest() {
name = "John Doe",
gender = "Male",
age = "27",
address = "Nairobi",
displayAddress = "Nairobi",
additionalData =
listOf(
AdditionalData(
Expand Down

0 comments on commit 8ac11e7

Please sign in to comment.