Uploading Tasks breaks the contact page

Hi Everyone.

I was following the tutorial steps to Building a Simple Task when all of a sudden the list of contacts I had disappeared.

To confirm what happened and recreated it this is what I did

  1. I changed the location of my database in cht-couchdb.yml to a new task-database for testing
  2. With the new location I could create a brand new contact
  3. After confirming all was okay, I added this task in tasks.js
module.exports = [{
  name: 'assessment-after-registration',
  title: 'First Assessment',
  icon: 'icon-healthcare-assessment',
  appliesTo: 'contacts',
  appliesToType: ['patient'],
  appliesIf: c => user.parent && user.parent.contact_type === 'chw_area' && !c.contact.date_of_death && !c.contact.muted,
  actions: [{ form: 'assessment' }],
  events: [{
    start: 7,
    days: 7,
    end: 0,
  }],
}];
  1. I uploaded my settings successfully…

  2. After the upload, my contacts is now broken, on reloading the application I cannot see the contact from above.

I think the problem is related to this other forum post.

Based on the form post this is what I have tried to resolve the issue.

  1. This is my tasks.js
const {
  isFormArraySubmittedInWindow,
} = require('./nools-extras');

module.exports = [
  {
    name: 'assessment-after-registration',
    title: 'First Assessment',
    icon: 'icon-healthcare-assessment',
    appliesTo: 'contacts',
    appliesToType: ['patient'],
    appliesIf: (c) =>
      user.parent &&
      user.parent.contact_type === 'chw_area' &&
      !c.contact.date_of_death &&
      !c.contact.muted,
    actions: [{ form: 'assessment' }],
    events: [
      {
        id: 'assessment 1',
        start: 7,
        days: 7,
        end: 0,
      },
    ],
    resolvedIf: function (c, r, event, dueDate) {
      return isFormArraySubmittedInWindow(
        c.reports,
        ['assessment'],
        dueDate,
        event,
        null,
        r._id
      );
    },
  },
];
  1. This is my nools-extra.js
// @ts-nocheck
const getField = (report, fieldPath) =>
  ['fields', ...(fieldPath || '').split('.')].reduce((prev, fieldName) => {
    if (prev === undefined) {
      return undefined;
    }
    return prev[fieldName];
  }, report);
  
const isFormArraySubmittedInWindow = (
  reports,
  formArray,
  dueDate,
  event,
  count,
  sourceID
) => {
  let found = false;
  let reportCount = 0;

  const start = Utils.addDate(dueDate, -event.start).getTime();
  const end = Utils.addDate(dueDate, event.end + 1).getTime();

  reports.forEach(function (report) {
    if (formArray.includes(report.form)) {
      if (report.reported_date >= start && report.reported_date <= end) {
        if (sourceID) {
          if (getField(report, 'inputs.source_id') === sourceID) {
            found = true;

            if (count) {
              reportCount++;
            }
          }
        } else {
          found = true;

          if (count) {
            reportCount++;
          }
        }
      }
    }
  });

  if (count) {
    return reportCount >= count;
  }
  return found;
};

module.exports = {
  isFormArraySubmittedInWindow,
};

At this point,

  1. I still can’t add a person using the medic admin
  2. When I create a new person a new task is not created.

Hi @Ben_Kiarie ,
Can you post what’s logged in browser console while you’re at Contacts page ? Also, did you try logging in as offline user and check if tasks window opens without error or not?

Hi @yuv

This is the console, when logged in as medic admin user. There are no errors

I changed the database location again, created an offline user and this image shows everything is working as expected.

At this point I uploaded my settings to configure my tasks and I was still able to login as the offline user, and create contacts. But when a new person is created, I dont get a new task.