diff --git a/DamageAssesmentApi/DamageAssesment.Api.Responses/Controllers/SurveyResponsesController.cs b/DamageAssesmentApi/DamageAssesment.Api.Responses/Controllers/SurveyResponsesController.cs index 9fce5f8..871106c 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Responses/Controllers/SurveyResponsesController.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Responses/Controllers/SurveyResponsesController.cs @@ -218,16 +218,16 @@ namespace DamageAssesment.Api.Responses.Controllers return NoContent(); } /// - /// Export surveys based on role . + /// Export all survey response data based on survey id. /// [HttpGet] - - [Route("responses/surveys/export")] - public async Task GetExcelSurveysAsync(string language,bool IsAdmin=false) + [Route("responses/surveys/export/{surveyid}")] + public async Task GetExcelSurveysAsync(int surveyid, string language, bool IsAdmin = false) { - var result = await this.surveyResponseProvider.ExportSurveyResponsesAsync(language, IsAdmin); - if (result.IsSuccess) + var result = await this.surveyResponseProvider.ExportSurveyResponsesAsync(surveyid, language, IsAdmin); + if (result.IsSuccess && result.surveyResponses.Count > 0) { + byte[] fileContents = excelExportService.ExportToExcel(result.surveyResponses); return File(fileContents, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "data.xlsx"); //return Ok(result.Surveys); diff --git a/DamageAssesmentApi/DamageAssesment.Api.Responses/Interfaces/ISurveysResponse.cs b/DamageAssesmentApi/DamageAssesment.Api.Responses/Interfaces/ISurveysResponse.cs index ae5d380..05a8eed 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Responses/Interfaces/ISurveysResponse.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Responses/Interfaces/ISurveysResponse.cs @@ -9,7 +9,7 @@ namespace DamageAssesment.Api.Responses.Interfaces Task<(bool IsSuccess, Models.SurveyResponse SurveyResponse, string ErrorMessage)> PostSurveyResponseAsync(Models.SurveyResponse surveyResponse); // Task<(bool IsSuccess,dynamic surveyResponses, string ErrorMessage)> GetSurveyResponseAsync(int responseId); Task<(bool IsSuccess, dynamic surveyResponses, string ErrorMessage)> GetSurveyResponsesAsync(int employeeid); - Task<(bool IsSuccess, List surveyResponses, string ErrorMessage)> ExportSurveyResponsesAsync(string language,bool IsAdmin); + Task<(bool IsSuccess, List surveyResponses, string ErrorMessage)> ExportSurveyResponsesAsync(int surveyId, string language, bool IsAdmin); Task<(bool IsSuccess, dynamic Surveys, string ErrorMessage)> GetActiveSurveysAsync(int? employeeid, string language); Task<(bool IsSuccess, dynamic Surveys, string ErrorMessage)> GetHistoricSurveysAsync(int? employeeid, string language); Task<(bool IsSuccess, Models.SurveyResponse SurveyResponse, string ErrorMessage)> PutSurveyResponseAsync(int Id, Models.SurveyResponse surveyResponse); diff --git a/DamageAssesmentApi/DamageAssesment.Api.Responses/Models/Survey.cs b/DamageAssesmentApi/DamageAssesment.Api.Responses/Models/Survey.cs index 8265046..5419251 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Responses/Models/Survey.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Responses/Models/Survey.cs @@ -2,13 +2,20 @@ namespace DamageAssesment.Api.Responses.Models { + public enum SurveyStatus + { + PENDING, + ACTIVE, + INACTIVE + } public class Survey { public int Id { get; set; } public bool IsEnabled { get; set; } - public DateTime StartDate { get; set; } - public DateTime EndDate { get; set; } + public DateTime? StartDate { get; set; } + public DateTime? EndDate { get; set; } public DateTime CreatedDate { get; set; } + public string Status { get; set; } public Dictionary Titles { get; set; } } } diff --git a/DamageAssesmentApi/DamageAssesment.Api.Responses/Providers/ExcelExportService.cs b/DamageAssesmentApi/DamageAssesment.Api.Responses/Providers/ExcelExportService.cs index b354b8d..2fadd9d 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Responses/Providers/ExcelExportService.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Responses/Providers/ExcelExportService.cs @@ -38,17 +38,22 @@ namespace DamageAssesment.Api.Responses.Providers { for (int col = 1; col <= properties.Length; col++) { - string value = Convert.ToString(properties[col - 1].GetValue(data[row - 2])); - if (IsAttchments.Where(a => a == col).Count()>0&& !string.IsNullOrEmpty(value)) + + string value = Convert.ToString(properties[col - 1].GetValue(data[row - 2])); + if (IsAttchments.Where(a => a == col).Count() > 0 && !string.IsNullOrEmpty(value)) { List attachments = value.Split("##").ToList(); - Uri linkUri = new Uri(attachments[1]); - worksheet.Cells[row, col].Hyperlink = linkUri; - worksheet.Cells[row, col].Value = attachments[0]; - worksheet.Cells[row, col].Style.Font.UnderLine = true; + try + { + Uri linkUri = new Uri(attachments[1]); + worksheet.Cells[row, col].Value = attachments[0]; + worksheet.Cells[row, col].Style.Font.UnderLine = true; + } + catch { worksheet.Cells[row, col].Value = attachments[1]; } } else worksheet.Cells[row, col].Value = value; + } } } diff --git a/DamageAssesmentApi/DamageAssesment.Api.Responses/Providers/SurveyResponsesProvider.cs b/DamageAssesmentApi/DamageAssesment.Api.Responses/Providers/SurveyResponsesProvider.cs index d007578..d52d153 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Responses/Providers/SurveyResponsesProvider.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Responses/Providers/SurveyResponsesProvider.cs @@ -121,7 +121,7 @@ namespace DamageAssesment.Api.Responses.Providers 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); - surveys = surveys.Where(s => s.IsEnabled == true && s.StartDate <= DateTime.Now && s.EndDate >= DateTime.Now).ToList(); + surveys = surveys.Where(s => s.IsEnabled == true && s.Status == SurveyStatus.ACTIVE.ToString()).ToList(); if (employeeid == null || employeeid == 0) return (true, surveys, null); List listOfsurveysId = await surveyResponseDbContext.SurveyResponses.Where(x => x.EmployeeId == employeeid.Value).Select(y => y.SurveyId).ToListAsync(); @@ -143,7 +143,7 @@ namespace DamageAssesment.Api.Responses.Providers 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(); + surveys = surveys.Where(s => s.Status == SurveyStatus.INACTIVE.ToString()).ToList(); if (employeeid == null || employeeid == 0) return (true, surveys, null); var surveyResponses = await surveyResponseDbContext.SurveyResponses.Where(x => x.EmployeeId == employeeid).ToListAsync(); @@ -309,11 +309,11 @@ namespace DamageAssesment.Api.Responses.Providers return (false, null, ex.Message); } } - public async Task<(bool IsSuccess, List surveyResponses, string ErrorMessage)> ExportSurveyResponsesAsync(string language,bool isadmin) + public async Task<(bool IsSuccess, List surveyResponses, string ErrorMessage)> ExportSurveyResponsesAsync(int surveyId, string language, bool isadmin) { try { - var responses = await getAllSurveyResponsesExcelAsync(language, isadmin); + var responses = await getAllSurveyResponsesExcelAsync(surveyId, language, isadmin); if (responses != null) return (true, responses, "Request Successful."); @@ -709,13 +709,13 @@ namespace DamageAssesment.Api.Responses.Providers } } //Method to get All Survey Responses for excel export - private async Task> getAllSurveyResponsesExcelAsync(string language,bool isadmin) + private async Task> getAllSurveyResponsesExcelAsync(int surveyId, string language, bool isadmin) { try { if (string.IsNullOrEmpty(language)) language = "en"; List surveyResonses; - surveyResonses = await surveyResponseDbContext.SurveyResponses.ToListAsync(); + surveyResonses = await surveyResponseDbContext.SurveyResponses.Where(a => a.SurveyId == surveyId).ToListAsync(); var answers = await answerServiceProvider.getAnswersAsync(); var Locations = await locationServiceProvider.getLocationsAsync(); var regions = await regionServiceProvider.getRegionsAsync(); @@ -726,33 +726,33 @@ namespace DamageAssesment.Api.Responses.Providers var allques = from res in surveyResonses join loc in Locations on res.LocationId equals loc.Id join reg in regions on loc.RegionId equals reg.Id - join ans in answers on res.Id equals ans.SurveyResponseId + join ans in answers on res.Id equals ans.SurveyResponseId join q in questions on ans.QuestionId equals q.Id join qc in categories on q.CategoryId equals qc.Id - select new - { - responseId = res.Id, - questionId = q.Id, - QuestionNumber = q.QuestionNumber, - Category = JsonSerializer.Deserialize>(qc.Titles.ToString())[language], - question = q.Text[language], - answerId = ans.Id, - AnswerText = ans.AnswerText, - Comment = ans.Comment, - Location=loc.LocationCode, - school=loc.Name, - Region=reg.Name, - MC=loc.MaintenanceCenter, - ResponseDate=res.CreatedDate, - EmployeeId=res.EmployeeId, - ClientDevice=res.ClientDevice, - Attachments = attachments.Where(a=>a.AnswerId==ans.Id).Select(a=>a.FileName+"##"+a.URI).ToList() - }; + select new + { + responseId = res.Id, + questionId = q.Id, + QuestionNumber = q.QuestionNumber, + Category = JsonSerializer.Deserialize>(qc.Titles.ToString())[language], + question = q.Text[language], + answerId = ans.Id, + AnswerText = ans.AnswerText, + Comment = ans.Comment, + Location = loc.LocationCode, + school = loc.Name, + Region = reg.Name, + MC = loc.MaintenanceCenter, + ResponseDate = res.CreatedDate, + EmployeeId = res.EmployeeId, + ClientDevice = res.ClientDevice, + Attachments = attachments.Where(a => a.AnswerId == ans.Id).Select(a => a.FileName + "##" + a.URI).ToList() + }; List allresoponses = new List(); foreach (var item in allques) { - - List ansattachments=item.Attachments.ToList(); + + List ansattachments = item.Attachments.ToList(); //// Initialize the attachment dictionary //var attachmentsobject = new Dictionary(); @@ -776,11 +776,11 @@ namespace DamageAssesment.Api.Responses.Providers } // Now, you can access the values using the variables - string att1 = variables[0],att2 = variables[1],att3 = variables[2],att4 = variables[3],att5 = variables[4]; + string att1 = variables[0], att2 = variables[1], att3 = variables[2], att4 = variables[3], att5 = variables[4]; object response; if (isadmin) { - response = new + response = new { SurveyQuestion = item.question, Answer = item.AnswerText, @@ -791,12 +791,12 @@ namespace DamageAssesment.Api.Responses.Providers MC = item.MC, ResponseDate = item.ResponseDate.ToString(), Notes = item.Comment, - Attachment1 = att1, - Attachment2 = att2, - Attachment3 = att3, - Attachment4 = att4, - Attachment5 = att5, - User = item.EmployeeId, + Attachment1 = att1, + Attachment2 = att2, + Attachment3 = att3, + Attachment4 = att4, + Attachment5 = att5, + User = item.EmployeeId, DeviceType = item.ClientDevice, Reference = item.responseId }; @@ -811,7 +811,7 @@ namespace DamageAssesment.Api.Responses.Providers Answer = item.AnswerText, Category = item.Category, School = item.school, - Location=item.Location, + Location = item.Location, Region = item.Region, MC = item.MC, ResponseDate = item.ResponseDate.ToString(), @@ -824,7 +824,7 @@ namespace DamageAssesment.Api.Responses.Providers }; // Add the attachment dictionary to the response object - // response = new { response, Attachments = attachments }; + // response = new { response, Attachments = attachments }; } allresoponses.Add(response); } diff --git a/DamageAssesmentApi/DamageAssesment.Api.Surveys/Db/Survey.cs b/DamageAssesmentApi/DamageAssesment.Api.Surveys/Db/Survey.cs index aa49acc..2f19281 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Surveys/Db/Survey.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Surveys/Db/Survey.cs @@ -13,9 +13,9 @@ namespace DamageAssesment.Api.Surveys.Db public bool IsEnabled { get; set; } - public DateTime StartDate { get; set; } + public DateTime? StartDate { get; set; } - public DateTime EndDate { get; set; } + public DateTime? EndDate { get; set; } public DateTime CreatedDate { get; set; } = DateTime.Now; /* diff --git a/DamageAssesmentApi/DamageAssesment.Api.Surveys/Models/Survey.cs b/DamageAssesmentApi/DamageAssesment.Api.Surveys/Models/Survey.cs index 38fa72a..746047c 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Surveys/Models/Survey.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Surveys/Models/Survey.cs @@ -21,8 +21,8 @@ namespace DamageAssesment.Api.Surveys.Models { public int Id { get; set; } public bool IsEnabled { get; set; } - public DateTime StartDate { get; set; } - public DateTime EndDate { get; set; } - public DateTime CreatedDate { get; set; } + public DateTime? StartDate { get; set; } + public DateTime? EndDate { get; set; } + public DateTime? CreatedDate { get; set; } } } diff --git a/DamageAssesmentApi/DamageAssesment.Api.Surveys/Providers/SurveysProvider.cs b/DamageAssesmentApi/DamageAssesment.Api.Surveys/Providers/SurveysProvider.cs index a6a111f..72b4aff 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Surveys/Providers/SurveysProvider.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Surveys/Providers/SurveysProvider.cs @@ -85,14 +85,21 @@ namespace DamageAssesment.Api.Surveys.Providers MultiLanguage = dict; return MultiLanguage; } - public string GetStatus(DateTime StartDate,DateTime EndDate) + public string GetStatus(DateTime? StartDate,DateTime? EndDate) { - if (StartDate > DateTime.Now) - return SurveyStatus.PENDING.ToString(); - else if (StartDate <= DateTime.Now && EndDate > DateTime.Now) - return SurveyStatus.ACTIVE.ToString(); - else + try + { + if (StartDate > DateTime.Now) + return SurveyStatus.PENDING.ToString(); + else if (StartDate <= DateTime.Now && EndDate > DateTime.Now) + return SurveyStatus.ACTIVE.ToString(); + else + return SurveyStatus.INACTIVE.ToString(); + } + catch + { return SurveyStatus.INACTIVE.ToString(); + } } // Method to get surveys asynchronously with multi-language support public async Task<(bool IsSuccess, IEnumerable Surveys, string ErrorMessage)> GetSurveysAsync(string language) @@ -136,7 +143,8 @@ namespace DamageAssesment.Api.Surveys.Providers try { logger?.LogInformation("Query Survey"); - var survey = await surveyDbContext.Surveys.SingleOrDefaultAsync(s => s.Id == id && s.IsEnabled == true); + // removed is enabled becuase we are using it in responses to get response + var survey = await surveyDbContext.Surveys.SingleOrDefaultAsync(s => s.Id == id); if (survey != null) {