feat: deals table
This commit is contained in:
		@@ -75,9 +75,10 @@ async def change_status(
 | 
			
		||||
    dependencies=[Depends(authorized_user)]
 | 
			
		||||
)
 | 
			
		||||
async def get_summary(
 | 
			
		||||
        session: Annotated[AsyncSession, Depends(get_session)]
 | 
			
		||||
        session: Annotated[AsyncSession, Depends(get_session)],
 | 
			
		||||
        full: Optional[bool]
 | 
			
		||||
):
 | 
			
		||||
    return await DealService(session).get_summary()
 | 
			
		||||
    return await DealService(session).get_summary(full)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@deal_router.post(
 | 
			
		||||
 
 | 
			
		||||
@@ -26,6 +26,7 @@ class DealSummary(BaseSchema):
 | 
			
		||||
    name: str
 | 
			
		||||
    client_name: str
 | 
			
		||||
    changed_at: datetime.datetime
 | 
			
		||||
    created_at: datetime.datetime
 | 
			
		||||
    deadline: datetime.datetime
 | 
			
		||||
    status: int
 | 
			
		||||
    total_price: int
 | 
			
		||||
 
 | 
			
		||||
@@ -165,7 +165,7 @@ class DealService(BaseService):
 | 
			
		||||
        )
 | 
			
		||||
        return final_subquery
 | 
			
		||||
 | 
			
		||||
    async def get_summary(self) -> DealSummaryResponse:
 | 
			
		||||
    async def get_summary(self, full: bool = False) -> DealSummaryResponse:
 | 
			
		||||
        price_subquery = self._get_price_subquery()
 | 
			
		||||
        q = (
 | 
			
		||||
            select(
 | 
			
		||||
@@ -184,10 +184,15 @@ class DealService(BaseService):
 | 
			
		||||
                price_subquery, Deal.id == price_subquery.c.deal_id)
 | 
			
		||||
            .where(
 | 
			
		||||
                Deal.is_deleted == False,
 | 
			
		||||
            )
 | 
			
		||||
        )
 | 
			
		||||
        if not full:
 | 
			
		||||
            q = q.where(
 | 
			
		||||
                Deal.is_completed == False,
 | 
			
		||||
                Deal.current_status != DealStatus.COMPLETED
 | 
			
		||||
            )
 | 
			
		||||
        )
 | 
			
		||||
        else:
 | 
			
		||||
            q = q.order_by(Deal.created_at.desc())
 | 
			
		||||
        deals_query = await self.session.execute(q)
 | 
			
		||||
        summaries = []
 | 
			
		||||
        for deal, total_price, rank in deals_query.all():
 | 
			
		||||
@@ -207,13 +212,47 @@ class DealService(BaseService):
 | 
			
		||||
                    status=last_status.to_status,
 | 
			
		||||
                    total_price=total_price,
 | 
			
		||||
                    rank=rank,
 | 
			
		||||
                    base_marketplace=base_marketplace
 | 
			
		||||
                    base_marketplace=base_marketplace,
 | 
			
		||||
                    created_at=deal.created_at
 | 
			
		||||
                )
 | 
			
		||||
            )
 | 
			
		||||
        return DealSummaryResponse(summaries=summaries)
 | 
			
		||||
 | 
			
		||||
    async def get_all(self) -> DealGetAllResponse:
 | 
			
		||||
        deals_query = await self.session.scalars(select(Deal).options(joinedload(Deal.client)))
 | 
			
		||||
        deals_query = (
 | 
			
		||||
            await self.session.scalars(
 | 
			
		||||
                select(
 | 
			
		||||
                    Deal
 | 
			
		||||
                )
 | 
			
		||||
                .options(
 | 
			
		||||
                    joinedload(Deal.shipping_warehouse),
 | 
			
		||||
                    joinedload(Deal.client)
 | 
			
		||||
                    .joinedload(Client.details),
 | 
			
		||||
                    selectinload(Deal.services)
 | 
			
		||||
                    .options(
 | 
			
		||||
                        joinedload(models.secondary.DealService.service).joinedload(Service.category),
 | 
			
		||||
                        selectinload(models.secondary.DealService.employees)
 | 
			
		||||
                    ),
 | 
			
		||||
                    selectinload(Deal.products)
 | 
			
		||||
                    .joinedload(models.secondary.DealProduct.product)
 | 
			
		||||
                    .joinedload(models.Product.client),
 | 
			
		||||
                    selectinload(Deal.products)
 | 
			
		||||
                    .joinedload(models.secondary.DealProduct.product)
 | 
			
		||||
                    .joinedload(models.Product.barcodes),
 | 
			
		||||
                    selectinload(Deal.products)
 | 
			
		||||
                    .joinedload(models.secondary.DealProduct.services)
 | 
			
		||||
                    .options(
 | 
			
		||||
                        joinedload(models.secondary.DealProductService.service),
 | 
			
		||||
                        selectinload(models.secondary.DealProductService.employees)
 | 
			
		||||
                    ),
 | 
			
		||||
                    selectinload(Deal.status_history)
 | 
			
		||||
                    .joinedload(DealStatusHistory.user),
 | 
			
		||||
                    selectinload(Deal.status_history)
 | 
			
		||||
                    .noload(DealStatusHistory.deal),
 | 
			
		||||
 | 
			
		||||
                )
 | 
			
		||||
            )
 | 
			
		||||
        )
 | 
			
		||||
        deals = deals_query.all()
 | 
			
		||||
        result = []
 | 
			
		||||
        for deal in deals:
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user