예제 #1
0
        public string AddUpdateUserLoginTime(UserLoginTimeVM model)
        {
            string id = string.Empty;

            SqlParameter[] parameters =
            {
                new SqlParameter {
                    ParameterName = "@TutorId", Value = model.TutorId
                },
                new SqlParameter {
                    ParameterName = "@ConfigurationType", Value = model.ConfigurationType
                },
                new SqlParameter {
                    ParameterName = "@LoginStartTime", Value = model.LoginStartTime
                },
                new SqlParameter {
                    ParameterName = "@LoginEndTime", Value = model.LoginEndTime
                },
                new SqlParameter {
                    ParameterName = "@IsActive", Value = model.IsActive
                },
                new SqlParameter {
                    ParameterName = "@CreatedBy", Value = model.CreatedBy
                },
                new SqlParameter {
                    ParameterName = "@UpdatedBy", Value = model.UpdatedBy
                }
            };

            id = Convert.ToString(DALHelper.ExecuteScalar("AddUpdateUserLoginTime", parameters));

            return(id);
        }
        public ActionResult UserLoginTime()
        {
            var userLoginDetails = userRepository.GetUserLoginTimeByTutor(LogInManager.LoggedInUser.Id);

            UserLoginTimeVM model = new UserLoginTimeVM();

            model.TutorId           = LogInManager.LoggedInUser.Id;
            model.ConfigurationType = UserLoginConfigurationType.RESTRICTED;

            if (userLoginDetails != null && userLoginDetails.Count > 0)
            {
                var userLoginDetail = userLoginDetails[0];

                model.ConfigurationType = userLoginDetail.ConfigurationType;
                model.LoginStartTime    = userLoginDetail.LoginStartTime;
                model.LoginEndTime      = userLoginDetail.LoginEndTime;
                model.UserName          = userLoginDetail.UserName;

                if (model.LoginStartTime != null && model.LoginStartTime.HasValue)
                {
                    DateTime startTime = DateTime.Today.Add(model.LoginStartTime.Value);
                    model.LoginStartTimeText = startTime.ToString("HH:mm");
                }

                if (model.LoginEndTime != null && model.LoginEndTime.HasValue)
                {
                    DateTime endTime = DateTime.Today.Add(model.LoginEndTime.Value);
                    model.LoginEndTimeText = endTime.ToString("HH:mm");
                }

                //Get user's login time configuration setting
                var loginTimeConfigurations = userRepository.GetUserLoginTimeConfigurationByTutor(LogInManager.LoggedInUser.Id, null);
                if (loginTimeConfigurations != null && loginTimeConfigurations.Count > 0)
                {
                    foreach (var item in loginTimeConfigurations)
                    {
                        if (item.IsAllowLogin)
                        {
                            DateTime startTime = DateTime.Today.Add(item.LoginStartTime.Value);
                            item.LoginStartTimeText = startTime.ToString("HH:mm");

                            DateTime endTime = DateTime.Today.Add(item.LoginEndTime.Value);
                            item.LoginEndTimeText = endTime.ToString("HH:mm");
                        }
                    }

                    model.Configurations = loginTimeConfigurations;
                }
                else
                {
                    List <UserLoginTimeConfigurationVM> configurations = new List <UserLoginTimeConfigurationVM>();
                    for (int i = 0; i < 7; i++) // Sunday to Saturday
                    {
                        UserLoginTimeConfigurationVM objTimeConfiguration = new UserLoginTimeConfigurationVM();
                        objTimeConfiguration.WeekDay            = i;
                        objTimeConfiguration.IsAllowLogin       = false;
                        objTimeConfiguration.LoginStartTimeText = string.Empty;
                        objTimeConfiguration.LoginEndTimeText   = string.Empty;

                        configurations.Add(objTimeConfiguration);
                    }
                    model.Configurations = configurations;
                }
            }
            return(View(model));
        }
        public ActionResult UserLoginTime(UserLoginTimeVM model)
        {
            try
            {
                DateTime startTime = DateTime.Now;
                DateTime endTime   = DateTime.Now;

                if (model.ConfigurationType == UserLoginConfigurationType.SET_LIMIT)
                {
                    #region Add/Update Time Configuration Setting

                    if (model.Configurations != null && model.Configurations.Count > 0)
                    {
                        var loginTimeConfigurations = userRepository.GetUserLoginTimeConfigurationByTutor(LogInManager.LoggedInUser.Id, null);
                        if (loginTimeConfigurations != null && loginTimeConfigurations.Count > 0)
                        {
                            for (int i = 0; i < 7; i++)  // Sunday to Saturday
                            {
                                var existingTimeConfiguration = loginTimeConfigurations.Where(p => p.WeekDay == i).FirstOrDefault();
                                var newTimeConfiguration      = model.Configurations.Where(p => p.WeekDay == i).FirstOrDefault();

                                if (existingTimeConfiguration != null)
                                {
                                    existingTimeConfiguration.IsAllowLogin = newTimeConfiguration.IsAllowLogin;

                                    if (existingTimeConfiguration.IsAllowLogin)
                                    {
                                        string todayDate = DateTime.Now.ToString("dd/MM/yyyy");

                                        string dateStart = (todayDate + " " + newTimeConfiguration.LoginStartTimeText);
                                        startTime = Convert.ToDateTime(dateStart);

                                        string dateEnd = (todayDate + " " + newTimeConfiguration.LoginEndTimeText);
                                        endTime = Convert.ToDateTime(dateEnd);

                                        existingTimeConfiguration.LoginStartTime = startTime.TimeOfDay;
                                        existingTimeConfiguration.LoginEndTime   = endTime.TimeOfDay;
                                    }
                                    else
                                    {
                                        existingTimeConfiguration.LoginStartTime = null;
                                        existingTimeConfiguration.LoginEndTime   = null;
                                    }

                                    existingTimeConfiguration.TutorId   = LogInManager.LoggedInUser.Id;
                                    existingTimeConfiguration.WeekDay   = newTimeConfiguration.WeekDay;
                                    existingTimeConfiguration.UpdatedBy = LogInManager.LoggedInUserId;

                                    //Update existing configuration
                                    userRepository.UpdateUserLoginTimeConfiguration(existingTimeConfiguration);
                                }
                                else
                                {
                                    if (newTimeConfiguration.IsAllowLogin)
                                    {
                                        string todayDate = DateTime.Now.ToString("dd/MM/yyyy");

                                        string dateStart = (todayDate + " " + newTimeConfiguration.LoginStartTimeText);
                                        startTime = Convert.ToDateTime(dateStart);

                                        string dateEnd = (todayDate + " " + newTimeConfiguration.LoginEndTimeText);
                                        endTime = Convert.ToDateTime(dateEnd);

                                        newTimeConfiguration.LoginStartTime = startTime.TimeOfDay;
                                        newTimeConfiguration.LoginEndTime   = endTime.TimeOfDay;
                                    }
                                    else
                                    {
                                        newTimeConfiguration.LoginStartTime = null;
                                        newTimeConfiguration.LoginEndTime   = null;
                                    }

                                    newTimeConfiguration.TutorId   = LogInManager.LoggedInUser.Id;
                                    newTimeConfiguration.CreatedBy = LogInManager.LoggedInUserId;

                                    //Add new configuration
                                    userRepository.AddUserLoginTimeConfiguration(newTimeConfiguration);
                                }
                            }
                        }
                        else
                        {
                            for (int i = 0; i < 7; i++) // Sunday to Saturday
                            {
                                var newTimeConfiguration = model.Configurations.Where(p => p.WeekDay == i).FirstOrDefault();

                                if (newTimeConfiguration != null)
                                {
                                    if (newTimeConfiguration.IsAllowLogin == true)
                                    {
                                        string todayDate = DateTime.Now.ToString("dd/MM/yyyy");

                                        string dateStart = (todayDate + " " + newTimeConfiguration.LoginStartTimeText);
                                        startTime = Convert.ToDateTime(dateStart);

                                        string dateEnd = (todayDate + " " + newTimeConfiguration.LoginEndTimeText);
                                        endTime = Convert.ToDateTime(dateEnd);

                                        newTimeConfiguration.LoginStartTime = startTime.TimeOfDay;
                                        newTimeConfiguration.LoginEndTime   = endTime.TimeOfDay;
                                    }
                                    else
                                    {
                                        newTimeConfiguration.LoginStartTime = null;
                                        newTimeConfiguration.LoginEndTime   = null;
                                    }

                                    newTimeConfiguration.TutorId   = LogInManager.LoggedInUser.Id;
                                    newTimeConfiguration.CreatedBy = LogInManager.LoggedInUserId;

                                    //Add new configuration
                                    userRepository.AddUserLoginTimeConfiguration(newTimeConfiguration);
                                }
                            }
                        }
                    }

                    #endregion
                }
                else
                {
                    #region Delete Time Configuration Setting If Exist

                    userRepository.DeleteUserLoginTimeConfigurationByTutor(LogInManager.LoggedInUser.Id, LogInManager.LoggedInUserId);

                    #endregion
                }

                #region Add / Update User Login Time

                model.LoginStartTime = null;
                model.LoginEndTime   = null;
                model.TutorId        = LogInManager.LoggedInUser.Id;
                model.CreatedBy      = LogInManager.LoggedInUserId;
                model.UpdatedBy      = LogInManager.LoggedInUserId;
                model.IsActive       = true;

                userRepository.AddUpdateUserLoginTime(model);

                #endregion

                #region Set Configuration to Restricted After End Time Limit Reach

                //if (model.ConfigurationType == UserLoginConfigurationType.SET_LIMIT)
                //{
                //    DateTime currentTime = DateTime.Now;

                //    if(currentTime < startTime)
                //    {
                //        double totalMinutes = ((endTime - currentTime).TotalMinutes + 1);

                //        //Start Background Schedule Job (Execute only once)
                //        BackgroundJob.Schedule(() => SetStudentLoginTimeToRestrict(model.TutorId), TimeSpan.FromMinutes(totalMinutes));
                //    }
                //    else if(currentTime > startTime && currentTime < endTime) //Between Start & End Time
                //    {
                //        double totalMinutes = ((endTime - currentTime).TotalMinutes + 1);

                //        //Start Background Schedule Job (Execute only once)
                //        BackgroundJob.Schedule(() => SetStudentLoginTimeToRestrict(model.TutorId), TimeSpan.FromMinutes(totalMinutes));
                //    }
                //    else if(endTime < currentTime)
                //    {
                //        DateTime scheduleDate = endTime.AddDays(1);
                //        scheduleDate = scheduleDate.AddMinutes(1);

                //        //Start Background Schedule Job (Execute only once)
                //        BackgroundJob.Schedule(() => SetStudentLoginTimeToRestrict(model.TutorId), scheduleDate.TimeOfDay);
                //    }
                //}

                #endregion

                return(Json(new
                {
                    IsSuccess = true
                }, JsonRequestBehavior.AllowGet));
            }
            catch (Exception e)
            {
                Utility.Utility.LogError(e, "UserLoginTime");

                return(Json(new
                {
                    IsSuccess = false,
                    errorMessage = e.Message
                }));
            }
        }
        public ActionResult Create(UserVM model)
        {
            try
            {
                string userId = string.Empty;
                model.CreatedBy = LogInManager.LoggedInUserId;
                model.Password  = Utility.Utility.Encrypt(model.Password, Utility.Utility.EncryptionKey);

                #region Get Tutor Role Id

                var userRoles   = userRoleRepository.GetUserRoles();
                var tutorRoleId = userRoles.Where(m => m.Code == "TUTOR").FirstOrDefault().Id;
                model.UserRoleId = tutorRoleId;

                #endregion

                #region Check User Email Exist.

                if (this.CheckUserEmailExist(model.Id, model.UserRoleId, model.Email) == false)
                {
                    return(Json(new
                    {
                        IsSuccess = false,
                        errorMessage = string.Format("Email : {0} already exist.", model.Email)
                    }, JsonRequestBehavior.AllowGet));
                }

                #endregion

                #region Get Max. User Id

                //Get Max. User Id.
                var newUserId = (userRepository.GetMaxUserId()) + 1;
                model.UserId = newUserId;

                #endregion

                #region Generate Cashier Number

                var firstTwoCharactersOfName = !string.IsNullOrWhiteSpace(model.Name) ? model.Name.Substring(0, 2) : "";
                model.CashierNumber = (firstTwoCharactersOfName + newUserId).ToUpper();

                #endregion Generate Cashier Number

                model.IsFromRegistrationPage = false;
                model.IsRecordActivity       = true;

                userId = userRepository.AddUserDetail(model);

                if (!string.IsNullOrWhiteSpace(userId))
                {
                    #region Add User Role Mapping

                    UserRoleMappingVM userRoleMapping = new UserRoleMappingVM();
                    userRoleMapping.UserId     = Guid.Parse(userId);
                    userRoleMapping.UserRoleId = model.UserRoleId;
                    userRoleMapping.IsActive   = true;
                    userRoleMapping.CreatedBy  = LogInManager.LoggedInUserId;
                    userRoleMapping.UpdatedBy  = LogInManager.LoggedInUserId;


                    userRepository.AddUpdateUserRoleMapping(userRoleMapping);

                    #endregion

                    #region Set Default Tutor Configuration Type to Restricted.

                    UserLoginTimeVM userLoginTime = new UserLoginTimeVM();

                    userLoginTime.ConfigurationType = UserLoginConfigurationType.RESTRICTED;
                    userLoginTime.LoginStartTime    = null;
                    userLoginTime.LoginEndTime      = null;
                    userLoginTime.TutorId           = Guid.Parse(userId);
                    userLoginTime.CreatedBy         = LogInManager.LoggedInUserId;
                    userLoginTime.UpdatedBy         = LogInManager.LoggedInUserId;
                    userLoginTime.IsActive          = true;

                    userRepository.AddUpdateUserLoginTime(userLoginTime);

                    #endregion

                    return(Json(new
                    {
                        IsSuccess = true,
                        data = new
                        {
                            UserId = userId
                        }
                    }, JsonRequestBehavior.AllowGet));
                }
                else
                {
                    return(Json(new
                    {
                        IsSuccess = false,
                        errorMessage = "Tutor details not saved successfully."
                    }, JsonRequestBehavior.AllowGet));
                }
            }
            catch (Exception e)
            {
                Utility.Utility.LogError(e, "Create");
                return(Json(new
                {
                    IsSuccess = false,
                    errorMessage = e.Message
                }));
            }
        }