Exemplo n.º 1
0
        public async Task <int> Payout([FromBody] HashSet <string> chatters)
        {
            try
            {
                var grouping = (await _dbContext.JoinUserModels().Where(u => chatters.Contains(u.UserIdentity.TwitchId))
                                .AsNoTracking()
                                .ToArrayAsync()).GroupBy(u => u.SubTier).ToArray();

                decimal       total       = 0;
                int           changedRows = 0;
                StringBuilder sql         = new StringBuilder();

                var pk = nameof(UserWallet.UserId);
                foreach (var group in grouping)
                {
                    decimal payout = 0;
                    var     level  = group.Key;
                    if (level == 0)
                    {
                        continue;
                    }
                    var ids = group.Select(u => u.UserId).ToArray();
                    if (level == 1)
                    {
                        payout = 0.25m;
                    }
                    else if (level == 2)
                    {
                        payout = .5m;
                    }
                    else
                    {
                        payout = 1;
                    }
                    sql.Append(TxUtils.ModifyNumber(nameof(UserWallet), nameof(UserWallet.Balance), payout, '+', pk, ids));
                    sql.Append(TxUtils.ModifyNumber(nameof(UserStat), nameof(UserStat.EarnedIdle), payout, '+', pk, ids));

                    int count = await _dbContext.Database.ExecuteSqlRawAsync(sql.ToString());

                    total       += payout * count;
                    changedRows += count;
                }
                if (changedRows > 0)
                {
                    await _dbContext.Database.ExecuteSqlRawAsync(TxUtils.ModifyNumber(nameof(UserWallet), nameof(UserWallet.Balance), total, '-', pk, BitcornHubPK));
                }
                //this endpoint is called frequently so can use this to check if there are tx's that need to be refunded
                await TxUtils.RefundUnclaimed(_dbContext);

                return(changedRows / 2);
            }
            catch (Exception e)
            {
                await BITCORNLogger.LogError(_dbContext, e);

                throw e;
            }
        }
        public async Task TestTipCornRefund()
        {
            var dbContext = TestUtils.CreateDatabase();

            try
            {
                var startFromUser = dbContext.TwitchQuery(_configuration["Config:TestFromUserId"]).FirstOrDefault().UserWallet.Balance;
                var txController  = new TxController(dbContext);
                txController.TimeToClaimTipMinutes = -1;
                var request = new TipRequest();
                request.Columns = new string[] { };
                string toTwitchId = "123123";
                request.To       = "twitch|" + toTwitchId;
                request.From     = "twitch|" + _configuration["Config:TestFromUserId"];
                request.Platform = "twitch";
                request.Amount   = 100;

                var toUser = await dbContext.TwitchAsync(toTwitchId);

                dbContext.RemoveRange(dbContext.UnclaimedTx);
                TestUtils.RemoveUserIfExists(dbContext, toUser);

                var response = await txController.Tipcorn(request);

                var receipt = response.Value[0];
                Assert.Null(receipt.Tx);
                Assert.NotNull(receipt.From);
                Assert.Null(receipt.To);

                using (var dbContext2 = TestUtils.CreateDatabase())
                {
                    var endFromUser = dbContext2.TwitchQuery(_configuration["Config:TestFromUserId"]).FirstOrDefault().UserWallet.Balance;
                    Assert.Equal(startFromUser - request.Amount, endFromUser);
                    await TxUtils.RefundUnclaimed(dbContext, 0);

                    /*
                     */
                }
                using (var dbContext2 = TestUtils.CreateDatabase())
                {
                    var balance = dbContext2.TwitchQuery(_configuration["Config:TestFromUserId"]).FirstOrDefault().UserWallet.Balance;
                    Assert.Equal(startFromUser, balance);
                    dbContext2.AddUser(new UserIdentity()
                    {
                        Auth0Id       = "temp",
                        Auth0Nickname = "temp",
                        TwitchId      = toTwitchId
                    }, new UserWallet());
                    var to = await dbContext2.TwitchAsync(toTwitchId);

                    int claim = await TxUtils.TryClaimTx(new PlatformId()
                    {
                        Platform = "twitch", Id = toTwitchId
                    }, to, dbContext2);

                    Assert.Equal(0, claim);
                }
                using (var dbContext2 = TestUtils.CreateDatabase())
                {
                    var to = await dbContext2.TwitchAsync(toTwitchId);

                    Assert.Equal(0, to.UserWallet.Balance);
                    TestUtils.RemoveUserIfExists(dbContext, to);
                }
            }
            finally
            {
                dbContext.Dispose();
            }
        }