-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🐛 when updating comp_task table always give actual wallet info (#4955)
- Loading branch information
1 parent
809a4d6
commit e04a211
Showing
3 changed files
with
79 additions
and
53 deletions.
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
services/web/server/src/simcore_service_webserver/director_v2/_api_utils.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
from aiohttp import web | ||
from models_library.projects import ProjectID | ||
from models_library.users import UserID | ||
from models_library.wallets import ZERO_CREDITS, WalletID, WalletInfo | ||
from pydantic import parse_obj_as | ||
|
||
from ..application_settings import get_settings | ||
from ..products.api import Product | ||
from ..projects import api as projects_api | ||
from ..users import preferences_api as user_preferences_api | ||
from ..users.exceptions import UserDefaultWalletNotFoundError | ||
from ..wallets import api as wallets_api | ||
from ..wallets.errors import WalletNotEnoughCreditsError | ||
|
||
|
||
async def get_wallet_info( | ||
app: web.Application, | ||
*, | ||
product: Product, | ||
user_id: UserID, | ||
project_id: ProjectID, | ||
product_name: str, | ||
) -> WalletInfo | None: | ||
app_settings = get_settings(app) | ||
if not ( | ||
product.is_payment_enabled and app_settings.WEBSERVER_CREDIT_COMPUTATION_ENABLED | ||
): | ||
return None | ||
project_wallet = await projects_api.get_project_wallet(app, project_id=project_id) | ||
if project_wallet is None: | ||
user_default_wallet_preference = await user_preferences_api.get_frontend_user_preference( | ||
app, | ||
user_id=user_id, | ||
product_name=product_name, | ||
preference_class=user_preferences_api.PreferredWalletIdFrontendUserPreference, | ||
) | ||
if user_default_wallet_preference is None: | ||
raise UserDefaultWalletNotFoundError(uid=user_id) | ||
project_wallet_id = parse_obj_as(WalletID, user_default_wallet_preference.value) | ||
await projects_api.connect_wallet_to_project( | ||
app, | ||
product_name=product_name, | ||
project_id=project_id, | ||
user_id=user_id, | ||
wallet_id=project_wallet_id, | ||
) | ||
else: | ||
project_wallet_id = project_wallet.wallet_id | ||
|
||
# Check whether user has access to the wallet | ||
wallet = await wallets_api.get_wallet_with_available_credits_by_user_and_wallet( | ||
app, | ||
user_id=user_id, | ||
wallet_id=project_wallet_id, | ||
product_name=product_name, | ||
) | ||
if wallet.available_credits <= ZERO_CREDITS: | ||
raise WalletNotEnoughCreditsError( | ||
reason=f"Wallet {wallet.wallet_id} credit balance {wallet.available_credits}" | ||
) | ||
return WalletInfo(wallet_id=project_wallet_id, wallet_name=wallet.name) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters