Having a if-else statement on tasks.js


We have this kind of issue/concern with the tasks.js,

we have this table that will gonna trigger when the age of the infant (on the left side of the image)


and then the event days will gonna be customized by its different days will show, due, etc.

And as of now, I am trying to modify the generateEventsBaseOnDOB, which is this one,

const generateEventsBasedOnDOB = (eventId, start, end, day) => ({
    id: eventId,
    dueDate: function (event, contact) {
        return DateTime.fromISO(getDOBISO(contact.contact)).plus({ days: day }).toJSDate();

So, when I want to add if-else statement here or inside the tasks formatted code, it doesn’t allow me to do that.

What do you think I can do on this one?

doing it tasks one by one per age range is impossible on our end because we already a tasks for it and we cannot change the tasks name for this one, we will just want to generateEvents based on age range.

Hi @Marcelo_De_Guzman

Can you please share how you add your if else statement and what happens when you do? “doesn’t allow” isn’t clear for me. Are you getting an error?


const generateEventsBasedOnDOB014 = () => {
    const age_in_days = Math.abs(DateTime.fromISO(getDOBISO(contact)).diffNow('days'));

    if (age_in_days >=0 && age_in_days<14){
        return { id:'cmami-screening-d14', start:7, end: 14, days: 14}}

This is the error I’ve got.

ERROR Failed to parse file C:\flourish-master\tasks.js. ReferenceError: contact is not defined 
ERROR Error: Declarative configuration schema validation errors
    at module.exports (C:\Users\Test\AppData\Roaming\npm\node_modules\medic-conf\src\lib\validate-declarative-schema.js:190:11)
    at compileDeclarativeFiles (C:\Users\Test\AppData\Roaming\npm\node_modules\medic-conf\src\lib\compile-nools-rules.js:45:3)
    at compileNoolsRules (C:\Users\Test\AppData\Roaming\npm\node_modules\medic-conf\src\lib\compile-nools-rules.js:30:12)
    at compileAppSettingsForProject (C:\Users\Test\AppData\Roaming\npm\node_modules\medic-conf\src\fn\compile-app-settings.js:45:18)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async Object.compileAppSettings [as execute] (C:\Users\Test\AppData\Roaming\npm\node_modules\medic-conf\src\fn\compile-app-settings.js:22:19)
    at async module.exports (C:\Users\Test\AppData\Roaming\npm\node_modules\medic-conf\src\lib\main.js:187:5)
    at async C:\Users\Test\AppData\Roaming\npm\node_modules\medic-conf\src\bin\medic-conf.js:11:18

Hi @Marcelo_De_Guzman

I believe that your problem comes from the fact that your function references contact:


when it’s not defined. In the previous example, you’re referencing contact in the dueDate function, which actually receives it as a parameter.
So it’s not the if statement which is a problem here.

I think you need to refactor your solution a bit and find a way to add this condition in a function that actually receives the contact somehow.

Is this possible in the events on the tasks.js ?

events: [
   if (getAgeInDays(contact.contact) >=3 && getAgeInDays(contact.contact)<14 {
         // this is for row 3 which is age start was 3 days old to 14 days old
   .... *and so on*
const generateEventsBasedOnDOB=(eventId, start, end, day) => ({
    id: eventId,
   dueDate: function (event, contact) {
          return DateTime.fromISO(getDOBISO(contact.contact)).plus({days: day}).toJSDate();

and on the nools-extras.js

function getDOBISO(contact){
   return contact && contact.date_of_birth;

Hi @Marcelo_De_Guzman

The events property needs to be an array, so adding code there is not javascript syntax compliant.
You’ll need to generate the events differently.

An alternative is to create different task configs. Have on task for a contact that is under two weeks old that has these additional events? Would that work?

Hi @diana ,

I think the best way to do this is by doing it by different tasks.

I ask also my data processing team and it says that it was still okay if the tasks have more than 1, it should be having only an identifyer to summarize all the same task.