Tasks not displaying for some patients

hello community

We are running into an issue where tasks for some patients are not displaying in the tasks tab yet the task configuration looks good for example with the current task config below triggered triggered 14 days after the patient missing on the their next appointment date displayed in the condition cards .

{
    name: 'no-contact-enrol',
    title: 'Refer to B2C',
    appliesTo: 'reports',
    appliesToType: ['enroll', 'enroll_with_barcode'],
    appliesIf: (contact, report ) => {
       console.log('contact',contact);
       if (!['enroll', 'enroll_with_barcode'].includes(report.form)) {
        return false;
      }
      return contact.contact.rapidpro && contact.contact.rapidpro.visit_date;
    },
    resolvedIf: (contact, report, event, dueDate) => {
      if (contact.contact.muted) {
        return true;
      }
      
      return Utils.isFormSubmittedInWindow(
        contact.reports,
        'day14_no_contact',
        Utils.addDate(dueDate, event.start).getTime(),
        Utils.addDate(dueDate, event.end + 1).getTime()
      ) ||

      Utils.getMostRecentReport(
        contact.reports,
        'tracing_outcome'
      );
    },
    events: [
      {
        dueDate: (event, contact) => {
          // console.log(contact);
          console.log('hey there',Utils.addDate(new Date(contact.contact.rapidpro.visit_date), 14));
          return Utils.addDate(new Date(contact.contact.rapidpro.visit_date), 14);
        },
        start: 3,
        end: 14
      }
    ],
    actions: [
      {
        form: 'day14_no_contact'
      }
    ]
  },

The task for the contact doc below from the DB failed to appear

{
  "_id": "e5927263-4b5c-97a9-cddfa8d98474",
  "_rev": "486-93e341c2f37a78737847837878378",
  "alt_phone": "",
  "consent": "yes",
  "filing_number": "FN765654",
  "first_name": "xxxxxxx",
  "geolocation": {
    "accuracy": 26.399999618530273,
    "altitude": 1040.9000244140625,
    "altitudeAccuracy": null,
    "heading": null,
    "latitude": -13.9912065,
    "longitude": 33.7753626,
    "speed": null
  },
 
  "language_preference": "xxxxxxx",
  "name": "xxxxxxxx",
  "national_id": "P1700015",
  "parent": {
    "_id": "49834he2b-6aab-404e-978e-7fc359346056a",
    "parent": {
      "_id": "e8b0f5ed-dff7-408b-8388238"
    }
  },
  "patient_id": "91078",
  "phone": "+265993664361",
  "randomization": "texting",
  "rapidpro": {
    "adherence": "true",
    "language": "",
    "optout": "false",
    "reminders": "true",
    "visit_date": "2024-10-03T00:00:00.000+00:00",
    "observation_date": "2024-04-04T11:31:57.000+00:00"
  },
  "rapidpro_uuid": "7fb489388384893889",
  "reported_date": 1646046518065,
  "role": "patient",
  "surname": "xxxxxxxxxx",
  "telegram_id": "",
  "type": "person",
  "art_status": "AliveOnART",
  "art_status_change_date": "2024-09-07T00:00:00.000Z",
  "last_visit": "2024-04-04T00:00:00.000Z",
  "InternalID": "12345",
  "new_national_patient_identifier": "YFKSBVN",
  "art_outcome": "On antiretrovirals"
}

yet i see

"rapidpro": {
    "adherence": "true",
    "language": "",
    "optout": "false",
    "reminders": "true",
    "visit_date": "2024-10-03T00:00:00.000+00:00",
    "observation_date": "2024-04-04T11:31:57.000+00:00"

which satisfies the appliesIf in the task configuration

    appliesIf: (contact, report ) => {
       console.log('contact',contact);
       if (!['enroll', 'enroll_with_barcode'].includes(report.form)) {
        return false;
      }
      return contact.contact.rapidpro && contact.contact.rapidpro.visit_date;
    },

cc @jkuester @binod

Hello team , could i be missing something here ?..

You can also query the tasks in CouchDB.

For example, if your source report/ document has"_id" : "abcde", you can find tasks triggered by it with this Mango query you can run in Fauxton:

{
   "selector": {
      "type": "task",
      "emission.actions": {
         "$elemMatch": {
            "content.source_id": "abcde"
         }
      }
   }
}

It might give you more insight into whether tasks were triggered, and if so, what was the state history. For example:

  "stateHistory": [
    {
      "state": "Ready",
      "timestamp": 1726987852432
    },
    {
      "state": "Failed",
      "timestamp": 1727601230459
    }
  ],
1 Like

thanks @binod … i have gotten the tasks history for one the contacts whose tasks have not not been appearing and noticed that stateHistory is Draft for all of them

"stateHistory": [
   {
    "state": "Draft",
    "timestamp": 1695828857696
   }
  ],

"state": "Draft"

yet the dates are already due ie

{
 "id": "task~org.couchdb.user:",
 "key": "owner-uuid",
 "value": null,
 "doc": {
  "_id": "task~org.couchdb.user:",
  "_rev": "4-5fffc1250de986a5f7e45615adff572d",
  "type": "task",
  "authoredOn": 1701253206530,
  "stateHistory": [
   {
    "state": "Draft",
    "timestamp": 1701253206530
   }
  ],
  "user": "org.couchdb.user:",
  "requester": "uuid",
  "owner": "uuid",
  "emission": {
   "_id": "c28d747f-c5c5-41d2-b87b-29b450cf57d4~0",
   "title": "Refer to B2C",
   "deleted": false,
   "resolved": null,
   "actions": [
    {
     "type": "report",
     "form": "day14_no_contact",
     "label": "Follow up",
     "content": {
      "source": "task",
      "source_id": "uuid"
     }
    }
   ],
   "contact": {
    "name": "name"
   },
   "dueDate": "2024-07-24",
   "startDate": "2024-07-24",
   "endDate": "2024-08-07",
   "forId": "9375cb18-ee37-401f-a1af-595d80405028"
  },
  "state": "Draft"
 }
}

@binod @jkuester any idea on why the state of the task is not changing from draft state to ready?

The only potential reasons I can think for this behaviour are:

  • the user not using the app
  • or, the device having incorrect date in the past

The tasks are created, updated, and resolved on the user’s device. If it is not used, there is no change.

2 Likes

Another thing you can check is database conflict.

If the task document has conflicts, it might fail to get updated. Please go through the above article to learn how to check for conflicts and resolve them.

Using the same credentials in multiple devices can increase the chance of conflicts.

For your task document, the URL for checking conflicts would be something like:
https://yourserver/medic/your_task_doc_id?conflicts=true

1 Like

@binod @jkuester received a list for the more patients who are not even in the contacts the task uses ie in the task config below when i log the contact , its not available yet in the DB the contact document is fully there

{
    name: 'no-contact-enrol',
    title: 'Refer to B2C',
    appliesTo: 'reports',
    appliesToType: ['enroll', 'enroll_with_barcode'],
    appliesIf: (contact) => {
      console.log(contact);
      return contact.contact.rapidpro && contact.contact.rapidpro.visit_date;
    },
    resolvedIf: (contact, report, event, dueDate) => {
      if (contact.contact.muted) {
        return true;
      }
      
      return Utils.isFormSubmittedInWindow(
        contact.reports,
        'day14_no_contact',
        Utils.addDate(dueDate, event.start).getTime(),
        Utils.addDate(dueDate, event.end + 1).getTime()
      ) ||

      Utils.getMostRecentReport(
        contact.reports,
        'tracing_outcome'
      );
    },
    events: [
      {
        dueDate: (event, contact) => {
          return Utils.addDate(new Date(contact.contact.rapidpro.visit_date), 14);
        },
        start: 8,
        end: 14
      }
    ],
    actions: [
      {
        form: 'day14_no_contact'
      }
    ]
  },

why does the contact miss in the task config yet its in the DB

hello @jkuester @binod any thoughts on this ?

Can you please share whether it was logged or not?

Please note that tasks are calculated only under certain conditions:

  • updated only after the data for their emitting contact changes or every 7 days
  • created in the database for any task due within the last 60 days
  • immutable once their state is “terminal” (Cancelled, Completed, Failed)

If your task is not recalculated, you won’t get anything on the log. To test, you can change something on the task configuration such as the task title to trigger task recalculation.

Also, it’s easier to debug if you follow these steps:

  1. Inside your appliesIf, add this statement debugger;. You can keep it after console.log(contact).
  2. Compile and upload with debug switch:
    cht compile-app-settings upload-app-settings -- --debug
  3. Log in as the offline user, keeping the Dev Tools open.
  4. Your task calculation (if running, if there are any changes) should pause and you can inspect more interactively.

You can learn more about the debugger statement here:

More about debugging Javascript in Chrome is here:

1 Like

thanks @binod …

I have noticed something about all the patients that are missing this task document . I checked in the task_by_contact database and for each of these patients there were old tasks documents for other old over due dates but still showing "state": "Draft" and "resolved": null, or "resolved": false, .
Could this be hindering the creation on new subsequent task documents for these patients ?

What are the task end dates?

thanks @binod , well some are dating back to 2023 and others for 2024

There are task start, due, and end dates.
If there are still tasks in Draft state with end dates in the past, the tasks are not being updated. Probably because of the reasons shared earlier:

or, devices not connected to the internet

1 Like

I also face this problem. Anyone answer in detail.

1 Like

thanks @binod makes sense , going to follow up with the team and will be sharing feedback here

1 Like