feat: add new column and delete old
This commit is contained in:
@@ -19,7 +19,7 @@ class IUserRepository(ABC):
|
||||
raise NotImplementedError
|
||||
|
||||
@abstractmethod
|
||||
async def set_crypto_wallet(self, user_id: str, wallet_address: str) -> UserEntity:
|
||||
async def set_encrypted_mnemonic(self, user_id: str, encrypted_mnemonic: str) -> UserEntity:
|
||||
raise NotImplementedError
|
||||
|
||||
@abstractmethod
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
from src.application.commands.get_me import GetMeCommand
|
||||
from src.application.commands.set_phone import SetPhoneCommand
|
||||
from src.application.commands.set_crypto_wallet_start import SetCryptoWalletStartCommand
|
||||
from src.application.commands.set_crypto_wallet_complete import SetCryptoWalletCompleteCommand
|
||||
from src.application.commands.set_encrypted_mnemonic_start import SetEncryptedMnemonicStartCommand
|
||||
from src.application.commands.set_encrypted_mnemonic_complete import SetEncryptedMnemonicCompleteCommand
|
||||
from src.application.commands.update_bank_details_start import UpdateBankDetailsStartCommand
|
||||
from src.application.commands.update_bank_details_complete import UpdateBankDetailsCompleteCommand
|
||||
from src.application.commands.change_password_start import ChangePasswordStartCommand
|
||||
|
||||
@@ -5,7 +5,7 @@ from src.application.domain.exceptions import ApplicationException
|
||||
from src.infrastructure.database.decorators import transactional
|
||||
|
||||
|
||||
class SetCryptoWalletCompleteCommand:
|
||||
class SetEncryptedMnemonicCompleteCommand:
|
||||
def __init__(
|
||||
self,
|
||||
unit_of_work: IUnitOfWork,
|
||||
@@ -19,42 +19,42 @@ class SetCryptoWalletCompleteCommand:
|
||||
self._logger = logger
|
||||
|
||||
@transactional
|
||||
async def __call__(self, *, user_id: str, code: str, wallet_address: str) -> UserEntity:
|
||||
async def __call__(self, *, user_id: str, code: str, encrypted_mnemonic: str) -> UserEntity:
|
||||
code = (code or '').strip()
|
||||
|
||||
USER_PREFIX = 'crypto_wallet:user:'
|
||||
CODE_PREFIX = 'crypto_wallet:code:'
|
||||
USER_PREFIX = 'encrypted_mnemonic:user:'
|
||||
CODE_PREFIX = 'encrypted_mnemonic:code:'
|
||||
|
||||
user_key = f'{USER_PREFIX}{user_id}'
|
||||
code_key = f'{CODE_PREFIX}{code}'
|
||||
|
||||
user = await self._unit_of_work.user_repository.get_user_by_id(user_id=user_id)
|
||||
if user.crypto_wallet is not None:
|
||||
self._logger.info(f'Crypto wallet already set for user_id={user_id}')
|
||||
raise ApplicationException(409, 'Crypto wallet already set and cannot be changed')
|
||||
if user.encrypted_mnemonic is not None:
|
||||
self._logger.info(f'Encrypted mnemonic already set for user_id={user_id}')
|
||||
raise ApplicationException(409, 'Encrypted mnemonic already set and cannot be changed')
|
||||
|
||||
cached_user_id = await self._cache.get(code_key)
|
||||
if not cached_user_id:
|
||||
self._logger.info(f'Crypto wallet set failed: code not found (user_id={user_id})')
|
||||
self._logger.info(f'Encrypted mnemonic set failed: code not found (user_id={user_id})')
|
||||
raise ApplicationException(400, 'Invalid or expired code')
|
||||
|
||||
if cached_user_id != user_id:
|
||||
self._logger.info(f'Crypto wallet set failed: code-user mismatch (user_id={user_id})')
|
||||
self._logger.info(f'Encrypted mnemonic set failed: code-user mismatch (user_id={user_id})')
|
||||
raise ApplicationException(400, 'Invalid or expired code')
|
||||
|
||||
code_hash = await self._cache.get(user_key)
|
||||
if not code_hash:
|
||||
self._logger.info(f'Crypto wallet set failed: user key missing (user_id={user_id})')
|
||||
self._logger.info(f'Encrypted mnemonic set failed: user key missing (user_id={user_id})')
|
||||
raise ApplicationException(400, 'Invalid or expired code')
|
||||
|
||||
ok = await self._hash_service.verify(hashed_value=code_hash, plain_value=code)
|
||||
if not ok:
|
||||
self._logger.info(f'Crypto wallet set failed: code hash mismatch (user_id={user_id})')
|
||||
self._logger.info(f'Encrypted mnemonic set failed: code hash mismatch (user_id={user_id})')
|
||||
raise ApplicationException(400, 'Invalid or expired code')
|
||||
|
||||
user = await self._unit_of_work.user_repository.set_crypto_wallet(
|
||||
user = await self._unit_of_work.user_repository.set_encrypted_mnemonic(
|
||||
user_id=user_id,
|
||||
wallet_address=wallet_address,
|
||||
encrypted_mnemonic=encrypted_mnemonic,
|
||||
)
|
||||
await self._cache.set_user(user_id, user)
|
||||
|
||||
@@ -62,7 +62,7 @@ class SetCryptoWalletCompleteCommand:
|
||||
await self._cache.delete(code_key)
|
||||
await self._cache.delete(user_key)
|
||||
except Exception as e:
|
||||
self._logger.warning(f'Crypto wallet set cleanup failed (user_id={user_id}): {e}')
|
||||
self._logger.warning(f'Encrypted mnemonic set cleanup failed (user_id={user_id}): {e}')
|
||||
|
||||
self._logger.info(f'Crypto wallet set for user_id={user_id}')
|
||||
self._logger.info(f'Encrypted mnemonic set for user_id={user_id}')
|
||||
return user
|
||||
@@ -9,7 +9,7 @@ from src.infrastructure.context_vars import trace_id_var
|
||||
from src.infrastructure.database.decorators import transactional
|
||||
|
||||
|
||||
class SetCryptoWalletStartCommand:
|
||||
class SetEncryptedMnemonicStartCommand:
|
||||
def __init__(
|
||||
self,
|
||||
hash_service: IHashService,
|
||||
@@ -30,15 +30,15 @@ class SetCryptoWalletStartCommand:
|
||||
LOCK_TTL = 30
|
||||
MAX_ATTEMPTS = 20
|
||||
|
||||
USER_PREFIX = 'crypto_wallet:user:'
|
||||
CODE_PREFIX = 'crypto_wallet:code:'
|
||||
LOCK_PREFIX = 'crypto_wallet:lock:'
|
||||
USER_PREFIX = 'encrypted_mnemonic:user:'
|
||||
CODE_PREFIX = 'encrypted_mnemonic:code:'
|
||||
LOCK_PREFIX = 'encrypted_mnemonic:lock:'
|
||||
|
||||
user = await self._unit_of_work.user_repository.get_user_by_id(user_id=user_id)
|
||||
|
||||
if user.crypto_wallet is not None:
|
||||
self._logger.info(f'Crypto wallet already set for user_id={user_id}')
|
||||
raise ApplicationException(409, 'Crypto wallet already set and cannot be changed')
|
||||
if user.encrypted_mnemonic is not None:
|
||||
self._logger.info(f'Encrypted mnemonic already set for user_id={user_id}')
|
||||
raise ApplicationException(409, 'Encrypted mnemonic already set and cannot be changed')
|
||||
|
||||
if not user.email:
|
||||
self._logger.warning(f'User {user_id} does not have an email address')
|
||||
@@ -51,7 +51,7 @@ class SetCryptoWalletStartCommand:
|
||||
lock_key = f'{LOCK_PREFIX}{user_id}'
|
||||
locked = await self._cache.set_nx(lock_key, '1', ttl=LOCK_TTL)
|
||||
if not locked:
|
||||
self._logger.info(f'Crypto wallet set throttled by lock (user_id={user_id})')
|
||||
self._logger.info(f'Encrypted mnemonic set throttled by lock (user_id={user_id})')
|
||||
raise ApplicationException(429, 'Too many requests. Please wait.')
|
||||
|
||||
try:
|
||||
@@ -59,7 +59,7 @@ class SetCryptoWalletStartCommand:
|
||||
|
||||
existing = await self._cache.get(user_key)
|
||||
if existing:
|
||||
self._logger.info(f'Crypto wallet set denied: code already exists for user_id={user_id}')
|
||||
self._logger.info(f'Encrypted mnemonic set denied: code already exists for user_id={user_id}')
|
||||
raise ApplicationException(429, 'Code already sent. Please wait before retrying.')
|
||||
|
||||
for _ in range(MAX_ATTEMPTS):
|
||||
@@ -75,7 +75,7 @@ class SetCryptoWalletStartCommand:
|
||||
saved = await self._cache.set(user_key, code_hash, ttl=TTL)
|
||||
if not saved:
|
||||
await self._cache.delete(code_key)
|
||||
self._logger.error(f'Crypto wallet set failed: cannot save code hash for user_id={user_id}')
|
||||
self._logger.error(f'Encrypted mnemonic set failed: cannot save code hash for user_id={user_id}')
|
||||
raise ApplicationException(503, 'Temporary error. Please try again.')
|
||||
|
||||
message_id = str(ULID())
|
||||
@@ -95,12 +95,12 @@ class SetCryptoWalletStartCommand:
|
||||
}
|
||||
|
||||
message = {
|
||||
'event': 'crypto_wallet_set',
|
||||
'event': 'encrypted_mnemonic_set',
|
||||
'payload': payload,
|
||||
'metadata': metadata,
|
||||
}
|
||||
|
||||
self._logger.info(f'Crypto wallet set code created for user_id={user_id}')
|
||||
self._logger.info(f'Encrypted mnemonic set code created for user_id={user_id}')
|
||||
|
||||
try:
|
||||
await self._messanger.publish_to_queue(
|
||||
@@ -118,12 +118,12 @@ class SetCryptoWalletStartCommand:
|
||||
except Exception as rollback_err:
|
||||
self._logger.error(f'Publish failed and rollback cache failed for user_id={user_id}: {str(rollback_err)}')
|
||||
|
||||
self._logger.error(f'Failed to publish crypto wallet set email for user_id={user_id}: {str(exception)}')
|
||||
self._logger.error(f'Failed to publish encrypted mnemonic set email for user_id={user_id}: {str(exception)}')
|
||||
raise ApplicationException(503, 'Temporary error. Please try again.')
|
||||
|
||||
return True
|
||||
|
||||
self._logger.error(f'Crypto wallet set failed: code space exhausted for user_id={user_id}')
|
||||
self._logger.error(f'Encrypted mnemonic set failed: code space exhausted for user_id={user_id}')
|
||||
raise ApplicationException(503, 'Temporary error. Please try again.')
|
||||
|
||||
finally:
|
||||
@@ -14,7 +14,7 @@ class UserEntity:
|
||||
last_name: str | None = None
|
||||
birth_date: date | None = None
|
||||
|
||||
crypto_wallet: str | None = None
|
||||
encrypted_mnemonic: str | None = None
|
||||
phone: str | None = None
|
||||
|
||||
passport_data: str | None = None
|
||||
|
||||
Reference in New Issue
Block a user