From 0959f594bdccbb032221d9129b5cf97e6dda5dfc Mon Sep 17 00:00:00 2001 From: Ho Ngoc Hai Date: Sun, 1 Mar 2026 04:14:36 +0700 Subject: [PATCH] fix(bff): add try-catch to GetWallets and GetWalletTransactions - Prevent 500 errors when wallet_service tables are missing - Return empty arrays on failure for graceful Finance page display - Now 7 total BFF endpoints hardened against missing tables --- .../Controllers/BffDataController.cs | 58 ++++++++++++------- 1 file changed, 37 insertions(+), 21 deletions(-) diff --git a/apps/web-client-tpos-net/src/WebClientTpos.Server/Controllers/BffDataController.cs b/apps/web-client-tpos-net/src/WebClientTpos.Server/Controllers/BffDataController.cs index b33a99f8..e6e73bd5 100644 --- a/apps/web-client-tpos-net/src/WebClientTpos.Server/Controllers/BffDataController.cs +++ b/apps/web-client-tpos-net/src/WebClientTpos.Server/Controllers/BffDataController.cs @@ -530,16 +530,24 @@ public class BffDataController : ControllerBase if (merchantId == null) return Ok(Array.Empty()); - await using var conn = new NpgsqlConnection(ConnStr("wallet_service")); - var wallets = await conn.QueryAsync( - @"SELECT w.id, w.balance, w.currency, w.owner_id, w.created_at, - (SELECT COALESCE(SUM(amount),0) FROM wallet_transactions wt WHERE wt.wallet_id = w.id AND wt.amount > 0) as total_income, - (SELECT COALESCE(SUM(ABS(amount)),0) FROM wallet_transactions wt WHERE wt.wallet_id = w.id AND wt.amount < 0) as total_expense - FROM wallets w - WHERE w.owner_id = @MerchantId::text - ORDER BY w.created_at DESC", - new { MerchantId = merchantId }); - return Ok(wallets); + try + { + await using var conn = new NpgsqlConnection(ConnStr("wallet_service")); + var wallets = await conn.QueryAsync( + @"SELECT w.id, w.balance, w.currency, w.owner_id, w.created_at, + (SELECT COALESCE(SUM(amount),0) FROM wallet_transactions wt WHERE wt.wallet_id = w.id AND wt.amount > 0) as total_income, + (SELECT COALESCE(SUM(ABS(amount)),0) FROM wallet_transactions wt WHERE wt.wallet_id = w.id AND wt.amount < 0) as total_expense + FROM wallets w + WHERE w.owner_id = @MerchantId::text + ORDER BY w.created_at DESC", + new { MerchantId = merchantId }); + return Ok(wallets); + } + catch (Exception ex) + { + Console.Error.WriteLine($"[BFF] GetWallets error: {ex.Message}"); + return Ok(Array.Empty()); + } } [HttpGet("wallet/transactions")] @@ -549,17 +557,25 @@ public class BffDataController : ControllerBase if (merchantId == null) return Ok(Array.Empty()); - await using var conn = new NpgsqlConnection(ConnStr("wallet_service")); - var txns = await conn.QueryAsync( - @"SELECT wt.id, wt.wallet_id, wt.amount, wt.description, wt.created_at, - wi.name as item_name - FROM wallet_transactions wt - JOIN wallets w ON wt.wallet_id = w.id - LEFT JOIN wallet_items wi ON wt.reference_id = wi.id - WHERE w.owner_id = @MerchantId::text - ORDER BY wt.created_at DESC LIMIT @Limit", - new { MerchantId = merchantId, Limit = limit }); - return Ok(txns); + try + { + await using var conn = new NpgsqlConnection(ConnStr("wallet_service")); + var txns = await conn.QueryAsync( + @"SELECT wt.id, wt.wallet_id, wt.amount, wt.description, wt.created_at, + wi.name as item_name + FROM wallet_transactions wt + JOIN wallets w ON wt.wallet_id = w.id + LEFT JOIN wallet_items wi ON wt.reference_id = wi.id + WHERE w.owner_id = @MerchantId::text + ORDER BY wt.created_at DESC LIMIT @Limit", + new { MerchantId = merchantId, Limit = limit }); + return Ok(txns); + } + catch (Exception ex) + { + Console.Error.WriteLine($"[BFF] GetWalletTransactions error: {ex.Message}"); + return Ok(Array.Empty()); + } } // ═══ DEVICES ═══