Generate task after a certain condition from the app form

I have a app form .i.e screening_form
In the screening form , there is a variable name “bp1_eq_bp2”
My requirement is , if “bp1_eq_bp2” =“yes” , then only the task should be generated.

module.exports = [
  {
    name: 'Hypertension Follow-Up Task',
    title: 'task.hypertension_followup',
    appliesTo: 'reports',
    appliesToType: [ 'screening_form' ],
    actions: [ { form: 'action_form' } ],
    events: [
      {
        id: 'hypertension-followup-1',
        days:3,
        start:2,
        end:2,
      },
      {
        id: 'hypertension-followup-2',
        days:14,
        start:2,
        end:2,
      }
    ],
    resolvedIf: function (contact, report, event, dueDate) {
      if (report.bp1_eq_bp2 === 'yes') {
        return Utils.isFormSubmittedInWindow(
          contact.reports,
          'screening_form',
          Utils.addDate(dueDate, -event.start).getTime(),
          Utils.addDate(dueDate, event.end + 1).getTime()
        );
      } else {
        return false;
      }
    }
  }
];

From the above code ;
The task is generated even if “bp1_eq_bp2” =“no” from the screening form

Hi @Sanjit7

The task configuration parameter which controls when tasks get created is appliesIf, you can find detailed explanations about this on the docs site: Understanding the parameters in the Task Schema | Community Health Toolkit
resolvedIf is only called after the task gets created. A true response from resolvedIf will clear the task, while a false response will keep it active.
The way your code works now, always returning false when “bp1_eq_bp2” === “no” means that your tasks will stay active for these reports, which is the opposite of what you’re looking for, as far as I understand.

Can you please try rewriting your task so that:

  • you use appliesIf to filter out reports that have report.bp1_eq_bp2 === 'no' (tasks.js | Community Health Toolkit)
  • you rewrite resolvedIf to complete your task when the desired report is submitted

Please let us know about your progress!

2 Likes
module.exports = [
  {
    name: 'FLHW Follow-Up Task',
    title: 'task.follow_up',
    appliesTo: 'reports',
    appliesToType: [ 'screening_form' ],
    actions: [ { form: 'follow_up' } ],
    events: [
      {
        id: 'hypertension-followup-1',
        days: 3,
        start: 2,
        end: 2,
      },
      {
        id: 'hypertension-followup-2',
        days: 14,
        start: 2,
        end: 2,
      },
    ],
    resolvedIf: function (contact, report, event, dueDate) {
      const isFCHV = contact.roles.includes('fchv');

      if (report.screening_form.hypertension_screening.bp1_eq_bp2 === 'yes' && isFCHV) {
        return Utils.isFormSubmittedInWindow(
          contact.reports,
          'screening_form',
          Utils.addDate(dueDate, -event.start).getTime(),
          Utils.addDate(dueDate, event.end + 1).getTime()
        );
      } else {
        return false;
      }
    },
  },
];

So, i have a code like this but still the task is created for every screening form we fill. and we are getting this error in flhw account but task is generated in fchv account.
image

You are still not using appliesIf as suggested above.

You might want to build your task configuration like this:

module.exports = [
  {
    name: 'FLHW Follow-Up Task',
    title: 'task.follow_up',
    appliesTo: 'reports',
    appliesToType: ['screening_form'],
    actions: [{ form: 'follow_up' }],
    events: [
      {
        id: 'hypertension-followup-1',
        days: 3,
        start: 2,
        end: 2,
      },
      {
        id: 'hypertension-followup-2',
        days: 14,
        start: 2,
        end: 2,
      },
    ],

    appliesIf: function (contact, report) {
      const isFCHVUser = cht.v1.hasPermissions(['can_view_tasks_group']);
      return isFCHVUser && Utils.getField(report, 'hypertension_screening.bp1_eq_bp2') === 'yes';
    }
  },
];

Apart from adding appliesIf, I have made other changes to your code:

  1. Identify whether the user is an FCHV by checking the permissions instead of contact.roles. Here, contact represents the patient contact, not the user. When using hasPermissions, please select the permission that is only given to the FCHV role. You can also define custom permissions in your app settings.
  2. Using Utils.getField() instead of accessing the report fields directly.
  3. Removing resolvedIf: Your task configuration should work fine without explicit resolvedIf because of the default resolvedIf behavior. This is optional.

so, our requirement is that we need to create the task only if bp1_eq_bp2, this is the variable defined in our form. and translation key for this variable is “report.screening_form.hypertension_screening.bp1_eq_bp2” And also we need to create a task for fchv only !!! To test it we are changing our date. But we are still getting the task for both FCHV and FLHW and also the task is generated even after selecting no in the form.

Hi @Sandip_Parajuli,

Please try the configuration above.

For this we have: Utils.getField(report, 'hypertension_screening.bp1_eq_bp2') === 'yes'.

For this, we have: isFCHVUser = cht.v1.hasPermissions(['can_view_tasks_group']);
From a copy of your old configuration that I have access to, this permission is only set for FCHVs. Please adjust it if it is no longer the case. If you don’t have any permission unique to FCHVs, you can add one in your base_settings.json file like this:

"permissions": {
    "is_fchv": [
      "fchv"
    ],

Later, you can check it in tasks.js like this:

const isFCHVUser = cht.v1.hasPermissions(['is_fchv']);

Please try changing the task name temporarily so that you can be sure that the configuration has been replaced successfully.

2 Likes

Thank you for your help, I tested it and it seems the issue is solved.

3 Likes