From c80749e292f23ba04ec392f535ec9d13a9866943 Mon Sep 17 00:00:00 2001 From: uppuv Date: Sun, 5 Nov 2023 13:20:29 -0500 Subject: [PATCH] ixed survey issue in response level, and added logic for start and end date as optional date. --- .../Controllers/SurveyResponsesController.cs | 6 ++--- .../Models/Survey.cs | 11 ++++++++-- .../Providers/ExcelExportService.cs | 16 +++++++++----- .../Providers/SurveyResponsesProvider.cs | 10 ++++----- .../DamageAssesment.Api.Surveys/Db/Survey.cs | 5 +++-- .../Models/Survey.cs | 6 ++--- .../Providers/SurveysProvider.cs | 22 +++++++++++++------ 7 files changed, 49 insertions(+), 27 deletions(-) diff --git a/DamageAssesmentApi/DamageAssesment.Api.Responses/Controllers/SurveyResponsesController.cs b/DamageAssesmentApi/DamageAssesment.Api.Responses/Controllers/SurveyResponsesController.cs index a8f36f4..871106c 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Responses/Controllers/SurveyResponsesController.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Responses/Controllers/SurveyResponsesController.cs @@ -222,10 +222,10 @@ namespace DamageAssesment.Api.Responses.Controllers /// [HttpGet] [Route("responses/surveys/export/{surveyid}")] - public async Task GetExcelSurveysAsync(int surveyid,string language, bool IsAdmin = false) + public async Task GetExcelSurveysAsync(int surveyid, string language, bool IsAdmin = false) { - var result = await this.surveyResponseProvider.ExportSurveyResponsesAsync(surveyid,language, IsAdmin); - if (result.IsSuccess&& result.surveyResponses.Count>0) + var result = await this.surveyResponseProvider.ExportSurveyResponsesAsync(surveyid, language, IsAdmin); + if (result.IsSuccess && result.surveyResponses.Count > 0) { byte[] fileContents = excelExportService.ExportToExcel(result.surveyResponses); 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 0740cc6..b0fb669 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Responses/Providers/ExcelExportService.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Responses/Providers/ExcelExportService.cs @@ -2,6 +2,7 @@ using DamageAssesment.Api.Responses.Models; using OfficeOpenXml; using System.Collections.Generic; +using System.ComponentModel; namespace DamageAssesment.Api.Responses.Providers { @@ -9,7 +10,7 @@ namespace DamageAssesment.Api.Responses.Providers { public byte[] ExportToExcel(List responses) { - ExcelPackage.LicenseContext = LicenseContext.NonCommercial; + ExcelPackage.LicenseContext = OfficeOpenXml.LicenseContext.NonCommercial; using (var package = new ExcelPackage()) { // Create the first worksheet and populate it with responses @@ -37,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)) { 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 2b5e3b0..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(); @@ -313,7 +313,7 @@ namespace DamageAssesment.Api.Responses.Providers { try { - var responses = await getAllSurveyResponsesExcelAsync(surveyId,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(int surveyId,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.Where(a=>a.SurveyId==surveyId).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(); diff --git a/DamageAssesmentApi/DamageAssesment.Api.Surveys/Db/Survey.cs b/DamageAssesmentApi/DamageAssesment.Api.Surveys/Db/Survey.cs index c031f57..2f19281 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Surveys/Db/Survey.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Surveys/Db/Survey.cs @@ -3,6 +3,7 @@ using System.ComponentModel.DataAnnotations.Schema; namespace DamageAssesment.Api.Surveys.Db { + [Table("Surveys")] public class Survey { [Key] @@ -12,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 5a84843..3a2e81c 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) {