avniproject / apfodishanutrition

GNU Affero General Public License v3.0
0 stars 0 forks source link

APF Prod Bug fixes - Batch 4: CMAM-related question is not being asked to a SAM child who has opted for 'treatment at home' in the Growth Monitoring form #376

Closed AnjaliBhagabati closed 2 weeks ago

AnjaliBhagabati commented 1 month ago

For Beneficiary, Rosan Mahanta, Issues:

  1. Growth Monitoring should have been scheduled on 1st August as per current scheduling logic
  2. Since this is the 1st Growth Monitoring, the question "Is the child enrolled in the CMAM program?" show basis the condition given in the solution analysis sheet

Steps to reproduce:

  1. User ID : 9668786491@apfodisha
  2. Child Program
  3. Search Beneficiary Name: Rosan Mahanta
  4. Action:

Reference: https://drive.google.com/file/d/13mtMDt--8oWasZff85zaLfRLad0pGIPM/view?usp=drive_link

Tech notes

It seems the rule is failing for 1st Growth Monitoring, this field - "Is the child enrolled in the CMAM program?" should be displayed in the first encounter

adamsanadi6 commented 1 month ago

After debugging the issue, I discovered it was not a bug and closed the card.

adamsanadi6 commented 1 month ago

Old Logic:

'use strict';
({params, imports}) => {
    const programEncounter = params.entity;
    const moment = imports.moment;
    const formElement = params.formElement;
    const _ = imports.lodash;
    let visibility = true;
    let value = null;
    let answersToSkip = [];
    let validationErrors = [];

    const condition11 = new imports.rulesConfig.RuleCondition({programEncounter, formElement}).when.valueInEncounter("2f136d99-f500-4710-89fe-d8007c977843").containsAnswerConceptName("397f526d-9247-4b3b-bb09-27c4e21fd062").matches();
    let totalEnc=programEncounter.programEnrolment.getEncountersOfType('Growth Monitoring');
    let completedEnc= _.filter(totalEnc,enc=> enc.encounterDateTime != null)

  visibility = condition11 && (completedEnc.length % 4 == 0);

    return new imports.rulesConfig.FormElementStatus(formElement.uuid, visibility, value, answersToSkip, validationErrors);
};

New Logic:

'use strict';
({params, imports}) => {
    const programEncounter = params.entity;
    const moment = imports.moment;
    const formElement = params.formElement;
    const _ = imports.lodash;
    let visibility = true;
    let value = null;
    let answersToSkip = [];
    let validationErrors = [];

    function hasBeenEnrolledInCMAMDuringLast4Encounters(programEncounter){
        let growthMonitoringEncounters = programEncounter.programEnrolment.getEncounters()
            .filter((enc) => 
                enc.encounterType.name === 'Growth Monitoring' && 
                enc.voided === false && 
                enc.uuid !== programEncounter.uuid
            );

        if(growthMonitoringEncounters.length == 0) return false;

        growthMonitoringEncounters = growthMonitoringEncounters.slice(0, 4);

        return growthMonitoringEncounters.some((enc) => enc.getObservationReadableValue('001b3307-731e-4606-a8f4-9aaa1e264000') === 'Yes');
    }

    const treatmentAtHome = new imports.rulesConfig.RuleCondition({programEncounter, formElement}).when.valueInEncounter("2f136d99-f500-4710-89fe-d8007c977843").containsAnswerConceptName("397f526d-9247-4b3b-bb09-27c4e21fd062").matches();

    const hasEverBeenEnrolledInCMAM = new imports.rulesConfig.RuleCondition({programEncounter, formElement}).when.valueInEntireEnrolment("001b3307-731e-4606-a8f4-9aaa1e264000").containsAnswerConceptName("8ebbf088-f292-483e-9084-7de919ce67b7").matches();

    visibility = treatmentAtHome && (!hasEverBeenEnrolledInCMAM || !hasBeenEnrolledInCMAMDuringLast4Encounters(programEncounter));

    return new imports.rulesConfig.FormElementStatus(formElement.uuid, visibility, value, answersToSkip, validationErrors);
};