Access property from topmost hierarchy place in the lowest level place

Consider the following hierarchy structure:
NPO
Team Area
Indawo
Dwelling
Household
Household member

Is it possible to access the project property of the NPO on both the household and household member items without using a data duplication approach, or moving the question to a lower level?
The use case is that we only want to show certain app forms if the NPO has agreed to participate on that project. Those forms should then become a required task to complete on households and household members.
The requirement must be satisfied when logging in as a chw which is a direct child of the indawo level - thus visually we only see from Indawo down.

Hi @Anro

Currently, offline user accounts (chws for example) do not have access to any contact that is higher than them in the hierarchy, so you would need this data duplication unfortunately. We do have an open issue to add this functionality, but it has not been prioritized yet: Make facility lineage available for reading to offline users · Issue #7570 · medic/cht-core · GitHub

1 Like

Hi @diana

Thank you for getting back to me regarding this.
We opted to move the projects value to the Indawo level for the intirim.

With regards to showing/hiding app forms based on this property.
Is it possible to do so in the <app_form_name>.properties.json file in the expression string?
And if so, could you please provide an example?

At the moment I only have access to the contact the app form is actioned on, not the parent.parent of that contact.
Eg: "expression": "contact.contact_type === 'hhm'"

Edit:
@jkuester I don’t know if you perhaps have any thoughts on this?

hi @Anro

The properties that you have access to in the form properties expression are:

  • contact - the contact document
  • user - the contact document that the logged in user is associated with
  • summary - the result of evaluating your contact summary script, when passed the contact.

The contact summary script will receive parents for a given contact (through the lineage property) if they are available on the local device. Can you try leveraging that?

1 Like

Hi @diana

Thank you for getting back to me regarding this and providing an explanation of the available properties.

That’s true, we currently use the contact and lineage in order to display various properties!
So the parent we’re interested in would be lineage[2] as from the bottom up it would be household -> dwelling -> indawo.
The full property reference being lineage[2].contact.project if I’m understanding correctly.

Though I am a tad bit unsure how to return this value we’re interested in.
Is it as simple as setting a variable equals to the reference mentioned above and then simply returning it in the module.exports = {...}, and then finally referencing it for consumption as summary.<returned_property_name>?

If my understanding horribly wrong, could I please trouble you for a very basic example for the solution mentioned?

Hi @Anro

Your understanding is correct. I encourage you to try updating your contact-summary to export a new value and then accessing that value in form properties.

Good Luck!

Hi @diana

Thank you for clarifying, I’ve unfortunately been unable to produce the expected results.

My contact-summary.templated.js:

const projects = thisContact.contact_type === 'household' ?
  lineage[1]['projects'] : thisContact.contact_type === 'hhm' ?
    lineage[2]['projects'] : undefined;
// Expose project specific checks for <form_name>.properties.json "expression" usage.
console.log(projects && projects.indexOf('C-SHARP') !== -1);
console.log(lineage);
const isCSHARPNode = true; // I've just statically set this to true for testing

...

module.exports = {
  cards,
  fields,
  isCSHARPNode,
};

The printout in the console confirms the checks and property value retrieval works as expected:

Though when I try to access the exposed value in the <form_name>.properties.json the form simply doesn’t show up:

{
    "title": [
      {
        "locale": "en",
        "content": "C-SHARP: Household Questionnaire"
      }
    ],
    "icon": "wcg-csharp-hh_questionnaire",
    "hidden_fields": [ ],
    "context": {
      "person": false,
      "place": true,
      "expression": "contact.contact_type === 'household' && summary.isCSHARPNode",
      "permission": "can_view_csharp_household_app_forms"
    }
  }

Just to note, everything did work as expected prior to adding && summary.isCSHARPNode.

Am I accessing the value incorrectly or is there something else missing?

---- Edit ----

After re-reading the documentation, I discovered I missed the wrapping context key in the export:

module.exports = {
  cards,
  fields,
  context: {
    isCSHARPNode,
  },
};

Everything now works as expected, thank you for all your help.

Note: Because our “projects” value is a list of items, and CHT stores space-separated values, an additional .split(' ') was also needed.

1 Like