From abd346bb9790c5d4dfcc05a10952a362fafd84ed Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Mon, 8 Sep 2025 13:42:15 +0800 Subject: [PATCH] :bug: Trying to fix payment award event --- DysonNetwork.Pass/Wallet/PaymentService.cs | 3 +- DysonNetwork.Shared/Stream/PaymentEvent.cs | 8 ++- .../Startup/BroadcastEventHandler.cs | 65 +++++++------------ 3 files changed, 30 insertions(+), 46 deletions(-) diff --git a/DysonNetwork.Pass/Wallet/PaymentService.cs b/DysonNetwork.Pass/Wallet/PaymentService.cs index 869c044..d9813b6 100644 --- a/DysonNetwork.Pass/Wallet/PaymentService.cs +++ b/DysonNetwork.Pass/Wallet/PaymentService.cs @@ -271,7 +271,8 @@ public class PaymentService( order.Currency, order.Amount, order.Remarks ?? $"Payment for Order #{order.Id}", - type: TransactionType.Order); + type: TransactionType.Order, + silent: true); order.TransactionId = transaction.Id; order.Transaction = transaction; diff --git a/DysonNetwork.Shared/Stream/PaymentEvent.cs b/DysonNetwork.Shared/Stream/PaymentEvent.cs index cada601..31d46b6 100644 --- a/DysonNetwork.Shared/Stream/PaymentEvent.cs +++ b/DysonNetwork.Shared/Stream/PaymentEvent.cs @@ -1,6 +1,11 @@ namespace DysonNetwork.Shared.Stream; -public class PaymentOrderEvent +public class PaymentOrderEvent : PaymentOrderEventBase +{ + public Dictionary Meta { get; set; } = null!; +} + +public class PaymentOrderEventBase { public static string Type => "payments.orders"; @@ -9,6 +14,5 @@ public class PaymentOrderEvent public Guid AccountId { get; set; } public string? AppIdentifier { get; set; } public string? ProductIdentifier { get; set; } - public Dictionary Meta { get; set; } = null!; public int Status { get; set; } } \ No newline at end of file diff --git a/DysonNetwork.Sphere/Startup/BroadcastEventHandler.cs b/DysonNetwork.Sphere/Startup/BroadcastEventHandler.cs index d12947d..2b13bb3 100644 --- a/DysonNetwork.Sphere/Startup/BroadcastEventHandler.cs +++ b/DysonNetwork.Sphere/Startup/BroadcastEventHandler.cs @@ -1,4 +1,5 @@ using System.Text.Json; +using System.Text.Json.Serialization; using DysonNetwork.Shared.Stream; using DysonNetwork.Sphere.Post; using Microsoft.EntityFrameworkCore; @@ -6,6 +7,20 @@ using NATS.Client.Core; namespace DysonNetwork.Sphere.Startup; +public class PaymentOrderAwardEvent : PaymentOrderEventBase +{ + public PaymentOrderAwardMeta Meta { get; set; } = null!; +} + +public class PaymentOrderAwardMeta +{ + [JsonPropertyName("account_id")] public Guid AccountId { get; set; } + [JsonPropertyName("post_id")] public Guid PostId { get; set; } + [JsonPropertyName("amount")] public decimal Amount { get; set; } + [JsonPropertyName("attitude")] public PostReactionAttitude Attitude { get; set; } + [JsonPropertyName("message")] public string? Message { get; set; } +} + public class BroadcastEventHandler( INatsConnection nats, ILogger logger, @@ -14,7 +29,7 @@ public class BroadcastEventHandler( { protected override async Task ExecuteAsync(CancellationToken stoppingToken) { - await foreach (var msg in nats.SubscribeAsync(PaymentOrderEvent.Type, cancellationToken: stoppingToken)) + await foreach (var msg in nats.SubscribeAsync(PaymentOrderEventBase.Type, cancellationToken: stoppingToken)) { PaymentOrderEvent? evt = null; try @@ -30,53 +45,17 @@ public class BroadcastEventHandler( { case "posts.award": { + var awardEvt = JsonSerializer.Deserialize(msg.Data); + if (awardEvt?.Meta == null) throw new ArgumentNullException(nameof(awardEvt)); + + var meta = awardEvt.Meta; + logger.LogInformation("Handling post award order: {OrderId}", evt.OrderId); - if (!evt.Meta.TryGetValue("account_id", out var accountIdObj) || - accountIdObj is not string accountIdStr || - !Guid.TryParse(accountIdStr, out var accountId)) - { - logger.LogWarning("Post award order {OrderId} missing or invalid account_id", evt.OrderId); - break; - } - - if (!evt.Meta.TryGetValue("post_id", out var postIdObj) || - postIdObj is not string postIdStr || - !Guid.TryParse(postIdStr, out var postId)) - { - logger.LogWarning("Post award order {OrderId} missing or invalid post_id", evt.OrderId); - break; - } - - if (!evt.Meta.TryGetValue("amount", out var amountObj) || - amountObj is not string amountStr || - !decimal.TryParse(amountStr, out var amount)) - { - logger.LogWarning("Post award order {OrderId} missing or invalid amount", evt.OrderId); - break; - } - - if (!evt.Meta.TryGetValue("attitude", out var attitudeObj) || - attitudeObj is not string attitudeStr || - !int.TryParse(attitudeStr, out var attitudeInt) || - !Enum.IsDefined(typeof(PostReactionAttitude), attitudeInt)) - { - logger.LogWarning("Post award order {OrderId} missing or invalid attitude", evt.OrderId); - break; - } - var attitude = (PostReactionAttitude)attitudeInt; - - string? message = null; - if (evt.Meta.TryGetValue("message", out var messageObj) && - messageObj is string messageStr) - { - message = messageStr; - } - await using var scope = serviceProvider.CreateAsyncScope(); var ps = scope.ServiceProvider.GetRequiredService(); - await ps.AwardPost(postId, accountId, amount, attitude, message); + await ps.AwardPost(meta.PostId, meta.AccountId, meta.Amount, meta.Attitude, meta.Message); logger.LogInformation("Post award for order {OrderId} handled successfully.", evt.OrderId);