From 9b8e8ffad25132bf8fcaa5e9b70e90acabad3be4 Mon Sep 17 00:00:00 2001 From: Vijay Uppu <913468@dadeschools.net> Date: Sat, 16 Dec 2023 12:40:33 -0500 Subject: [PATCH] Modified logic to get latest response based on survetid, locationId, ane employee id --- .../Providers/SurveyResponsesProvider.cs | 52 ++++++++++++------- 1 file changed, 34 insertions(+), 18 deletions(-) diff --git a/DamageAssesmentApi/DamageAssesment.Api.Responses/Providers/SurveyResponsesProvider.cs b/DamageAssesmentApi/DamageAssesment.Api.Responses/Providers/SurveyResponsesProvider.cs index 333fd37..1e10263 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Responses/Providers/SurveyResponsesProvider.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Responses/Providers/SurveyResponsesProvider.cs @@ -439,7 +439,6 @@ namespace DamageAssesment.Api.Responses.Providers var answersList = await answerServiceProvider.getAnswersAsync(token); if (answersList == null || !answersList.Any()) return null; - //get all the answers for the particular survey var surveyAnswers = answersList.Join( surveyResponses, @@ -535,14 +534,14 @@ namespace DamageAssesment.Api.Responses.Providers surveyResponse.LocationId, surveyResponse.EmployeeId, surveyResponse.ClientDevice, - surveyResponse.KeyAnswerResult, + // surveyResponse.KeyAnswerResult, surveyResponse.Longitute, surveyResponse.Latitude, Employee = employee, answers = from ans in answers select new { - ans.QuestionId, + // ans.QuestionId, ans.Id, ans.AnswerText, ans.Comment, @@ -578,6 +577,11 @@ namespace DamageAssesment.Api.Responses.Providers surveyResonses = await surveyResponseDbContext.SurveyResponses.Where(x => x.SurveyId == surveyId && x.EmployeeId == employeeid).ToListAsync(); employee = await employeeServiceProvider.getEmployeeAsync(employeeid, token); } + 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(token); var questions = await questionServiceProvider.getQuestionsAsync(null, token); @@ -594,16 +598,16 @@ namespace DamageAssesment.Api.Responses.Providers r.LocationId, r.EmployeeId, r.ClientDevice, - r.KeyAnswerResult, + // r.KeyAnswerResult, r.Longitute, r.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(), @@ -646,7 +650,11 @@ namespace DamageAssesment.Api.Responses.Providers } } - + 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(token); var questions = await questionServiceProvider.getQuestionsAsync(null, token); var attachments = await attachmentServiceProvider.getAttachmentsAsync(token); @@ -659,16 +667,16 @@ namespace DamageAssesment.Api.Responses.Providers r.LocationId, r.EmployeeId, r.ClientDevice, - r.KeyAnswerResult, + // r.KeyAnswerResult, r.Longitute, r.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(), @@ -907,7 +915,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(token); var questions = await questionServiceProvider.getQuestionsAsync(null, token); var surveyQuestions = from q in questions where q.SurveyId == surveyId select q; @@ -921,16 +933,16 @@ namespace DamageAssesment.Api.Responses.Providers r.LocationId, r.EmployeeId, r.ClientDevice, - r.KeyAnswerResult, + // r.KeyAnswerResult, r.Longitute, r.Latitude, - Employee = (from e in employees where r.EmployeeId == e.Id select new { e.Id, e.Name, e.BirthDate, e.Email, e.OfficePhoneNumber }).SingleOrDefault(), + // 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, @@ -973,7 +985,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(token); @@ -987,10 +1003,10 @@ namespace DamageAssesment.Api.Responses.Providers r.LocationId, r.EmployeeId, r.ClientDevice, - r.KeyAnswerResult, + // r.KeyAnswerResult, r.Longitute, r.Latitude, - Employee = (from e in employees where r.EmployeeId == e.Id select new { e.Id, e.Name, e.BirthDate, e.Email, e.OfficePhoneNumber }).SingleOrDefault(), + // 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 @@ -998,7 +1014,7 @@ namespace DamageAssesment.Api.Responses.Providers select new { - ans.QuestionId, + // ans.QuestionId, AnswerId = ans.Id, ans.AnswerText, ans.Comment,