From ccd9dbcdbf92fb755d385163d60518396c0302bd Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Sat, 1 Nov 2025 12:37:39 +0800 Subject: [PATCH] :bug: Fix dozens of issue in PaymentServiceGrpc --- .../Thought/ThoughtService.cs | 76 ++++++++++++------- .../Wallet/PaymentServiceGrpc.cs | 58 +++++++++----- DysonNetwork.Shared/Proto/wallet.proto | 18 ++--- 3 files changed, 95 insertions(+), 57 deletions(-) diff --git a/DysonNetwork.Insight/Thought/ThoughtService.cs b/DysonNetwork.Insight/Thought/ThoughtService.cs index 520a82f..4f4ac01 100644 --- a/DysonNetwork.Insight/Thought/ThoughtService.cs +++ b/DysonNetwork.Insight/Thought/ThoughtService.cs @@ -8,15 +8,24 @@ using WalletService = DysonNetwork.Shared.Proto.WalletService; namespace DysonNetwork.Insight.Thought; -public class ThoughtService(AppDatabase db, ICacheService cache, PaymentService.PaymentServiceClient paymentService, WalletService.WalletServiceClient walletService) +public class ThoughtService( + AppDatabase db, + ICacheService cache, + PaymentService.PaymentServiceClient paymentService, + WalletService.WalletServiceClient walletService +) { - public async Task GetOrCreateSequenceAsync(Guid accountId, Guid? sequenceId, - string? topic = null) + public async Task GetOrCreateSequenceAsync( + Guid accountId, + Guid? sequenceId, + string? topic = null + ) { if (sequenceId.HasValue) { var seq = await db.ThinkingSequences.FindAsync(sequenceId.Value); - if (seq == null || seq.AccountId != accountId) return null; + if (seq == null || seq.AccountId != accountId) + return null; return seq; } else @@ -46,7 +55,7 @@ public class ThoughtService(AppDatabase db, ICacheService cache, PaymentService. Role = role, TokenCount = tokenCount, ModelName = model, - Chunks = chunks ?? new List() + Chunks = chunks ?? new List(), }; db.ThinkingThoughts.Add(thought); @@ -65,25 +74,35 @@ public class ThoughtService(AppDatabase db, ICacheService cache, PaymentService. public async Task> GetPreviousThoughtsAsync(SnThinkingSequence sequence) { var cacheKey = $"thoughts:{sequence.Id}"; - var (found, cachedThoughts) = await cache.GetAsyncWithStatus>(cacheKey); + var (found, cachedThoughts) = await cache.GetAsyncWithStatus>( + cacheKey + ); if (found && cachedThoughts != null) { return cachedThoughts; } - var thoughts = await db.ThinkingThoughts - .Where(t => t.SequenceId == sequence.Id) + var thoughts = await db + .ThinkingThoughts.Where(t => t.SequenceId == sequence.Id) .OrderByDescending(t => t.CreatedAt) .ToListAsync(); // Cache for 10 minutes - await cache.SetWithGroupsAsync(cacheKey, thoughts, [$"sequence:{sequence.Id}"], TimeSpan.FromMinutes(10)); + await cache.SetWithGroupsAsync( + cacheKey, + thoughts, + [$"sequence:{sequence.Id}"], + TimeSpan.FromMinutes(10) + ); return thoughts; } - public async Task<(int total, List sequences)> ListSequencesAsync(Guid accountId, int offset, - int take) + public async Task<(int total, List sequences)> ListSequencesAsync( + Guid accountId, + int offset, + int take + ) { var query = db.ThinkingSequences.Where(s => s.AccountId == accountId); var totalCount = await query.CountAsync(); @@ -98,8 +117,8 @@ public class ThoughtService(AppDatabase db, ICacheService cache, PaymentService. public async Task SettleThoughtBills(ILogger logger) { - var sequences = await db.ThinkingSequences - .Where(s => s.PaidToken < s.TotalToken) + var sequences = await db + .ThinkingSequences.Where(s => s.PaidToken < s.TotalToken) .ToListAsync(); if (sequences.Count == 0) @@ -117,29 +136,32 @@ public class ThoughtService(AppDatabase db, ICacheService cache, PaymentService. var totalUnpaidTokens = accountGroup.Sum(s => s.TotalToken - s.PaidToken); var cost = (long)Math.Ceiling(totalUnpaidTokens / 1000.0); - if (cost == 0) continue; + if (cost == 0) + continue; try { - var walletResponse = await walletService.GetWalletAsync(new GetWalletRequest { AccountId = accountId.ToString() }); - var walletId = Guid.Parse(walletResponse.Id); - var date = DateTime.Now.ToString("yyyy-MM-dd"); - await paymentService.CreateTransactionAsync(new CreateTransactionRequest - { - PayerWalletId = walletId.ToString(), - PayeeWalletId = null, - Currency = WalletCurrency.SourcePoint, - Amount = cost.ToString(), - Remarks = $"Wage for SN-chan on {date}", - Type = TransactionType.System - }); + await paymentService.CreateTransactionWithAccountAsync( + new CreateTransactionWithAccountRequest + { + PayeeAccountId = accountId.ToString(), + Currency = WalletCurrency.SourcePoint, + Amount = cost.ToString(), + Remarks = $"Wage for SN-chan on {date}", + Type = TransactionType.System, + } + ); // Mark all sequences for this account as paid foreach (var sequence in accountGroup) sequence.PaidToken = sequence.TotalToken; - logger.LogInformation("Billed {cost} points for account {accountId}", cost, accountId); + logger.LogInformation( + "Billed {cost} points for account {accountId}", + cost, + accountId + ); } catch (Exception ex) { diff --git a/DysonNetwork.Pass/Wallet/PaymentServiceGrpc.cs b/DysonNetwork.Pass/Wallet/PaymentServiceGrpc.cs index 9a6f5cd..fc2e233 100644 --- a/DysonNetwork.Pass/Wallet/PaymentServiceGrpc.cs +++ b/DysonNetwork.Pass/Wallet/PaymentServiceGrpc.cs @@ -5,15 +5,21 @@ using NodaTime; namespace DysonNetwork.Pass.Wallet; -public class PaymentServiceGrpc(PaymentService paymentService) : Shared.Proto.PaymentService.PaymentServiceBase +public class PaymentServiceGrpc(PaymentService paymentService) + : Shared.Proto.PaymentService.PaymentServiceBase { - public override async Task CreateOrder(CreateOrderRequest request, ServerCallContext context) + public override async Task CreateOrder( + CreateOrderRequest request, + ServerCallContext context + ) { var order = await paymentService.CreateOrderAsync( request.HasPayeeWalletId ? Guid.Parse(request.PayeeWalletId) : null, request.Currency, decimal.Parse(request.Amount), - request.Expiration is not null ? Duration.FromSeconds(request.Expiration.Seconds) : null, + request.Expiration is not null + ? Duration.FromSeconds(request.Expiration.Seconds) + : null, request.HasAppIdentifier ? request.AppIdentifier : SnWalletOrder.InternalAppIdentifier, request.HasProductIdentifier ? request.ProductIdentifier : null, request.HasRemarks ? request.Remarks : null, @@ -26,50 +32,65 @@ public class PaymentServiceGrpc(PaymentService paymentService) : Shared.Proto.Pa } public override async Task CreateTransactionWithAccount( - CreateTransactionWithAccountRequest request, ServerCallContext context) + CreateTransactionWithAccountRequest request, + ServerCallContext context + ) { var transaction = await paymentService.CreateTransactionWithAccountAsync( - request.HasPayerAccountId ? Guid.Parse(request.PayerAccountId) : null, - request.HasPayeeAccountId ? Guid.Parse(request.PayeeAccountId) : null, + request.PayerAccountId is not null ? Guid.Parse(request.PayerAccountId) : null, + request.PayeeAccountId is not null ? Guid.Parse(request.PayeeAccountId) : null, request.Currency, decimal.Parse(request.Amount), - request.HasRemarks ? request.Remarks : null, + request.Remarks is not null ? request.Remarks : null, (Shared.Models.TransactionType)request.Type ); return transaction.ToProtoValue(); } - public override async Task CreateTransaction(CreateTransactionRequest request, - ServerCallContext context) + public override async Task CreateTransaction( + CreateTransactionRequest request, + ServerCallContext context + ) { var transaction = await paymentService.CreateTransactionAsync( - request.HasPayerWalletId ? Guid.Parse(request.PayerWalletId) : null, - request.HasPayeeWalletId ? Guid.Parse(request.PayeeWalletId) : null, + request.PayerWalletId is not null ? Guid.Parse(request.PayerWalletId) : null, + request.PayeeWalletId is not null ? Guid.Parse(request.PayeeWalletId) : null, request.Currency, decimal.Parse(request.Amount), - request.HasRemarks ? request.Remarks : null, + request.Remarks is not null ? request.Remarks : null, (Shared.Models.TransactionType)request.Type ); return transaction.ToProtoValue(); } - public override async Task CancelOrder(CancelOrderRequest request, ServerCallContext context) + public override async Task CancelOrder( + CancelOrderRequest request, + ServerCallContext context + ) { var order = await paymentService.CancelOrderAsync(Guid.Parse(request.OrderId)); return order.ToProtoValue(); } - public override async Task RefundOrder(RefundOrderRequest request, ServerCallContext context) + public override async Task RefundOrder( + RefundOrderRequest request, + ServerCallContext context + ) { - var (order, refundTransaction) = await paymentService.RefundOrderAsync(Guid.Parse(request.OrderId)); + var (order, refundTransaction) = await paymentService.RefundOrderAsync( + Guid.Parse(request.OrderId) + ); return new RefundOrderResponse { Order = order.ToProtoValue(), - RefundTransaction = refundTransaction.ToProtoValue() + RefundTransaction = refundTransaction.ToProtoValue(), }; } - public override async Task Transfer(TransferRequest request, ServerCallContext context) + public override async Task Transfer( + TransferRequest request, + ServerCallContext context + ) { var transaction = await paymentService.TransferAsync( Guid.Parse(request.PayerAccountId), @@ -79,4 +100,5 @@ public class PaymentServiceGrpc(PaymentService paymentService) : Shared.Proto.Pa ); return transaction.ToProtoValue(); } -} \ No newline at end of file +} + diff --git a/DysonNetwork.Shared/Proto/wallet.proto b/DysonNetwork.Shared/Proto/wallet.proto index 705eb8a..a5283de 100644 --- a/DysonNetwork.Shared/Proto/wallet.proto +++ b/DysonNetwork.Shared/Proto/wallet.proto @@ -188,7 +188,6 @@ service PaymentService { rpc CreateOrder(CreateOrderRequest) returns (Order); rpc CreateTransactionWithAccount(CreateTransactionWithAccountRequest) returns (Transaction); rpc CreateTransaction(CreateTransactionRequest) returns (Transaction); - rpc PayOrder(PayOrderRequest) returns (Order); rpc CancelOrder(CancelOrderRequest) returns (Order); rpc RefundOrder(RefundOrderRequest) returns (RefundOrderResponse); rpc Transfer(TransferRequest) returns (Transaction); @@ -252,28 +251,23 @@ enum TransactionType { } message CreateTransactionWithAccountRequest { - optional string payer_account_id = 1; - optional string payee_account_id = 2; + google.protobuf.StringValue payer_account_id = 1; + google.protobuf.StringValue payee_account_id = 2; string currency = 3; string amount = 4; - optional string remarks = 5; + google.protobuf.StringValue remarks = 5; TransactionType type = 6; } message CreateTransactionRequest { - optional string payer_wallet_id = 1; - optional string payee_wallet_id = 2; + google.protobuf.StringValue payer_wallet_id = 1; + google.protobuf.StringValue payee_wallet_id = 2; string currency = 3; string amount = 4; - optional string remarks = 5; + google.protobuf.StringValue remarks = 5; TransactionType type = 6; } -message PayOrderRequest { - string order_id = 1; - string payer_wallet_id = 2; -} - message CancelOrderRequest { string order_id = 1; }