public async Task <List <int> > GetBranchesForFiltersAsync(UserBranches userBranches, BaseFilter filter = null)
        {
            List <int> branchList = new List <int>();

            if (userBranches == null)
            {
                return(branchList);
            }
            var userRoles = await _userRoleRepository.GetUserRolesAsync(userBranches.UserId, userBranches.BranchId);

            var top_priority = userRoles.Min(p => p.Role.Order);

            switch (top_priority)
            {
            case ConstRolesOrder.Admin: return(branchList);

            case ConstRolesOrder.User:
                return(branchList);

            case ConstRolesOrder.Customer:
                return(branchList);

            default:
                branchList.Add(userBranches.BranchId);
                break;
            }
            ;
            branchList.Add(userBranches.BranchId);
            return(branchList);
        }
Exemplo n.º 2
0
        public async Task <ResultListDto> GetBranchesAsync(BranchFilterDto branchFilterDto)
        {
            if (branchFilterDto != null && branchFilterDto.UserBranchDto != null)
            {
                UserBranches userBranches = new UserBranches();
                userBranches = _mapper.Map <UserBranches>(branchFilterDto.UserBranchDto);
                if (branchFilterDto.Branches == null || branchFilterDto.Branches.Count == 0)
                {
                    branchFilterDto.Branches = await _branchService.GetBranchesForFiltersAsync(userBranches);
                }
            }
            var branchFilter = _mapper.Map <BranchFilter>(branchFilterDto);

            var branchList = await _branchService.GetBranchesAsync(branchFilter);

            var branchDtoList = _mapper.Map <IEnumerable <BranchDto> >(branchList.Results);

            ResultListDto finalResult = new ResultListDto()
            {
                MaxPageRows = branchList.MaxPageRows,
                TotalRows   = branchList.TotalRows,
                Results     = branchDtoList
            };

            return(finalResult);
        }
        public async Task <ResultObject> CreateUserBranchesAsync(UserBranchesDto userBranchesDto)
        {
            var userBranch = new UserBranches()
            {
                UserId = userBranchesDto.UserId, BranchId = userBranchesDto.BranchId
            };
            var resultObject = await _userService.CreateUserbranchAsync(userBranch);

            await _transactionManager.SaveAllAsync();

            return(resultObject);
        }
        public async Task <List <User> > GetUsersBasedOnRoleAsync(UserBranches userBranches)
        {
            List <User> userList  = new List <User>();
            var         userRoles = await userRolesRepository.GetUserRolesAsync(userBranches.BranchId, userBranches.UserId);

            foreach (var item in userRoles)
            {
                if (item.RoleId == ConstRoles.Admin)
                {
                    UserFilter userFilter = new UserFilter();
                    var        users      = await userRepository.GetByPredicateAsync(X => X.ID != userBranches.UserId);

                    userList = users.ToList();
                    return(userList);
                }
                else if (item.RoleId == ConstRoles.SuperViser)
                {
                    var branch = await branchRepository.GetBranchAsync(userBranches.BranchId);

                    var zone = await zoneRepository.GetZoneInfoByZoneIDAsync(branch.ZoneID);

                    var branches = await zoneRepository.GetProvinceBranchesByProvinceCode(zone.OSTANCode);

                    var users = await userRepository.GetUsereOfBranches(branches);

                    users = users.Where(X => X.ID != userBranches.UserId);

                    userList = users.ToList();
                    return(userList);
                }
                else
                {
                    // var users = await userRepository.GetUsereOfBranche(userBranches.BranchId);

                    //users = users.Where(X => X.ID != userBranches.UserId);

                    //userList = users.ToList();
                }
            }
            return(null);
        }
        public async Task <ResultObject> CreateCustomerAsync(CustomerHeadDto customerDto)
        {
            ResultObject result = new ResultObject();
            var          cu     = await _customerService.GetCustomerByNationalCodeAndBranchIDAsync(customerDto.NationalCode);

            if (cu != null)
            {
                result.ServerErrors.Add(new ServerErr()
                {
                    Hint = "کد ملی مشتری نمیتواند تکراری باشد"
                });
                return(result);
            }
            customerDto.FillCustomerDetail();
            result = ValidateCustomerDetail(customerDto, result);
            if (result.ServerErrors.Count > 0)
            {
                return(result);
            }

            var customer = _mapper.Map <CustomerHead>(customerDto);

            if (customerDto.Skills != null && customerDto.Skills.Count > 0)
            {
                foreach (var skillID in customerDto.Skills)
                {
                    customer.CustomerSkills.Add(new CustomerSkill()
                    {
                        SkillID = skillID
                    });
                }
            }

            var mainUser = await _userAppService.GetUserByNationalCodeAsync(customerDto.NationalCode);

            User user = new User();
            IDbContextTransaction transaction;

            if (mainUser == null)
            {
                user.FirstName           = customerDto.FirstName;
                user.LastName            = customerDto.LastName;
                user.UserName            = customerDto.NationalCode;
                user.NationalCode        = customerDto.NationalCode;
                user.IsActive            = true;
                user.IsRequireChangePass = true;
                user.Password            = "******";

                UserBranches userBranch = new UserBranches()
                {
                    BranchId = customerDto.BranchID, UserId = user.ID
                };
                UserBranchRole userBranchRole = new UserBranchRole()
                {
                    BranchId = customerDto.BranchID, UserId = user.ID, RoleId = ConstRoles.Customer
                };
                user.UserBranches.Add(userBranch);
                user.UserBranchRoles.Add(userBranchRole);
                transaction = await _userService.CreateUserByUserBranchAsync(user);

                await _transactionManager.SaveAllAsync();

                customer.CustomerUserID = user.ID;
                await _customerService.CreateCustomerAsync(customer);

                var res = await _transactionManager.SaveAllAsync();

                transaction.Commit();
            }
            else
            {
                if (result.ServerErrors.Count > 0)
                {
                    return(result);
                }
                customer.CustomerUserID = mainUser.ID;
                await _customerService.CreateCustomerAsync(customer);

                var res = await _transactionManager.SaveAllAsync();
            }

            if (customer.IsMale)
            {
                _notificationAppService.SendMessage($"جناب آقای {customer.FirstName + " " + customer.LastName} " +
                                                    $"{Environment.NewLine} اطلاعات شما با شماره مشتری {customer.Serial} در سامانه صندوق های خرد محلی {customerDto.BranchName} ثبت گردید {Environment.NewLine} نام کاربری {user.UserName}",
                                                    customerDto.MobileCustomerDetail.Value);
            }
            else
            {
                _notificationAppService.SendMessage($"سرکار خانم {customer.FirstName + " " + customer.LastName} {Environment.NewLine} اطلاعات شما با شماره مشتری {customer.Serial} در سامانه صندوق های خرد محلی {customerDto.BranchName} ثبت گردید {Environment.NewLine} نام کاربری {user.UserName}",
                                                    customerDto.MobileCustomerDetail.Value);
            }

            result.Result       = customer.Serial;
            result.ServerErrors = null;
            return(result);
        }
 public async Task RemoveUserBrancheAsync(UserBranches userBranch)
 {
     await userBranchRepository.RemoveAsync(userBranch.ID);
 }
 public async Task <ResultObject> UpdateUserBranchAsync(UserBranches userBranch)
 {
     return(await userBranchRepository.UpdateUserBranchAsync(userBranch, userBranch.ID));
 }
 public async Task <ResultObject> CreateUserbranchAsync(UserBranches userBranch)
 {
     return(await userBranchRepository.AddUserBranchAsync(userBranch));
 }
Exemplo n.º 9
0
        public async Task <ActionResult> RegisterUser(UserViewModels model)
        {
            if (ModelState.IsValid)
            {
                int PasswordExpiryDate = int.Parse(ConfigurationHandler.GetAppSettingsValue("PasswordExpiryDateRange"));
                model.PasswordExpiryDate = DateTime.Now.AddDays(PasswordExpiryDate);


                var user = new ApplicationUser
                {
                    UserName           = model.UserName,
                    Email              = model.Email,
                    Employee_Id        = model.Employee_Id,
                    PasswordExpiryDate = model.PasswordExpiryDate,
                    Defaultbranch_Id   = model.Defaultbranch_Id,
                    IsActive           = true,
                    Created_User_Id    = CurrentUser.Id,
                    //Created_User_Id = 1,
                    Created_Branch_Id = CurrentBranchId,
                    Created_Date      = DateTime.Now,
                    // Modified_User_Id = CurrentUser.Id,
                    //Modified_User_Id = 2,
                    //  Modified_Branch_Id = 2,
                    // Modified_Date = DateTime.Now
                };



                ApplicationUserRole userrole = new ApplicationUserRole();
                userrole.RoleId            = model.Role_Id;
                userrole.Created_User_Id   = CurrentUser.Id;
                userrole.Created_Branch_Id = CurrentBranchId;
                userrole.Created_Date      = DateTime.Now;
                user.Roles.Add(userrole);

                var result = await _userManager.CreateAsync(user, model.Password);

                int userId = user.Id;
                if (result.Succeeded)
                {
                    List <UserBranches> userBranches = new List <UserBranches>();
                    foreach (string selectedBranch in model.SubmittedBranches)
                    {
                        int branchId = 0;
                        if (int.TryParse(selectedBranch, out branchId))
                        {
                            UserBranches userbranch = new UserBranches()
                            {
                                Branch_Id         = branchId,
                                User_Id           = userId,
                                Created_User_Id   = CurrentUser.Id,
                                Created_Branch_Id = CurrentBranchId,
                                Created_Date      = DateTime.Now,
                                // Modified_User_Id = CurrentUser.Id,
                                // Modified_Branch_Id = CurrentBranchId,
                                // Modified_Date = DateTime.Now
                            };
                            userBranches.Add(userbranch);
                        }
                    }
                    string errorMsg = string.Empty;
                    int    Id       = user.Id;
                    if (userDb.SaveUserBranches(userBranches, Id, ref errorMsg))
                    {
                        return(RedirectToAction("Index", "User"));
                    }
                    else
                    {
                        ViewBag.AppErrorMessage = errorMsg;
                        return(View("Error"));
                    }
                }
                else
                {
                    AddErrors(result);
                }
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
Exemplo n.º 10
0
        public async Task <ActionResult> EditUser(UserViewModels model)
        {
            if (ModelState.IsValid == false)
            {
                var user = _userManager.FindById(model.Id);

                // var user = new ApplicationUser();

                user.UserName           = model.UserName;
                user.Email              = model.Email;
                user.Employee_Id        = model.Employee_Id;
                user.Defaultbranch_Id   = model.Defaultbranch_Id;
                user.IsActive           = model.IsActive;
                user.Modified_User_Id   = CurrentUser.Id;
                user.Modified_Branch_Id = CurrentBranchId;
                user.Modified_Date      = DateTime.Now;


                try
                {
                    //user.Roles.FirstOrDefault().RoleId = model.Role_Id;
                    //user.Roles.FirstOrDefault().Modified_User_Id = CurrentUser.Id;
                    user.Roles.FirstOrDefault().Modified_User_Id   = CurrentUser.Id;
                    user.Roles.FirstOrDefault().Modified_Branch_Id = CurrentBranchId;
                    user.Roles.FirstOrDefault().Modified_Date      = DateTime.Now;


                    var result = await _userManager.UpdateAsync(user);

                    if (result.Succeeded)
                    {
                        List <UserBranches> userBranches = new List <UserBranches>();
                        foreach (string selectedBranch in model.SubmittedBranches)
                        {
                            int branchId = 0;
                            if (int.TryParse(selectedBranch, out branchId))
                            {
                                UserBranches userbranch = new UserBranches()
                                {
                                    Branch_Id = branchId,
                                    User_Id   = user.Id,
                                    // Created_User_Id = CurrentUser.Id,
                                    // Created_Branch_Id = CurrentBranchId,
                                    // Created_Date = DateTime.Now,
                                    Modified_User_Id   = CurrentUser.Id,
                                    Modified_Branch_Id = CurrentBranchId,
                                    Modified_Date      = DateTime.Now
                                };
                                userBranches.Add(userbranch);
                            }
                        }
                        string errorMsg = string.Empty;
                        int    Id       = user.Id;
                        if (userDb.SaveUserBranches(userBranches, Id, ref errorMsg))
                        {
                            return(RedirectToAction("Index", "User"));
                        }
                        else
                        {
                            ViewBag.AppErrorMessage = errorMsg;
                            AddErrors(result);
                            return(View("Error"));
                        }
                    }
                    else
                    {
                        ViewBag.AppErrorMessage = result.Errors.DefaultIfEmpty();
                        AddErrors(result);
                        return(View("Error"));
                    }
                }
                catch (Exception ex)
                {
                    LogHandler.WriteLog(ex.Message);
                    ViewBag.AppErrorMessage = ex.Message;
                    return(View("Error"));
                }
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }