ixed survey issue in response level, and added logic for start and end date as optional date.

This commit is contained in:
uppuv 2023-11-05 13:20:29 -05:00
parent 6f10e99627
commit c80749e292
7 changed files with 49 additions and 27 deletions

View File

@ -222,10 +222,10 @@ namespace DamageAssesment.Api.Responses.Controllers
/// </summary> /// </summary>
[HttpGet] [HttpGet]
[Route("responses/surveys/export/{surveyid}")] [Route("responses/surveys/export/{surveyid}")]
public async Task<ActionResult> GetExcelSurveysAsync(int surveyid,string language, bool IsAdmin = false) public async Task<ActionResult> GetExcelSurveysAsync(int surveyid, string language, bool IsAdmin = false)
{ {
var result = await this.surveyResponseProvider.ExportSurveyResponsesAsync(surveyid,language, IsAdmin); var result = await this.surveyResponseProvider.ExportSurveyResponsesAsync(surveyid, language, IsAdmin);
if (result.IsSuccess&& result.surveyResponses.Count>0) if (result.IsSuccess && result.surveyResponses.Count > 0)
{ {
byte[] fileContents = excelExportService.ExportToExcel<object>(result.surveyResponses); byte[] fileContents = excelExportService.ExportToExcel<object>(result.surveyResponses);

View File

@ -2,13 +2,20 @@
namespace DamageAssesment.Api.Responses.Models namespace DamageAssesment.Api.Responses.Models
{ {
public enum SurveyStatus
{
PENDING,
ACTIVE,
INACTIVE
}
public class Survey public class Survey
{ {
public int Id { get; set; } public int Id { get; set; }
public bool IsEnabled { get; set; } 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; } public DateTime CreatedDate { get; set; }
public string Status { get; set; }
public Dictionary<string, string> Titles { get; set; } public Dictionary<string, string> Titles { get; set; }
} }
} }

View File

@ -2,6 +2,7 @@
using DamageAssesment.Api.Responses.Models; using DamageAssesment.Api.Responses.Models;
using OfficeOpenXml; using OfficeOpenXml;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel;
namespace DamageAssesment.Api.Responses.Providers namespace DamageAssesment.Api.Responses.Providers
{ {
@ -9,7 +10,7 @@ namespace DamageAssesment.Api.Responses.Providers
{ {
public byte[] ExportToExcel<T1>(List<object> responses) public byte[] ExportToExcel<T1>(List<object> responses)
{ {
ExcelPackage.LicenseContext = LicenseContext.NonCommercial; ExcelPackage.LicenseContext = OfficeOpenXml.LicenseContext.NonCommercial;
using (var package = new ExcelPackage()) using (var package = new ExcelPackage())
{ {
// Create the first worksheet and populate it with responses // 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++) for (int col = 1; col <= properties.Length; col++)
{ {
string value = Convert.ToString(properties[col - 1].GetValue(data[row - 2])); string value = Convert.ToString(properties[col - 1].GetValue(data[row - 2]));
if (IsAttchments.Where(a => a == col).Count() > 0 && !string.IsNullOrEmpty(value)) if (IsAttchments.Where(a => a == col).Count() > 0 && !string.IsNullOrEmpty(value))
{ {
List<string> attachments = value.Split("##").ToList(); List<string> attachments = value.Split("##").ToList();
try
{
Uri linkUri = new Uri(attachments[1]); Uri linkUri = new Uri(attachments[1]);
worksheet.Cells[row, col].Hyperlink = linkUri;
worksheet.Cells[row, col].Value = attachments[0]; worksheet.Cells[row, col].Value = attachments[0];
worksheet.Cells[row, col].Style.Font.UnderLine = true; worksheet.Cells[row, col].Style.Font.UnderLine = true;
} }
catch { worksheet.Cells[row, col].Value = attachments[1]; }
}
else else
worksheet.Cells[row, col].Value = value; worksheet.Cells[row, col].Value = value;
} }
} }
} }

View File

@ -121,7 +121,7 @@ namespace DamageAssesment.Api.Responses.Providers
logger?.LogInformation("Querying to get SurveyResponse object from DB"); logger?.LogInformation("Querying to get SurveyResponse object from DB");
//get all the survey that already taken by the employee //get all the survey that already taken by the employee
var surveys = await surveyServiceProvider.getSurveysAsync(language); 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) if (employeeid == null || employeeid == 0)
return (true, surveys, null); return (true, surveys, null);
List<int> listOfsurveysId = await surveyResponseDbContext.SurveyResponses.Where(x => x.EmployeeId == employeeid.Value).Select(y => y.SurveyId).ToListAsync(); List<int> 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); var surveys = await surveyServiceProvider.getSurveysAsync(language);
// returning only historic data: end date is less than current date. // 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) if (employeeid == null || employeeid == 0)
return (true, surveys, null); return (true, surveys, null);
var surveyResponses = await surveyResponseDbContext.SurveyResponses.Where(x => x.EmployeeId == employeeid).ToListAsync(); var surveyResponses = await surveyResponseDbContext.SurveyResponses.Where(x => x.EmployeeId == employeeid).ToListAsync();
@ -313,7 +313,7 @@ namespace DamageAssesment.Api.Responses.Providers
{ {
try try
{ {
var responses = await getAllSurveyResponsesExcelAsync(surveyId,language, isadmin); var responses = await getAllSurveyResponsesExcelAsync(surveyId, language, isadmin);
if (responses != null) if (responses != null)
return (true, responses, "Request Successful."); return (true, responses, "Request Successful.");
@ -709,13 +709,13 @@ namespace DamageAssesment.Api.Responses.Providers
} }
} }
//Method to get All Survey Responses for excel export //Method to get All Survey Responses for excel export
private async Task<List<object>> getAllSurveyResponsesExcelAsync(int surveyId,string language, bool isadmin) private async Task<List<object>> getAllSurveyResponsesExcelAsync(int surveyId, string language, bool isadmin)
{ {
try try
{ {
if (string.IsNullOrEmpty(language)) language = "en"; if (string.IsNullOrEmpty(language)) language = "en";
List<Db.SurveyResponse> surveyResonses; List<Db.SurveyResponse> 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 answers = await answerServiceProvider.getAnswersAsync();
var Locations = await locationServiceProvider.getLocationsAsync(); var Locations = await locationServiceProvider.getLocationsAsync();
var regions = await regionServiceProvider.getRegionsAsync(); var regions = await regionServiceProvider.getRegionsAsync();

View File

@ -3,6 +3,7 @@ using System.ComponentModel.DataAnnotations.Schema;
namespace DamageAssesment.Api.Surveys.Db namespace DamageAssesment.Api.Surveys.Db
{ {
[Table("Surveys")]
public class Survey public class Survey
{ {
[Key] [Key]
@ -12,9 +13,9 @@ namespace DamageAssesment.Api.Surveys.Db
public bool IsEnabled { get; set; } 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; public DateTime CreatedDate { get; set; } = DateTime.Now;
/* /*

View File

@ -21,8 +21,8 @@ namespace DamageAssesment.Api.Surveys.Models
{ {
public int Id { get; set; } public int Id { get; set; }
public bool IsEnabled { get; set; } 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; } public DateTime? CreatedDate { get; set; }
} }
} }

View File

@ -85,7 +85,9 @@ namespace DamageAssesment.Api.Surveys.Providers
MultiLanguage = dict; MultiLanguage = dict;
return MultiLanguage; return MultiLanguage;
} }
public string GetStatus(DateTime StartDate, DateTime EndDate) public string GetStatus(DateTime? StartDate, DateTime? EndDate)
{
try
{ {
if (StartDate > DateTime.Now) if (StartDate > DateTime.Now)
return SurveyStatus.PENDING.ToString(); return SurveyStatus.PENDING.ToString();
@ -94,6 +96,11 @@ namespace DamageAssesment.Api.Surveys.Providers
else else
return SurveyStatus.INACTIVE.ToString(); return SurveyStatus.INACTIVE.ToString();
} }
catch
{
return SurveyStatus.INACTIVE.ToString();
}
}
// Method to get surveys asynchronously with multi-language support // Method to get surveys asynchronously with multi-language support
public async Task<(bool IsSuccess, IEnumerable<Models.MultiLanSurvey> Surveys, string ErrorMessage)> GetSurveysAsync(string language) public async Task<(bool IsSuccess, IEnumerable<Models.MultiLanSurvey> Surveys, string ErrorMessage)> GetSurveysAsync(string language)
{ {
@ -136,7 +143,8 @@ namespace DamageAssesment.Api.Surveys.Providers
try try
{ {
logger?.LogInformation("Query Survey"); 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) if (survey != null)
{ {