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
This commit is contained in:
Ho Ngoc Hai
2026-03-01 04:14:36 +07:00
parent f045dbf5ed
commit 0959f594bd

View File

@@ -530,16 +530,24 @@ public class BffDataController : ControllerBase
if (merchantId == null)
return Ok(Array.Empty<object>());
await using var conn = new NpgsqlConnection(ConnStr("wallet_service"));
var wallets = await conn.QueryAsync<dynamic>(
@"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<dynamic>(
@"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<object>());
}
}
[HttpGet("wallet/transactions")]
@@ -549,17 +557,25 @@ public class BffDataController : ControllerBase
if (merchantId == null)
return Ok(Array.Empty<object>());
await using var conn = new NpgsqlConnection(ConnStr("wallet_service"));
var txns = await conn.QueryAsync<dynamic>(
@"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<dynamic>(
@"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<object>());
}
}
// ═══ DEVICES ═══