feat: updating order status
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
from abc import ABC,abstractmethod
|
||||
|
||||
from src.application.domain.entities.order import OrderEntity
|
||||
from src.application.domain.enums import OrderStatus
|
||||
|
||||
|
||||
class IOrderRepository(ABC):
|
||||
@@ -36,4 +37,9 @@ class IOrderRepository(ABC):
|
||||
|
||||
@abstractmethod
|
||||
async def update_after_itpay_failure(self,order: OrderEntity) -> OrderEntity:
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
@abstractmethod
|
||||
async def update_status(self,*,order_id:str,status:OrderStatus) -> None:
|
||||
raise NotImplementedError
|
||||
@@ -2,7 +2,7 @@ from __future__ import annotations
|
||||
from decimal import Decimal
|
||||
from src.application.abstractions import IUnitOfWork
|
||||
from src.application.contracts import ILogger,IReceipt
|
||||
from src.application.domain.enums import PaymentStatus
|
||||
from src.application.domain.enums import OrderStatus,PaymentStatus
|
||||
from src.application.domain.exceptions import ApplicationException,NotFoundException,PaymentMetadataException,ReceiptDataException
|
||||
from src.infrastructure.database.decorators import transactional
|
||||
|
||||
@@ -91,3 +91,13 @@ class CreateCryptoTransferCompletedCommand:
|
||||
receipt_cloudekassir_id=str(receipt_model.get('Id') or '') or None,
|
||||
receipt_cloudekassir_link=str(receipt_model.get('ReceiptLocalUrl') or '') or None,
|
||||
)
|
||||
await self._unit_of_work.order_repository.update_status(
|
||||
order_id=order_id,
|
||||
status=OrderStatus.COMPLETED,
|
||||
)
|
||||
self._logger.info({
|
||||
'event': 'order_status_changed',
|
||||
'order_id': order_id,
|
||||
'user_id': user_id,
|
||||
'status': OrderStatus.COMPLETED.value,
|
||||
})
|
||||
|
||||
@@ -2,7 +2,7 @@ from __future__ import annotations
|
||||
from ulid import ULID
|
||||
from src.application.abstractions import IUnitOfWork
|
||||
from src.application.contracts import ILogger,IQueueMessanger
|
||||
from src.application.domain.enums import PaymentStatus
|
||||
from src.application.domain.enums import OrderStatus
|
||||
from src.application.domain.exceptions import PaymentMetadataException
|
||||
from src.infrastructure.config import settings
|
||||
from src.infrastructure.database.decorators import transactional
|
||||
@@ -15,6 +15,20 @@ class CreatePaymentCommand:
|
||||
self._logger = logger
|
||||
self._queue_messanger = queue_messanger
|
||||
|
||||
|
||||
async def _mark_order_completed(self, order_id: str, user_id: str) -> None:
|
||||
await self._unit_of_work.order_repository.update_status(
|
||||
order_id=order_id,
|
||||
status=OrderStatus.COMPLETED,
|
||||
)
|
||||
self._logger.info({
|
||||
'event': 'order_status_changed',
|
||||
'order_id': order_id,
|
||||
'user_id': user_id,
|
||||
'status': OrderStatus.COMPLETED.value,
|
||||
})
|
||||
|
||||
|
||||
@transactional
|
||||
async def __call__(self, payment: ItpayPaymentData) -> None:
|
||||
if str(payment.status).strip().lower() != 'completed':
|
||||
@@ -38,7 +52,14 @@ class CreatePaymentCommand:
|
||||
expired_date=str(payment.expired_date) if payment.expired_date is not None else None,
|
||||
)
|
||||
if not payment_created:
|
||||
await self._mark_order_completed(order_id, user_id)
|
||||
return
|
||||
await self._mark_order_completed(order_id, user_id)
|
||||
self._logger.info({
|
||||
'event': 'payment_money_accepted',
|
||||
'order_id': order_id,
|
||||
'user_id': user_id,
|
||||
})
|
||||
message_id = str(ULID())
|
||||
message: dict[str,str] = {
|
||||
'order_id': order_id,
|
||||
@@ -71,13 +92,3 @@ class CreatePaymentCommand:
|
||||
'user_id': user_id,
|
||||
'message_id': message_id,
|
||||
})
|
||||
await self._unit_of_work.payment_repository.update_status(
|
||||
order_id=order_id,
|
||||
status=PaymentStatus.WEB3_PROCESSING,
|
||||
)
|
||||
self._logger.info({
|
||||
'event': 'payment_status_changed',
|
||||
'order_id': order_id,
|
||||
'user_id': user_id,
|
||||
'status': PaymentStatus.WEB3_PROCESSING.value,
|
||||
})
|
||||
|
||||
@@ -6,5 +6,4 @@ class OrderStatus(str, Enum):
|
||||
REJECTED = 'rejected'
|
||||
COMPLETED = 'completed'
|
||||
CANCELLED = 'cancelled'
|
||||
ERROR = 'error'
|
||||
CANCELED = 'canceled'
|
||||
ERROR = 'error'
|
||||
@@ -7,6 +7,7 @@ from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from src.application.abstractions.repositories.i_order_repository import IOrderRepository
|
||||
from src.application.contracts import ILogger
|
||||
from src.application.domain.entities.order import OrderEntity
|
||||
from src.application.domain.enums import OrderStatus
|
||||
from src.infrastructure.database.models.order import Order
|
||||
|
||||
|
||||
@@ -159,4 +160,14 @@ class OrderRepository(IOrderRepository):
|
||||
)
|
||||
await self._session.execute(stmt)
|
||||
await self._session.flush()
|
||||
return order
|
||||
return order
|
||||
|
||||
|
||||
async def update_status(self,*,order_id:str,status:OrderStatus) -> None:
|
||||
stmt=(
|
||||
update(Order)
|
||||
.where(Order.id==order_id)
|
||||
.values(status=status)
|
||||
)
|
||||
await self._session.execute(stmt)
|
||||
await self._session.flush()
|
||||
Reference in New Issue
Block a user