added status field in survey micro service

This commit is contained in:
uppuv 2023-10-23 11:56:39 -04:00
commit d3a751a6ad
2 changed files with 24 additions and 4 deletions

View File

@ -2,9 +2,16 @@
namespace DamageAssesment.Api.Surveys.Models
{
public enum SurveyStatus
{
PENDING,
ACTIVE,
INACTIVE
}
public class MultiLanSurvey : BaseSurvey
{
public object Titles { get; set; }
public string Status { get; set; }
}
public class Survey : BaseSurvey
{

View File

@ -26,9 +26,9 @@ namespace DamageAssesment.Api.Surveys.Providers
{
if (!surveyDbContext.Surveys.Any())
{
var survey1 = new Db.Survey { IsEnabled = true, StartDate = DateTime.Now, EndDate = DateTime.Now.AddDays(90), CreatedDate = DateTime.Now };
var survey2 = new Db.Survey { IsEnabled = true, StartDate = DateTime.Now, EndDate = DateTime.Now.AddDays(90), CreatedDate = DateTime.Now };
var survey3 = new Db.Survey { IsEnabled = true, StartDate = DateTime.Now, EndDate = DateTime.Now.AddDays(90), CreatedDate = DateTime.Now };
var survey1 = new Db.Survey { IsEnabled = true, StartDate = DateTime.Now.AddDays(10), EndDate = DateTime.Now.AddDays(90), CreatedDate = DateTime.Now };
var survey2 = new Db.Survey { IsEnabled = true, StartDate = DateTime.Now.AddDays(-10), EndDate = DateTime.Now.AddDays(90), CreatedDate = DateTime.Now.AddDays(-10) };
var survey3 = new Db.Survey { IsEnabled = true, StartDate = DateTime.Now.AddDays(-100), EndDate = DateTime.Now.AddDays(-10), CreatedDate = DateTime.Now.AddDays(-100) };
surveyDbContext.Surveys.Add(survey1);
surveyDbContext.Surveys.Add(survey2);
@ -85,7 +85,15 @@ namespace DamageAssesment.Api.Surveys.Providers
MultiLanguage = dict;
return MultiLanguage;
}
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
return SurveyStatus.INACTIVE.ToString();
}
// Method to get surveys asynchronously with multi-language support
public async Task<(bool IsSuccess, IEnumerable<Models.MultiLanSurvey> Surveys, string ErrorMessage)> GetSurveysAsync(string language)
{
@ -106,6 +114,7 @@ namespace DamageAssesment.Api.Surveys.Providers
EndDate = s.EndDate,
IsEnabled = s.IsEnabled,
CreatedDate = s.CreatedDate,
Status= GetStatus(s.StartDate,s.EndDate),
Titles = CreateMultiLanguageObject(GetSurveyTranslations(s.Id, null, language))
};
@ -140,6 +149,7 @@ namespace DamageAssesment.Api.Surveys.Providers
EndDate = survey.EndDate,
IsEnabled = survey.IsEnabled,
CreatedDate = survey.CreatedDate,
Status = GetStatus(survey.StartDate, survey.EndDate),
Titles = CreateMultiLanguageObject(GetSurveyTranslations(survey.Id, null, language))
};
logger?.LogInformation($"Survey Id: {id} found");
@ -173,6 +183,7 @@ namespace DamageAssesment.Api.Surveys.Providers
}
await surveyDbContext.SaveChangesAsync();
var result = mapper.Map<Db.Survey, Models.MultiLanSurvey>(_survey);
result.Status = GetStatus(_survey.StartDate, _survey.EndDate);
result.Titles = CreateMultiLanguageObject(GetSurveyTranslations(_survey.Id, survey.Titles, ""));
return (true, result, "Successful");
}
@ -217,6 +228,7 @@ namespace DamageAssesment.Api.Surveys.Providers
await surveyDbContext.SaveChangesAsync();
var result = mapper.Map<Db.Survey, Models.MultiLanSurvey>(_survey);
result.Status = GetStatus(_survey.StartDate, _survey.EndDate);
result.Titles = CreateMultiLanguageObject(GetSurveyTranslations(_survey.Id, survey.Titles, ""));
return (true, result, "Successful");
}
@ -249,6 +261,7 @@ namespace DamageAssesment.Api.Surveys.Providers
if (survey != null)
{
var result = mapper.Map<Db.Survey, Models.MultiLanSurvey>(survey);
result.Status = GetStatus(survey.StartDate, survey.EndDate);
result.Titles = CreateMultiLanguageObject(GetSurveyTranslations(survey.Id, null, ""));
surveyDbContext.Surveys.Remove(survey);
await surveyDbContext.SaveChangesAsync();