feat: add summary to completed orders
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
from abc import ABC,abstractmethod
|
from abc import ABC,abstractmethod
|
||||||
|
from decimal import Decimal
|
||||||
|
|
||||||
from src.application.domain.entities.order import OrderEntity
|
from src.application.domain.entities.order import OrderEntity
|
||||||
from src.application.domain.enums import OrderStatus
|
from src.application.domain.enums import OrderStatus
|
||||||
@@ -25,6 +26,11 @@ class IOrderRepository(ABC):
|
|||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
async def sum_by_date(self,*,year: int,month: int) -> Decimal:
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
async def count_all(self,*,search: str | None = None) -> int:
|
async def count_all(self,*,search: str | None = None) -> int:
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|||||||
@@ -52,7 +52,7 @@ class ListOrdersByDateCommand:
|
|||||||
|
|
||||||
|
|
||||||
@transactional
|
@transactional
|
||||||
async def __call__(self, *, limit: int, offset: int, year: int, month: int | None = None) -> tuple[list[OrderEntity], int]:
|
async def __call__(self, *, limit: int, offset: int, year: int, month: int | None = None) -> tuple[list[OrderEntity], int, Decimal]:
|
||||||
orders = await self._unit_of_work.order_repository.list_by_date(
|
orders = await self._unit_of_work.order_repository.list_by_date(
|
||||||
year=year,
|
year=year,
|
||||||
month=month,
|
month=month,
|
||||||
@@ -61,8 +61,9 @@ class ListOrdersByDateCommand:
|
|||||||
)
|
)
|
||||||
|
|
||||||
total = await self._unit_of_work.order_repository.count_by_date(year=year, month=month)
|
total = await self._unit_of_work.order_repository.count_by_date(year=year, month=month)
|
||||||
|
summary = await self._unit_of_work.order_repository.sum_by_date(year=year, month=month)
|
||||||
|
|
||||||
return orders, total
|
return orders, total, summary
|
||||||
|
|
||||||
|
|
||||||
class GetOrderCommand:
|
class GetOrderCommand:
|
||||||
|
|||||||
@@ -126,6 +126,19 @@ class OrderRepository(IOrderRepository):
|
|||||||
return [self._to_entity(model) for model in res.scalars().all()]
|
return [self._to_entity(model) for model in res.scalars().all()]
|
||||||
|
|
||||||
|
|
||||||
|
async def sum_by_date(self, *, year: int, month: int) -> Decimal:
|
||||||
|
stmt = (
|
||||||
|
select(func.coalesce(func.sum(OrderModel.service_fee), 0))
|
||||||
|
.where(
|
||||||
|
extract("year", OrderModel.created_at) == year,
|
||||||
|
extract("month", OrderModel.created_at) == month,
|
||||||
|
OrderModel.status == OrderStatus.COMPLETED.value,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
res = await self._session.execute(stmt)
|
||||||
|
return res.scalar_one()
|
||||||
|
|
||||||
|
|
||||||
async def count_all(self,*,search: str | None = None) -> int:
|
async def count_all(self,*,search: str | None = None) -> int:
|
||||||
stmt = select(func.count()).select_from(OrderModel)
|
stmt = select(func.count()).select_from(OrderModel)
|
||||||
search_filter = self._search_filter(search)
|
search_filter = self._search_filter(search)
|
||||||
@@ -135,7 +148,7 @@ class OrderRepository(IOrderRepository):
|
|||||||
return int(res.scalar_one())
|
return int(res.scalar_one())
|
||||||
|
|
||||||
|
|
||||||
async def count_by_date(self,*,year: int,month: int ) -> int:
|
async def count_by_date(self,*,year: int,month: int) -> int:
|
||||||
stmt = select(func.count()).select_from(OrderModel)
|
stmt = select(func.count()).select_from(OrderModel)
|
||||||
stmt = stmt.where(
|
stmt = stmt.where(
|
||||||
extract("year", OrderModel.created_at) == year,
|
extract("year", OrderModel.created_at) == year,
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ from src.presentation.schemas.mappers import order_to_response
|
|||||||
from src.presentation.schemas.order import (
|
from src.presentation.schemas.order import (
|
||||||
OrdersResponse,
|
OrdersResponse,
|
||||||
OrderResponse,
|
OrderResponse,
|
||||||
|
OrdersByDateResponse,
|
||||||
OrderDetailResponse,
|
OrderDetailResponse,
|
||||||
)
|
)
|
||||||
from src.application.commands.orders_commands import (
|
from src.application.commands.orders_commands import (
|
||||||
@@ -69,7 +70,7 @@ async def get_order(
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@orders_router.get('/filter/date', response_model=OrdersResponse)
|
@orders_router.get('/filter/date', response_model=OrdersByDateResponse)
|
||||||
async def list_orders_by_date(
|
async def list_orders_by_date(
|
||||||
year: int = Query(..., ge=2000, le=2100),
|
year: int = Query(..., ge=2000, le=2100),
|
||||||
month: int = Query(..., ge=1, le=12),
|
month: int = Query(..., ge=1, le=12),
|
||||||
@@ -78,8 +79,9 @@ async def list_orders_by_date(
|
|||||||
auth: AdminAuthContext = Depends(require_admin_access),
|
auth: AdminAuthContext = Depends(require_admin_access),
|
||||||
command: ListOrdersByDateCommand = Depends(get_list_orders_by_date_command),
|
command: ListOrdersByDateCommand = Depends(get_list_orders_by_date_command),
|
||||||
):
|
):
|
||||||
orders, total = await command(year=year, month=month, limit=limit, offset=offset)
|
orders, total, summary = await command(year=year, month=month, limit=limit, offset=offset)
|
||||||
return OrdersResponse(
|
return OrdersByDateResponse(
|
||||||
items=[order_to_response(o) for o in orders],
|
items=[order_to_response(o) for o in orders],
|
||||||
total=total,
|
total=total,
|
||||||
|
summed_service_fees=summary,
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -40,4 +40,9 @@ class OrderResponse(BaseModel):
|
|||||||
|
|
||||||
class OrdersResponse(BaseModel):
|
class OrdersResponse(BaseModel):
|
||||||
items: list[OrderResponse]
|
items: list[OrderResponse]
|
||||||
total: int
|
total: int
|
||||||
|
|
||||||
|
class OrdersByDateResponse(BaseModel):
|
||||||
|
items: list[OrderResponse]
|
||||||
|
total: int
|
||||||
|
summed_service_fees: Decimal
|
||||||
Reference in New Issue
Block a user