From e3f34e17c327671a6d69553b555eacce9185c7d5 Mon Sep 17 00:00:00 2001 From: Vijay Uppu <913468@dadeschools.net> Date: Sat, 16 Dec 2023 12:45:36 -0500 Subject: [PATCH] Modified logic to get latest response based on survetid, locationId, ane employee id --- .../Providers/SurveyResponsesProvider.cs | 49 ++++++++++++------- 1 file changed, 30 insertions(+), 19 deletions(-) diff --git a/DamageAssesmentApi/DamageAssesment.Api.Responses/Providers/SurveyResponsesProvider.cs b/DamageAssesmentApi/DamageAssesment.Api.Responses/Providers/SurveyResponsesProvider.cs index 8dedee9..94e6d58 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Responses/Providers/SurveyResponsesProvider.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Responses/Providers/SurveyResponsesProvider.cs @@ -424,7 +424,6 @@ namespace DamageAssesment.Api.Responses.Providers var answersList = await answerServiceProvider.getAnswersAsync(); if (answersList == null || !answersList.Any()) return null; - //get all the answers for the particular survey var surveyAnswers = answersList.Join( surveyResponses, @@ -565,7 +564,11 @@ namespace DamageAssesment.Api.Responses.Providers surveyResonses = await surveyResponseDbContext.SurveyResponses.Where(x => x.SurveyId == surveyId && x.EmployeeId == employeeid).ToListAsync(); employee = await employeeServiceProvider.getEmployeeAsync(employeeid); } - + surveyResonses = surveyResonses + .OrderByDescending(obj => obj.Id) + .GroupBy(obj => new { obj.SurveyId, obj.EmployeeId, obj.LocationId }) + .Select(group => group.FirstOrDefault()) // or .FirstOrDefault() if you want to handle empty groups + .ToList(); var answers = await answerServiceProvider.getAnswersAsync(); var questions = await questionServiceProvider.getQuestionsAsync(null); var Locations = await locationServiceProvider.getLocationsAsync(); @@ -586,18 +589,18 @@ namespace DamageAssesment.Api.Responses.Providers r.LocationId, r.EmployeeId, r.ClientDevice, - r.KeyAnswerResult, + // r.KeyAnswerResult, loc.DataValue, loc.Enrollment, loc.Longitute, loc.Latitude, - Employee = (from e in employees where e.Id == r.EmployeeId select new { e.Id, e.Name, e.BirthDate, e.Email, e.OfficePhoneNumber }).SingleOrDefault(), + // Employee = (from e in employees where e.Id == r.EmployeeId select new { e.Id, e.Name, e.BirthDate, e.Email, e.OfficePhoneNumber }).SingleOrDefault(), answers = from ans in answers where ans.SurveyResponseId == r.Id select new { ans.Id, - ans.QuestionId, + // ans.QuestionId, ans.AnswerText, ans.Comment, Questions = (from q in surveyQuestions where q.Id == ans.QuestionId select new { q.Id, q.QuestionNumber, q.CategoryId, q.Text }).SingleOrDefault(), @@ -623,18 +626,18 @@ namespace DamageAssesment.Api.Responses.Providers r.LocationId, r.EmployeeId, r.ClientDevice, - r.KeyAnswerResult, + // r.KeyAnswerResult, loc.DataValue, loc.Enrollment, loc.Longitute, loc.Latitude, - Employee = _employee, + // Employee = _employee, answers = from ans in answers where ans.SurveyResponseId == r.Id select new { ans.Id, - ans.QuestionId, + // ans.QuestionId, ans.AnswerText, ans.Comment, Questions = (from q in questions where q.Id == ans.QuestionId select new { q.Id, q.QuestionNumber, q.CategoryId, q.Text }).SingleOrDefault(), @@ -693,18 +696,18 @@ namespace DamageAssesment.Api.Responses.Providers r.LocationId, r.EmployeeId, r.ClientDevice, - r.KeyAnswerResult, + // r.KeyAnswerResult, loc.DataValue, loc.Enrollment, loc.Longitute, loc.Latitude, - Employee = employeeid != 0 ? _employee : (from e in employees where r.EmployeeId == e.Id select new { e.Id, e.Name, e.BirthDate, e.Email, e.OfficePhoneNumber }).SingleOrDefault(), + // Employee = employeeid != 0 ? _employee : (from e in employees where r.EmployeeId == e.Id select new { e.Id, e.Name, e.BirthDate, e.Email, e.OfficePhoneNumber }).SingleOrDefault(), answers = from ans in answers where ans.SurveyResponseId == r.Id select new { ans.Id, - ans.QuestionId, + // ans.QuestionId, ans.AnswerText, ans.Comment, Questions = (from q in questions where q.Id == ans.QuestionId select new { q.Id, q.QuestionNumber, q.CategoryId, q.Text }).SingleOrDefault(), @@ -945,7 +948,11 @@ namespace DamageAssesment.Api.Responses.Providers _employee = new { employee.Id, employee.Name, employee.BirthDate, employee.Email, employee.OfficePhoneNumber }; } } - + surveyResonses = surveyResonses + .OrderByDescending(obj => obj.Id) + .GroupBy(obj => new { obj.SurveyId, obj.EmployeeId, obj.LocationId }) + .Select(group => group.FirstOrDefault()) // or .FirstOrDefault() if you want to handle empty groups + .ToList(); var answers = await answerServiceProvider.getAnswersAsync(); var questions = await questionServiceProvider.getQuestionsAsync(null); var surveyQuestions = from q in questions where q.SurveyId == surveyId select q; @@ -961,18 +968,18 @@ namespace DamageAssesment.Api.Responses.Providers r.LocationId, r.EmployeeId, r.ClientDevice, - r.KeyAnswerResult, + // r.KeyAnswerResult, loc.DataValue, loc.Enrollment, loc.Longitute, loc.Latitude, - Employee = employeeid != 0 ? _employee : (from e in employees where r.EmployeeId == e.Id select new { e.Id, e.Name, e.BirthDate, e.Email, e.OfficePhoneNumber }).SingleOrDefault(), + // Employee = employeeid != 0 ? _employee : (from e in employees where r.EmployeeId == e.Id select new { e.Id, e.Name, e.BirthDate, e.Email, e.OfficePhoneNumber }).SingleOrDefault(), answers = from ans in answers where ans.SurveyResponseId == r.Id select new { - ans.QuestionId, + // ans.QuestionId, ans.Id, ans.AnswerText, ans.Comment, @@ -1016,7 +1023,11 @@ namespace DamageAssesment.Api.Responses.Providers _employee = new { employee.Id, employee.Name, employee.BirthDate, employee.Email, employee.OfficePhoneNumber }; } } - + surveyResponses = surveyResponses + .OrderByDescending(obj => obj.Id) + .GroupBy(obj => new { obj.SurveyId, obj.EmployeeId, obj.LocationId }) + .Select(group => group.FirstOrDefault()) // or .FirstOrDefault() if you want to handle empty groups + .ToList(); //var surveyResponses = await surveyResponseDbContext.Responses.Where(x => x.SurveyId == survey.Id).ToListAsync(); // var employees = await employeeServiceProvider.getEmployeesAsync(); var answers = await answerServiceProvider.getAnswersAsync(); @@ -1031,12 +1042,12 @@ namespace DamageAssesment.Api.Responses.Providers r.LocationId, r.EmployeeId, r.ClientDevice, - r.KeyAnswerResult, + // r.KeyAnswerResult, loc.DataValue, loc.Enrollment, loc.Longitute, loc.Latitude, - Employee = employeeid != 0 ? _employee : (from e in employees where r.EmployeeId == e.Id select new { e.Id, e.Name, e.BirthDate, e.Email, e.OfficePhoneNumber }).SingleOrDefault(), + // Employee = employeeid != 0 ? _employee : (from e in employees where r.EmployeeId == e.Id select new { e.Id, e.Name, e.BirthDate, e.Email, e.OfficePhoneNumber }).SingleOrDefault(), answers = from ans in answers where ans.SurveyResponseId == r.Id && ans.QuestionId == question.Id @@ -1044,7 +1055,7 @@ namespace DamageAssesment.Api.Responses.Providers select new { - ans.QuestionId, + // ans.QuestionId, AnswerId = ans.Id, ans.AnswerText, ans.Comment,