init
This commit is contained in:
79
src/application/commands/purchase_request_commands.py
Normal file
79
src/application/commands/purchase_request_commands.py
Normal file
@@ -0,0 +1,79 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from decimal import Decimal
|
||||
|
||||
from src.application.abstractions import IUnitOfWork
|
||||
from src.application.commands.legal_entity_guard import require_legal_entity
|
||||
from src.application.contracts import ILogger
|
||||
from src.application.domain.entities.purchase_request import PurchaseRequestEntity
|
||||
from src.application.domain.exceptions import NotFoundException
|
||||
from src.infrastructure.database.decorators import transactional
|
||||
|
||||
|
||||
class CreatePurchaseRequestCommand:
|
||||
def __init__(self, unit_of_work: IUnitOfWork, logger: ILogger):
|
||||
self._unit_of_work = unit_of_work
|
||||
self._logger = logger
|
||||
|
||||
@transactional
|
||||
async def __call__(
|
||||
self,
|
||||
user_id: str,
|
||||
*,
|
||||
usdt_amount: Decimal,
|
||||
comment: str | None = None,
|
||||
target_wallet_chain: str | None = None,
|
||||
target_wallet_address: str | None = None,
|
||||
) -> PurchaseRequestEntity:
|
||||
legal_entity = await require_legal_entity(user_id, self._unit_of_work)
|
||||
item = await self._unit_of_work.purchase_request_repository.create(
|
||||
organization_id=legal_entity.id,
|
||||
usdt_amount=usdt_amount,
|
||||
comment=comment,
|
||||
target_wallet_chain=target_wallet_chain,
|
||||
target_wallet_address=target_wallet_address,
|
||||
)
|
||||
self._logger.info(f'Purchase request created id={item.id} organization_id={legal_entity.id}')
|
||||
return item
|
||||
|
||||
|
||||
class ListPurchaseRequestsCommand:
|
||||
def __init__(self, unit_of_work: IUnitOfWork, logger: ILogger):
|
||||
self._unit_of_work = unit_of_work
|
||||
self._logger = logger
|
||||
|
||||
@transactional
|
||||
async def __call__(
|
||||
self,
|
||||
user_id: str,
|
||||
*,
|
||||
status: str | None = None,
|
||||
limit: int = 50,
|
||||
offset: int = 0,
|
||||
) -> tuple[list[PurchaseRequestEntity], int]:
|
||||
legal_entity = await require_legal_entity(user_id, self._unit_of_work)
|
||||
items = await self._unit_of_work.purchase_request_repository.list_by_organization(
|
||||
organization_id=legal_entity.id,
|
||||
status=status,
|
||||
limit=limit,
|
||||
offset=offset,
|
||||
)
|
||||
total = await self._unit_of_work.purchase_request_repository.count_by_organization(
|
||||
organization_id=legal_entity.id,
|
||||
status=status,
|
||||
)
|
||||
return items, total
|
||||
|
||||
|
||||
class GetPurchaseRequestCommand:
|
||||
def __init__(self, unit_of_work: IUnitOfWork, logger: ILogger):
|
||||
self._unit_of_work = unit_of_work
|
||||
self._logger = logger
|
||||
|
||||
@transactional
|
||||
async def __call__(self, user_id: str, request_id: str) -> PurchaseRequestEntity:
|
||||
legal_entity = await require_legal_entity(user_id, self._unit_of_work)
|
||||
item = await self._unit_of_work.purchase_request_repository.get_by_id(request_id)
|
||||
if item.organization_id != legal_entity.id:
|
||||
raise NotFoundException(message='Purchase request not found')
|
||||
return item
|
||||
Reference in New Issue
Block a user