diff --git a/packages/postgres-database/src/simcore_postgres_database/utils_users.py b/packages/postgres-database/src/simcore_postgres_database/utils_users.py index 9026cdd27b4..082cb7c2952 100644 --- a/packages/postgres-database/src/simcore_postgres_database/utils_users.py +++ b/packages/postgres-database/src/simcore_postgres_database/utils_users.py @@ -134,8 +134,8 @@ async def join_and_update_from_pre_registration_details( ) @staticmethod - async def get_billing_details(conn: SAConnection, user_id: int) -> RowProxy | None: - result = await conn.execute( + def get_billing_details_query(user_id: int): + return ( sa.select( users.c.first_name, users.c.last_name, @@ -155,6 +155,12 @@ async def get_billing_details(conn: SAConnection, user_id: int) -> RowProxy | No ) .where(users.c.id == user_id) ) + + @staticmethod + async def get_billing_details(conn: SAConnection, user_id: int) -> RowProxy | None: + result = await conn.execute( + UsersRepo.get_billing_details_query(user_id=user_id) + ) value: RowProxy | None = await result.fetchone() return value diff --git a/services/web/server/src/simcore_service_webserver/users/_models.py b/services/web/server/src/simcore_service_webserver/users/_models.py index e76adf6ca66..f92567f8e40 100644 --- a/services/web/server/src/simcore_service_webserver/users/_models.py +++ b/services/web/server/src/simcore_service_webserver/users/_models.py @@ -1,4 +1,4 @@ -from typing import Annotated, Any, Self +from typing import Annotated, Any, NamedTuple, Self from models_library.emails import LowerCaseEmailStr from pydantic import BaseModel, ConfigDict, Field diff --git a/services/web/server/src/simcore_service_webserver/users/_schemas.py b/services/web/server/src/simcore_service_webserver/users/_schemas.py index 8db89fe110b..df59ee58a78 100644 --- a/services/web/server/src/simcore_service_webserver/users/_schemas.py +++ b/services/web/server/src/simcore_service_webserver/users/_schemas.py @@ -20,12 +20,10 @@ field_validator, model_validator, ) -from servicelib.aiohttp import status from servicelib.request_keys import RQT_USERID_KEY from simcore_postgres_database.models.users import UserStatus from .._constants import RQ_PRODUCT_KEY -from ._schemas import PreUserProfile class UsersRequestContext(BaseModel): diff --git a/services/web/server/src/simcore_service_webserver/users/_users_repository.py b/services/web/server/src/simcore_service_webserver/users/_users_repository.py index 3fb1944a6f8..349a2bf236e 100644 --- a/services/web/server/src/simcore_service_webserver/users/_users_repository.py +++ b/services/web/server/src/simcore_service_webserver/users/_users_repository.py @@ -5,7 +5,7 @@ from models_library.users import GroupID, UserBillingDetails, UserID from simcore_postgres_database.models.groups import groups, user_to_groups from simcore_postgres_database.models.products import products -from simcore_postgres_database.models.users import UserStatus, user_to_groups, users +from simcore_postgres_database.models.users import UserStatus, users from simcore_postgres_database.models.users_details import ( users_pre_registration_details, ) @@ -243,7 +243,9 @@ async def get_user_billing_details( BillingDetailsNotFoundError """ async with pass_or_acquire_connection(engine, connection) as conn: - user_billing_details = await UsersRepo.get_billing_details(conn, user_id) - if not user_billing_details: + query = UsersRepo.get_billing_details_query(user_id=user_id) + result = await conn.stream(query) + row = await result.fetchone() + if not row: raise BillingDetailsNotFoundError(user_id=user_id) - return UserBillingDetails.model_validate(user_billing_details) + return UserBillingDetails.model_validate(row) diff --git a/services/web/server/src/simcore_service_webserver/users/_users_rest.py b/services/web/server/src/simcore_service_webserver/users/_users_rest.py index 1786f250ac3..fb94275c2c7 100644 --- a/services/web/server/src/simcore_service_webserver/users/_users_rest.py +++ b/services/web/server/src/simcore_service_webserver/users/_users_rest.py @@ -66,7 +66,7 @@ async def wrapper(request: web.Request) -> web.StreamResponse: async def get_my_profile(request: web.Request) -> web.Response: req_ctx = UsersRequestContext.model_validate(request) profile: MyProfileGet = await api.get_user_profile( - request.app, req_ctx.user_id, req_ctx.product_name + request.app, user_id=req_ctx.user_id, product_name=req_ctx.product_name ) return envelope_json_response(profile) diff --git a/services/web/server/src/simcore_service_webserver/users/_users_service.py b/services/web/server/src/simcore_service_webserver/users/_users_service.py index 7287de756dd..35cf2168891 100644 --- a/services/web/server/src/simcore_service_webserver/users/_users_service.py +++ b/services/web/server/src/simcore_service_webserver/users/_users_service.py @@ -4,10 +4,10 @@ from aiohttp import web from models_library.emails import LowerCaseEmailStr from models_library.payments import UserInvoiceAddress +from models_library.products import ProductName from models_library.users import UserBillingDetails, UserID from pydantic import TypeAdapter from simcore_postgres_database.models.users import UserStatus -from simcore_service_webserver.products._api import ProductName from ..db.plugin import get_asyncpg_engine from . import _schemas, _users_repository diff --git a/services/web/server/src/simcore_service_webserver/users/api.py b/services/web/server/src/simcore_service_webserver/users/api.py index 36e5b55f6f3..3276d06a402 100644 --- a/services/web/server/src/simcore_service_webserver/users/api.py +++ b/services/web/server/src/simcore_service_webserver/users/api.py @@ -11,7 +11,6 @@ import simcore_postgres_database.errors as db_errors import sqlalchemy as sa from aiohttp import web -from aiopg.sa.result import RowProxy from models_library.api_schemas_webserver.users import ( MyProfileGet, MyProfilePatch, @@ -31,6 +30,7 @@ transaction_context, ) from simcore_postgres_database.utils_users import generate_alternative_username +from sqlalchemy.engine.row import Row from ..db.plugin import get_asyncpg_engine from ..login.storage import AsyncpgStorage, get_plugin_storage @@ -62,7 +62,7 @@ def _convert_groups_db_to_schema( - db_row: RowProxy, *, prefix: str | None = "", **kwargs + db_row: Row, *, prefix: str | None = "", **kwargs ) -> dict: # NOTE: Deprecated. has to be replaced with converted_dict = { @@ -346,10 +346,11 @@ async def get_user(app: web.Application, user_id: UserID) -> dict[str, Any]: """ :raises UserNotFoundError: """ - row = await _users_repository.get_user_or_raise( + row: Row = await _users_repository.get_user_or_raise( engine=get_asyncpg_engine(app), user_id=user_id ) - return row._asdict() + user: dict[str, Any] = row._asdict() + return user async def get_user_id_from_gid(app: web.Application, primary_gid: int) -> UserID: