88from models_library .wallets import WalletID
99from pydantic import BaseModel , ConfigDict , PositiveInt
1010from simcore_postgres_database .utils_payments_autorecharge import AutoRechargeStmts
11+ from simcore_postgres_database .utils_repos import (
12+ pass_or_acquire_connection ,
13+ transaction_context ,
14+ )
15+ from sqlalchemy .ext .asyncio import AsyncConnection
1116
12- from ..db .plugin import get_database_engine_legacy
17+ from ..db .plugin import get_asyncpg_engine
1318from .errors import InvalidPaymentMethodError
1419
1520_logger = logging .getLogger (__name__ )
@@ -29,18 +34,20 @@ class PaymentsAutorechargeGetDB(BaseModel):
2934
3035async def get_wallet_autorecharge (
3136 app : web .Application ,
37+ connection : AsyncConnection | None = None ,
3238 * ,
3339 wallet_id : WalletID ,
3440) -> PaymentsAutorechargeGetDB | None :
35- async with get_database_engine_legacy ( app ). acquire ( ) as conn :
41+ async with pass_or_acquire_connection ( get_asyncpg_engine ( app ), connection ) as conn :
3642 stmt = AutoRechargeStmts .get_wallet_autorecharge (wallet_id )
3743 result = await conn .execute (stmt )
38- row = await result .first ()
44+ row = result .one_or_none ()
3945 return PaymentsAutorechargeGetDB .model_validate (row ) if row else None
4046
4147
4248async def replace_wallet_autorecharge (
4349 app : web .Application ,
50+ connection : AsyncConnection | None = None ,
4451 * ,
4552 user_id : UserID ,
4653 wallet_id : WalletID ,
@@ -51,7 +58,7 @@ async def replace_wallet_autorecharge(
5158 InvalidPaymentMethodError: if `new` includes some invalid 'primary_payment_method_id'
5259
5360 """
54- async with get_database_engine_legacy ( app ). acquire ( ) as conn :
61+ async with transaction_context ( get_asyncpg_engine ( app ), connection ) as conn :
5562 stmt = AutoRechargeStmts .is_valid_payment_method (
5663 user_id = user_id ,
5764 wallet_id = new .wallet_id ,
@@ -71,6 +78,5 @@ async def replace_wallet_autorecharge(
7178 monthly_limit_in_usd = new .monthly_limit_in_usd ,
7279 )
7380 result = await conn .execute (stmt )
74- row = await result .first ()
75- assert row # nosec
81+ row = result .one ()
7682 return PaymentsAutorechargeGetDB .model_validate (row )
0 commit comments