I'm still very new to Javascript and software development in general as this is my first job, and right now I am debugging a critical defect related to validation.
I have a scenario where I have an array, and within that array each product has an attribute labeled Unit Volume with a code of ASC_Sales_Volume.
The current defect is that when a user configures more than one product, but leaves one with a unit volume of zero, the user is still allowed to navigate forward to the next page despite there being a validation check in place to prevent this. The condition that would block the user from moving forward is if $scope.bpTree.response.errorCheckVolume = true.
WIth what is currently written by the former dev, the forEach loop keeps changing the $scope.bpTree.response.errorCheckVolume back to false when the user clicks on the button to fire the functions.
The Next button itself is an OOTB component that I cannot modify directly.
What would be the next best step in troubleshooting this? Basically, when the forEach evaluates a product with a Unit Volume value of 0, the $scope.bpTree.response.errorCheckVolume should always equal true regardless of subsequent Unit Volume values, until the user changes the Unit Volume value for that particular product and then clicks the Next button and the logic fires again. If the error records (as in, any product in the array with a Unit Volume of 0) are greater than zero, then errorCheckVolume should always evaluate to true and the user should be blocked from progressing.
Would I use something similar to Array.prototype.some()? I've just been staring at my screen for hours trying different things to no avail.
Here is the code snippet:
function validateAllPlansUnitVolume(triggeredFromNextBtn) {
var coveragesData = $scope.sortedCoverages[$scope.childProduct.instanceKey || $scope.childProduct.productName || $scope.childProduct.Name],
errorRecords = checkForInvalidPlans(coveragesData, triggeredFromNextBtn);
if(errorRecords > 0) {
$scope.bpTree.response.errorCheckVolume = true;
}
else {
$scope.bpTree.response.errorCheckVolume = false;
}
}
function checkForInvalidPlans(coveragesData, triggeredFromNextBtn){
if(!!coveragesData && coveragesData.length) {
coveragesData.forEach(function(product){
if(!!product.attributeCategories){
unitVolumeAttrNodes = product.attributeCategories.records[0].productAttributes.records.filter((ele) => ele.code == "ASC_Sales_Volume");
if(!!unitVolumeAttrNodes && unitVolumeAttrNodes.length){
unitVolumeAttrNodes.forEach(function(attrNode){
if(unitVolumeAttrNodes[0].userValues === 0){
$scope.bpTree.response.errorCheckVolume = true;
product.unitVolumeErr = true;
product.unitVolumeErrMsg = "Unit Volume cannot be zero. Please update before proceeding to the next screen";
}
else {
product.unitVolumeErrMsg = "";
product.unitVolumeErr = false;
$scope.bpTree.response.errorCheckVolume = false;
}
});
}
}
});
return coveragesData.filter((ele) => ele.unitVolumeErr).length;
}
return 0;
}