public async Task <IEnumerable <UserBranchRole> > GetUserRolesAsync(UserBranchRole userBranchRole)
        {
            var UserBranchRoles = await Context.UserBranchRoles.Where(a => a.UserId == userBranchRole.UserId &&
                                                                      a.BranchId == userBranchRole.BranchId).Include(p => p.Role)
                                  .Include(p => p.Branch).ToListAsync();

            return(UserBranchRoles);
        }
        public async Task <ResultObject> CreateUserRoleAsync(UserBranchRoleDto userRolesDto)
        {
            var userRole = new UserBranchRole()
            {
                UserId = userRolesDto.UserId, RoleId = userRolesDto.RoleId, BranchId = userRolesDto.BranchId
            };
            var resultObj = await _userService.CreateUserRoleAsync(userRole);

            var user = await _userService.GetUserAsync(userRole.UserId);

            resultObj.Result = await _transactionManager.SaveAllAsync();

            return(resultObj);
        }
        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 <IEnumerable <UserBranchRole> > GetUserRolesAsync(UserBranchRole userBranchRole)
 {
     return(await userRepository.GetUserRolesAsync(userBranchRole));
 }
 public async Task RemoveUserRoleAsync(UserBranchRole userRole)
 {
     await userRolesRepository.RemoveAsync(userRole.ID);
 }
 public async Task <ResultObject> UpdateUserRoleAsync(UserBranchRole userRole)
 {
     return(await userRolesRepository.UpdateUserBranchRoleAsync(userRole));
 }