🐛 Fix unable to claim fund due to db issue

This commit is contained in:
2025-11-17 01:12:00 +08:00
parent 260b3e7bc6
commit 82afdb3922
3 changed files with 16 additions and 10 deletions

View File

@@ -165,9 +165,7 @@ public class PaymentService(
db.PaymentTransactions.Add(transaction); db.PaymentTransactions.Add(transaction);
if (autoSave) if (autoSave)
{
await db.SaveChangesAsync(); await db.SaveChangesAsync();
}
if (!silent) if (!silent)
await NotifyNewTransaction(transaction, payerWallet, payeeWallet); await NotifyNewTransaction(transaction, payerWallet, payeeWallet);
@@ -614,7 +612,7 @@ public class PaymentService(
public async Task<SnWalletTransaction> ReceiveFundAsync(Guid recipientAccountId, Guid fundId) public async Task<SnWalletTransaction> ReceiveFundAsync(Guid recipientAccountId, Guid fundId)
{ {
// Use a transaction to ensure atomicity // Use a transaction to ensure atomicity
await using var transactionScope = await db.Database.BeginTransactionAsync(IsolationLevel.Serializable); await using var transactionScope = await db.Database.BeginTransactionAsync();
try try
{ {
@@ -649,9 +647,12 @@ public class PaymentService(
{ {
RecipientAccountId = recipientAccountId, RecipientAccountId = recipientAccountId,
Amount = amount, Amount = amount,
IsReceived = false IsReceived = false,
FundId = fund.Id,
}; };
fund.Recipients.Add(recipient); db.WalletFundRecipients.Add(recipient);
await db.SaveChangesAsync();
fund.RemainingAmount -= amount; fund.RemainingAmount -= amount;
} }
else if (recipient is null) else if (recipient is null)
@@ -670,6 +671,9 @@ public class PaymentService(
fund.RemainingAmount -= amount; fund.RemainingAmount -= amount;
} }
db.Update(fund);
await db.SaveChangesAsync();
if (recipient.IsReceived) if (recipient.IsReceived)
throw new InvalidOperationException("You have already received this fund"); throw new InvalidOperationException("You have already received this fund");
@@ -678,15 +682,14 @@ public class PaymentService(
throw new InvalidOperationException("Recipient wallet not found"); throw new InvalidOperationException("Recipient wallet not found");
// Create transaction to transfer funds to recipient // Create transaction to transfer funds to recipient
// This call will save changes once
var walletTransaction = await CreateTransactionAsync( var walletTransaction = await CreateTransactionAsync(
payerWalletId: null, // System transfer payerWalletId: null, // System transfer
payeeWalletId: recipientWallet.Id, payeeWalletId: recipientWallet.Id,
currency: fund.Currency, currency: fund.Currency,
amount: recipient.Amount, amount: recipient.Amount,
remarks: $"Received fund portion from {fund.CreatorAccountId}", remarks: $"Received fund portion from {fund.CreatorAccountId}",
type: Shared.Models.TransactionType.System, type: Shared.Models.TransactionType.System
silent: true,
autoSave: false
); );
// Mark as received // Mark as received
@@ -708,6 +711,7 @@ public class PaymentService(
: Shared.Models.FundStatus.PartiallyReceived; : Shared.Models.FundStatus.PartiallyReceived;
} }
db.Update(fund);
await db.SaveChangesAsync(); await db.SaveChangesAsync();
await transactionScope.CommitAsync(); await transactionScope.CommitAsync();

View File

@@ -17,7 +17,8 @@ public class WalletController(
WalletService ws, WalletService ws,
PaymentService payment, PaymentService payment,
AuthService auth, AuthService auth,
ICacheService cache ICacheService cache,
ILogger<WalletController> logger
) : ControllerBase ) : ControllerBase
{ {
[HttpPost] [HttpPost]
@@ -369,6 +370,7 @@ public class WalletController(
} }
catch (Exception err) catch (Exception err)
{ {
logger.LogError(err, "Failed to receive fund...");
return BadRequest(err.Message); return BadRequest(err.Message);
} }
} }

View File

@@ -44,7 +44,7 @@ message WalletFund {
string remaining_amount = 10; string remaining_amount = 10;
FundSplitType split_type = 4; FundSplitType split_type = 4;
FundStatus status = 5; FundStatus status = 5;
optional string message = 6; google.protobuf.StringValue message = 6;
string creator_account_id = 7; string creator_account_id = 7;
google.protobuf.Timestamp expired_at = 8; google.protobuf.Timestamp expired_at = 8;
repeated WalletFundRecipient recipients = 9; repeated WalletFundRecipient recipients = 9;