Skip to content

Commit

Permalink
✨🗃️ adding product_name to the wallets api (ITISFoundation#4780)
Browse files Browse the repository at this point in the history
  • Loading branch information
matusdrobuliak66 authored Sep 22, 2023
1 parent 1d1c5d6 commit 21a5223
Show file tree
Hide file tree
Showing 17 changed files with 175 additions and 39 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"""adding product_name to wallets

Revision ID: 61fa093c21bb
Revises: ae72826e75fc
Create Date: 2023-09-20 14:42:10.661569+00:00

"""
import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision = "61fa093c21bb"
down_revision = "ae72826e75fc"
branch_labels = None
depends_on = None


def upgrade():
op.add_column("wallets", sa.Column("product_name", sa.String(), nullable=True))
op.execute(
sa.DDL("UPDATE wallets SET product_name = 'osparc' WHERE product_name IS NULL")
)
op.alter_column(
"wallets",
"product_name",
existing_type=sa.String(),
nullable=False,
)


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_column("wallets", "product_name")
# ### end Alembic commands ###
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class WalletStatus(str, enum.Enum):
),
column_created_datetime(timezone=True),
column_modified_datetime(timezone=True),
sa.Column("product_name", sa.String, nullable=False, doc="Product name"),
)

# ------------------------ TRIGGERS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ async def start_computation(request: web.Request) -> web.Response:
if project_wallet and app_settings.WEBSERVER_DEV_FEATURES_ENABLED:
# Check whether user has access to the wallet
await wallets_api.get_wallet_by_user(
request.app, req_ctx.user_id, project_wallet.wallet_id
request.app, req_ctx.user_id, project_wallet.wallet_id, req_ctx.product_name
)
wallet_info = WalletInfo(
wallet_id=project_wallet.wallet_id, wallet_name=project_wallet.name
Expand Down
19 changes: 14 additions & 5 deletions services/web/server/src/simcore_service_webserver/payments/_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
PaymentTransaction,
WalletPaymentCreated,
)
from models_library.products import ProductName
from models_library.users import UserID
from models_library.wallets import WalletID
from simcore_postgres_database.models.payments_transactions import (
Expand All @@ -27,10 +28,13 @@


async def check_wallet_permissions(
app: web.Application, user_id: UserID, wallet_id: WalletID
app: web.Application,
user_id: UserID,
wallet_id: WalletID,
product_name: ProductName,
):
permissions = await get_wallet_with_permissions_by_user(
app, user_id=user_id, wallet_id=wallet_id
app, user_id=user_id, wallet_id=wallet_id, product_name=product_name
)
if not permissions.read or not permissions.write:
raise WalletAccessForbiddenError(
Expand Down Expand Up @@ -83,7 +87,9 @@ async def create_payment_to_wallet(
user = await get_user_name_and_email(app, user_id=user_id)

# check permissions
await check_wallet_permissions(app, user_id=user_id, wallet_id=wallet_id)
await check_wallet_permissions(
app, user_id=user_id, wallet_id=wallet_id, product_name=product_name
)

# hold timestamp
initiated_at = arrow.utcnow().datetime
Expand Down Expand Up @@ -170,7 +176,7 @@ async def complete_payment(
if completion_state == PaymentTransactionState.SUCCESS:
# notifying RUT
user_wallet = await get_wallet_by_user(
app, transaction.user_id, transaction.wallet_id
app, transaction.user_id, transaction.wallet_id, transaction.product_name
)
await add_credits_to_wallet(
app=app,
Expand All @@ -193,8 +199,11 @@ async def cancel_payment_to_wallet(
payment_id: PaymentID,
user_id: UserID,
wallet_id: WalletID,
product_name: ProductName,
) -> PaymentTransaction:
await check_wallet_permissions(app, user_id=user_id, wallet_id=wallet_id)
await check_wallet_permissions(
app, user_id=user_id, wallet_id=wallet_id, product_name=product_name
)

return await complete_payment(
app,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
PaymentMethodInit,
PaymentMethodTransaction,
)
from models_library.products import ProductName
from models_library.users import UserID
from models_library.wallets import WalletID
from simcore_postgres_database.models.payments_methods import InitPromptAckFlowState
Expand Down Expand Up @@ -68,7 +69,11 @@ def _to_api_model(


async def init_creation_of_wallet_payment_method(
app: web.Application, *, user_id: UserID, wallet_id: WalletID
app: web.Application,
*,
user_id: UserID,
wallet_id: WalletID,
product_name: ProductName,
) -> PaymentMethodInit:
"""

Expand All @@ -78,7 +83,9 @@ async def init_creation_of_wallet_payment_method(
"""

# check permissions
await check_wallet_permissions(app, user_id=user_id, wallet_id=wallet_id)
await check_wallet_permissions(
app, user_id=user_id, wallet_id=wallet_id, product_name=product_name
)

# hold timestamp
initiated_at = arrow.utcnow().datetime
Expand Down Expand Up @@ -149,9 +156,12 @@ async def cancel_creation_of_wallet_payment_method(
user_id: UserID,
wallet_id: WalletID,
payment_method_id: PaymentMethodID,
product_name: ProductName,
):
"""Acks as CANCELED"""
await check_wallet_permissions(app, user_id=user_id, wallet_id=wallet_id)
await check_wallet_permissions(
app, user_id=user_id, wallet_id=wallet_id, product_name=product_name
)

await _complete_create_of_wallet_payment_method(
app,
Expand All @@ -176,10 +186,16 @@ async def cancel_creation_of_wallet_payment_method(


async def list_wallet_payment_methods(
app: web.Application, *, user_id: UserID, wallet_id: WalletID
app: web.Application,
*,
user_id: UserID,
wallet_id: WalletID,
product_name: ProductName,
) -> list[PaymentMethodGet]:
# check permissions
await check_wallet_permissions(app, user_id=user_id, wallet_id=wallet_id)
await check_wallet_permissions(
app, user_id=user_id, wallet_id=wallet_id, product_name=product_name
)

# get acked
acked = await list_successful_payment_methods(
Expand Down Expand Up @@ -218,9 +234,12 @@ async def get_wallet_payment_method(
user_id: UserID,
wallet_id: WalletID,
payment_method_id: PaymentMethodID,
product_name: ProductName,
) -> PaymentMethodGet:
# check permissions
await check_wallet_permissions(app, user_id=user_id, wallet_id=wallet_id)
await check_wallet_permissions(
app, user_id=user_id, wallet_id=wallet_id, product_name=product_name
)

acked = await get_successful_payment_method(
app, user_id=user_id, wallet_id=wallet_id, payment_method_id=payment_method_id
Expand All @@ -246,9 +265,12 @@ async def delete_wallet_payment_method(
user_id: UserID,
wallet_id: WalletID,
payment_method_id: PaymentMethodID,
product_name: ProductName,
):
# check permissions
await check_wallet_permissions(app, user_id=user_id, wallet_id=wallet_id)
await check_wallet_permissions(
app, user_id=user_id, wallet_id=wallet_id, product_name=product_name
)
assert payment_method_id # nosec

acked = await get_successful_payment_method(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,10 @@ async def connect_wallet_to_project(request: web.Request):
)
# ensure the wallet can be used by the user
wallet: WalletGet = await wallet_api.get_wallet_by_user(
request.app, user_id=req_ctx.user_id, wallet_id=path_params.wallet_id
request.app,
user_id=req_ctx.user_id,
wallet_id=path_params.wallet_id,
product_name=req_ctx.product_name,
)

await db.connect_wallet_to_project(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ async def _start_dynamic_service(
if project_wallet and app_settings.WEBSERVER_DEV_FEATURES_ENABLED:
# Check whether user has access to the wallet
await wallets_api.get_wallet_by_user(
request.app, user_id, project_wallet.wallet_id
request.app, user_id, project_wallet.wallet_id, product_name
)
wallet_info = WalletInfo(
wallet_id=project_wallet.wallet_id, wallet_name=project_wallet.name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ async def _on_user_disconnected(
product_name: ProductName,
) -> None:
# Get all user wallets and unsubscribe
user_wallet = await wallets_api.list_wallets_for_user(app, user_id=user_id)
user_wallet = await wallets_api.list_wallets_for_user(
app, user_id=user_id, product_name=product_name
)
disconnect_tasks = [
wallet_osparc_credits.unsubscribe(app, wallet.wallet_id)
for wallet in user_wallet
Expand All @@ -40,7 +42,9 @@ async def _on_user_connected(
user_id: int, app: web.Application, product_name: str
) -> None:
# Get all user wallets and subscribe
user_wallet = await wallets_api.list_wallets_for_user(app, user_id=user_id)
user_wallet = await wallets_api.list_wallets_for_user(
app, user_id=user_id, product_name=product_name
)
connect_tasks = [
wallet_osparc_credits.subscribe(app, wallet.wallet_id) for wallet in user_wallet
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ async def list_usage_services(
else:
wallet: WalletGetPermissions = (
await wallet_api.get_wallet_with_permissions_by_user(
app=app, user_id=user_id, wallet_id=wallet_id
app=app, user_id=user_id, wallet_id=wallet_id, product_name=product_name
)
)
access_all_wallet_usage = wallet.write is True
Expand Down
22 changes: 15 additions & 7 deletions services/web/server/src/simcore_service_webserver/wallets/_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ async def create_wallet(
wallet_name: str,
description: str | None,
thumbnail: str | None,
product_name: ProductName,
) -> WalletGet:
user: dict = await users_api.get_user(app, user_id)
wallet_db: WalletDB = await db.create_wallet(
Expand All @@ -36,18 +37,19 @@ async def create_wallet(
wallet_name=wallet_name,
description=description,
thumbnail=thumbnail,
product_name=product_name,
)
wallet_api: WalletGet = parse_obj_as(WalletGet, wallet_db)
return wallet_api


async def list_wallets_with_available_credits_for_user(
app: web.Application,
product_name: ProductName,
user_id: UserID,
product_name: ProductName,
) -> list[WalletGetWithAvailableCredits]:
user_wallets: list[UserWalletDB] = await db.list_wallets_for_user(
app=app, user_id=user_id
app=app, user_id=user_id, product_name=product_name
)

# Now we return the user wallets with available credits
Expand Down Expand Up @@ -78,9 +80,10 @@ async def list_wallets_with_available_credits_for_user(
async def list_wallets_for_user(
app: web.Application,
user_id: UserID,
product_name: ProductName,
) -> list[WalletGet]:
user_wallets: list[UserWalletDB] = await db.list_wallets_for_user(
app=app, user_id=user_id
app=app, user_id=user_id, product_name=product_name
)
wallets_api = parse_obj_as(list[WalletGet], user_wallets)

Expand All @@ -95,9 +98,10 @@ async def update_wallet(
description: str | None,
thumbnail: str | None,
status: WalletStatus,
product_name: ProductName,
) -> WalletGet:
wallet: UserWalletDB = await db.get_wallet_for_user(
app=app, user_id=user_id, wallet_id=wallet_id
app=app, user_id=user_id, wallet_id=wallet_id, product_name=product_name
)
if wallet.write is False:
raise WalletAccessForbiddenError(
Expand All @@ -111,6 +115,7 @@ async def update_wallet(
description=description,
thumbnail=thumbnail,
status=status,
product_name=product_name,
)

wallet_api: WalletGet = parse_obj_as(WalletGet, wallet_db)
Expand All @@ -121,9 +126,10 @@ async def delete_wallet(
app: web.Application,
user_id: UserID,
wallet_id: WalletID,
product_name: ProductName,
) -> None:
wallet: UserWalletDB = await db.get_wallet_for_user(
app=app, user_id=user_id, wallet_id=wallet_id
app=app, user_id=user_id, wallet_id=wallet_id, product_name=product_name
)
if wallet.delete is False:
raise WalletAccessForbiddenError(
Expand All @@ -137,9 +143,10 @@ async def get_wallet_by_user(
app: web.Application,
user_id: UserID,
wallet_id: WalletID,
product_name: ProductName,
) -> WalletGet:
wallet: UserWalletDB = await db.get_wallet_for_user(
app=app, user_id=user_id, wallet_id=wallet_id
app=app, user_id=user_id, wallet_id=wallet_id, product_name=product_name
)
if wallet.read is False:
raise WalletAccessForbiddenError(
Expand All @@ -163,9 +170,10 @@ async def get_wallet_with_permissions_by_user(
app: web.Application,
user_id: UserID,
wallet_id: WalletID,
product_name: ProductName,
) -> WalletGetPermissions:
wallet: UserWalletDB = await db.get_wallet_for_user(
app=app, user_id=user_id, wallet_id=wallet_id
app=app, user_id=user_id, wallet_id=wallet_id, product_name=product_name
)

permissions: WalletGetPermissions = parse_obj_as(WalletGetPermissions, wallet)
Expand Down
Loading

0 comments on commit 21a5223

Please sign in to comment.