Modified logic to get latest response based on survetid, locationId, ane employee id

This commit is contained in:
Vijay Uppu 2023-12-16 12:46:04 -05:00
parent 1c26852670
commit 8b05f61f44

View File

@ -424,7 +424,6 @@ namespace DamageAssesment.Api.Responses.Providers
var answersList = await answerServiceProvider.getAnswersAsync(); var answersList = await answerServiceProvider.getAnswersAsync();
if (answersList == null || !answersList.Any()) if (answersList == null || !answersList.Any())
return null; return null;
//get all the answers for the particular survey //get all the answers for the particular survey
var surveyAnswers = answersList.Join( var surveyAnswers = answersList.Join(
surveyResponses, surveyResponses,
@ -565,7 +564,11 @@ namespace DamageAssesment.Api.Responses.Providers
surveyResonses = await surveyResponseDbContext.SurveyResponses.Where(x => x.SurveyId == surveyId && x.EmployeeId == employeeid).ToListAsync(); surveyResonses = await surveyResponseDbContext.SurveyResponses.Where(x => x.SurveyId == surveyId && x.EmployeeId == employeeid).ToListAsync();
employee = await employeeServiceProvider.getEmployeeAsync(employeeid); 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 answers = await answerServiceProvider.getAnswersAsync();
var questions = await questionServiceProvider.getQuestionsAsync(null); var questions = await questionServiceProvider.getQuestionsAsync(null);
var Locations = await locationServiceProvider.getLocationsAsync(); var Locations = await locationServiceProvider.getLocationsAsync();
@ -586,18 +589,18 @@ namespace DamageAssesment.Api.Responses.Providers
r.LocationId, r.LocationId,
r.EmployeeId, r.EmployeeId,
r.ClientDevice, r.ClientDevice,
r.KeyAnswerResult, // r.KeyAnswerResult,
loc.DataValue, loc.DataValue,
loc.Enrollment, loc.Enrollment,
loc.Longitute, loc.Longitute,
loc.Latitude, 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 answers = from ans in answers
where ans.SurveyResponseId == r.Id where ans.SurveyResponseId == r.Id
select new select new
{ {
ans.Id, ans.Id,
ans.QuestionId, // ans.QuestionId,
ans.AnswerText, ans.AnswerText,
ans.Comment, ans.Comment,
Questions = (from q in surveyQuestions where q.Id == ans.QuestionId select new { q.Id, q.QuestionNumber, q.CategoryId, q.Text }).SingleOrDefault(), 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.LocationId,
r.EmployeeId, r.EmployeeId,
r.ClientDevice, r.ClientDevice,
r.KeyAnswerResult, // r.KeyAnswerResult,
loc.DataValue, loc.DataValue,
loc.Enrollment, loc.Enrollment,
loc.Longitute, loc.Longitute,
loc.Latitude, loc.Latitude,
Employee = _employee, // Employee = _employee,
answers = from ans in answers answers = from ans in answers
where ans.SurveyResponseId == r.Id where ans.SurveyResponseId == r.Id
select new select new
{ {
ans.Id, ans.Id,
ans.QuestionId, // ans.QuestionId,
ans.AnswerText, ans.AnswerText,
ans.Comment, ans.Comment,
Questions = (from q in questions where q.Id == ans.QuestionId select new { q.Id, q.QuestionNumber, q.CategoryId, q.Text }).SingleOrDefault(), 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.LocationId,
r.EmployeeId, r.EmployeeId,
r.ClientDevice, r.ClientDevice,
r.KeyAnswerResult, // r.KeyAnswerResult,
loc.DataValue, loc.DataValue,
loc.Enrollment, loc.Enrollment,
loc.Longitute, loc.Longitute,
loc.Latitude, 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 answers = from ans in answers
where ans.SurveyResponseId == r.Id where ans.SurveyResponseId == r.Id
select new select new
{ {
ans.Id, ans.Id,
ans.QuestionId, // ans.QuestionId,
ans.AnswerText, ans.AnswerText,
ans.Comment, ans.Comment,
Questions = (from q in questions where q.Id == ans.QuestionId select new { q.Id, q.QuestionNumber, q.CategoryId, q.Text }).SingleOrDefault(), 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 }; _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 answers = await answerServiceProvider.getAnswersAsync();
var questions = await questionServiceProvider.getQuestionsAsync(null); var questions = await questionServiceProvider.getQuestionsAsync(null);
var surveyQuestions = from q in questions where q.SurveyId == surveyId select q; var surveyQuestions = from q in questions where q.SurveyId == surveyId select q;
@ -961,18 +968,18 @@ namespace DamageAssesment.Api.Responses.Providers
r.LocationId, r.LocationId,
r.EmployeeId, r.EmployeeId,
r.ClientDevice, r.ClientDevice,
r.KeyAnswerResult, // r.KeyAnswerResult,
loc.DataValue, loc.DataValue,
loc.Enrollment, loc.Enrollment,
loc.Longitute, loc.Longitute,
loc.Latitude, 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 answers = from ans in answers
where ans.SurveyResponseId == r.Id where ans.SurveyResponseId == r.Id
select new select new
{ {
ans.QuestionId, // ans.QuestionId,
ans.Id, ans.Id,
ans.AnswerText, ans.AnswerText,
ans.Comment, ans.Comment,
@ -1016,7 +1023,11 @@ namespace DamageAssesment.Api.Responses.Providers
_employee = new { employee.Id, employee.Name, employee.BirthDate, employee.Email, employee.OfficePhoneNumber }; _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 surveyResponses = await surveyResponseDbContext.Responses.Where(x => x.SurveyId == survey.Id).ToListAsync();
// var employees = await employeeServiceProvider.getEmployeesAsync(); // var employees = await employeeServiceProvider.getEmployeesAsync();
var answers = await answerServiceProvider.getAnswersAsync(); var answers = await answerServiceProvider.getAnswersAsync();
@ -1031,12 +1042,12 @@ namespace DamageAssesment.Api.Responses.Providers
r.LocationId, r.LocationId,
r.EmployeeId, r.EmployeeId,
r.ClientDevice, r.ClientDevice,
r.KeyAnswerResult, // r.KeyAnswerResult,
loc.DataValue, loc.DataValue,
loc.Enrollment, loc.Enrollment,
loc.Longitute, loc.Longitute,
loc.Latitude, 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 answers = from ans in answers
where ans.SurveyResponseId == r.Id where ans.SurveyResponseId == r.Id
&& ans.QuestionId == question.Id && ans.QuestionId == question.Id
@ -1044,7 +1055,7 @@ namespace DamageAssesment.Api.Responses.Providers
select new select new
{ {
ans.QuestionId, // ans.QuestionId,
AnswerId = ans.Id, AnswerId = ans.Id,
ans.AnswerText, ans.AnswerText,
ans.Comment, ans.Comment,