public async Task <Guid> Handle(BuyPositionInMatrixCommand command, CancellationToken cancellationToken = default(CancellationToken)) { var userMultiAccount = await _userMultiAccountRepository.GetAsync(command.UserMultiAccountId); await ValidateUserMultiAccount(userMultiAccount, command.MatrixLevel); var sponsorAccountId = userMultiAccount.SponsorId.Value; var invitingUserMatrix = await _matrixPositionHelper.GetMatrixForGivenMultiAccountAsync(sponsorAccountId, command.MatrixLevel); if (invitingUserMatrix is null) { throw new ValidationException($"The inviting user from reflink does not have structure on level: {command.MatrixLevel}"); } MatrixPosition matrixPosition; if (_matrixPositionHelper.CheckIfMatrixHasEmptySpace(invitingUserMatrix)) { matrixPosition = invitingUserMatrix .OrderBy(x => x.DepthLevel) .First(x => x.UserMultiAccountId == null); } else { var userAccount = await _userAccountDataRepository.GetAsync(userMultiAccount.UserAccountDataId); var userMultiAccountIds = userAccount.UserMultiAccounts.Select(x => x.Id).ToList(); matrixPosition = await _matrixPositionHelper.FindTheNearestEmptyPositionFromGivenAccountWhereInParentsMatrixThereIsNoAnyMultiAccountAsync( sponsorAccountId, userMultiAccountIds, command.MatrixLevel); if (matrixPosition is null) { throw new ValidationException("There is no empty space in the structure where account can be assigned"); } await ChangeUserSponsor(userMultiAccount, matrixPosition); } matrixPosition.AssignMultiAccount(command.UserMultiAccountId); await _matrixPositionRepository.UpdateAsync(matrixPosition); _backgroundJobClient.Enqueue <MatrixPositionHasBeenBoughtJob>( job => job.Execute(matrixPosition.Id, null)); _backgroundJobClient.Enqueue <UserBoughtMatrixPositionJob>( job => job.Execute(userMultiAccount.Id, null)); _backgroundJobClient.Enqueue <InitWithdrawalJob>( job => job.Execute(new InitWithdrawalModel { MatrixPositionId = matrixPosition.Id, WithdrawalFor = WithdrawalForHelper.AssignmentInMatrix }, null)); return(matrixPosition.Id); }
private async Task ValidateCommand(PayMembershipsFeeCommand command) { var user = await _userAccountDataRepository.GetAsync(command.UserAccountDataId); if (user is null) { throw new ValidationException("User with given ID was not found"); } }
public async Task <Unit> Handle(UpdateUserCommand command, CancellationToken cancellationToken = default(CancellationToken)) { var userToUpdate = await _userAccountDataRepository.GetAsync(command.UserId); ValidatePermission(userToUpdate, command.RequestedUser, command.Role); var userWithEmail = await _userAccountDataRepository.GetAsync(command.Email); if (userWithEmail != null && userWithEmail.Id != userToUpdate.Id) { throw new ValidationException("Email does already exists."); } userToUpdate.UpdateInformation(command.Email, command.FirstName, command.LastName, command.Street, command.City, command.ZipCode, command.Country, command.BtcWalletAddress, command.InitiativeDescription); userToUpdate.UpdateRole(command.Role); await _userAccountDataRepository.UpdateAsync(userToUpdate); return(Unit.Value); }
private async Task MembershipsFeePaid(Guid orderId) { var userAccount = await _userAccountDataRepository.GetAsync(orderId); if (userAccount is null) { throw new ValidationException($"Cannot find the UserAccountData from PaymentHistory with OrderId: {orderId}"); } userAccount.PaidMembershipFee(); await _userAccountDataRepository.UpdateAsync(userAccount); }
private async Task ValidateIfMultiAccountCanBeCreated() { var userAccount = await _userAccountDataRepository.GetAsync(_command.UserAccountId); if (userAccount is null) { throw new AccountNotFoundException("User with given ID does not exist"); } var sponsor = await _userMultiAccountRepository.GetByReflinkAsync(_command.SponsorReflink); if (sponsor is null) { throw new AccountNotFoundException("Account with given reflink does not exist"); } if (CheckIfReflinkBelongsToRequestedUser(sponsor)) { throw new ValidationException("Given reflink belongs to the requested user account"); } if (!userAccount.IsMembershipFeePaid) { throw new ValidationException("The main account did not pay the membership's fee yet"); } var userMultiAccountIds = userAccount.UserMultiAccounts.Select(x => x.Id).ToList(); if (!await CheckIfAllMultiAccountsAreInMatrixPositions(userMultiAccountIds)) { throw new ValidationException("Not all user multi accounts are available in matrix positions"); } if (userAccount.UserMultiAccounts.Count > 20) { throw new ValidationException("You cannot have more than 20 multi accounts attached to the main account"); } }
public async Task <string> GenerateNextMultiAccountName(Guid userAccountDataId) { var userAccount = await _userAccountDataRepository.GetAsync(userAccountDataId); var numberOfMultiAccounts = userAccount.UserMultiAccounts.Count; if (numberOfMultiAccounts == 0) { return(userAccount.Login); } return($"{userAccount.Login}-{numberOfMultiAccounts:000}"); }
public async Task <IEnumerable <UserMultiAccountModel> > Handle(GetMultiAccountsRequest request, CancellationToken cancellationToken = default(CancellationToken)) { var userAccount = await _userAccountDataRepository.GetAsync(request.UserAccountId); if (userAccount is null) { throw new AccountNotFoundException("User with given ID does not exist"); } var userMultiAccountModels = _mapper.Map <IEnumerable <UserMultiAccountModel> >(userAccount.UserMultiAccounts); return(userMultiAccountModels); }
public async Task <Unit> Handle(ChangeAvatarCommand request, CancellationToken cancellationToken = default(CancellationToken)) { var user = await _userAccountDataRepository.GetAsync(request.UserAccountDataId); if (user is null) { throw new AccountNotFoundException($"User with given ID cannot be found - {request.UserAccountDataId}"); } user.ChangeAvatar(request.AvatarPath); await _userAccountDataRepository.UpdateAsync(user); return(Unit.Value); }
public async Task <string> Handle(LoginCommand command, CancellationToken cancellationToken = default(CancellationToken)) { var user = await _userAccountDataRepository.GetAsync(command.LoginOrEmail); if (user is null) { throw new ValidationException("Invalid credentials"); } VerifyLoginCredentials(command.Password, user.Hash, user.Salt); var jwtTokenString = CreateTokenString(user.Id, user.Email, user.Role); return(jwtTokenString); }
public async Task <Guid> Handle(CreateArticleCommand command, CancellationToken cancellationToken = default(CancellationToken)) { var user = await _userAccountDataRepository.GetAsync(command.UserAccountDataId); var article = new Article( id: Guid.NewGuid(), creatorId: user.Id, title: command.Title, text: command.Text, articleType: command.ArticleType ); await _articleRepository.CreateAsync(article); return(article.Id); }
private async Task <Guid> UpgradeMatrixForUser(MatrixPosition adminPosition, AdminStructureSide adminStructure) { MatrixPosition upgradedPosition; if (!_multiAccount.SponsorId.HasValue) { throw new ValidationException("FATAL! User does not have sponsor"); } var userAccount = await _userAccountDataRepository.GetAsync(_multiAccount.UserAccountDataId); var userMultiAccountIds = userAccount.UserMultiAccounts.Select(x => x.Id).ToList(); // Need for cycles in the future var sponsorPositionOnUpgradedMatrix = await _matrixPositionHelper.GetPositionForAccountAtLevelAsync(_multiAccount.SponsorId.Value, _command.MatrixLevel); if (sponsorPositionOnUpgradedMatrix is null) { upgradedPosition = await _matrixPositionHelper.FindTheNearestEmptyPositionFromGivenAccountWhereInParentsMatrixThereIsNoAnyMultiAccountAsync( adminPosition.UserMultiAccountId.Value, userMultiAccountIds, _command.MatrixLevel, adminStructure); } else { upgradedPosition = await _matrixPositionHelper.FindTheNearestEmptyPositionFromGivenAccountWhereInParentsMatrixThereIsNoAnyMultiAccountAsync( _multiAccount.SponsorId.Value, userMultiAccountIds, _command.MatrixLevel); } if (upgradedPosition is null) { throw new ValidationException($"There is no empty space in the structure level - {_command.MatrixLevel} - where account can be assigned"); } upgradedPosition.AssignMultiAccount(_multiAccount.Id); await _matrixPositionRepository.UpdateAsync(upgradedPosition); _backgroundJobClient.Enqueue <MatrixPositionHasBeenUpgradedJob>( job => job.Execute(upgradedPosition.Id, null)); _backgroundJobClient.Enqueue <InitWithdrawalJob>( job => job.Execute(new InitWithdrawalModel { MatrixPositionId = upgradedPosition.Id, WithdrawalFor = WithdrawalForHelper.UpgradedMatrix }, null)); return(upgradedPosition.Id); }
public async Task <GetArticlesViewModel> Handle(GetArticlesRequest request, CancellationToken cancellationToken = default(CancellationToken)) { if (request.ArticleType == ArticleType.CryptoBlog) { var user = await _userAccountDataRepository.GetAsync(request.RequestedUser.Id); if (!user.IsMembershipFeePaid) { throw new ValidationException("You have to pay membership's fee to get cryptoblog articles"); } } var articles = await _articleRepository.GetAllByStatusAsync(request.ArticleType); var articleModels = _mapper.Map <IEnumerable <ArticleModel> >(articles); return(new GetArticlesViewModel(articleModels)); }
public async Task <UserAccountDataModel> Handle(GetUserRequest request, CancellationToken cancellationToken = default(CancellationToken)) { ValidateRequest(request); var user = await _userAccountDataRepository.GetAsync(request.UserId); var userModel = _mapper.Map <UserAccountDataModel>(user); // More details for the home page var userWithdrawals = await _withdrawalRepository.GetAllAsync(user.UserMultiAccounts.Select(x => x.Id)); var accountsWhereRequestedUserIsSponsor = await _multiAccountHelper.GetAllWhereMultiAccountsAreSponsors(user.UserMultiAccounts.Select(x => x.Id)); userModel.EarnedBtc = userWithdrawals.Sum(x => x.Amount); userModel.InvitedAccountsTotalCount = accountsWhereRequestedUserIsSponsor.Count; userModel.AccountsInMatrixTotalCount = user.UserMultiAccounts.Count(x => x.RefLink != null); return(userModel); }
public async Task <IEnumerable <PaymentHistoryModel> > Handle(GetPaymentHistoriesRequest request, CancellationToken cancellationToken) { var user = await _userAccountDataRepository.GetAsync(request.UserAccountId); if (user is null) { throw new AccountNotFoundException("User with given ID does not exist"); } var allPayments = new List <PaymentHistoryModel>(); var userPayments = await _paymentHistoryRepository.GetPaymentsByUser(user.Id); var userPaymentModels = _mapper.Map <List <PaymentHistoryModel> >(userPayments); foreach (var userPaymentModel in userPaymentModels) { userPaymentModel.AccountName = user.Login; } allPayments.AddRange(userPaymentModels); foreach (var userUserMultiAccount in user.UserMultiAccounts) { var payment = await _paymentHistoryRepository.GetPaymentsByUser(userUserMultiAccount.Id); var paymentModels = _mapper.Map <List <PaymentHistoryModel> >(payment); foreach (var paymentModel in paymentModels) { paymentModel.AccountName = userUserMultiAccount.MultiAccountName; } allPayments.AddRange(paymentModels); } return(allPayments); }