Closed kameshbhariya closed 1 month ago
Backup: 1. Reporting Year
'use strict';
({params, imports}) => {
const encounter = params.entity;
const moment = imports.moment;
const formElement = params.formElement;
const _ = imports.lodash;
let visibility = true;
let value = null;
let answersToSkip = [];
let validationErrors = [];
function isYearValid(yearString) {
const yearValidation = /^(19\d{2}|20\d{2})$/;
const fourDigitValidation = /^\d{4}$/;
if (!yearValidation.test(yearString)) {
validationErrors.push("Enter a valid year");
return false;
}
return true;
}
let reportingYear = encounter.getObservationValue("7cafd024-e7c8-4de0-b6fc-6511db23b4ca");
reportingYear = moment(reportingYear, 'YYYY').year();
const isDefined = new imports.rulesConfig.RuleCondition({
encounter,
formElement
}).when.valueInEncounter("7cafd024-e7c8-4de0-b6fc-6511db23b4ca").defined.matches();
if (isDefined && isYearValid(reportingYear) && (reportingYear > moment().year())) {
validationErrors.push('Reporting year must be less than current year');
}
return new imports.rulesConfig.FormElementStatus(formElement.uuid, visibility, value, answersToSkip, validationErrors);
};
Reporting Month
'use strict';
({params, imports}) => {
const encounter = params.entity;
const moment = imports.moment;
const formElement = params.formElement;
const _ = imports.lodash;
let visibility = true;
let value = null;
let answersToSkip = [];
let validationErrors = [];
const currentDate = moment();
let reportingYear = encounter.getObservationValue("7cafd024-e7c8-4de0-b6fc-6511db23b4ca");
reportingYear = moment(reportingYear, 'YYYY').year();
let reportingMonth = encounter.getObservationReadableValue("634f0447-3f29-4ec2-95bd-e3ed59aad349");
reportingMonth = moment(reportingMonth, 'MMM').month();
const condition11 = new imports.rulesConfig.RuleCondition({encounter, formElement}).when.valueInEncounter("7cafd024-e7c8-4de0-b6fc-6511db23b4ca").defined.matches();
const condition21 = new imports.rulesConfig.RuleCondition({encounter, formElement}).when.valueInEncounter("634f0447-3f29-4ec2-95bd-e3ed59aad349").defined.matches();
if (condition11 && condition21) {
if (reportingYear > currentDate.year() || (reportingYear == currentDate.year() && reportingMonth >= currentDate.month())){
validationErrors.push(`Reporting month must be before ${moment().month(currentDate.month()).format('MMMM')}`);
}
}
return new imports.rulesConfig.FormElementStatus(formElement.uuid, visibility, value, answersToSkip, validationErrors);
};
Make the changes in UAT.
[x] 1. For the question Reporting Year, we need to add the below validation:
[x] a). Year cannot be more than 4 digits
[x] b) Future year should not be allowed
[x] c) Allow only current and past 1 year to enter. eg for this year, allow only 2023 and 2024
Analysis Link
Note
There is already a validation which is not working, try to debug why is it failing. And fix the same.