Exemplo n.º 1
0
        public async Task <ActionResult <IResult> > SubmitDeclarationFormData([FromBody] HealthTrackViewModel healthTrackViewModel)
        {
            _logger.LogInformation("Save employee declaration detail");
            var result = await _healthTrackService.CreateHealthTrack(healthTrackViewModel);

            return(StatusCode((int)result.StatusCode, result));
        }
        private async Task <HealthTrackViewModel> SaveHealthTrack(HealthTrackViewModel healthTrackViewModel)
        {
            // user can add declaration
            var healthTrackModel = new HealthTrack();

            // To map health Track detail
            healthTrackModel.MapFromViewModel(healthTrackViewModel, (ClaimsIdentity)_principal.Identity);
            var healthTrackSymptoms = new List <HealthTrackSymptom>();

            if (healthTrackViewModel.HealthTrackSymptoms.Any())
            {
                // To map HealthTrack Symptoms
                healthTrackSymptoms = healthTrackViewModel.HealthTrackSymptoms.Select(t =>
                {
                    var symptom = new HealthTrackSymptom();
                    symptom.MapFromViewModel(t);
                    return(symptom);
                }).ToList();
            }

            var healthTrackQuestionAnswer = new List <HealthTrackQuestionAnswer>();

            if (healthTrackViewModel.HealthTrackQuestionAnswers.Any())
            {
                healthTrackQuestionAnswer = healthTrackViewModel.HealthTrackQuestionAnswers.Select(t =>
                {
                    var questionAns = new HealthTrackQuestionAnswer();
                    questionAns.MapFromViewModel(t);
                    return(questionAns);
                }).ToList();
            }

            healthTrackModel.HealthTrackSymptoms  = healthTrackSymptoms;
            healthTrackModel.HealthTrackQuestions = healthTrackQuestionAnswer;

            healthTrackModel = await _healthTrackRepository.CreateHealthTrack(healthTrackModel);

            healthTrackViewModel.Id = healthTrackModel.Id;
            var employeeVm = new EmployeeViewModel();

            employeeVm.MapFromModel(healthTrackModel.Employee);
            healthTrackViewModel.Employee = employeeVm;
            // send email to HR and security
            await SendEmail(healthTrackViewModel);

            return(healthTrackViewModel);
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="search"></param>
        /// <returns></returns>
        public IResult GetDeclarationsByUserId(SearchSortModel search)
        {
            var healthViewModelList = new List <HealthTrackViewModel>();
            var result = new Result
            {
                Operation  = Operation.Read,
                Status     = Status.Success,
                StatusCode = HttpStatusCode.OK
            };

            try
            {
                var declarationList = _healthTrackRepository.GetDeclarationsByUserId(search);
                if (declarationList.Any())
                {
                    healthViewModelList = declarationList.Select(declaration =>
                    {
                        var healthViewModel = new HealthTrackViewModel();
                        healthViewModel.MapFromModel(declaration);
                        var employeeVm = new EmployeeViewModel();
                        employeeVm.MapFromModel(declaration.Employee);
                        healthViewModel.Employee = employeeVm;
                        return(healthViewModel);
                    }).ToList();

                    search.SearchResult = healthViewModelList;
                    result.Body         = search;
                }
                else
                {
                    result.Status     = Status.Fail;
                    result.StatusCode = HttpStatusCode.NotFound;
                    result.Message    = "No declaration exists";
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, ex.Message);
                result.Status     = Status.Error;
                result.Message    = ex.Message;
                result.StatusCode = HttpStatusCode.InternalServerError;
            }
            return(result);
        }
        private async Task SendEmail(HealthTrackViewModel healthViewModel)
        {
            var appEmailHelper = new AppEmailHelper();
            var hrEmployeeList = await _employeeRepository.GetEmployeeDetailsByRole(EmployeeRolesEnum.HRManager.ToString());

            if (hrEmployeeList.Count > 0)
            {
                foreach (var hrEmployee in hrEmployeeList)
                {
                    appEmailHelper.ToMailAddresses.Add(new MailAddress(hrEmployee.Email, hrEmployee.Name));
                }
            }


            // send to security as well
            var securityEmpList = await _employeeRepository.GetEmployeeDetailsByRole(EmployeeRolesEnum.SecurityManager.ToString());

            if (securityEmpList.Count > 0)
            {
                foreach (var securityEmp in securityEmpList)
                {
                    appEmailHelper.ToMailAddresses.Add(new MailAddress(securityEmp.Email, securityEmp.Name));
                }
            }
            // appEmailHelper.ToMailAddresses.Add(new MailAddress(hrEmployee.Email, hrEmployee.Name));
            // appEmailHelper.CCMailAddresses.Add(new MailAddress(securityEmp.Email, securityEmp.Name));

            appEmailHelper.MailTemplate = MailTemplate.EmployeeDeclaration;
            appEmailHelper.Subject      = "Self declaration submission";
            HealthTrackEmailViewModel emailVm = new HealthTrackEmailViewModel();

            emailVm.MapFromViewModel(healthViewModel);
            emailVm.LinkUrl = $"{ _configuration["AppUrl"]}declaration/{healthViewModel.RequestNumber}/{healthViewModel.EmployeeId}";
            appEmailHelper.MailBodyViewModel = emailVm;
            await appEmailHelper.InitMailMessage();
        }
        /// <summary>
        ///  Returns data to export in excel
        /// </summary>
        /// <returns></returns>
        public IResult GetAllDeclarations(SearchSortModel search)
        {
            var healthViewModelList = new List <HealthTrackViewModel>();
            var result = new Result
            {
                Operation  = Operation.Read,
                Status     = Status.Success,
                StatusCode = HttpStatusCode.OK
            };

            try
            {
                var declarationList = _healthTrackRepository.GetAllDeclarations(search);
                if (declarationList.Any())
                {
                    healthViewModelList = declarationList.Select(declaration =>
                    {
                        var healthViewModel = new HealthTrackViewModel();
                        healthViewModel.MapFromModel(declaration);
                        var employeeVm = new EmployeeViewModel();
                        employeeVm.MapFromModel(declaration.Employee);
                        healthViewModel.Employee = employeeVm;
                        var symptoms             = new List <HealthTrackSymptomViewModel>();
                        var questions            = new List <HealthTrackQuestionAnswerViewModel>();
                        if (declaration.HealthTrackQuestions.Any())
                        {
                            questions = declaration.HealthTrackQuestions.Select(t =>
                            {
                                var questions = new HealthTrackQuestionAnswerViewModel();
                                questions.MapFromModel(t);
                                questions.Question = t.Question.Name;
                                return(questions);
                            }).ToList();
                        }

                        if (declaration.HealthTrackSymptoms.Any())
                        {
                            symptoms = declaration.HealthTrackSymptoms.Select(t =>
                            {
                                var symptom = new HealthTrackSymptomViewModel();
                                symptom.MapFromModel(t);
                                symptom.Name = t.Symptom.Name;
                                return(symptom);
                            }).ToList();
                        }
                        healthViewModel.HealthTrackSymptoms        = symptoms;
                        healthViewModel.HealthTrackQuestionAnswers = questions;
                        return(healthViewModel);
                    }).ToList();

                    result.Body = healthViewModelList;
                }
                else
                {
                    result.Status     = Status.Fail;
                    result.StatusCode = HttpStatusCode.NotFound;
                    result.Message    = "No declaration exists";
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, ex.Message);
                result.Status     = Status.Error;
                result.Message    = ex.Message;
                result.StatusCode = HttpStatusCode.InternalServerError;
            }
            return(result);
        }
        /// <summary>
        ///  Returns data to bind on declaration form
        /// </summary>
        /// <returns></returns>
        public async Task <IResult> GetSelfDeclarationByEmployeeForRequest(int employeedId, string requestNumber)
        {
            var healthViewModelList = new List <HealthTrackViewModel>();
            var result = new Result
            {
                Operation  = Operation.Read,
                Status     = Status.Success,
                StatusCode = HttpStatusCode.OK
            };

            try
            {
                var declarationList = await _healthTrackRepository.GetSelfDeclarationByEmployeeForRequest(employeedId, requestNumber);

                if (declarationList.Any())
                {
                    healthViewModelList = declarationList.Select(declaration =>
                    {
                        var healthViewModel = new HealthTrackViewModel();
                        healthViewModel.MapFromModel(declaration);
                        var employeeVm = new EmployeeViewModel();
                        employeeVm.MapFromModel(declaration.Employee);
                        healthViewModel.Employee = employeeVm;
                        var symptoms             = new List <HealthTrackSymptomViewModel>();
                        var questions            = new List <HealthTrackQuestionAnswerViewModel>();
                        if (declaration.HealthTrackQuestions.Any())
                        {
                            questions = declaration.HealthTrackQuestions.Select(t =>
                            {
                                var questions = new HealthTrackQuestionAnswerViewModel();
                                questions.MapFromModel(t);
                                return(questions);
                            }).ToList();
                        }

                        if (declaration.HealthTrackSymptoms.Any())
                        {
                            symptoms = declaration.HealthTrackSymptoms.Select(t =>
                            {
                                var symptom = new HealthTrackSymptomViewModel();
                                symptom.MapFromModel(t);
                                return(symptom);
                            }).ToList();
                        }
                        healthViewModel.HealthTrackSymptoms        = symptoms;
                        healthViewModel.HealthTrackQuestionAnswers = questions;
                        return(healthViewModel);
                    }).ToList();

                    result.Body = healthViewModelList;
                }
                else
                {
                    result.Status     = Status.Fail;
                    result.StatusCode = HttpStatusCode.NotFound;
                    result.Message    = "No declaration exists";
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, ex.Message);
                result.Status     = Status.Error;
                result.Message    = ex.Message;
                result.StatusCode = HttpStatusCode.InternalServerError;
            }
            return(result);
        }
        /// <summary>
        /// Create Health Track
        /// </summary>
        /// <param name="healthTrackViewModel"></param>
        /// <returns></returns>
        public async Task <IResult> CreateHealthTrack(HealthTrackViewModel healthTrackViewModel)
        {
            var result = new Result
            {
                Operation  = Operation.Create,
                Status     = Status.Success,
                StatusCode = HttpStatusCode.OK
            };

            try
            {
                if (healthTrackViewModel != null)
                {
                    var     todayDate = DateTime.Now.Date;
                    dynamic request   = null;
                    if (string.IsNullOrEmpty(healthTrackViewModel.RequestNumber))
                    {
                        // find recent approved request
                        var searchModel = new SearchSortModel();
                        searchModel.userId       = healthTrackViewModel.EmployeeId;
                        searchModel.roleId       = 0;
                        searchModel.SearchString = "";
                        var requests = _requestRepository.GetRequestsListByUserId(searchModel);
                        request = requests.OrderBy(x => x.FromDate)
                                  .Where(x => x.IsApproved &&
                                         (x.FromDate.Date <= todayDate && x.ToDate.Date >= todayDate)).FirstOrDefault();
                        if (request != null)
                        {
                            healthTrackViewModel.RequestNumber = request.RequestNumber;
                        }
                        else
                        {
                            result.Status     = Status.Fail;
                            result.StatusCode = HttpStatusCode.NotFound;
                            result.Message    = "Either you do not have any approved request or you can not declare prior to the request From date";
                            return(result);
                        }
                    }
                    else
                    {
                        request = await _requestRepository.GetRequestByNumber(healthTrackViewModel.RequestNumber);

                        if (request.IsDeclined)
                        {
                            result.Status     = Status.Fail;
                            result.StatusCode = HttpStatusCode.NotAcceptable;
                            result.Message    = "You cannot submit declaration for declined request";
                            return(result);
                        }
                    }


                    // check if user has declarations for a request
                    var declarations = await _healthTrackRepository.GetSelfDeclarationByEmployeeForRequest(healthTrackViewModel.EmployeeId, healthTrackViewModel.RequestNumber);

                    if (declarations.Any())
                    {
                        // if user has already submiited declaration for today
                        if (declarations.Any(d => d.CreatedDate.Date == todayDate))
                        {
                            result.Status     = Status.Fail;
                            result.StatusCode = HttpStatusCode.NotAcceptable;
                            result.Message    = "You already have submitted declaration for today";
                        }
                        // if request has expired
                        else if (todayDate > request.ToDate.Date)
                        {
                            result.Status     = Status.Fail;
                            result.StatusCode = HttpStatusCode.Forbidden;
                            result.Message    = "Your request has expired. Please raise a new request to submit declaration";
                        }
                        else
                        {
                            result.Body = await SaveHealthTrack(healthTrackViewModel);
                        }
                    }
                    else
                    {
                        if (todayDate <= request.ToDate.Date)
                        {
                            // now add logic to create declaration
                            result.Body = await SaveHealthTrack(healthTrackViewModel);
                        }
                        else
                        {
                            // You have to raise a new request to submit declaration
                            result.Status     = Status.Fail;
                            result.StatusCode = HttpStatusCode.Forbidden;
                            result.Message    = "Your request has expired. Please raise a new request to submit declaration";
                        }
                    }
                    return(result);
                }
                result.Status     = Status.Fail;
                result.StatusCode = HttpStatusCode.BadRequest;
                return(result);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, ex.Message);
                result.Status     = Status.Error;
                result.Message    = ex.Message;
                result.StatusCode = HttpStatusCode.InternalServerError;
                return(result);
            }
        }