Beispiel #1
0
        public ActionResult _EditDepartmentUser(int DepartmentUserId)
        {
            try
            {
                ViewBag.Error        = "";
                ViewBag.SessionError = "";
                var userData = MvcApplication.GetUserData(User.Identity.Name) ?? new UserData();
                if (userData.UserId < 1)
                {
                    ViewBag.SessionError = "Your session has expired! Please re-login";
                    return(View(new List <DepartmentUserObj>()));
                }
                if (DepartmentUserId < 1)
                {
                    ViewBag.Error = "Invalid selection";
                    return(View(new DepartmentUserObj()));
                }

                if (!(Session["_DepartmentUserList_"] is List <DepartmentUserObj> DepartmentUserList) || DepartmentUserList.Count < 1)
                {
                    ViewBag.Error = "Error Occurred! Unable to process selected item";
                    return(View(new DepartmentUserObj()));
                }

                var DepartmentUser = DepartmentUserList.Find(m => m.DepartmentUserId == DepartmentUserId);
                if (DepartmentUser == null || DepartmentUser.DepartmentUserId < 1)
                {
                    ViewBag.Error = "Error Occurred! Unable to process selected item";
                    return(View(new DepartmentUserObj()));
                }

                Session["_CurrentSelDepartmentUser_"] = DepartmentUser;

                return(View(DepartmentUser));
            }
            catch (Exception ex)
            {
                ViewBag.Error = "Error Occurred! Please try again later";
                UtilTools.LogE(ex.StackTrace, ex.Source, ex.Message);
                return(View(new DepartmentUserObj()));
            }
        }
Beispiel #2
0
        public JsonResult ProcessAddDepartmentUserRequest(RegDepartmentUserObj model)
        {
            try
            {
                #region Current User Session Check

                var userData = MvcApplication.GetUserData(User.Identity.Name) ?? new UserData();
                if (userData.UserId < 1)
                {
                    return(Json(new { IsSuccessful = false, Error = "Your session has expired", IsAuthenticated = false }));
                }


                #endregion

                #region Model Validations

                if (model == null)
                {
                    return(Json(new { IsSuccessful = false, Error = "Your session has expired", IsAuthenticated = false }));
                }

                if (model.ClientId < 1)
                {
                    return(Json(new { isauthenticated = true, issuccessful = false, isreload = false, error = "client required " }));
                }

                if (model.ProductItemId < 1)
                {
                    return(Json(new { isauthenticated = true, issuccessful = false, isreload = false, error = "Product Item required " }));
                }

                if (model.ProductId < 1)
                {
                    return(Json(new { isauthenticated = true, issuccessful = false, isreload = false, error = "Product required " }));
                }

                if (model.DepartmentId < 1)
                {
                    return(Json(new { isauthenticated = true, issuccessful = false, isreload = false, error = "Department  required " }));
                }

                if (model.UserId < 1)
                {
                    return(Json(new { isauthenticated = true, issuccessful = false, isreload = false, error = "User required " }));
                }

                #endregion

                #region Check if User Exist in Department Session

                if (!(Session["_DepartmentUserList_"] is List <DepartmentUserObj> DepartmentUserList && !DepartmentUserList.Any()))
                {
                }
                var previousDepartmentList = (List <DepartmentUserObj>)Session["_DepartmentUserList_"];

                if (previousDepartmentList != null)
                {
                    if (previousDepartmentList.Count(x => x.DepartmentId == model.DepartmentId &&
                                                     x.ClientId == model.ClientId &&
                                                     x.ProductId == model.ProductId &&
                                                     x.ProductItemId == model.ProductItemId &&
                                                     x.UserId == model.UserId) > 0)
                    {
                        return(Json(new { IsAuthenticated = true, IsSuccessful = false, IsReload = false, Error = "User Already Added to Department" }));
                    }
                }

                #endregion

                #region Request from Users Service

                var searchObj2 = new UserSearchObj
                {
                    AdminUserId = userData.UserId,
                    Status      = -2,
                    StopDate    = "",
                    StartDate   = "",
                    UserId      = 0,
                };

                var userRetVal = new PortalUserManager().LoadUsers(searchObj2, userData.Username);

                if (userRetVal?.Status == null)
                {
                    return(Json(new { IsAuthenticated = true, IsSuccessful = false, IsReload = false, Error = "Error Occurred! Please try again later" }));
                }

                var userList = userRetVal.Users.OrderBy(x => x.UserId).ToList();

                #endregion



                #region Build Request Object

                var requestObj = new RegDepartmentUserObj
                {
                    ClientId      = model.ClientId,
                    ProductId     = model.ProductId,
                    AdminUserId   = userData.UserId,
                    Status        = 1,
                    ProductItemId = model.ProductItemId,
                    DepartmentId  = model.DepartmentId,
                    UserId        = model.UserId,
                    UserRoles     = string.Join(",", userList.FirstOrDefault(x => x.UserId == model.UserId).RoleNames),
                    Email         = userList.FirstOrDefault(x => x.UserId == model.UserId).Email,
                    FullName      = userList.FirstOrDefault(x => x.UserId == model.UserId).FirstName + " " + userList.FirstOrDefault(x => x.UserId == model.UserId).LastName,
                };

                #endregion

                #region Response and validation
                var response = DepartmentUserServices.AddDepartmentUser(requestObj, userData.Username);

                if (response?.Status == null)
                {
                    return(Json(new { IsAuthenticated = true, IsSuccessful = false, IsReload = false, Error = "Error Occurred! Please try again later" }));
                }

                if (!response.Status.IsSuccessful)
                {
                    return(Json(new { IsAuthenticated = true, IsSuccessful = false, IsReload = false, Error = string.IsNullOrEmpty(response.Status.Message.TechnicalMessage) ? "Process Failed! Unable to add nomination Source" : response.Status.Message.TechnicalMessage }));
                }
                var searchObj = new DepartmentUserSearchObj
                {
                    AdminUserId      = userData.UserId,
                    DepartmentUserId = 0,
                };

                var retVal = DepartmentUserServices.LoadDepartmentUsers(searchObj, userData.Username);

                if (retVal?.Status != null && retVal.DepartmentUsers != null)
                {
                    var DepartmentUsers = retVal.DepartmentUsers.OrderBy(m => m.DepartmentUserId)
                                          .Where(m => m.ClientId == model.ClientId && m.ProductId == model.ProductItemId && m.ProductItemId == model.ProductItemId)
                                          .ToList();
                    Session["_DepartmentUserList_"] = DepartmentUsers;
                }

                #endregion

                return(Json(new { IsAuthenticated = true, IsSuccessful = true, IsReload = false, Error = "" }));
            }
            catch (Exception ex)
            {
                UtilTools.LogE(ex.StackTrace, ex.Source, ex.Message);
                return(Json(new { IsAuthenticated = true, IsSuccessful = false, IsReload = false, Error = "Process Error Occurred! Please try again later" }));
            }
        }