Pulling report data for a submitted form

Hello @diana

Would like to inquire oh how can one pull data from a form data has been submitted forexample i did pull all the docs in CHT instance as seen below

cliff@cliff-Latitude-5580:~$  curl https://medic:password@192-168-138-70.my.local-ip.co/medic/_all_docs
{"total_rows":51,"offset":0,"rows":[
{"id":"4ddf391d-b850-4e08-bada-57284b32d1bd","key":"4ddf391d-b850-4e08-bada-57284b32d1bd","value":{"rev":"2-ab2db6deeda3e2d7d6b5fddaf9f61f02"}},
{"id":"6b770877-3c8e-4100-aafc-a74ebd21ec78","key":"6b770877-3c8e-4100-aafc-a74ebd21ec78","value":{"rev":"2-5f900451a1ad9d5c017556846f336571"}},
{"id":"_design/medic","key":"_design/medic","value":{"rev":"1-8c795c53ee2f1c5446a9d49b13ffd259"}},
{"id":"_design/medic-admin","key":"_design/medic-admin","value":{"rev":"1-38489089baa73a423aedef67b95b9b7f"}},
{"id":"_design/medic-client","key":"_design/medic-client","value":{"rev":"2-d60abe859da71f06455336ab97f3f292"}},
{"id":"_design/medic-conflicts","key":"_design/medic-conflicts","value":{"rev":"1-1b58225c3445e1d231744709a8da4beb"}},
{"id":"_design/medic-scripts","key":"_design/medic-scripts","value":{"rev":"1-acaab43b66da75cd754508d36a9530b4"}},
{"id":"_design/medic-sms","key":"_design/medic-sms","value":{"rev":"1-a3bc44079a73015423ca52325107ceac"}},
{"id":"a00cc3f0-196c-4477-a9a1-c2c04dd3f7f4","key":"a00cc3f0-196c-4477-a9a1-c2c04dd3f7f4","value":{"rev":"3-f798541464d800d5ec1f71ecb9609dbf"}},
{"id":"af6a5cb8-f45b-46e4-aa06-0686b4d17458","key":"af6a5cb8-f45b-46e4-aa06-0686b4d17458","value":{"rev":"2-556228229dbb7c841b105b7c8a8abbe1"}},
{"id":"branding","key":"branding","value":{"rev":"1-db13aa8a83fca924eb5793ab5d01d74b"}},
{"id":"form:care","key":"form:care","value":{"rev":"2-3b276a3f543e9851a33589b0b9dc8d74"}},
{"id":"form:contact:clinic:create","key":"form:contact:clinic:create","value":{"rev":"4-75e0d6b27b09de22e3b85c9855333afa"}},
{"id":"form:contact:clinic:edit","key":"form:contact:clinic:edit","value":{"rev":"2-c1c955042db963a95470bdf891bcef6f"}},
{"id":"form:contact:district_hospital:create","key":"form:contact:district_hospital:create","value":{"rev":"2-42b8b579b38ca6c2978c238e7d9c78fe"}},
{"id":"form:contact:district_hospital:edit","key":"form:contact:district_hospital:edit","value":{"rev":"2-4360bb0eb6f6ff863849a07adac7e851"}},
{"id":"form:contact:health_center:create","key":"form:contact:health_center:create","value":{"rev":"2-c9b86820479f211c8e505eba150c5203"}},
{"id":"form:contact:health_center:edit","key":"form:contact:health_center:edit","value":{"rev":"2-19b3da5f9f146be07ec1854a372e374b"}},
{"id":"form:contact:person:create","key":"form:contact:person:create","value":{"rev":"2-230d39c41682da720b83e4909b138c9b"}},
{"id":"form:contact:person:edit","key":"form:contact:person:edit","value":{"rev":"2-a5b769f3a462172bed6ef4f2227beb41"}},
{"id":"form:death_report","key":"form:death_report","value":{"rev":"2-97cfa3cc24ee743dfea03c019d644bec"}},
{"id":"form:delivery","key":"form:delivery","value":{"rev":"2-adf37c0da7d5e58c8e4d27fc940f70e6"}},
{"id":"form:pnc_danger_sign_follow_up_baby","key":"form:pnc_danger_sign_follow_up_baby","value":{"rev":"2-12e8989f6774828bb10aa4b675bc43ff"}},
{"id":"form:pnc_danger_sign_follow_up_mother","key":"form:pnc_danger_sign_follow_up_mother","value":{"rev":"2-62f58b6e0ade10d70f15df73ddb6a025"}},
{"id":"form:pregnancy","key":"form:pregnancy","value":{"rev":"2-4fa3db96cae638c4cb041125c9970496"}},
{"id":"form:pregnancy_danger_sign","key":"form:pregnancy_danger_sign","value":{"rev":"2-49758bfd894fb5db6e719bff864f9da9"}},
{"id":"form:pregnancy_danger_sign_follow_up","key":"form:pregnancy_danger_sign_follow_up","value":{"rev":"2-a9a2b6800d6f93e151a3a63359620976"}},
{"id":"form:pregnancy_facility_visit_reminder","key":"form:pregnancy_facility_visit_reminder","value":{"rev":"2-0e3deaf22966166d05bcf803615499c8"}},
{"id":"form:pregnancy_home_visit","key":"form:pregnancy_home_visit","value":{"rev":"2-283eb323e61a7fc756bda61f9d9be382"}},
{"id":"form:undo_death_report","key":"form:undo_death_report","value":{"rev":"2-3a4ca8d678f0338d7044cb1bc35d103d"}},
{"id":"horti-upgrade____13-e4157f81f9950ed739623f4230fbe164____tombstone","key":"horti-upgrade____13-e4157f81f9950ed739623f4230fbe164____tombstone","value":{"rev":"1-e17cab2673f4642db435007320a78d1c"}},
{"id":"messages-bm","key":"messages-bm","value":{"rev":"1-b4b1e121ddf4bbacb09f9ae245fd7d96"}},
{"id":"messages-en","key":"messages-en","value":{"rev":"2-e6c8593d69afef39819ac86d20f88f50"}},
{"id":"messages-es","key":"messages-es","value":{"rev":"1-d38e8afcdd95903d99ad826d59f34c24"}},
{"id":"messages-fr","key":"messages-fr","value":{"rev":"1-d92c7c6b5e939034b1da3f2a9eaa3f27"}},
{"id":"messages-hi","key":"messages-hi","value":{"rev":"1-b6bd36565442fb75def6207f17a04810"}},
{"id":"messages-id","key":"messages-id","value":{"rev":"1-b40329d30324c92ddd02a3bbfd0ad87d"}},
{"id":"messages-ne","key":"messages-ne","value":{"rev":"1-075e81ec5813354e6d0448a0b075b442"}},
{"id":"messages-sw","key":"messages-sw","value":{"rev":"1-ea9ad41c43f59e8c0b00fe68316f313d"}},
{"id":"migration-log","key":"migration-log","value":{"rev":"29-d4032350bd407b839dffd9ccf9aeda7c"}},
{"id":"org.couchdb.user:admin","key":"org.couchdb.user:admin","value":{"rev":"2-6fbe8767752ef71e1c4322c5996588b4"}},
{"id":"org.couchdb.user:git","key":"org.couchdb.user:git","value":{"rev":"2-94483b2c6e2a91e18fd9c9d4faa56a3e"}},
{"id":"org.couchdb.user:horticulturalist","key":"org.couchdb.user:horticulturalist","value":{"rev":"2-a992c018de24ad98e04b137760cddab6"}},
{"id":"org.couchdb.user:medic","key":"org.couchdb.user:medic","value":{"rev":"4-05b9a43c9721e4ed069676d9bff9d3c0"}},
{"id":"org.couchdb.user:medic-api","key":"org.couchdb.user:medic-api","value":{"rev":"2-c00baed13bb97410d5005685bd92899b"}},
{"id":"org.couchdb.user:medic-couch2pg","key":"org.couchdb.user:medic-couch2pg","value":{"rev":"2-45f4075bce134d0d66083126d82c5c6e"}},
{"id":"org.couchdb.user:medic-sentinel","key":"org.couchdb.user:medic-sentinel","value":{"rev":"2-10f3cae3adec3a71e74c8cf69d6a1b30"}},
{"id":"resources","key":"resources","value":{"rev":"2-829966961db41474c748a7af216a22ed"}},
{"id":"service-worker-meta","key":"service-worker-meta","value":{"rev":"1-d79fee6b9d89d2000d95b92e094fa495"}},
{"id":"settings","key":"settings","value":{"rev":"2-0e5460c439f310bfcb15a7c3378a5bf9"}},
{"id":"target~2022-03~4ddf391d-b850-4e08-bada-57284b32d1bd~org.couchdb.user:git","key":"target~2022-03~4ddf391d-b850-4e08-bada-57284b32d1bd~org.couchdb.user:git","value":{"rev":"4-a7c7173d36e96b53da2e29b47c14108a"}}
]}

And am interest in how can i get info for a particular form ie for {"id":"form:care","key":"form:care","value":{"rev":"2-3b276a3f543e9851a33589b0b9dc8d74"}},

Thanks

Hi @cliff

Since you’re using CouchDb APIs, I think the best place to get information is the CouchDb documentation website.
If you want to get information about a specific doc, use:

https://medic:password@192-168-138-70.my.local-ip.co/medic/<doc_id>
1 Like

thanks @diana for the response above

I am building a condition card to pull data of the total score from a report and display it on a patient profile …


From the screen shot above i have a column name variable called score_note in my app form that i want to pull the data from and use it as the value in card configuration code as seen below. The form name is care

{
    label: 'risk.status',
     appliesToType: 'person',
    appliesIf: allReports.form === 'care',
    fields: [
      {
        label: 'risk.category',
        value: function(allReports) { return allReports.fields.score_note; },
        width: 6
      }
    ],
  },

However the card doesn’t display on compiling the coed ,any pointers ?

Hi @cliff, there are a couple aspects that need attention in that condition card:

  • You want to be going through reports, not the person’s data fields. To do that make sure appliesToType is set to report, as in appliesToType: 'report'
  • Conditions in the appliesIf must be in a function, as in this example from the default config.
  • If you are using allReports similarly to the default config, then keep in mind that it is an array of all reports for that contact, and you can’t access the fields directly. You can go through the individual reports manually, but given what you are trying to do here you could use the single form submission in report instead: report.fields.score_note, or using the getField function: getField(report, 'score_note');

Hope this helps!

2 Likes

thanks @marc

{
    label: 'risk.status',
     appliesToType: 'report',
     appliesIf: function (report, contact) {
       if (contact.type !== 'person') { return false; }
       if (report.form === 'care') { return true; }
       return false;
     },

       fields: [
      {
        label: 'risk.category',
        value : function(report) {
          var edd =  report.fields.score_note;
          return edd;
        },
        width: 6
      }
    ]

  },

i have used the above configurations based on the recommendations above though still the card is not appearance in CHT contact profile

Hi Cliff, that’s looking better. Can explain how you debugged the code? Are there any errors in your console?

An approach I usually take is to reduce the code to basic parts and ensure it works, then gradually build in the more complex parts. That way you can more clearly see what is working, and identify the aspect that breaks. For condition cards that generally means starting with a simple card with no condition and static data (value: 'random string'). If that works you can then add the condition to make sure it appears only when expected. Once you have that you can add the code to extract the necessary data, and then work on presenting it properly. When a part is not working as intended you can inspect the values to make sure they are what you’d expect. For contact summary this could be done by outputting values to the console with console.log(value).

In your case, given that the card is not showing I would suspect that the appliesIf function is behaving differently than you’d expect. It would be worthwhile to check the values passed to your function, since you might find that one of the values is unexpectedly undefined. If that’s the case, you might find this example helpful, especially since the function signature is different. You could follow the same pattern and use the globally available contact, indirectly used as thisContact in the example.

3 Likes

thanks @marc

The card appears under each person profile when i use appliesToType: 'person' with a value ie value: 'hey CHT' in the fields array object for the card

I have switched to the globally availbale contact defined above as const thisContact = contact;

{
    label: 'risk.status',
     appliesToType: 'report',
     appliesIf: function (report){
    
        if (thisContact.type !== 'person') { return false; }
        if (report.form === 'care') { return true; }
    
     return false;
    },

       fields: [
      {
        label: 'risk.category',
        value : function(report) {
          var edd =  report.fields.score_note;
          return edd;
        },
        width: 6
      }
    ]
 

  },

Looks like if the card can only appear on a coantact profile when the value of appliesToType is set to person ,for appliesToType: 'report' it creates a report card as stated here ,not sure if this report card appears on the contacts profile or not

the tricky bit here is how to fetch the data from the app report fields and display it in a report card to appear on the contact’s profile , i have changed the configurations like below

const isReport = (r) => (r.reported_date !== null);

{
    label: 'risk.status',
     appliesToType: ['report'],
     appliesIf: isReport, 
     fields: function(report){
      const fields = [];
      let xxx = '';
      xxx = getField(report, 'score_note');
      fields.push(
        { label: 'risk.category:', value: xxx, width: 6 });
      return fields;
    }

  },

But haven’t be yet succeeded on this ,
@marc @diana @mrjones Any thoughts would be highly appreciated

It looks like your card is at least showing up, which is a good start! What value do you see with the code above? Are there any errors in your console?

thanks @marc
yeah its showing if appliesToType: ['person'] but disappears with appliesToType: ['report']

No errors in the console

cliff@cliff-Latitude-5580:~/Documents/project/medic/mydhi-cht-config/config$ cht --url=https://medic:password@192-168-138-70.my.local-ip.co compile-app-settings upload-app-settings
INFO Checking that https://medic:****@192-168-138-70.my.local-ip.co/medic/ is available... 
INFO Processing config in config. 
INFO Actions:
     - compile-app-settings
     - upload-app-settings 
INFO Starting action: compile-app-settings… 
WARN app_settings.json file should not be edited directly.
    Please create a base_settings.json file in app_settings folder and move any manually defined configurations there. 
INFO Packaging contact-summary 
INFO Hash: 5126bd4ef37272cf946b
Version: webpack 4.46.0
Child
    Hash: 5126bd4ef37272cf946b
    Time: 844ms
    Built at: 03/29/2022 6:57:20 PM
                   Asset      Size  Chunks             Chunk Names
    ./contact-summary.js  16.7 KiB       0  [emitted]  main
    Entrypoint main = ./contact-summary.js
    [0] /usr/lib/node_modules/cht-conf/src/contact-summary/lib.js 203 bytes {0} [built]
    [1] ./contact-summary.templated.js 17.1 KiB {0} [built]
    [2] ./contact-summary-extras.js 13.5 KiB {0} [built]
    [3] /usr/lib/node_modules/cht-conf/src/contact-summary/contact-summary-emitter.js 3.41 KiB {0} [built] 
INFO Packaging nools 
INFO Hash: f14e9bc4badd3365e6d6
Version: webpack 4.46.0
Child
    Hash: f14e9bc4badd3365e6d6
    Time: 460ms
    Built at: 03/29/2022 6:57:20 PM
         Asset      Size  Chunks             Chunk Names
    ./nools.js  27.5 KiB       0  [emitted]  main
    Entrypoint main = ./nools.js
    [0] ./nools-extras.js 7.57 KiB {0} [built]
    [1] /usr/lib/node_modules/cht-conf/src/nools/definition-preparation.js 1.36 KiB {0} [built]
    [2] /usr/lib/node_modules/cht-conf/src/nools/lib.js 328 bytes {0} [built]
    [3] ./tasks.js 14 KiB {0} [built]
    [4] ./targets.js 18 KiB {0} [built]
    [5] /usr/lib/node_modules/cht-conf/src/nools/task-emitter.js 5.24 KiB {0} [built]
    [6] /usr/lib/node_modules/cht-conf/src/nools/task-defaults.js 1.32 KiB {0} [built]
    [7] /usr/lib/node_modules/cht-conf/src/nools/target-emitter.js 2.77 KiB {0} [built] 
INFO compile-app-settings complete. 
INFO Starting action: upload-app-settings… 
INFO Settings updated successfully 
INFO upload-app-settings complete. 
INFO All actions completed. 
cliff@cliff-Latitude-5580:~/Documents/project/medic/mydhi-cht-config/config$ appliesToType: ['report']

this is how the data looks like when pulled from the DB about the care form
{"id":"20e108b7-5b74-4bf9-9b9b-d64cdc12e8e1","key":"20e108b7-5b74-4bf9-9b9b-d64cdc12e8e1","value":{"rev":"2-2090ce698a9f7c4e54d65795a1a72798"},"doc":{"_id":"20e108b7-5b74-4bf9-9b9b-d64cdc12e8e1","_rev":"2-2090ce698a9f7c4e54d65795a1a72798","form":"care","type":"data_record","content_type":"xml","reported_date":1648468798532,"contact":{"_id":"4ddf391d-b850-4e08-bada-57284b32d1bd","parent":{"_id":"a00cc3f0-196c-4477-a9a1-c2c04dd3f7f4"}},"from":"+256705475234","hidden_fields":["meta"],"fields":{"inputs":"","contact":{"_id":"b4ae429f-c321-4d4d-a5d7-63a5217c9642","patient_id":"","name":"ggg"},"care":{"medical":"fifty","knowledge":"none","adherence":"rarely","mental":"failed_treatment","transportation":"tr","support":"only","communication":"xx","employment":"unemployed","living":"homless","score":"31","score_note":"","category":""},"meta":{"instanceID":"uuid:a5686b67-48bd-40c7-9dc9-67051d810229","deprecatedID":""}},"geolocation":{"latitude":0.3407872,"longitude":32.5910528,"altitude":null,"accuracy":5378.323948523854,"altitudeAccuracy":null,"heading":null,"speed":null},"_attachments":{"content":{"content_type":"application/xml","revpos":1,"digest":"md5-xbSTavJykVpwsrqsL4vZBA==","length":968,"stub":true}}}},

Ah, I had meant the developer console in the browser. That’s where you would see runtime errors or output that you put in your contact summary code. I tested something similar to what you have with appliesToType: ['report'] and it is working as expected.

Given that it doesn’t run on your side you’ll need to dig into your appliesIf function to see what reports it is looking at, if any. I would suggest using a console.log(r) statement in your appliesIf function to get a better sense of what is going on.

1 Like

thanks @marc, could you please share that piece of code here

        appliesIf: function(r){
        console.log(r);
        console.log('xxxxxxxxxxxxxxxxxxxxxxxxxxx');
         return r.form === 'care';
      },

i have applied the logging to the console in the appliesIf function but for some reason the values are not being logged into the console

i have applied the logging to the console in the appliesIf function but for some reason the values are not being logged into the console

That’s a good finding! Do you have any forms submitted for this contact? In the screenshot I don’t see any for the previous 3 months… are there any at all? If not, then that is expected, as a card will only show when a report meets the appliesIf condition.

@marc i dont have any reports submitted for this contact except for the care form i filled out and submitted …

pardon on what is expected to happen here

Your right, i have set the condition that if the report has care as the value for r.form === 'care' then the card should appear

appliesIf: function(r){
      console.log(r);
      console.log('xxxxxxxxxxxxxxxxxxxxxxxxxxx');
       return r.form === 'care';
    },

Are you trying to imply that for the card to appear there must be seen a report submitted under


from which submitted report the data is pull from to generate the info in the card ?

Yes, that’s correct. Cards with appliesToType: ['report'] can only show if a report is submitted, and the appliesIf condition is true.

Conversely, cards that are defined for the contact type can show up for that contact whether or not a report has been submitted, assuming the type matches and appliesIf condition is true.

In the overview it says “Condition cards can be permanent or conditional; set to appear only when a specific type of report is submitted”, yet the reference documentation doesn’t make that as clear. Sorry that you struggled with this, and we’ll update the documentation so that others don’t have the same misunderstanding.

1 Like

thanks @marc makes sense now ,going to follow that …
may be if i may inquire on what makes a task


disappear from under Tasks when the form is submitted and comes under Reports as seen below?

This is what am trying to achieve but i dont see the Level of care assessment form task moving from the Tasks and coming under Reports when the form is submitted…
Is it achieved with the resolvedIf parameter of task configuration ?

A post was split to a new topic: Task is not resolving

thanks so much @marc ,this was the reason and now able to pull data from the submitted report

2 Likes