Exemplo n.º 1
0
        private async Task <UserMultiAccount> GetSponsor()
        {
            var userAccount = await _userAccountDataRepository.GetAsync(_command.UserAccountId);

            var userMultiAccountIds = userAccount.UserMultiAccounts.Select(x => x.Id).ToList();

            // TODO: How to verify the reflink user's matrix level? Is it 0, 1...9?
            var sponsor = await _userMultiAccountRepository.GetByReflinkAsync(_command.SponsorReflink);

            var sponsorsMatrix = await _matrixPositionHelper.GetMatrixForGivenMultiAccountAsync(sponsor.Id);

            if (_matrixPositionHelper.CheckIfAnyAccountExistInMatrix(sponsorsMatrix, userMultiAccountIds) ||
                !_matrixPositionHelper.CheckIfMatrixHasEmptySpace(sponsorsMatrix))
            {
                // Nie ma miejsca w matrycy sponsora lub w matrycy sponsora jest któreś z kont użytkownika
                // Wtedy szukamy nowego sponsora
                var emptyMatrixPositionUnderAvailableSponsor = await _matrixPositionHelper
                                                               .FindTheNearestEmptyPositionFromGivenAccountWhereInParentsMatrixThereIsNoAnyMultiAccountAsync(sponsor.Id, userMultiAccountIds, 0);

                if (emptyMatrixPositionUnderAvailableSponsor == null)
                {
                    throw new ValidationException("There is no space in this matrix level to put this account.");
                }

                var parentPosition = await _matrixPositionRepository.GetAsync(emptyMatrixPositionUnderAvailableSponsor.ParentId.Value);

                sponsor = await _userMultiAccountRepository.GetAsync(parentPosition.UserMultiAccountId.Value);
            }

            return(sponsor);
        }
Exemplo n.º 2
0
        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);
        }
        public async Task <Guid> Handle(BuyPositionInMatrixWithoutPaymentValidation_TMPCommand command, CancellationToken cancellationToken)
        {
            var userMultiAccount = await _userMultiAccountRepository.GetAsync(command.UserMultiAccountId);

            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 matrix on level: {command.MatrixLevel}");
            }

            MatrixPosition matrixPosition;
            var            matrixPositions = invitingUserMatrix as MatrixPosition[] ?? invitingUserMatrix.ToArray();

            if (_matrixPositionHelper.CheckIfMatrixHasEmptySpace(matrixPositions))
            {
                matrixPosition = matrixPositions
                                 .OrderBy(x => x.DepthLevel)
                                 .First(x => x.UserMultiAccountId == null);
            }
            else
            {
                throw new ValidationException("There is no empty space in matrix");
            }

            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));

            return(matrixPosition.Id);
        }