Error fetching tasks

Hi team,
Any insights why the following task definition throws the following error:
const {isAlive, getField } = require(‘./contact-summary-extras’);

module.exports = [
{
name: ‘htn.linkage’,
title: ‘task.htn_followup’,
appliesTo: ‘reports’,
appliesToType: [‘reach_htn’],
appliesIf: function(contact, report) {
return getField(report, ‘eligible_shtn’) === ‘1’ && isAlive(contact);
},
actions:[{form: ‘care_htn’}],
events: [
{
id: ‘htn-followup’,
days:7,
start:2,
end:2
}
]
}
];

Error
Error getting tasks for all contacts Error: Invalid rule definition
Thank you.

@ewafula - I did a light analysis of your code and the first issue was that my IDE didn’t like the use of and . On the off chance that it’s an easy fix, can you try the code with just 's?

Here it is with them replaced for you to try with:

const {isAlive, getField } = require('./contact-summary-extras');
module.exports = [
  {
    name: 'htn.linkage',
    title: 'task.htn_followup',
    appliesTo: 'reports',
    appliesToType: ['reach_htn'],
    appliesIf: function(contact, report) {
      return getField(report, 'eligible_shtn') === '1' && isAlive(contact);
    },
    actions:[{form: 'care_htn'}],
    events: [{
      id: 'htn-followup',
      days:7,
      start:2,
      end:2
    }]
  }
];
1 Like

Thank you @mrjones , The major issue here is why the appliesIf function is not being resolved properly. I’ve observed that when I drop the appliesIf function the task is loaded properly. Any guidance why the function is failing.
Thank you.
Erick

Ah - that’s helpful to know! I wonder if what the value of the eligible_shtn field is here?

getField(report, 'eligible_shtn') === '1'

Instead of checking if it is the literal string '1' with the triple equals, is it valid to check your form’s field for a truthy value? I noticed the truthy use in an example and thought it might help you.

Hi @mrjones ,
I’ve tried using a truthy value but for reason . It is not working. below id the complete code and the error as well.
The task code snippet:
const {getField } = require(‘./contact-summary-extras’);

module.exports = [
{
name: ‘htn.linkage’,
title: ‘task.htn_followup’,
appliesTo: ‘reports’,
appliesToType: [‘reach_htn’],
appliesIf: function (contact, report) {

  return getField(report, 'eligible_shtn'); 
},
actions:[{form: 'care_htn'}],
events: [{
  id: 'htn-followup',
  days:7,
  start:2,
  end:2
}]

}
];

The Error

Error getting tasks for all contacts Error: Invalid rule definition
Unable to match { in { when { c: Contact } then { !function(e){var t={};function n(r){if(t[r])return t[r].exports;var i=t[r]={i:r,l:!1,exports:{}};return e[r].call(i.exports,i,i.exports,n),i.l=!0,i.exports}n.m=e,n.c=t,n.d=function(e,t,r){n.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:r})},n.r=function(e){‘undefined’!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:‘Module’}),Object.defineProperty(e,‘__esModule’,{value:!0})},n.t=function(e,t){if(1&t&&(e=n(e)),8&t)return e;if(4&t&&‘object’==typeof e&&e&&e.__esModule)return e;var r=Object.create(null);if(n.r(r),Object.defineProperty(r,‘default’,{enumerable:!0,value:e}),2&t&&‘string’!=typeof e)for(var i in e)n.d(r,i,function(t){return e[t]}.bind(null,i));return r},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,‘a’,t),t},n.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},n.p=‘’,n(n.s=1)}([function(e,…

Hi @ewafula

I believe your issue is related to requiring contact-summary-extras in task code.
My guess is that the error is caused by that file requiring an external library, moment, to process dates. It’s a known issue that task code cannot use moment right now: compile-app-settings - Nools can't process scripts which `require('moment')` · Issue #225 · medic/cht-conf · GitHub

Luckily, we have an identical function to getField available in another library, nools-extras that you can use safely. Can you please try changing your code to:


const { getField } = require('./nools-extras');
.... <no changes are required in the task definition>

Let us know how that goes.

1 Like

Hi @diana,
You are right, I’ve changed to nools-extras and that works fine.
Thank you.
Erick

Thanks @diana ! I’d compared @ewafula 's work to our sample tasks in the default config, but didn’t see until you pointed it out that we use require('./nools-extras') there as you suggested. Good to remember for next time!