2023-08-15 22:52:30 -05:00
|
|
|
|
using AutoMapper;
|
2023-10-04 17:45:51 -05:00
|
|
|
|
using DamageAssesment.Api.Responses.Db;
|
|
|
|
|
using DamageAssesment.Api.Responses.Interfaces;
|
|
|
|
|
using DamageAssesment.Api.Responses.Models;
|
2023-08-15 22:52:30 -05:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
|
2023-10-04 17:45:51 -05:00
|
|
|
|
namespace DamageAssesment.Api.Responses.Providers
|
2023-08-15 22:52:30 -05:00
|
|
|
|
{
|
|
|
|
|
public class SurveyResponsesProvider : ISurveysResponse
|
|
|
|
|
{
|
|
|
|
|
private readonly SurveyResponseDbContext surveyResponseDbContext;
|
|
|
|
|
private readonly ILogger<SurveyResponsesProvider> logger;
|
|
|
|
|
private readonly IAnswerServiceProvider answerServiceProvider;
|
|
|
|
|
private readonly IRegionServiceProvider regionServiceProvider;
|
|
|
|
|
private readonly ILocationServiceProvider locationServiceProvider;
|
|
|
|
|
private readonly IEmployeeServiceProvider employeeServiceProvider;
|
|
|
|
|
private readonly IAttachmentServiceProvider attachmentServiceProvider;
|
|
|
|
|
private readonly IQuestionServiceProvider questionServiceProvider;
|
|
|
|
|
private readonly ISurveyServiceProvider surveyServiceProvider;
|
|
|
|
|
private readonly IMapper mapper;
|
|
|
|
|
|
|
|
|
|
public SurveyResponsesProvider(SurveyResponseDbContext surveyResponseDbContext, ILogger<SurveyResponsesProvider> logger, IAnswerServiceProvider answerServiceProvider, IRegionServiceProvider regionServiceProvider, ILocationServiceProvider locationServiceProvider, IEmployeeServiceProvider employeeServiceProvider, IAttachmentServiceProvider attachmentServiceProvider, IQuestionServiceProvider questionServiceProvider, ISurveyServiceProvider surveyServiceProvider, IMapper mapper)
|
|
|
|
|
{
|
|
|
|
|
this.surveyResponseDbContext = surveyResponseDbContext;
|
|
|
|
|
this.logger = logger;
|
|
|
|
|
this.answerServiceProvider = answerServiceProvider;
|
|
|
|
|
this.regionServiceProvider = regionServiceProvider;
|
|
|
|
|
this.locationServiceProvider = locationServiceProvider;
|
|
|
|
|
this.employeeServiceProvider = employeeServiceProvider;
|
|
|
|
|
this.attachmentServiceProvider = attachmentServiceProvider;
|
|
|
|
|
this.questionServiceProvider = questionServiceProvider;
|
|
|
|
|
this.surveyServiceProvider = surveyServiceProvider;
|
|
|
|
|
this.mapper = mapper;
|
2023-10-06 17:22:37 -05:00
|
|
|
|
SeedData();
|
2023-08-15 22:52:30 -05:00
|
|
|
|
}
|
|
|
|
|
|
2023-10-06 17:22:37 -05:00
|
|
|
|
public void SeedData()
|
2023-08-15 22:52:30 -05:00
|
|
|
|
{
|
2023-10-06 17:22:37 -05:00
|
|
|
|
// Check if SurveyResponses exist, if not, seed data
|
2023-08-15 22:52:30 -05:00
|
|
|
|
if (!surveyResponseDbContext.SurveyResponses.Any())
|
|
|
|
|
{
|
2023-10-06 17:22:37 -05:00
|
|
|
|
|
|
|
|
|
// Create and save SurveyResponse records with references to existing Employee and Location records
|
2023-10-04 14:03:59 -05:00
|
|
|
|
surveyResponseDbContext.SurveyResponses.Add(new Db.SurveyResponse { SurveyId = 1, EmployeeId = 1, LocationId = 1, ClientDevice = "Mobile", Latitude = 98.8767, Longitute = -129.9897, KeyAnswerResult = "true", CreatedDate = DateTime.Now });
|
2023-10-06 17:22:37 -05:00
|
|
|
|
surveyResponseDbContext.SurveyResponses.Add(new Db.SurveyResponse { SurveyId = 1, EmployeeId = 2, LocationId = 2, ClientDevice = "Mobile", Latitude = 98.8767, Longitute = -129.9897, KeyAnswerResult = "true", CreatedDate = DateTime.Now });
|
|
|
|
|
|
2023-08-15 22:52:30 -05:00
|
|
|
|
surveyResponseDbContext.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-06 17:22:37 -05:00
|
|
|
|
|
2023-09-20 23:58:29 -05:00
|
|
|
|
public async Task<(bool IsSuccess, dynamic Answers, string ErrorMessage)> GetAnswersByRegionAsync(int surveyId, int employeeid)
|
2023-08-15 22:52:30 -05:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
logger?.LogInformation("Querying to get SurveyResponse object from DB");
|
2023-09-20 23:58:29 -05:00
|
|
|
|
IQueryable<Db.SurveyResponse> listSurveyResponse = null;
|
|
|
|
|
if (employeeid == 0)
|
|
|
|
|
{
|
|
|
|
|
listSurveyResponse = surveyResponseDbContext.SurveyResponses.Where(s => s.SurveyId == surveyId);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
listSurveyResponse = surveyResponseDbContext.SurveyResponses.Where(s => s.SurveyId == surveyId && s.EmployeeId == employeeid);
|
|
|
|
|
}
|
2023-08-15 22:52:30 -05:00
|
|
|
|
|
|
|
|
|
if (listSurveyResponse.Any())
|
|
|
|
|
{
|
|
|
|
|
var answers = await getAnswersByRegionAndSurveyIdAsync(listSurveyResponse);
|
|
|
|
|
return (true, answers, "Request Successful.");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return (false, null, "Not found");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
logger?.LogError(ex.ToString());
|
|
|
|
|
return (false, null, ex.Message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<(bool IsSuccess, dynamic SurveyResponse, string ErrorMessage)> GetSurveyResponseByIdAsync(int responseId)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
logger?.LogInformation("Querying to get SurveyResponse object from DB");
|
|
|
|
|
var surveyResponse = surveyResponseDbContext.SurveyResponses.Where(s => s.Id == responseId).SingleOrDefault();
|
|
|
|
|
|
|
|
|
|
if (surveyResponse != null)
|
|
|
|
|
{
|
|
|
|
|
var answers = await getSurveyResponseByResponseIdAsync(surveyResponse);
|
|
|
|
|
|
|
|
|
|
if (answers != null)
|
|
|
|
|
return (true, answers, "Request Successful.");
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
answers = new { };
|
|
|
|
|
return (true, answers, "Empty object returned");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return (false, null, "Not found");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
logger?.LogError(ex.ToString());
|
|
|
|
|
return (false, null, ex.Message);
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-10-16 13:13:20 -05:00
|
|
|
|
|
|
|
|
|
public async Task<(bool IsSuccess, dynamic Surveys, string ErrorMessage)> GetActiveSurveysAsync(int? employeeid, string language)
|
2023-09-20 23:58:29 -05:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
logger?.LogInformation("Querying to get SurveyResponse object from DB");
|
|
|
|
|
//get all the survey that already taken by the employee
|
|
|
|
|
var surveys = await surveyServiceProvider.getSurveysAsync(language);
|
2023-10-16 13:13:20 -05:00
|
|
|
|
surveys = surveys.Where(s => s.IsEnabled == true && s.StartDate <= DateTime.Now && s.EndDate >= DateTime.Now).ToList();
|
|
|
|
|
if (employeeid == null || employeeid == 0)
|
2023-09-20 23:58:29 -05:00
|
|
|
|
return (true, surveys, null);
|
2023-10-16 13:13:20 -05:00
|
|
|
|
List<int> listOfsurveysId = await surveyResponseDbContext.SurveyResponses.Where(x => x.EmployeeId == employeeid.Value).Select(y => y.SurveyId).ToListAsync();
|
|
|
|
|
var activeSurveys = surveys.Where(s => !listOfsurveysId.Contains(s.Id));
|
2023-09-20 23:58:29 -05:00
|
|
|
|
return (true, activeSurveys, null);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
logger?.LogError(ex.ToString());
|
|
|
|
|
return (false, null, ex.Message);
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-08-15 22:52:30 -05:00
|
|
|
|
|
2023-10-16 13:13:20 -05:00
|
|
|
|
public async Task<(bool IsSuccess, dynamic Surveys, string ErrorMessage)> GetHistoricSurveysAsync(int? employeeid, string language)
|
2023-09-20 23:58:29 -05:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
logger?.LogInformation("Querying to get SurveyResponse object from DB");
|
2023-08-15 22:52:30 -05:00
|
|
|
|
|
2023-10-16 13:13:20 -05:00
|
|
|
|
var surveys = await surveyServiceProvider.getSurveysAsync(language);
|
|
|
|
|
// returning only historic data: end date is less than current date.
|
|
|
|
|
surveys = surveys.Where(s => s.EndDate < DateTime.Now).ToList();
|
|
|
|
|
if (employeeid == null || employeeid == 0)
|
|
|
|
|
return (true, surveys, null);
|
|
|
|
|
var surveyResponses = await surveyResponseDbContext.SurveyResponses.Where(x => x.EmployeeId == employeeid).ToListAsync();
|
2023-09-20 23:58:29 -05:00
|
|
|
|
var historicSurveys = from s in surveys
|
|
|
|
|
from r in surveyResponses
|
|
|
|
|
where s.Id == r.SurveyId
|
|
|
|
|
select s;
|
|
|
|
|
return (true, historicSurveys, null);
|
2023-10-16 13:13:20 -05:00
|
|
|
|
|
2023-09-20 23:58:29 -05:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
logger?.LogError(ex.ToString());
|
|
|
|
|
return (false, null, ex.Message);
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-08-15 22:52:30 -05:00
|
|
|
|
|
2023-09-20 23:58:29 -05:00
|
|
|
|
public async Task<(bool IsSuccess, dynamic SurveyResponses, string ErrorMessage)> GetSurveyResponsesBySurveyAsync(int surveyId, int employeeid)
|
2023-08-15 22:52:30 -05:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
logger?.LogInformation("Querying to get Survey object from microservice");
|
|
|
|
|
var survey = await surveyServiceProvider.getSurveyAsync(surveyId);
|
|
|
|
|
|
|
|
|
|
if (survey != null)
|
|
|
|
|
{
|
2023-09-20 23:58:29 -05:00
|
|
|
|
var answers = await getSurveyResponsesBySurveyIdAsync(surveyId, employeeid);
|
2023-08-15 22:52:30 -05:00
|
|
|
|
|
|
|
|
|
if (answers != null)
|
|
|
|
|
return (true, answers, "Request Successful.");
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
answers = new List<Models.SurveyResponse>();
|
|
|
|
|
return (true, answers, "Empty object returned");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return (false, null, "Not found");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
logger?.LogError(ex.ToString());
|
|
|
|
|
return (false, null, ex.Message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-20 23:58:29 -05:00
|
|
|
|
public async Task<(bool IsSuccess, dynamic SurveyResponses, string ErrorMessage)> GetSurveyResponsesBySurveyAndLocationAsync(int surveyId, int locationId, int employeeid)
|
2023-08-15 22:52:30 -05:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
logger?.LogInformation("Querying to get Survey object from microservice");
|
|
|
|
|
var survey = await surveyServiceProvider.getSurveyAsync(surveyId);
|
|
|
|
|
|
|
|
|
|
if (survey != null)
|
|
|
|
|
{
|
2023-09-20 23:58:29 -05:00
|
|
|
|
var answers = await getSurveyResponsesBySurveyIdLocationIdAsync(surveyId, locationId, employeeid);
|
2023-08-15 22:52:30 -05:00
|
|
|
|
|
|
|
|
|
if (answers != null)
|
|
|
|
|
return (true, answers, "Request Successful.");
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
answers = new List<Models.SurveyResponse>();
|
|
|
|
|
return (true, answers, "Empty object returned");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return (false, null, "Not found");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
logger?.LogError(ex.ToString());
|
|
|
|
|
return (false, null, ex.Message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-20 23:58:29 -05:00
|
|
|
|
public async Task<(bool IsSuccess, dynamic SurveyResponses, string ErrorMessage)> GetSurveyResponsesByMaintenanceCenterAsync(int surveyId, int employeeid)
|
2023-08-15 22:52:30 -05:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
logger?.LogInformation("Querying to get Survey object from microservice");
|
|
|
|
|
var survey = await surveyServiceProvider.getSurveyAsync(surveyId);
|
|
|
|
|
|
|
|
|
|
if (survey != null)
|
|
|
|
|
{
|
2023-09-20 23:58:29 -05:00
|
|
|
|
var answers = await getResultsByMaintenanceCenterAsync(surveyId, employeeid);
|
2023-08-15 22:52:30 -05:00
|
|
|
|
|
|
|
|
|
if (answers != null)
|
|
|
|
|
return (true, answers, "Request Successful.");
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
answers = new List<Models.SurveyResponse>();
|
|
|
|
|
return (true, answers, "Empty object returned");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return (false, null, "Not found");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
logger?.LogError(ex.ToString());
|
|
|
|
|
return (false, null, ex.Message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-20 23:58:29 -05:00
|
|
|
|
public async Task<(bool IsSuccess, dynamic SurveyResponses, string ErrorMessage)> GetResponsesByAnswerAsync(int surveyId, int questionId, string answer, int employeeid)
|
2023-08-15 22:52:30 -05:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
logger?.LogInformation("Querying to get Survey object from microservice");
|
|
|
|
|
var survey = await surveyServiceProvider.getSurveyAsync(surveyId);
|
|
|
|
|
var question = await questionServiceProvider.getQuestionsAsync(questionId);
|
|
|
|
|
bool IsCorrectAnswer = answer.ToLower().Equals("yes") || answer.ToLower().Equals("no") ? true : false;
|
|
|
|
|
|
2023-09-04 20:31:41 -05:00
|
|
|
|
|
2023-08-15 22:52:30 -05:00
|
|
|
|
if (survey != null && question != null && IsCorrectAnswer)
|
|
|
|
|
{
|
2023-09-20 23:58:29 -05:00
|
|
|
|
var answers = await getSurveyResponsesByAnswerAsync(survey, question, answer, employeeid);
|
2023-08-15 22:52:30 -05:00
|
|
|
|
|
|
|
|
|
if (answers != null)
|
|
|
|
|
return (true, answers, "Request Successful.");
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
answers = new List<Models.SurveyResponse>();
|
|
|
|
|
return (true, answers, "Empty object returned");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return (false, null, "Not found");
|
2023-09-04 20:31:41 -05:00
|
|
|
|
}
|
2023-08-15 22:52:30 -05:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
logger?.LogError(ex.ToString());
|
|
|
|
|
return (false, null, ex.Message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2023-09-20 23:58:29 -05:00
|
|
|
|
public async Task<(bool IsSuccess, dynamic surveyResponses, string ErrorMessage)> GetSurveyResponsesAsync(int employeeid)
|
2023-08-15 22:52:30 -05:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2023-09-20 23:58:29 -05:00
|
|
|
|
var responses = await getAllSurveyResponsesAsync(employeeid);
|
2023-08-15 22:52:30 -05:00
|
|
|
|
|
2023-09-20 23:58:29 -05:00
|
|
|
|
if (responses != null)
|
|
|
|
|
return (true, responses, "Request Successful.");
|
2023-08-15 22:52:30 -05:00
|
|
|
|
else
|
|
|
|
|
{
|
2023-09-20 23:58:29 -05:00
|
|
|
|
responses = new List<Models.SurveyResponse>();
|
|
|
|
|
return (true, responses, "Empty object returned");
|
2023-08-15 22:52:30 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
logger?.LogError(ex.ToString());
|
|
|
|
|
return (false, null, ex.Message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<(bool IsSuccess, Models.SurveyResponse SurveyResponse, string ErrorMessage)> PostSurveyResponseAsync(Models.SurveyResponse surveyResponse)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (surveyResponse != null)
|
|
|
|
|
{
|
2023-08-27 10:55:58 -05:00
|
|
|
|
var _surveyResponse = mapper.Map<Models.SurveyResponse, Db.SurveyResponse>(surveyResponse);
|
|
|
|
|
surveyResponseDbContext.SurveyResponses.Add(_surveyResponse);
|
|
|
|
|
await surveyResponseDbContext.SaveChangesAsync();
|
|
|
|
|
surveyResponse.Id = _surveyResponse.Id;
|
2023-08-15 22:52:30 -05:00
|
|
|
|
return (true, surveyResponse, "Request Successful");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2023-08-27 10:55:58 -05:00
|
|
|
|
logger?.LogInformation($"SurveyResponse cannot be added");
|
|
|
|
|
return (false, null, "SurveyResponse cannot be added");
|
2023-08-15 22:52:30 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
logger?.LogError(ex.ToString());
|
|
|
|
|
return (false, null, ex.Message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public async Task<(bool IsSuccess, Models.SurveyResponse SurveyResponse, string ErrorMessage)> PutSurveyResponseAsync(int Id, Models.SurveyResponse SurveyResponse)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (SurveyResponse != null)
|
|
|
|
|
{
|
2023-09-13 00:28:24 -05:00
|
|
|
|
var _SurveyResponse = await surveyResponseDbContext.SurveyResponses.AsNoTracking().Where(s => s.Id == Id).FirstOrDefaultAsync();
|
2023-08-15 22:52:30 -05:00
|
|
|
|
|
|
|
|
|
if (_SurveyResponse != null)
|
|
|
|
|
{
|
2023-09-13 00:28:24 -05:00
|
|
|
|
var response = mapper.Map<Models.SurveyResponse, Db.SurveyResponse>(SurveyResponse);
|
|
|
|
|
surveyResponseDbContext.Update(response);
|
2023-08-27 10:55:58 -05:00
|
|
|
|
await surveyResponseDbContext.SaveChangesAsync();
|
2023-09-13 00:28:24 -05:00
|
|
|
|
return (true, SurveyResponse, "Successful");
|
2023-08-15 22:52:30 -05:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
logger?.LogInformation($"SurveyReponseId = {Id} Not found");
|
|
|
|
|
return (false, null, "Not Found");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
logger?.LogInformation($"SurveyReponseId = {Id} Bad Request");
|
|
|
|
|
return (false, null, "Bad request");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
logger?.LogError(ex.ToString());
|
|
|
|
|
return (false, null, ex.Message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<(bool IsSuccess, Models.SurveyResponse SurveyResponse, string ErrorMessage)> DeleteSurveyResponseAsync(int Id)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var _SurveyResponse = await surveyResponseDbContext.SurveyResponses.Where(s => s.Id == Id).FirstOrDefaultAsync();
|
|
|
|
|
|
|
|
|
|
if (_SurveyResponse != null)
|
|
|
|
|
{
|
|
|
|
|
surveyResponseDbContext.Remove(_SurveyResponse);
|
2023-08-27 10:55:58 -05:00
|
|
|
|
await surveyResponseDbContext.SaveChangesAsync();
|
2023-08-15 22:52:30 -05:00
|
|
|
|
return (true, mapper.Map<Db.SurveyResponse, Models.SurveyResponse>(_SurveyResponse), "Successful");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
logger?.LogInformation($"SurveyReponseId = {Id} Not found");
|
|
|
|
|
return (false, null, "Not Found");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
logger?.LogError(ex.ToString());
|
|
|
|
|
return (false, null, ex.Message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Method to get Answers by region with surveyId as input parameter
|
|
|
|
|
private async Task<dynamic> getAnswersByRegionAndSurveyIdAsync(IQueryable<Db.SurveyResponse> surveyResponses)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
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,
|
|
|
|
|
answer => answer.SurveyResponseId,
|
|
|
|
|
surveyResponse => surveyResponse.Id,
|
|
|
|
|
(answer, surveyResponse) => new
|
2023-09-20 23:58:29 -05:00
|
|
|
|
|
2023-08-15 22:52:30 -05:00
|
|
|
|
{
|
2023-09-13 00:28:24 -05:00
|
|
|
|
answer.Id,
|
|
|
|
|
answer.QuestionId,
|
|
|
|
|
answer.AnswerText,
|
|
|
|
|
answer.Comment,
|
|
|
|
|
surveyResponse.LocationId,
|
2023-08-15 22:52:30 -05:00
|
|
|
|
SurveyResponseId = surveyResponse.Id
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (surveyAnswers == null || !surveyAnswers.Any())
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
var regions = await regionServiceProvider.getRegionsAsync();
|
|
|
|
|
var locations = await locationServiceProvider.getLocationsAsync();
|
|
|
|
|
|
|
|
|
|
if (regions == null || !regions.Any() || locations == null || !locations.Any())
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//get all the answers based on the locations
|
|
|
|
|
var result = from answer in surveyAnswers
|
|
|
|
|
from location in locations
|
2023-09-13 00:28:24 -05:00
|
|
|
|
where answer.LocationId.Equals(location.Id)
|
2023-09-20 23:58:29 -05:00
|
|
|
|
select new
|
2023-08-15 22:52:30 -05:00
|
|
|
|
{
|
2023-09-20 23:58:29 -05:00
|
|
|
|
answer.Id,
|
|
|
|
|
answer.QuestionId,
|
|
|
|
|
answer.AnswerText,
|
|
|
|
|
answer.Comment,
|
|
|
|
|
location.RegionId,
|
|
|
|
|
LocationId = location.Id,
|
|
|
|
|
answer.SurveyResponseId
|
2023-08-15 22:52:30 -05:00
|
|
|
|
};
|
|
|
|
|
|
2023-09-20 23:58:29 -05:00
|
|
|
|
|
2023-09-13 00:28:24 -05:00
|
|
|
|
|
2023-08-15 22:52:30 -05:00
|
|
|
|
//group records by answer and region
|
|
|
|
|
var q = from e in result
|
|
|
|
|
group e by (e.RegionId, e.AnswerText) into g
|
2023-09-13 00:28:24 -05:00
|
|
|
|
select new
|
2023-08-15 22:52:30 -05:00
|
|
|
|
{
|
2023-09-13 00:28:24 -05:00
|
|
|
|
g.Key.RegionId,
|
2023-09-20 23:58:29 -05:00
|
|
|
|
Answers = new
|
2023-08-15 22:52:30 -05:00
|
|
|
|
{
|
2023-09-13 00:28:24 -05:00
|
|
|
|
g.Key.AnswerText,
|
2023-08-15 22:52:30 -05:00
|
|
|
|
Counter = g.Count()
|
|
|
|
|
}
|
|
|
|
|
};
|
2023-09-20 23:58:29 -05:00
|
|
|
|
|
2023-08-15 22:52:30 -05:00
|
|
|
|
//build the result
|
2023-09-13 00:28:24 -05:00
|
|
|
|
|
|
|
|
|
List<dynamic> resultList = new List<dynamic>();
|
2023-08-15 22:52:30 -05:00
|
|
|
|
foreach (Region region in regions)
|
|
|
|
|
{
|
2023-09-13 00:28:24 -05:00
|
|
|
|
var answers = from u in q.ToList()
|
|
|
|
|
where u.RegionId.Equals(region.Id)
|
2023-09-20 23:58:29 -05:00
|
|
|
|
select u.Answers;
|
|
|
|
|
|
|
|
|
|
resultList.Add(new { RegionId = region.Id, region.Name, region.Abbreviation, Answers = answers });
|
2023-08-15 22:52:30 -05:00
|
|
|
|
}
|
|
|
|
|
//return the object result
|
2023-09-13 00:28:24 -05:00
|
|
|
|
return new { Regions = resultList };
|
2023-08-15 22:52:30 -05:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
logger?.LogError($"Exception Found : {ex.Message} - Ref: SurveyResponsesProvider.getSurveyResultAsync()");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Method to get Survey Response by ResponseId
|
|
|
|
|
private async Task<dynamic> getSurveyResponseByResponseIdAsync(Db.SurveyResponse surveyResponse)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var employee = await employeeServiceProvider.getEmployeeAsync(surveyResponse.EmployeeId);
|
|
|
|
|
var answers = await answerServiceProvider.GetAnswersByResponseIdAsync(surveyResponse.Id);
|
|
|
|
|
var allQuestions = await questionServiceProvider.getQuestionsAsync();
|
2023-09-04 20:31:41 -05:00
|
|
|
|
var questions = allQuestions.Where(s => s.SurveyId == surveyResponse.SurveyId);
|
2023-08-15 22:52:30 -05:00
|
|
|
|
var attachments = await attachmentServiceProvider.getAttachmentsAsync();
|
|
|
|
|
|
2023-09-04 20:31:41 -05:00
|
|
|
|
var result = new
|
|
|
|
|
{
|
|
|
|
|
surveyResponse.Id,
|
|
|
|
|
surveyResponse.SurveyId,
|
|
|
|
|
surveyResponse.LocationId,
|
|
|
|
|
surveyResponse.EmployeeId,
|
|
|
|
|
surveyResponse.ClientDevice,
|
|
|
|
|
surveyResponse.KeyAnswerResult,
|
|
|
|
|
surveyResponse.Longitute,
|
|
|
|
|
surveyResponse.Latitude,
|
|
|
|
|
Employee = employee,
|
|
|
|
|
answers = from ans in answers
|
|
|
|
|
select new
|
|
|
|
|
{
|
|
|
|
|
ans.QuestionId,
|
|
|
|
|
ans.Id,
|
|
|
|
|
ans.AnswerText,
|
|
|
|
|
ans.Comment,
|
2023-09-13 00:28:24 -05:00
|
|
|
|
Questions = (from q in questions where q.Id == ans.QuestionId select new { q.Id, q.QuestionNumber, q.CategoryId, q.Text }).SingleOrDefault(),
|
2023-09-04 20:31:41 -05:00
|
|
|
|
Attachments = from att in attachments where att.AnswerId == ans.Id select new { att.Id, att.URI }
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
return result;
|
2023-08-15 22:52:30 -05:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
logger?.LogError($"Exception Found : {ex.Message} - Ref: SurveyResponsesProvider.getSurveyResultAsync()");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//Method to get Survey Responses by surveyId
|
2023-09-20 23:58:29 -05:00
|
|
|
|
private async Task<dynamic> getSurveyResponsesBySurveyIdAsync(int surveyId, int employeeid)
|
2023-08-15 22:52:30 -05:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2023-09-20 23:58:29 -05:00
|
|
|
|
List<Db.SurveyResponse> surveyResonses = null;
|
|
|
|
|
Employee employee = null;
|
|
|
|
|
List<Employee> employees = null;
|
|
|
|
|
if (employeeid == 0)
|
|
|
|
|
{
|
|
|
|
|
surveyResonses = await surveyResponseDbContext.SurveyResponses.Where(x => x.SurveyId == surveyId).ToListAsync();
|
|
|
|
|
employees = await employeeServiceProvider.getEmployeesAsync();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
surveyResonses = await surveyResponseDbContext.SurveyResponses.Where(x => x.SurveyId == surveyId && x.EmployeeId == employeeid).ToListAsync();
|
|
|
|
|
employee = await employeeServiceProvider.getEmployeeAsync(employeeid);
|
|
|
|
|
}
|
2023-08-15 22:52:30 -05:00
|
|
|
|
|
|
|
|
|
var answers = await answerServiceProvider.getAnswersAsync();
|
|
|
|
|
var questions = await questionServiceProvider.getQuestionsAsync();
|
|
|
|
|
var surveyQuestions = from q in questions where q.SurveyId == surveyId select q;
|
|
|
|
|
|
|
|
|
|
//var surveyQuestions = await questionServiceProvider.getSurveyQuestionsAsync(surveyId);
|
|
|
|
|
|
|
|
|
|
var attachments = await attachmentServiceProvider.getAttachmentsAsync();
|
|
|
|
|
|
2023-09-20 23:58:29 -05:00
|
|
|
|
if (employeeid == 0)
|
|
|
|
|
{
|
|
|
|
|
var result = from r in surveyResonses
|
|
|
|
|
select new
|
|
|
|
|
{
|
|
|
|
|
r.Id,
|
|
|
|
|
r.SurveyId,
|
|
|
|
|
r.LocationId,
|
|
|
|
|
r.EmployeeId,
|
|
|
|
|
r.ClientDevice,
|
|
|
|
|
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(),
|
|
|
|
|
answers = from ans in answers
|
|
|
|
|
where ans.SurveyResponseId == r.Id
|
|
|
|
|
select new
|
|
|
|
|
{
|
|
|
|
|
ans.Id,
|
|
|
|
|
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(),
|
|
|
|
|
Attachments = from att in attachments where att.AnswerId == ans.Id select new { att.Id, att.URI }
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
object _employee = new { };
|
|
|
|
|
if (employee != null)
|
|
|
|
|
{
|
|
|
|
|
_employee = new { employee.Id, employee.Name, employee.BirthDate, employee.Email, employee.OfficePhoneNumber };
|
|
|
|
|
}
|
|
|
|
|
var result = from r in surveyResonses
|
|
|
|
|
select new
|
|
|
|
|
{
|
|
|
|
|
r.Id,
|
|
|
|
|
r.SurveyId,
|
|
|
|
|
r.LocationId,
|
|
|
|
|
r.EmployeeId,
|
|
|
|
|
r.ClientDevice,
|
|
|
|
|
r.KeyAnswerResult,
|
|
|
|
|
r.Longitute,
|
|
|
|
|
r.Latitude,
|
|
|
|
|
Employee = _employee,
|
|
|
|
|
answers = from ans in answers
|
|
|
|
|
where ans.SurveyResponseId == r.Id
|
|
|
|
|
select new
|
|
|
|
|
{
|
|
|
|
|
ans.Id,
|
|
|
|
|
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(),
|
|
|
|
|
Attachments = from att in attachments where att.AnswerId == ans.Id select new { att.Id, att.URI }
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2023-08-15 22:52:30 -05:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
logger?.LogError($"Exception Found : {ex.Message} - Ref: SurveyResponsesProvider.getSurveyResponseBySurveyIdAsync()");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//Method to get All Survey Responses
|
2023-09-20 23:58:29 -05:00
|
|
|
|
private async Task<dynamic> getAllSurveyResponsesAsync(int employeeid)
|
2023-08-15 22:52:30 -05:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2023-09-20 23:58:29 -05:00
|
|
|
|
List<Db.SurveyResponse> surveyResonses = null;
|
|
|
|
|
Employee employee = null;
|
|
|
|
|
List<Employee> employees = null;
|
|
|
|
|
object _employee = new { };
|
|
|
|
|
if (employeeid == 0)
|
|
|
|
|
{
|
|
|
|
|
surveyResonses = await surveyResponseDbContext.SurveyResponses.ToListAsync();
|
|
|
|
|
employees = await employeeServiceProvider.getEmployeesAsync();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
surveyResonses = await surveyResponseDbContext.SurveyResponses.Where(x => x.EmployeeId == employeeid).ToListAsync();
|
|
|
|
|
employee = await employeeServiceProvider.getEmployeeAsync(employeeid);
|
|
|
|
|
|
|
|
|
|
if (employee != null)
|
|
|
|
|
{
|
|
|
|
|
_employee = new { employee.Id, employee.Name, employee.BirthDate, employee.Email, employee.OfficePhoneNumber };
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-15 22:52:30 -05:00
|
|
|
|
|
|
|
|
|
var answers = await answerServiceProvider.getAnswersAsync();
|
|
|
|
|
var questions = await questionServiceProvider.getQuestionsAsync();
|
|
|
|
|
var attachments = await attachmentServiceProvider.getAttachmentsAsync();
|
|
|
|
|
|
|
|
|
|
var result = from r in surveyResonses
|
|
|
|
|
select new
|
|
|
|
|
{
|
|
|
|
|
r.Id,
|
|
|
|
|
r.SurveyId,
|
|
|
|
|
r.LocationId,
|
|
|
|
|
r.EmployeeId,
|
2023-09-04 20:31:41 -05:00
|
|
|
|
r.ClientDevice,
|
|
|
|
|
r.KeyAnswerResult,
|
|
|
|
|
r.Longitute,
|
|
|
|
|
r.Latitude,
|
2023-09-20 23:58:29 -05:00
|
|
|
|
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(),
|
2023-08-15 22:52:30 -05:00
|
|
|
|
answers = from ans in answers
|
|
|
|
|
where ans.SurveyResponseId == r.Id
|
|
|
|
|
select new
|
|
|
|
|
{
|
|
|
|
|
ans.Id,
|
|
|
|
|
ans.QuestionId,
|
|
|
|
|
ans.AnswerText,
|
|
|
|
|
ans.Comment,
|
2023-09-13 00:28:24 -05:00
|
|
|
|
Questions = (from q in questions where q.Id == ans.QuestionId select new { q.Id, q.QuestionNumber, q.CategoryId, q.Text }).SingleOrDefault(),
|
2023-08-15 22:52:30 -05:00
|
|
|
|
Attachments = from att in attachments where att.AnswerId == ans.Id select new { att.Id, att.URI }
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
return result;
|
2023-09-20 23:58:29 -05:00
|
|
|
|
|
|
|
|
|
|
2023-08-15 22:52:30 -05:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
logger?.LogError($"Exception Found : {ex.Message} - Ref: SurveyResponsesProvider.getSurveyResponseBySurveyIdAsync()");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//Method to get Answers By Maintenance Center by surveyId
|
2023-09-20 23:58:29 -05:00
|
|
|
|
private async Task<dynamic> getResultsByMaintenanceCenterAsync(int surveyId, int employeeid)
|
2023-08-15 22:52:30 -05:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2023-09-20 23:58:29 -05:00
|
|
|
|
List<Db.SurveyResponse> surveyResponses = null;
|
|
|
|
|
if (employeeid == 0)
|
|
|
|
|
{
|
|
|
|
|
surveyResponses = await surveyResponseDbContext.SurveyResponses.Where(x => x.SurveyId == surveyId).ToListAsync();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
surveyResponses = await surveyResponseDbContext.SurveyResponses.Where(x => x.SurveyId == surveyId && x.EmployeeId == employeeid).ToListAsync();
|
|
|
|
|
}
|
2023-08-15 22:52:30 -05:00
|
|
|
|
var answers = await answerServiceProvider.getAnswersAsync();
|
|
|
|
|
var locations = await locationServiceProvider.getLocationsAsync();
|
|
|
|
|
var maintenanceCenters = locations.DistinctBy(m => m.MaintenanceCenter);
|
|
|
|
|
|
|
|
|
|
//get all the answers for the particular survey
|
|
|
|
|
var surveyAnswers = from resp in surveyResponses
|
|
|
|
|
from ans in answers
|
|
|
|
|
where ans.SurveyResponseId.Equals(resp.Id)
|
|
|
|
|
select new
|
|
|
|
|
{
|
|
|
|
|
AnswerId = ans.Id,
|
|
|
|
|
ans.QuestionId,
|
|
|
|
|
ans.AnswerText,
|
|
|
|
|
ans.Comment,
|
|
|
|
|
resp.LocationId,
|
|
|
|
|
ResponseId = resp.Id
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//get all the answers with location
|
|
|
|
|
var surveyAnswersLocations = from surveyAns in surveyAnswers
|
|
|
|
|
from location in locations
|
2023-09-13 00:28:24 -05:00
|
|
|
|
where surveyAns.LocationId.Equals(location.Id)
|
2023-08-15 22:52:30 -05:00
|
|
|
|
select new { surveyAns, location.MaintenanceCenter };
|
|
|
|
|
|
|
|
|
|
//aggreting the answers
|
|
|
|
|
var aggregatedResult = from e in surveyAnswersLocations
|
|
|
|
|
group e by (e.MaintenanceCenter, e.surveyAns.AnswerText) into g
|
|
|
|
|
select new
|
|
|
|
|
{
|
|
|
|
|
g.Key.MaintenanceCenter,
|
2023-09-20 23:58:29 -05:00
|
|
|
|
Answers = new
|
2023-08-15 22:52:30 -05:00
|
|
|
|
{
|
2023-09-13 00:28:24 -05:00
|
|
|
|
g.Key.AnswerText,
|
2023-08-15 22:52:30 -05:00
|
|
|
|
Counter = g.Count()
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//format the result foreach maintenance center
|
|
|
|
|
List<dynamic> results = new List<dynamic>();
|
|
|
|
|
foreach (Location m in maintenanceCenters)
|
|
|
|
|
{
|
|
|
|
|
var _answers = aggregatedResult.Where(x => x.MaintenanceCenter.Equals(m.MaintenanceCenter)).Select(x => x.Answers).ToList();
|
|
|
|
|
results.Add(new { m.Name, m.Id, m.RegionId, m.MaintenanceCenter, m.SchoolType, Answers = _answers });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return results;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
logger?.LogError($"Exception Found : {ex.Message} - Ref: SurveyResponsesProvider.getSurveyResponseBySurveyIdAsync()");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Method to get Survey Responses by surveyId and LocationId
|
2023-09-20 23:58:29 -05:00
|
|
|
|
private async Task<dynamic> getSurveyResponsesBySurveyIdLocationIdAsync(int surveyId, int locationId, int employeeid)
|
2023-08-15 22:52:30 -05:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2023-09-20 23:58:29 -05:00
|
|
|
|
List<Db.SurveyResponse> surveyResonses = null;
|
|
|
|
|
Employee employee = null;
|
|
|
|
|
List<Employee> employees = null;
|
|
|
|
|
object _employee = new { };
|
|
|
|
|
|
|
|
|
|
if (employeeid == 0)
|
|
|
|
|
{
|
|
|
|
|
surveyResonses = await surveyResponseDbContext.SurveyResponses.Where(x => x.SurveyId == surveyId && x.LocationId == locationId).ToListAsync();
|
|
|
|
|
employees = await employeeServiceProvider.getEmployeesAsync();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
surveyResonses = await surveyResponseDbContext.SurveyResponses.Where(x => x.SurveyId == surveyId && x.EmployeeId == employeeid && x.LocationId == locationId).ToListAsync();
|
|
|
|
|
employee = await employeeServiceProvider.getEmployeeAsync(employeeid);
|
|
|
|
|
|
|
|
|
|
if (employee != null)
|
|
|
|
|
{
|
|
|
|
|
_employee = new { employee.Id, employee.Name, employee.BirthDate, employee.Email, employee.OfficePhoneNumber };
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-08-15 22:52:30 -05:00
|
|
|
|
|
|
|
|
|
var answers = await answerServiceProvider.getAnswersAsync();
|
|
|
|
|
var questions = await questionServiceProvider.getQuestionsAsync();
|
|
|
|
|
var surveyQuestions = from q in questions where q.SurveyId == surveyId select q;
|
|
|
|
|
var attachments = await attachmentServiceProvider.getAttachmentsAsync();
|
|
|
|
|
|
|
|
|
|
var result = from r in surveyResonses
|
|
|
|
|
select new
|
|
|
|
|
{
|
|
|
|
|
r.Id,
|
|
|
|
|
r.SurveyId,
|
|
|
|
|
r.LocationId,
|
|
|
|
|
r.EmployeeId,
|
2023-09-04 20:31:41 -05:00
|
|
|
|
r.ClientDevice,
|
|
|
|
|
r.KeyAnswerResult,
|
|
|
|
|
r.Longitute,
|
|
|
|
|
r.Latitude,
|
2023-09-20 23:58:29 -05:00
|
|
|
|
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(),
|
2023-08-15 22:52:30 -05:00
|
|
|
|
answers = from ans in answers
|
|
|
|
|
where ans.SurveyResponseId == r.Id
|
|
|
|
|
|
|
|
|
|
select new
|
|
|
|
|
{
|
|
|
|
|
ans.QuestionId,
|
|
|
|
|
ans.Id,
|
|
|
|
|
ans.AnswerText,
|
|
|
|
|
ans.Comment,
|
2023-09-13 00:28:24 -05:00
|
|
|
|
Questions = (from q in surveyQuestions where q.Id == ans.QuestionId select new { q.Id, q.QuestionNumber, q.CategoryId, q.Text }).SingleOrDefault(),
|
2023-08-15 22:52:30 -05:00
|
|
|
|
Attachments = from att in attachments where att.AnswerId == ans.Id select new { att.Id, att.URI }
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
return result;
|
2023-09-20 23:58:29 -05:00
|
|
|
|
|
2023-08-15 22:52:30 -05:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
logger?.LogError($"Exception Found : {ex.Message} - Ref: SurveyResponsesProvider.getSurveyResponseBySurveyIdAsync()");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//Method to get Survey Responses by surveyId questionId and answer
|
2023-09-20 23:58:29 -05:00
|
|
|
|
private async Task<dynamic> getSurveyResponsesByAnswerAsync(Survey survey, Question question, string answer, int employeeid)
|
2023-08-15 22:52:30 -05:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2023-09-20 23:58:29 -05:00
|
|
|
|
List<Db.SurveyResponse> surveyResponses = null;
|
|
|
|
|
Employee employee = null;
|
|
|
|
|
List<Employee> employees = null;
|
|
|
|
|
object _employee = new { };
|
|
|
|
|
|
|
|
|
|
if (employeeid == 0)
|
|
|
|
|
{
|
|
|
|
|
surveyResponses = await surveyResponseDbContext.SurveyResponses.Where(x => x.SurveyId == survey.Id).ToListAsync();
|
|
|
|
|
employees = await employeeServiceProvider.getEmployeesAsync();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
surveyResponses = await surveyResponseDbContext.SurveyResponses.Where(x => x.SurveyId == survey.Id && x.EmployeeId == employeeid).ToListAsync();
|
|
|
|
|
employee = await employeeServiceProvider.getEmployeeAsync(employeeid);
|
|
|
|
|
|
|
|
|
|
if (employee != null)
|
|
|
|
|
{
|
|
|
|
|
_employee = new { employee.Id, employee.Name, employee.BirthDate, employee.Email, employee.OfficePhoneNumber };
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-04 17:45:51 -05:00
|
|
|
|
//var surveyResponses = await surveyResponseDbContext.Responses.Where(x => x.SurveyId == survey.Id).ToListAsync();
|
2023-09-20 23:58:29 -05:00
|
|
|
|
// var employees = await employeeServiceProvider.getEmployeesAsync();
|
2023-08-15 22:52:30 -05:00
|
|
|
|
var answers = await answerServiceProvider.getAnswersAsync();
|
|
|
|
|
var attachments = await attachmentServiceProvider.getAttachmentsAsync();
|
|
|
|
|
|
|
|
|
|
var result = from r in surveyResponses
|
|
|
|
|
select new
|
|
|
|
|
{
|
|
|
|
|
r.Id,
|
|
|
|
|
r.SurveyId,
|
|
|
|
|
r.LocationId,
|
|
|
|
|
r.EmployeeId,
|
2023-09-04 20:31:41 -05:00
|
|
|
|
r.ClientDevice,
|
|
|
|
|
r.KeyAnswerResult,
|
|
|
|
|
r.Longitute,
|
|
|
|
|
r.Latitude,
|
2023-09-20 23:58:29 -05:00
|
|
|
|
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(),
|
2023-08-15 22:52:30 -05:00
|
|
|
|
answers = from ans in answers
|
|
|
|
|
where ans.SurveyResponseId == r.Id
|
|
|
|
|
&& ans.QuestionId == question.Id
|
|
|
|
|
&& ans.AnswerText.ToLower().Equals(answer.ToLower())
|
|
|
|
|
|
|
|
|
|
select new
|
|
|
|
|
{
|
|
|
|
|
ans.QuestionId,
|
|
|
|
|
AnswerId = ans.Id,
|
|
|
|
|
ans.AnswerText,
|
|
|
|
|
ans.Comment,
|
|
|
|
|
Question = question,
|
|
|
|
|
Attachments = from att in attachments where att.AnswerId == ans.Id select new { att.Id, att.URI }
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
logger?.LogError($"Exception Found : {ex.Message} - Ref: SurveyResponsesProvider.getSurveyResponseBySurveyIdAsync()");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2023-08-27 10:55:58 -05:00
|
|
|
|
async Task<bool> ProcessAnswers(AnswerRequest answerRequest, int surveyResponseId)
|
2023-08-15 22:52:30 -05:00
|
|
|
|
{
|
2023-08-27 10:55:58 -05:00
|
|
|
|
if (answerRequest != null)
|
|
|
|
|
{
|
2023-09-04 20:31:41 -05:00
|
|
|
|
var answer = await answerServiceProvider.PostAnswersAsync(new Models.Answer { QuestionId = answerRequest.QuestionId, AnswerText = answerRequest.AnswerText, Comment = answerRequest.Comment, SurveyResponseId = surveyResponseId });
|
|
|
|
|
if (answer != null)
|
|
|
|
|
{
|
|
|
|
|
List<AnswerInfo> listAnswerInfo = new List<AnswerInfo>();
|
|
|
|
|
listAnswerInfo.Add(new AnswerInfo { AnswerId = answer.Id, postedFiles = answerRequest.PostedFiles });
|
|
|
|
|
var attachments = attachmentServiceProvider.PostAttachmentsAsync(new AttachmentInfo { ResponseId = surveyResponseId, Answers = listAnswerInfo });
|
|
|
|
|
|
|
|
|
|
string message = $"Answer for question {answerRequest.QuestionId} saved to the database";
|
|
|
|
|
logger?.LogInformation(message);
|
|
|
|
|
return (true);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
string message = $"Answer for question {answerRequest.QuestionId} cannot be saved to the database";
|
|
|
|
|
logger?.LogInformation(message);
|
|
|
|
|
return (false);
|
|
|
|
|
}
|
2023-08-15 22:52:30 -05:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2023-08-27 10:55:58 -05:00
|
|
|
|
var message = $"Answer for question {answerRequest.QuestionId} cannot be saved to the database - answerRequest object is null";
|
2023-08-15 22:52:30 -05:00
|
|
|
|
logger?.LogInformation(message);
|
|
|
|
|
return (false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2023-08-27 10:55:58 -05:00
|
|
|
|
public async Task<(bool IsSuccess, Models.SurveyResponse SurveyResponse, string ErrorMessage)> PostSurveyAnswersAsync(Models.Request request)
|
2023-08-15 22:52:30 -05:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2023-08-27 10:55:58 -05:00
|
|
|
|
if (request != null)
|
2023-08-15 22:52:30 -05:00
|
|
|
|
{
|
2023-09-20 23:58:29 -05:00
|
|
|
|
var response = await PostSurveyResponseAsync(new Models.SurveyResponse { SurveyId = request.SurveyId, EmployeeId = request.EmployeeId, LocationId = request.LocationId, ClientDevice = request.ClientDevice, KeyAnswerResult = request.KeyAnswerResult, Latitude = Convert.ToDouble(request.Latitude), Longitute = Convert.ToDouble(request.Longitute), CreatedDate = DateTime.Now });
|
2023-08-15 22:52:30 -05:00
|
|
|
|
if (response.IsSuccess)
|
|
|
|
|
{
|
|
|
|
|
var surveyResponse = response.SurveyResponse;
|
2023-09-04 20:31:41 -05:00
|
|
|
|
var tasks = request.Answers.Select(x => ProcessAnswers(x, surveyResponse.Id));
|
2023-08-27 10:55:58 -05:00
|
|
|
|
await Task.WhenAll(tasks);
|
2023-08-15 22:52:30 -05:00
|
|
|
|
return (true, surveyResponse, null);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var message = "Survey answers cannot be saved to the database";
|
|
|
|
|
logger?.LogInformation(message);
|
|
|
|
|
return (false, null, message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var message = "Request failed because null object is submitted";
|
|
|
|
|
logger?.LogInformation(message);
|
|
|
|
|
return (false, null, message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
logger?.LogError(ex.ToString());
|
|
|
|
|
return (false, null, ex.Message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|