How can one view or query reports belonging to a particular contact in couch DB ?
Hey @cliff !
Can you give more context on the workflow your trying to support? Maybe this is this for an offline CHW user, an online user, for an API call to support an integration or something all together different?
thanks!
thanks @mrjones
i do have this task configuration below
{
name: 'no-contact-enrol',
title: 'Refer to B2C',
appliesTo: 'reports',
appliesToType: ['enroll', 'enroll_with_barcode'],
appliesIf: (contact, report ) => {
console.log('contact',contact);
if (!['enroll', 'enroll_with_barcode'].includes(report.form)) {
return false;
}
return contact.contact.rapidpro && contact.contact.rapidpro.visit_date;
},
resolvedIf: (contact, report, event, dueDate) => {
if (contact.contact.muted) {
return true;
}
return Utils.isFormSubmittedInWindow(
contact.reports,
'day14_no_contact',
Utils.addDate(dueDate, event.start).getTime(),
Utils.addDate(dueDate, event.end + 1).getTime()
) ||
Utils.getMostRecentReport(
contact.reports,
'tracing_outcome'
);
},
events: [
{
dueDate: (event, contact) => {
// console.log(contact);
console.log('hey there',Utils.addDate(new Date(contact.contact.rapidpro.visit_date), 14));
return Utils.addDate(new Date(contact.contact.rapidpro.visit_date), 14);
},
start: 10,
end: 14
}
],
actions: [
{
form: 'day14_no_contact'
}
]
},
which ideally i supposed to be triggered 14 days after the patient missing on the their next appointment date displayed in the condition cards .
We noticed that this task did not get triggered for about 8 patients some time back in september after they had missed turning up 14 days later from their next appointment date…
Though its working now for other patients , so wanted to investigate why it failed for those 8 patients by trying to retrieve reports for any of those 8 contacts in the DB
Ah - gotcha! thanks for the background.
Assuming you have the UUID for each of the 8 patients, you should be able to run a Mango Query in Fauxton. For example, I have a patient with UUID dcdc5f40-0b9c-4471-8363-51dc440e76a9
. On an example death report of theirs with ID 147ede50-9982-4e9c-8e21-84601ef3eeb7
with JSON of the death report at /_utils/#database/medic/147ede50-9982-4e9c-8e21-84601ef3eeb7
, I see this:
{
"_id": "147ede50-9982-4e9c-8e21-84601ef3eeb7",
"_rev": "2-9b84441024820f5ee3aa2b42c5422f80",
"form": "death_report",
"type": "data_record",
"content_type": "xml",
"reported_date": 1729098136985,
"contact": {
"_id": "dcdc5f40-0b9c-4471-8363-51dc440e76a9",
"parent": {
"_id": "6bbbfdbd-a798-44e2-aef3-c65f33e39b76"
}
},
...
By taking the path of contact._id
which is where the patient ID is stored in the report, we can use the Fauxton URL of /_utils/#database/medic/_find
to craft a Mango Query like this :
{
"selector": {
"contact._id": {
"$eq": "dcdc5f40-0b9c-4471-8363-51dc440e76a9"
}
}
}
Which returns the 2 reports for that patient.
Note that this same request can be automated with curl
if need be:
curl -s --header "Content-Type: application/json" --request POST --data '{"selector":{"contact._id":{"$eq":"dcdc5f40-0b9c-4471-8363-51dc440e76a9"}},"execution_stats":true,"limit":21,"skip":0}' https://medic:password@192-168-68-26.local-ip.medicmobile.org:10454/medic/_find | jq '.docs'
A Mango query is an option here, but, @mrjones, one tricky thing about your particular solution is that technically in the CHT data model the report.contact_id
field is not what associates a report with a particular patient. Instead reports are associated with contacts based on their “subject id”. The subject id of a report is the identifier found in any of these 4 fields in the report doc:
fields.patient_uuid
fields.place_uuid
fields.patient_id
fields.place_id
For a report to be associated with a contact, the app form should populate one of these fields with the identifying information for the contact. (patient_uuid
/place_uuid
would contain the _id
value for the contact while patient_id
/place_id
could be populated with the contact’s shortcode.)
With all that in mind, the reports_by_subject
view index in medic
database already contains a mapping of reports by their subject id. So, to get all the reports for a particular contact, you can simply query this view with the contact’s _id
value and their shortcode (if they have one):
{{url}}/medic/_design/medic-client/_view/reports_by_subject?keys=["8fd9fab7-f512-4952-979d-5ef36407771e", "5134523453"]
Querying an existing view index like this will also be much more efficient than running an ad hoc mango query.
Wonderful - thanks for the course correction @jkuester - Medic teammates and the community at large are learning here on the forums ; )
thanks @mrjones @jkuester for this , sorry for the delayed resposne …
I have tried out
cliff@cliffs-MacBook-Pro ~ % https://<domain-name>/_utils/#/database/medic/_design/medic-client/_view/reports_by_subject?keys=fbd20c79-634a-4184-970b-94b3f7c4e9c
zsh: no matches found: https://<domain-name>/_utils/#/database/medic/_design/medic-client/_view/reports_by_subject?keys=fbd20c79-634a-4184-970b-94b3f7c4e9c
cliff@cliffs-MacBook-Pro ~ % https://<domain-name>/_utils/#/database/medic/_design/medic-client/_view/reports_by_subject?keys=[fbd70c79-634a-4184-970b-944b3f7c4e9c]
zsh: no matches found: https://<domain-name>/_utils/#/database/medic/_design/medic-client/_view/reports_by_subject?keys=[fbd70c79-634a-4184-970b-944b3f7c4e9c]
cliff@cliffs-MacBook-Pro ~ %
i get no matches found
. Secondly how do i determine the shortcode of a contact , i see though the docs_by_shortcode
view
Hi @cliff
You will need to quote the keys value and not use a fauxton URL:
https://<domain-name>/medic/_design/medic-client/_view/reports_by_subject?keys=["fbd70c79-634a-4184-970b-944b3f7c4e9c"]
If you wish to use Fauxton, your query will look like:
If you are still not getting matches, it means that your reports don’t carry the patient_uuid field, so the view does not index them with that field, and you will probably need to use the contact patient_id
to search. patient_id
is also the shortcode that you mentioned, but you don’t need to use shortcode view, reports_by_subject
indexes by shortcode as well.
@cliff - Looking closely at your sample call
cliff@cliffs-MacBook-Pro ~ % https://<domain-name>/_utils/#/database/medic/_design/medic-client/_view/reports_by_subject?keys=[fbd70c79-634a-4184-970b-944b3f7c4e9c]
zsh: no matches found: https://<domain-name>/_utils/#/database/medic/_design/medic-client/_view/reports_by_subject?keys=[fbd70c79-634a-4184-970b-944b3f7c4e9c]
I wasn’t sure if that was an example or the literal call. To be safe, let’s assume it’s the literal call. If yes, then there’s a few corrections we need to make to follow @diana’s advice:
- since you’re using
zsh
be sure to put the whole URL in single quotes. This will escape the?
character (among others) and should avoid thezsh: no matches found
error - preface the call with
curl
. I didn’t see this in your command so I wanted to be sure it’s there - include the actual IP or URL of your instance, and not
<domain-name>
- pass in a username and password after the
https://
- finally, you’ll need to turn off of curl’s glob pattern by adding a
-g
to the curl call. while we’re there, we can adds
to silent upcurl
For example, my host URL and port is 192-168-68-235.local-ip.medicmobile.org:10473
. My username is medic
and my password is password
. The contact ID I want is f83c41fa-780f-44f0-a01b-e68d06c39683
.
My final, full curl
command looks like this:
curl -sg 'https://medic:password@192-168-68-235.local-ip.medicmobile.org:10473/medic/_design/medic-client/_view/reports_by_subject?keys=["f83c41fa-780f-44f0-a01b-e68d06c39683"]'
This gives me back this JSON:
{
"total_rows": 2,
"offset": 1,
"rows": [
{
"id": "60fa9b75-1c97-4cbf-91ff-ff0ecad4ccb2",
"key": "f83c41fa-780f-44f0-a01b-e68d06c39683",
"value": 1728055841081
}
]
}
âžś
thanks @diana the fauxton has worked , added the key and run the query
oh yeah sure @mrjones, had not added the whole URL in single quotes, makes sense