Balance history
This commit is contained in:
24
NostrStreamer/ApiModel/BalanceHistoryItem.cs
Normal file
24
NostrStreamer/ApiModel/BalanceHistoryItem.cs
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
using Newtonsoft.Json;
|
||||||
|
|
||||||
|
namespace NostrStreamer.ApiModel;
|
||||||
|
|
||||||
|
public enum BalanceHistoryItemType
|
||||||
|
{
|
||||||
|
Credit,
|
||||||
|
Debit
|
||||||
|
}
|
||||||
|
|
||||||
|
public class BalanceHistoryItem
|
||||||
|
{
|
||||||
|
[JsonProperty("created")]
|
||||||
|
public DateTime Created { get; init; }
|
||||||
|
|
||||||
|
[JsonProperty("type")]
|
||||||
|
public BalanceHistoryItemType Type { get; init; }
|
||||||
|
|
||||||
|
[JsonProperty("amount")]
|
||||||
|
public decimal Amount { get; init; }
|
||||||
|
|
||||||
|
[JsonProperty("desc")]
|
||||||
|
public string? Description { get; init; }
|
||||||
|
}
|
@ -362,11 +362,46 @@ public class NostrController : Controller
|
|||||||
if (string.IsNullOrEmpty(userPubkey))
|
if (string.IsNullOrEmpty(userPubkey))
|
||||||
return BadRequest();
|
return BadRequest();
|
||||||
|
|
||||||
var (fee, preimage) = await _userService.WithdrawFunds(userPubkey, invoice);
|
try
|
||||||
return Json(new
|
|
||||||
{
|
{
|
||||||
fee, preimage
|
var (fee, preimage) = await _userService.WithdrawFunds(userPubkey, invoice);
|
||||||
});
|
return Json(new
|
||||||
|
{
|
||||||
|
fee, preimage
|
||||||
|
});
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
return Json(new
|
||||||
|
{
|
||||||
|
error = e.Message
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpGet("history")]
|
||||||
|
public async Task<IActionResult> BalanceHistory([FromQuery] int page = 0, [FromQuery] int pageSize = 100)
|
||||||
|
{
|
||||||
|
var userPubkey = GetPubKey();
|
||||||
|
if (string.IsNullOrEmpty(userPubkey))
|
||||||
|
return BadRequest();
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var txns = await _userService.BalanceHistory(userPubkey, page * pageSize, pageSize);
|
||||||
|
return Json(new
|
||||||
|
{
|
||||||
|
items = txns,
|
||||||
|
page, pageSize
|
||||||
|
});
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
return Json(new
|
||||||
|
{
|
||||||
|
error = e.Message
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task<User?> GetUser()
|
private async Task<User?> GetUser()
|
||||||
|
@ -149,13 +149,44 @@ public class UserService
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<List<Payment>> ListPayments(string pubkey)
|
public async Task<List<Payment>> ListPayments(string pubkey, int offset = 0, int limit = 100)
|
||||||
{
|
{
|
||||||
return await _db.Payments
|
return await _db.Payments
|
||||||
.Where(a => a.PubKey == pubkey && a.IsPaid)
|
.Where(a => a.PubKey == pubkey && a.IsPaid)
|
||||||
|
.Skip(offset)
|
||||||
|
.Take(limit)
|
||||||
.ToListAsync();
|
.ToListAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task<List<BalanceHistoryItem>> BalanceHistory(string pubkey, int offset = 0, int limit = 100)
|
||||||
|
{
|
||||||
|
return await _db.Payments
|
||||||
|
.Where(a => a.PubKey == pubkey && a.IsPaid)
|
||||||
|
.Select(t => new BalanceHistoryItem
|
||||||
|
{
|
||||||
|
Created = t.Created,
|
||||||
|
Type = t.Type == PaymentType.Withdrawal ? BalanceHistoryItemType.Debit : BalanceHistoryItemType.Credit,
|
||||||
|
Description = t.Type == PaymentType.Withdrawal
|
||||||
|
? "Withdrawal"
|
||||||
|
: (t.Type == PaymentType.Credit ? "Admin Credit" : ""),
|
||||||
|
Amount = t.Amount / 1000m
|
||||||
|
})
|
||||||
|
.Union(_db.Streams
|
||||||
|
.Where(a => a.PubKey == pubkey && a.State == UserStreamState.Ended)
|
||||||
|
.Select(t => new BalanceHistoryItem
|
||||||
|
{
|
||||||
|
Created = t.Starts,
|
||||||
|
Description = t.Event,
|
||||||
|
Type = BalanceHistoryItemType.Debit,
|
||||||
|
Amount = t.MilliSatsCollected / 1000m
|
||||||
|
}))
|
||||||
|
.OrderByDescending(a => a.Created)
|
||||||
|
.Skip(offset)
|
||||||
|
.Take(limit)
|
||||||
|
.ToListAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public async Task<long> MaxWithdrawalAmount(string pubkey)
|
public async Task<long> MaxWithdrawalAmount(string pubkey)
|
||||||
{
|
{
|
||||||
var credit = await _db.Payments
|
var credit = await _db.Payments
|
||||||
|
Reference in New Issue
Block a user