public async Task <OperationDTO[]> Handle(AccountOperationHistoryRequest request, CancellationToken cancellationToken)
        {
            User currentUser = await currentUserService.GetCurrentUserAsync();

            var response = await operationRepository.Set().Where(x => x.Account.Wallet.Id == currentUser.Wallet.Id)
                           .OrderBy(x => x.Date)
                           .Select(x => new OperationDTO()
            {
                Amount        = x.Amount,
                Currency      = x.Account.Currency.IsoAlfaCode,
                Direction     = x.Direction.ToString(),
                OperationDate = x.Date,
                Id            = x.Id
            })
                           .ToArrayAsync();

            return(response);
        }
            public async Task <AchievementViewModel> Handle(AddAchievementCommand request, CancellationToken cancellationToken)
            {
                var achievement = await _context
                                  .Achievements
                                  .Where(a => a.Code == request.Code)
                                  .FirstOrDefaultAsync(cancellationToken);

                if (achievement == null)
                {
                    throw new NotFoundException(request.Code, nameof(Domain.Entities.Achievement));
                }

                var user = await _currentUserService.GetCurrentUserAsync(cancellationToken);

                var userHasAchievement = await _context
                                         .UserAchievements
                                         .Where(ua => ua.UserId == user.Id)
                                         .Where(ua => ua.AchievementId == achievement.Id)
                                         .AnyAsync(cancellationToken);

                if (userHasAchievement)
                {
                    throw new AlreadyAwardedException(user.Id, achievement.Name);
                }

                await _context
                .UserAchievements
                .AddAsync(new Domain.Entities.UserAchievement
                {
                    UserId        = user.Id,
                    AchievementId = achievement.Id
                }, cancellationToken);

                try
                {
                    await _context.SaveChangesAsync(cancellationToken);
                }
                catch (Exception)
                {
                    throw new AlreadyAwardedException(user.Id, achievement.Name);
                }

                return(_mapper.Map <AchievementViewModel>(achievement));
            }
Example #3
0
        public async Task <T> Map <T>(TransactionCommandDto source) where T : TransactionCommand, new()
        {
            var currency = await currencyRepository.FindAsync(source.CurrencyId);

            var user = await currentUserService.GetCurrentUserAsync();

            var cantorWallet   = cantorWalletRepository.GetCantorWallet();
            var cantorCurrency = cantorWallet.CantorCurrencies
                                 .FindByCurrency(source.CurrencyId);
            var userBoughtCurrency = user.Currencies.FindByCurrency(currency);

            return(new T()
            {
                Currency = currency,
                Amount = source.Amount,
                User = user,
                CantorWallet = cantorWallet,
                CantorCurrency = cantorCurrency,
                UserCurrency = userBoughtCurrency,
            });
        }
Example #4
0
            public async Task <ClaimRewardResult> Handle(ClaimRewardCommand request, CancellationToken cancellationToken)
            {
                var reward = await _context
                             .Rewards
                             .Where(r => r.Code == request.Code)
                             .FirstOrDefaultAsync(cancellationToken);

                if (reward == null)
                {
                    return(new ClaimRewardResult
                    {
                        status = RewardStatus.NotFound
                    });
                }

                var user = await _currentUserService.GetCurrentUserAsync(cancellationToken);

                var userRewards = await _context
                                  .UserRewards
                                  .Where(ur => ur.UserId == user.Id)
                                  .ToListAsync(cancellationToken);

                int pointsUsed = userRewards.Sum(ur => ur.Reward.Cost);

                int balance = user.Points - pointsUsed;

                if (balance < reward.Cost)
                {
                    return(new ClaimRewardResult
                    {
                        status = RewardStatus.NotEnoughPoints
                    });
                }

                // TECH DEBT: the following logic is intended to 'debounce' reward
                // claiming, to prevent users claiming the same reward twice
                // within a 5 minute window. This workaround is only required on
                // the current 'milestone' model for points. Once we move to the
                // 'currency' model, this will not be required anymore.
                // see: https://github.com/SSWConsulting/SSW.Rewards/issues/100
                var userHasReward = userRewards
                                    .Where(ur => ur.RewardId == reward.Id)
                                    .FirstOrDefault();

                if (userHasReward != null && userHasReward.AwardedAt >= _dateTimeProvider.Now.AddMinutes(-5))
                {
                    return(new ClaimRewardResult
                    {
                        status = RewardStatus.Duplicate
                    });
                }

                await _context
                .UserRewards
                .AddAsync(new Domain.Entities.UserReward
                {
                    UserId   = user.Id,
                    RewardId = reward.Id
                }, cancellationToken);

                await _context.SaveChangesAsync(cancellationToken);

                var dbUser = await _context.Users
                             .Where(u => u.Id == user.Id)
                             .FirstAsync(cancellationToken);

                await _rewardSender.SendRewardAsync(dbUser, reward, cancellationToken);

                var rewardModel = _mapper.Map <RewardViewModel>(reward);

                return(new ClaimRewardResult
                {
                    viewModel = rewardModel,
                    status = RewardStatus.Claimed
                });
            }