示例#1
0
        public async Task LeaveReportAdminTestAsync()
        {
            await CreateUserAndMockingHttpContextToReturnAccessToken();

            var response     = Task.FromResult(_stringConstant.UserDetailsFromOauthServer);
            var requestIdUrl = string.Format("{0}{1}", _stringConstant.EmployeeIdForTest, _stringConstant.UserDetailUrl);

            _mockHttpClient.Setup(x => x.GetAsync(_stringConstant.UserUrl, requestIdUrl, _stringConstant.AccessTokenForTest, _stringConstant.Bearer)).Returns(response);
            leave.EmployeeId = _stringConstant.EmployeeIdForTest;
            await _leaveRequestRepository.ApplyLeaveAsync(leave);

            var leaveReports = _leaveReportRepository.LeaveReportAsync(_stringConstant.EmployeeIdForTest).Result;

            Assert.Equal(true, leaveReports.Any());
        }
        public async void ApplyLeave()
        {
            await _leaveRequestRepository.ApplyLeaveAsync(leave);

            Assert.Equal(1, leave.Id);
        }
        /// <summary>
        /// Method to apply leave
        /// </summary>
        /// <param name="slackRequest">list of string contain leave slash command parameters</param>
        /// <param name="leave">leave slash command</param>
        /// <param name="accessToken">User's access token</param>
        /// <returns>Reply text to be send</returns>
        private async Task <string> LeaveApplyAsync(List <string> slackRequest, SlashCommand leave, string accessToken, string userId)
        {
            try
            {
                LeaveType leaveType;
                DateTime  startDate, endDate, reJoinDate;
                User      user       = new User();
                string    dateFormat = Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern;
                _logger.Debug("LeaveApplyAsync Date format of leave command : " + dateFormat);
                _logger.Debug("LeaveApplyAsync Current Culture info : " + Thread.CurrentThread.CurrentCulture.DisplayName);
                // checking whether string can convert to date of independent culture or not, if return true then further process will be conduct
                bool startDateConvertorResult = DateTime.TryParseExact(slackRequest[3], dateFormat, CultureInfo.InvariantCulture,
                                                                       DateTimeStyles.None, out startDate);
                if (startDateConvertorResult)
                {
                    user = await _oauthCallsRepository.GetUserByUserIdAsync(userId, accessToken);

                    LeaveRequest leaveRequest = new LeaveRequest();
                    // Method to check leave's start date is not beyond today. Back date checking
                    bool validStartDate = LeaveStartDateValid(startDate);
                    if (validStartDate)
                    {
                        // checking whether string can convert to leave type or not, if return true then further process will be conduct
                        bool leaveTypeConvertorResult = Enum.TryParse(slackRequest[1], out leaveType);
                        if (leaveTypeConvertorResult)
                        {
                            // converting string to leave type of indian culture
                            switch (leaveType)
                            {
                            case LeaveType.cl:
                            {
                                _logger.Debug("LeaveApplyAsync Casual leave");
                                // checking whether string can convert to date of indian culture or not, if return true then further process will be conduct
                                bool endDateConvertorResult = DateTime.TryParseExact(slackRequest[4], dateFormat,
                                                                                     CultureInfo.InvariantCulture, DateTimeStyles.None, out endDate);
                                bool reJoinDateConvertorResult = DateTime.TryParseExact(slackRequest[5], dateFormat,
                                                                                        CultureInfo.InvariantCulture, DateTimeStyles.None, out reJoinDate);
                                if (endDateConvertorResult && reJoinDateConvertorResult)
                                {
                                    // Method to check leave's end date is not beyond start date and re-join date is not beyond end date
                                    bool validDate = ValidDateTimeForLeave(startDate, endDate, reJoinDate);
                                    if (validDate)
                                    {
                                        // get user details from oAuth server
                                        leaveRequest.EndDate    = endDate;
                                        leaveRequest.RejoinDate = reJoinDate;
                                        leaveRequest.Status     = Condition.Pending;
                                        leaveRequest.Type       = leaveType;
                                        leaveRequest.Reason     = slackRequest[2];
                                        leaveRequest.FromDate   = startDate;
                                        leaveRequest.CreatedOn  = DateTime.UtcNow;
                                        // if user doesn't exist in OAuth server then user can't apply leave
                                        if (user.Id != null)
                                        {
                                            _logger.Debug("LeaveApplyAsync leave apply user name : " + user.UserName);
                                            // Method to check more than one leave cannot be applied on that date
                                            validDate = await LeaveDateDuplicate(user.Id, startDate, endDate);

                                            if (!validDate)
                                            {
                                                leaveRequest.EmployeeId = user.Id;
                                                await _leaveRepository.ApplyLeaveAsync(leaveRequest);

                                                _logger.Debug("LeaveApplyAsync Leave applied sucessfully");
                                                replyText = _attachmentRepository.ReplyText(leave.Username, leaveRequest);
                                                _logger.Debug("LeaveApplyAsync Client Repository - SendMessageWithAttachmentIncomingWebhookAsync");
                                                // method to send slack notification and email to team leaders and management
                                                await _clientRepository.SendMessageWithAttachmentIncomingWebhookAsync(leaveRequest,
                                                                                                                      accessToken, replyText, user.Id);
                                            }
                                            else
                                            {
                                                replyText = _stringConstant.LeaveAlreadyExistOnSameDate;
                                            }
                                        }
                                        else
                                        {
                                            // if user doesn't exist in OAuth server then user can't apply leave, will get this message
                                            replyText = _stringConstant.SorryYouCannotApplyLeave;
                                        }
                                    }
                                    else
                                    {
                                        replyText = _stringConstant.InValidDateErrorMessage;
                                    }
                                }
                                else
                                {
                                    // if date is not proper than date format error message will be send to user
                                    replyText = string.Format(_stringConstant.DateFormatErrorMessage, dateFormat);
                                }
                            }
                            break;

                            case LeaveType.sl:
                            {
                                _logger.Debug("LeaveApplyAsync Sick leave");
                                bool isAdmin = false;
                                User newUser = new User();
                                if (slackRequest.Count > 4)
                                {
                                    isAdmin = await _oauthCallsRepository.UserIsAdminAsync(userId, accessToken);

                                    _logger.Debug("LeaveApplyAsync User is admin : " + isAdmin);
                                    if (isAdmin)
                                    {
                                        _logger.Debug("LeaveApplyAsync Sick Leave applying for other");
                                        SlackUserDetailAc slackUser = await _slackUserRepository.GetBySlackNameAsync(slackRequest[4]);

                                        // get user details from oAuth server for other user
                                        var newUserDetails = await _userManagerRepository.FirstOrDefaultAsync(x => x.SlackUserId == slackUser.UserId);

                                        newUser = await _oauthCallsRepository.GetUserByUserIdAsync(newUserDetails.Id, accessToken);
                                    }
                                    else
                                    {
                                        replyText = _stringConstant.AdminErrorMessageApplySickLeave;
                                    }
                                }
                                else
                                {
                                    _logger.Debug("LeaveApplyAsync Sick Leave applying for own");
                                    // get user details from oAuth server for own
                                    newUser = await _oauthCallsRepository.GetUserByUserIdAsync(userId, accessToken);

                                    leaveRequest.EndDate    = startDate;
                                    leaveRequest.RejoinDate = startDate.AddDays(1);
                                }
                                leaveRequest.Status    = Condition.Approved;
                                leaveRequest.Type      = leaveType;
                                leaveRequest.Reason    = slackRequest[2];
                                leaveRequest.FromDate  = startDate;
                                leaveRequest.CreatedOn = DateTime.UtcNow;
                                // if user doesn't exist in OAuth server then user can't apply leave
                                if (newUser.Id != null)
                                {
                                    // Method to check more than one leave cannot be applied on that date
                                    bool validDate = await LeaveDateDuplicate(newUser.Id, startDate, null);

                                    if (!validDate)
                                    {
                                        leaveRequest.EmployeeId = newUser.Id;
                                        await _leaveRepository.ApplyLeaveAsync(leaveRequest);

                                        _logger.Debug("LeaveApplyAsync Leave applied successfully");
                                        replyText = _attachmentRepository.ReplyTextSick(newUser.FirstName, leaveRequest);
                                        _logger.Debug("LeaveApplyAsync Client Repository - SendMessageWithoutButtonAttachmentIncomingWebhookAsync");
                                        await _clientRepository.SendMessageWithoutButtonAttachmentIncomingWebhookAsync(leaveRequest,
                                                                                                                       accessToken, replyText, newUser.Id);

                                        if (isAdmin)
                                        {
                                            _logger.Debug("LeaveApplyAsync Client Repository - SendSickLeaveMessageToUserIncomingWebhookAsync");
                                            await _clientRepository.SendSickLeaveMessageToUserIncomingWebhookAsync(leaveRequest,
                                                                                                                   user.Email, replyText, newUser);
                                        }
                                    }
                                    else
                                    {
                                        replyText = _stringConstant.LeaveAlreadyExistOnSameDate;
                                    }
                                }
                                else
                                {
                                    // if user doesn't exist in OAuth server then user can't apply leave, will get this message
                                    replyText += _stringConstant.SorryYouCannotApplyLeave;
                                }
                            }
                            break;
                            }
                        }
                        else
                        {
                            // if leave type is not proper than not of leave type format error message will be send to user
                            replyText = _stringConstant.NotTypeOfLeave;
                        }
                    }
                    else
                    {
                        replyText = _stringConstant.BackDateErrorMessage;
                    }
                }
                else
                {
                    // if date is not proper than date format error message will be send to user
                    replyText = string.Format(_stringConstant.DateFormatErrorMessage, dateFormat);
                }
            }
            catch (SmtpException ex)
            {
                // error message will be send to email. But leave will be applied
                replyText = string.Format(_stringConstant.ReplyTextForSMTPExceptionErrorMessage,
                                          _stringConstant.ErrorWhileSendingEmail, ex.Message.ToString());
            }
            return(replyText);
        }