Untitled

 avatar
unknown
csharp
2 years ago
3.2 kB
4
Indexable
private void ProcessVariableStatusForOneSurveyIterationRespondent(int surveyIterationRespondentId)
    {
        var ruleResultRespondents = _db.RuleResultRespondent.Where(r => r.SurveyIterationRespondentId == surveyIterationRespondentId).ToList();
        var ruleIdsInQuestion = ruleResultRespondents.Select(x => x.RuleId).ToList();
        var variablesNeedingProcessing = _db.RuleVariableAssociation.Where(x => ruleIdsInQuestion.Contains(x.RuleId)).Select(x => x ).ToList();
 
        foreach (var v in variablesNeedingProcessing) //all variables that are part of the rules in question
        {
            //get rules associated with variable.variables
            var ruleIdsToCheckVariableAgainst = variablesNeedingProcessing.Where(x => x.VariableName== v.VariableName).Select(x => x.RuleId).ToList();
            var ruleStatusesForThisVariable = ruleResultRespondents.Where(x => ruleIdsToCheckVariableAgainst.Contains(x.RuleId)).ToList(); //this holds the rules for this variable with status for each rule
            var ruleCountForThisVariable = ruleStatusesForThisVariable.Count;
            if (ruleCountForThisVariable > 0)
            {
                var varStatus = "Fail";
                var offPathCount = ruleStatusesForThisVariable.Where(x => x.ValidityStatus == "Off Path").Count();
                var invalidCount = ruleStatusesForThisVariable.Where(x => x.ValidityStatus == "Invalid").Count();
                if(invalidCount == 0) { varStatus = "Pass"; }// no errors
                if (offPathCount == ruleCountForThisVariable) { varStatus = "Off Path"; }//all rules are off path so set variable to
                var errorCount = invalidCount; //this many rules failed for variable 
                //update variable in response table
                var resp = _db.Response.Where(x => x.VariableName == v.VariableName && x.SurveyIterationRespondentId == surveyIterationRespondentId).SingleOrDefault();
                
                if (resp != null)
                {
                    resp.ErrorsCount = errorCount;
                    resp.VariableStatus = varStatus;
                    _db.SaveChanges(1);//saves the error count and variable status for one variable for the survey iteration respondent
                }      
            }
            var failedList = _db.Response.Where(x => x.SurveyIterationRespondentId == surveyIterationRespondentId && x.VariableStatus == "Fail").ToList();
            var respStatus = "";
            if(failedList.Count> 0)
            {
                var sumError = failedList.Sum(x => x.ErrorsCount);
                respStatus = $"Dirty ({sumError})"; 
            }
            else
            {
                respStatus = "Clean"; //todo: make sure this is ok or do we only want clean if at least one variable passed
            }
 
            var sirToUpdate = _db.SurveyIterationRespondent.Find(surveyIterationRespondentId);
            sirToUpdate.ResponseStatus= respStatus;
            _db.SaveChanges(1);
            //todo: here we look at sum of errors if there all failed records and write value to the sir table with responsestatus Fail (15) or Pass if no errors.
        }   
    }
Editor is loading...