opensrp / fhircore

FHIR Core / OpenSRP 2 is a Kotlin application for delivering offline-capable, mobile-first healthcare project implementations from local community to national and international scale using FHIR and WHO Smart Guidelines on Android.
https://smartregister.org
Apache License 2.0
50 stars 39 forks source link

Remove patient overflow menu and editing registration info item duplicates patient in household profile #3142

Closed SebaMutuku closed 2 months ago

SebaMutuku commented 3 months ago

Describe the bug A clear and concise description of what the bug is.

To Reproduce Scenario 1 Steps to reproduce the behavior: Remove patient

  1. Log in to any app whose workflow begins with household register
  2. Open any household with patients
  3. Click on any patient and open overflow menu items
  4. Click remove patient and fill in the questionnaire and save it
  5. Go to household profile . You will notice that the patient you remove has duplicated on the household profile

Scenario 2 Steps to reproduce the behavior: Edit registration info

  1. Log in to any app whose workflow begins with household register
  2. Open any household with patients
  3. Click on any patient and open overflow menu items
  4. Click registration info and edit any field in the questionnaire and save it
  5. Go to household profile . You will notice that the patient you edit has duplicated on the household profile

Expected behavior Removing a patient from household should grey out the patient or remove him/her from the household profile based on different app but not duplicate the patient Editing patient details should save the changes but not duplicate the patient

Screenshots.

image

Smartphone (please complete the following information):

Additional context Add any other context about the problem here.

f-odhiambo commented 3 months ago

@allan-on Add more context here

SebaMutuku commented 3 months ago

Findings. When an extraction was performed, a patient with below payload is created

{
        "resourceType": "Patient",
        "id": "7d0121aa-6682-423f-8c3b-2daab283c3b5",
        "active": false
      }

An entry in the localChangeEntity is created with the below details

[{"op":"replace","path":"\/active","value":false}]

with patient id above. A single patient with id above only, exists on the DB and the status is updated to false.Below is the sample resource

{
  "resourceType": "Patient",
  "id": "7d0121aa-6682-423f-8c3b-2daab283c3b5",
  "meta": {
    "lastUpdated": "2024-03-19T11:06:47.612+03:00",
    "tag": [
      {
        "system": "https://smartregister.org/app-version",
        "code": "Not defined",
        "display": "Application Version"
      },
      {
        "system": "https://smartregister.org/location-tag-id",
        "code": "Not defined",
        "display": "Practitioner Location"
      },
      {
        "system": "https://smartregister.org/care-team-tag-id",
        "code": "13235df1-6229-49a2-933a-cf1dc00ee66d",
        "display": "Practitioner CareTeam"
      },
      {
        "system": "https://smartregister.org/organisation-tag-id",
        "code": "a43bf680-8518-499e-abcc-364dbf8a7008",
        "display": "Practitioner Organization"
      },
      {
        "system": "https://smartregister.org/practitioner-tag-id",
        "code": "1fb68b79-d038-44f2-baed-d41c79c40afa",
        "display": "Practitioner"
      }
    ]
  },
  "text": {
    "status": "generated",
    "div": "<div xmlns=\"http://www.w3.org/1999/xhtml\"><div class=\"hapiHeaderText\"><b>MATT </b></div><table class=\"hapiPropertyTable\"><tbody><tr><td>Identifier</td><td/></tr><tr><td>Address</td><td/></tr><tr><td>Date of birth</td><td><span>13 December 1977</span></td></tr></tbody></table></div>"
  },
  "identifier": [
    {
      "use": "secondary",
      "period": {
        "start": "2024-03-14T13:59:03-04:00"
      }
    },
    {
      "use": "usual",
      "value": "d19efa47-eaa6-4e78-bbd3-334bf8d2c5b5",
      "period": {
        "start": "2024-03-14T13:59:03-04:00"
      }
    },
    {
      "use": "official",
      "value": "4444422242224442",
      "period": {
        "start": "2024-03-14T13:59:03-04:00"
      }
    },
    {
      "use": "secondary",
      "period": {
        "start": "2024-03-14T13:59:03-04:00"
      }
    },
    {
      "use": "secondary",
      "period": {
        "start": "2024-03-14T13:59:03-04:00"
      }
    }
  ],
  "active": false,
  "name": [
    {
      "use": "official",
      "text": "Berg",
      "family": "Matt"
    }
  ],
  "telecom": [
    {
      "value": "33333344422",
      "use": "mobile"
    }
  ],
  "gender": "male",
  "birthDate": "1977-12-13",
  "address": [
    {
      "use": "home",
      "type": "physical",
      "district": "46 Brewer Parkway"
    }
  ],
  "generalPractitioner": [
    {
      "reference": "Practitioner/1fb68b79-d038-44f2-baed-d41c79c40afa"
    }
  ],
  "managingOrganization": {
    "reference": "Organization/a43bf680-8518-499e-abcc-364dbf8a7008"
  }
}

This might be a caching issue @ellykits, do you have sometime to give some pointers on how to invalidate cache on the app when launching any workflow?

ellykits commented 3 months ago

Thanks, @SebaMutuku for this report. I will verify and revert.

SebaMutuku commented 2 months ago

After further investigation, this narrows down to

  private suspend fun searchRelatedResources(
    search: Search,
    relatedResourcesConfigsMap: Map<ResourceType, List<ResourceConfig>>,
    relatedResourceWrapper: RelatedResourceWrapper,
    configComputedRuleValues: Map<String, Any>,
  ) {
    kotlin
      .runCatching { fhirEngine.search<Resource>(search) }

which loads data saved in LocalChangeEntity and ResourcesEntity databases but instead of combining the two patients, it returns both as different resources

SebaMutuku commented 2 months ago

The query for retrieving the encounters returns 2 records of the same parent encounter

SELECT * FROM (
SELECT  rie.index_name, rie.index_value, re.resourceId,  re.serializedResource
FROM ResourceEntity re
JOIN ReferenceIndexEntity rie
ON re.resourceUuid = rie.resourceUuid
LEFT JOIN DateIndexEntity b
ON re.resourceType = b.resourceType AND re.resourceUuid = b.resourceUuid AND b.index_name = '_lastUpdated'
LEFT JOIN DateTimeIndexEntity c
ON re.resourceType = c.resourceType AND re.resourceUuid = c.resourceUuid AND c.index_name = '_lastUpdated'
WHERE rie.resourceType = 'Encounter'  AND rie.index_name = 'subject'  AND rie.index_value IN ('Patient/607155f4-44a9-4dde-a429-5102a80b4420') AND re.resourceType = 'Encounter'
ORDER BY b.index_from DESC, c.index_from DESC
)

Result - Records 2 & 4 are similar


1 | subject | Patient/607155f4-44a9-4dde-a429-5102a80b4420 | c4c69f24-187a-4ce0-aa13-060b95d64297 | {"resourceType":"Encounter","id":"c4c69f24-187a-4ce0-aa13-060b95d64297","meta":{"lastUpdated":"2024-04-11T14:09:02.874+03:00","tag":[{"system":"https://smartregister.org/care-team-tag-id","code":"3e005baf-854b-40a7-bdd5-9b73f63aa9a3","display":"Practitioner CareTeam"},{"system":"https://smartregister.org/location-tag-id","code":"b11dd2d1-d2c4-4573-bdd1-5bbade37b886","display":"Practitioner Location"},{"system":"https://smartregister.org/organisation-tag-id","code":"41eae946-bdc4-4179-b404-6503ff12f59c","display":"Practitioner Organization"},{"system":"https://smartregister.org/practitioner-tag-id","code":"49b72a3d-44cd-4a74-9459-4dc9f6b543fa","display":"Practitioner"},{"system":"https://smartregister.org/app-version","code":"1.1.0-diabetesCompassClinic","display":"Application Version"}]},"status":"finished","class":{"system":"http://terminology.hl7.org/CodeSystem/v3-ActCode","code":"AMB"},"serviceType":{"coding":[{"system":"http://terminology.hl7.org/CodeSystem/service-type","code":"335"}]},"subject":{"reference":"Patient/607155f4-44a9-4dde-a429-5102a80b4420"},"participant":[{"individual":{"reference":"Practitioner/49b72a3d-44cd-4a74-9459-4dc9f6b543fa"}}],"serviceProvider":{"reference":"Organization/41eae946-bdc4-4179-b404-6503ff12f59c"},"partOf":{"reference":"Encounter/patient-visit-2024-04-11-607155f4-44a9-4dde-a429-5102a80b4420"}}
-- | -- | -- | -- | --
2 | subject | Patient/607155f4-44a9-4dde-a429-5102a80b4420 | patient-visit-2024-04-11-607155f4-44a9-4dde-a429-5102a80b4420 | {"resourceType":"Encounter","id":"patient-visit-2024-04-11-607155f4-44a9-4dde-a429-5102a80b4420","meta":{"lastUpdated":"2024-04-11T14:09:02.778+03:00","tag":[{"system":"https://smartregister.org/care-team-tag-id","code":"3e005baf-854b-40a7-bdd5-9b73f63aa9a3","display":"Practitioner CareTeam"},{"system":"https://smartregister.org/location-tag-id","code":"b11dd2d1-d2c4-4573-bdd1-5bbade37b886","display":"Practitioner Location"},{"system":"https://smartregister.org/organisation-tag-id","code":"41eae946-bdc4-4179-b404-6503ff12f59c","display":"Practitioner Organization"},{"system":"https://smartregister.org/practitioner-tag-id","code":"49b72a3d-44cd-4a74-9459-4dc9f6b543fa","display":"Practitioner"},{"system":"https://smartregister.org/app-version","code":"1.1.0-diabetesCompassClinic","display":"Application Version"}]},"status":"finished","class":{"system":"http://terminology.hl7.org/CodeSystem/v3-ActCode","code":"AMB"},"serviceType":{"coding":[{"system":"http://terminology.hl7.org/CodeSystem/service-type","code":"335"}]},"subject":{"reference":"Patient/607155f4-44a9-4dde-a429-5102a80b4420"},"participant":[{"individual":{"reference":"Practitioner/49b72a3d-44cd-4a74-9459-4dc9f6b543fa"}}],"period":{"start":"2024-04-11T00:00:00.00Z","end":"2024-04-11T00:00:00.00Z"},"serviceProvider":{"reference":"Organization/41eae946-bdc4-4179-b404-6503ff12f59c"}}
3 | subject | Patient/607155f4-44a9-4dde-a429-5102a80b4420 | b0ba8f6e-3cff-424d-b184-5dc6d796f4b9 | {"resourceType":"Encounter","id":"b0ba8f6e-3cff-424d-b184-5dc6d796f4b9","meta":{"lastUpdated":"2024-04-11T14:05:42.159+03:00","tag":[{"system":"https://smartregister.org/care-team-tag-id","code":"3e005baf-854b-40a7-bdd5-9b73f63aa9a3","display":"Practitioner CareTeam"},{"system":"https://smartregister.org/location-tag-id","code":"b11dd2d1-d2c4-4573-bdd1-5bbade37b886","display":"Practitioner Location"},{"system":"https://smartregister.org/organisation-tag-id","code":"41eae946-bdc4-4179-b404-6503ff12f59c","display":"Practitioner Organization"},{"system":"https://smartregister.org/practitioner-tag-id","code":"49b72a3d-44cd-4a74-9459-4dc9f6b543fa","display":"Practitioner"},{"system":"https://smartregister.org/app-version","code":"1.1.0-diabetesCompassClinic","display":"Application Version"}]},"status":"finished","class":{"system":"http://terminology.hl7.org/CodeSystem/v3-ActCode","code":"AMB"},"serviceType":{"coding":[{"system":"http://terminology.hl7.org/CodeSystem/service-type","code":"335"}]},"subject":{"reference":"Patient/607155f4-44a9-4dde-a429-5102a80b4420"},"participant":[{"individual":{"reference":"Practitioner/49b72a3d-44cd-4a74-9459-4dc9f6b543fa"}}],"serviceProvider":{"reference":"Organization/41eae946-bdc4-4179-b404-6503ff12f59c"},"partOf":{"reference":"Encounter/patient-visit-2024-04-11-607155f4-44a9-4dde-a429-5102a80b4420"}}
4 | subject | Patient/607155f4-44a9-4dde-a429-5102a80b4420 | patient-visit-2024-04-11-607155f4-44a9-4dde-a429-5102a80b4420 | {"resourceType":"Encounter","id":"patient-visit-2024-04-11-607155f4-44a9-4dde-a429-5102a80b4420","meta":{"lastUpdated":"2024-04-11T14:09:02.778+03:00","tag":[{"system":"https://smartregister.org/care-team-tag-id","code":"3e005baf-854b-40a7-bdd5-9b73f63aa9a3","display":"Practitioner CareTeam"},{"system":"https://smartregister.org/location-tag-id","code":"b11dd2d1-d2c4-4573-bdd1-5bbade37b886","display":"Practitioner Location"},{"system":"https://smartregister.org/organisation-tag-id","code":"41eae946-bdc4-4179-b404-6503ff12f59c","display":"Practitioner Organization"},{"system":"https://smartregister.org/practitioner-tag-id","code":"49b72a3d-44cd-4a74-9459-4dc9f6b543fa","display":"Practitioner"},{"system":"https://smartregister.org/app-version","code":"1.1.0-diabetesCompassClinic","display":"Application Version"}]},"status":"finished","class":{"system":"http://terminology.hl7.org/CodeSystem/v3-ActCode","code":"AMB"},"serviceType":{"coding":[{"system":"http://terminology.hl7.org/CodeSystem/service-type","code":"335"}]},"subject":{"reference":"Patient/607155f4-44a9-4dde-a429-5102a80b4420"},"participant":[{"individual":{"reference":"Practitioner/49b72a3d-44cd-4a74-9459-4dc9f6b543fa"}}],"period":{"start":"2024-04-11T00:00:00.00Z","end":"2024-04-11T00:00:00.00Z"},"serviceProvider":{"reference":"Organization/41eae946-bdc4-4179-b404-6503ff12f59c"}}
5 | subject | Patient/607155f4-44a9-4dde-a429-5102a80b4420 | af306880-090e-474a-ab16-46bd54a44525 | {"resourceType":"Encounter","id":"af306880-090e-474a-ab16-46bd54a44525","meta":{"lastUpdated":"2024-04-11T14:04:59.076+03:00","tag":[{"system":"https://smartregister.org/care-team-tag-id","code":"3e005baf-854b-40a7-bdd5-9b73f63aa9a3","display":"Practitioner CareTeam"},{"system":"https://smartregister.org/location-tag-id","code":"b11dd2d1-d2c4-4573-bdd1-5bbade37b886","display":"Practitioner Location"},{"system":"https://smartregister.org/organisation-tag-id","code":"41eae946-bdc4-4179-b404-6503ff12f59c","display":"Practitioner Organization"},{"system":"https://smartregister.org/practitioner-tag-id","code":"49b72a3d-44cd-4a74-9459-4dc9f6b543fa","display":"Practitioner"},{"system":"https://smartregister.org/app-version","code":"1.1.0-diabetesCompassClinic","display":"Application Version"}]},"status":"finished","class":{"system":"http://terminology.hl7.org/CodeSystem/v3-ActCode","code":"HH"},"type":[{"coding":[{"system":"http://snomed.info/sct","code":"184047000","display":"Patient registration"}],"text":"Patient registration"}],"priority":{"coding":[{"system":"http://snomed.info/sct","code":"17621005","display":"Normal"}],"text":"Normal"},"subject":{"reference":"Patient/607155f4-44a9-4dde-a429-5102a80b4420"},"participant":[{"individual":{"reference":"Practitioner/49b72a3d-44cd-4a74-9459-4dc9f6b543fa"}}],"period":{"start":"2024-04-11T14:04:59+03:00","end":"2024-04-11T14:04:59+03:00"},"reasonCode":[{"coding":[{"system":"http://smartregsiter.org/","code":"client_registration","display":"Client Registration"}],"text":"Client Registration"}],"location":[{"status":"completed","period":{"start":"2024-04-11T14:04:59+03:00","end":"2024-04-11T14:04:59+03:00"}}],"serviceProvider":{"reference":"Organization/41eae946-bdc4-4179-b404-6503ff12f59c"}}