Beispiel #1
0
        public static DepartmentUserRegRespObj AddDepartmentUser(RegDepartmentUserObj regObj, string username)
        {
            var response = new DepartmentUserRegRespObj
            {
                Status = new APIResponseStatus
                {
                    IsSuccessful = false,
                    Message      = new APIResponseMessage(),
                },
            };

            try
            {
                var apiResponse = new APIHelper(APIEndpoints.ADD_DEPARTMENT_USERS_ENDPOINT, username, Method.POST).ProcessAPI <RegDepartmentUserObj, DepartmentUserRegRespObj>(regObj, out var msg);
                if (msg.Code == 0 && string.IsNullOrEmpty(msg.TechMessage) && string.IsNullOrEmpty(msg.Message))
                {
                    return(apiResponse);
                }

                response.Status.Message.FriendlyMessage  = msg.Message;
                response.Status.Message.TechnicalMessage = msg.TechMessage;
                return(response);
            }
            catch (Exception ex)
            {
                UtilTools.LogE(ex.StackTrace, ex.Source, ex.GetBaseException().Message);
                response.Status.Message.FriendlyMessage  = "Error Occurred! Please try again later";
                response.Status.Message.TechnicalMessage = "Error: " + ex.GetBaseException().Message;
                return(response);
            }
        }
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" }));
            }
        }