From 1c32bdcb3f49503fe22b3de6cb344a19d6629657 Mon Sep 17 00:00:00 2001 From: Noloquideus Date: Mon, 11 May 2026 15:33:08 +0300 Subject: [PATCH] feat: update logger logic --- .../commands/create_crypto_transfer_completed_command.py | 8 ++++---- src/application/commands/create_order_command.py | 2 +- src/application/commands/create_payment_command.py | 5 ++++- src/application/contracts/i_itpay_service.py | 2 +- src/infrastructure/itpay/client.py | 3 ++- src/presentation/dependencies/commands.py | 3 ++- src/presentation/messaging/crypto_transfer.py | 5 +++-- src/presentation/routing/order.py | 6 +++++- 8 files changed, 22 insertions(+), 12 deletions(-) diff --git a/src/application/commands/create_crypto_transfer_completed_command.py b/src/application/commands/create_crypto_transfer_completed_command.py index 8682fe1..31b37b3 100644 --- a/src/application/commands/create_crypto_transfer_completed_command.py +++ b/src/application/commands/create_crypto_transfer_completed_command.py @@ -1,16 +1,16 @@ from __future__ import annotations from decimal import Decimal -from ulid import ULID from src.application.abstractions import IUnitOfWork -from src.application.contracts import IReceipt +from src.application.contracts import ILogger,IReceipt from src.application.domain.exceptions import ApplicationException from src.infrastructure.database.decorators import transactional class CreateCryptoTransferCompletedCommand: - def __init__(self, *, unit_of_work: IUnitOfWork, receipt: IReceipt): + def __init__(self, *, unit_of_work: IUnitOfWork, receipt: IReceipt, logger: ILogger): self._unit_of_work = unit_of_work self._receipt = receipt + self._logger = logger @transactional @@ -72,7 +72,7 @@ class CreateCryptoTransferCompletedCommand: customer_info=customer_info, customer_inn=customer_inn, customer_birthday=customer_birthday, - request_id=str(ULID()), + request_id=self._logger.get_trace_id(), ) receipt_model = receipt_response.get('Model') if not isinstance(receipt_model, dict): diff --git a/src/application/commands/create_order_command.py b/src/application/commands/create_order_command.py index 6a0d4c6..4b40ad9 100644 --- a/src/application/commands/create_order_command.py +++ b/src/application/commands/create_order_command.py @@ -66,7 +66,7 @@ class CreateOrderCommand: ) saved = await self._unit_of_work.order_repository.create(order) - with_itpay = await self._itpay_service.create_payment(saved) + with_itpay = await self._itpay_service.create_payment(saved,self._logger.get_trace_id()) if with_itpay.status in ( OrderStatus.CANCELLED, OrderStatus.REJECTED, diff --git a/src/application/commands/create_payment_command.py b/src/application/commands/create_payment_command.py index 276ab9e..ef24042 100644 --- a/src/application/commands/create_payment_command.py +++ b/src/application/commands/create_payment_command.py @@ -21,6 +21,8 @@ class CreatePaymentCommand: metadata = payment.metadata or {} order_id = str(metadata.get('order_id') or '') user_id = str(metadata.get('user_id') or '') + trace_id = str(metadata.get('trace_id') or self._logger.get_trace_id()) + self._logger.set_trace_id(trace_id) if not order_id: raise ApplicationException(status_code=400, message='Itpay webhook metadata missing order_id') if not user_id: @@ -40,7 +42,7 @@ class CreatePaymentCommand: message: dict[str,str] = { 'order_id': order_id, 'user_id': user_id, - 'trace_id': self._logger.get_trace_id(), + 'trace_id': trace_id, 'message_id': message_id, } await self._queue_messanger.publish_to_queue( @@ -48,4 +50,5 @@ class CreatePaymentCommand: message=message, message_id=message_id, correlation_id=message['trace_id'], + headers={'trace_id': trace_id}, ) diff --git a/src/application/contracts/i_itpay_service.py b/src/application/contracts/i_itpay_service.py index fbc5c71..7dc01c4 100644 --- a/src/application/contracts/i_itpay_service.py +++ b/src/application/contracts/i_itpay_service.py @@ -7,5 +7,5 @@ class IItPayService(ABC): @abstractmethod - async def create_payment(self,order: OrderEntity) -> OrderEntity: + async def create_payment(self,order: OrderEntity,trace_id: str) -> OrderEntity: pass diff --git a/src/infrastructure/itpay/client.py b/src/infrastructure/itpay/client.py index 12b4747..84e5fd7 100644 --- a/src/infrastructure/itpay/client.py +++ b/src/infrastructure/itpay/client.py @@ -26,11 +26,12 @@ class ItPayClient(IItPayService): self._api_secret = api_secret self._timeout = ClientTimeout(total=timeout_seconds) - async def create_payment(self, order: OrderEntity) -> OrderEntity: + async def create_payment(self, order: OrderEntity, trace_id: str) -> OrderEntity: total = order.total_price if order.total_price is not None else Decimal('0') amount = total if isinstance(total, Decimal) else Decimal(str(total)) amount_str = str(amount.quantize(Decimal('0.01'))) metadata: dict[str,Any] = { + 'trace_id': trace_id, 'order_id': order.id, 'user_id': order.user_id, 'usdt_amount': str(order.usdt_amount) if order.usdt_amount is not None else None, diff --git a/src/presentation/dependencies/commands.py b/src/presentation/dependencies/commands.py index 2022151..18cba5d 100644 --- a/src/presentation/dependencies/commands.py +++ b/src/presentation/dependencies/commands.py @@ -56,7 +56,8 @@ def get_cloud_kassir_receipt() -> IReceipt: def get_crypto_transfer_completed_command( + logger: ILogger = Depends(get_logger), unit_of_work: IUnitOfWork = Depends(get_unit_of_work), receipt: IReceipt = Depends(get_cloud_kassir_receipt), ) -> CreateCryptoTransferCompletedCommand: - return CreateCryptoTransferCompletedCommand(unit_of_work=unit_of_work,receipt=receipt) \ No newline at end of file + return CreateCryptoTransferCompletedCommand(unit_of_work=unit_of_work,receipt=receipt,logger=logger) \ No newline at end of file diff --git a/src/presentation/messaging/crypto_transfer.py b/src/presentation/messaging/crypto_transfer.py index 8dacba3..f23a7e4 100644 --- a/src/presentation/messaging/crypto_transfer.py +++ b/src/presentation/messaging/crypto_transfer.py @@ -16,7 +16,7 @@ crypto_transfer_router=RabbitRouter(settings.RABBIT_URL) class CryptoTransferCompletedMessage(BaseModel): user_id: str order_id: str - trace_id: str + trace_id: str | None = None message_id: str web3_transaction_hash: str | None = None transaction_hash: str | None = None @@ -30,7 +30,8 @@ async def crypto_transfer_completed_handler( command: CreateCryptoTransferCompletedCommand = Depends(get_crypto_transfer_completed_command), logger: ILogger = Depends(get_logger), ) -> None: - trace_id=msg_body.trace_id + headers=message.headers or {} + trace_id=msg_body.trace_id or message.correlation_id or str(headers.get('trace_id') or '') token=trace_id_var.set(trace_id) try: payload=msg_body.model_dump(mode='json') diff --git a/src/presentation/routing/order.py b/src/presentation/routing/order.py index 5d38710..d0cc126 100644 --- a/src/presentation/routing/order.py +++ b/src/presentation/routing/order.py @@ -103,6 +103,10 @@ async def itpay_webhook( else: payload = orjson.loads(raw) data = payload.get('data') if isinstance(payload.get('data'), dict) else {} + metadata = data.get('metadata') if isinstance(data.get('metadata'), dict) else {} + trace_id = str(metadata.get('trace_id') or '').strip() + if trace_id: + logger.set_trace_id(trace_id) status = str(data.get('status') or '').strip().lower() log_payload = { 'event': 'itpay_webhook_received', @@ -111,7 +115,7 @@ async def itpay_webhook( 'payment_id': data.get('id'), 'client_payment_id': data.get('client_payment_id'), 'payment_status': status, - 'itpay_metadata': data.get('metadata'), + 'itpay_metadata': metadata, } logger.info(log_payload) if status == 'completed':