feat: groups
This commit is contained in:
@ -1,5 +1,5 @@
|
||||
from sqlalchemy import func
|
||||
from sqlalchemy.orm import joinedload
|
||||
from sqlalchemy.orm import joinedload, selectinload
|
||||
|
||||
from models import Deal, Board, DealStatusHistory
|
||||
from modules.fulfillment_base.models import (
|
||||
@ -10,7 +10,7 @@ from modules.fulfillment_base.models import (
|
||||
)
|
||||
from repositories.mixins import *
|
||||
from schemas.base import SortDir
|
||||
from schemas.deal import UpdateDealSchema, CreateDealSchema, DealSchema
|
||||
from schemas.deal import UpdateDealSchema, CreateDealSchema
|
||||
from utils.sorting import apply_sorting
|
||||
|
||||
|
||||
@ -95,7 +95,11 @@ class DealRepository(
|
||||
products_quantity_subquery,
|
||||
Deal.id == products_quantity_subquery.c.deal_id,
|
||||
)
|
||||
.options(joinedload(Deal.status), joinedload(Deal.board))
|
||||
.options(
|
||||
joinedload(Deal.status),
|
||||
joinedload(Deal.board),
|
||||
selectinload(Deal.group),
|
||||
)
|
||||
.where(Deal.is_deleted.is_(False))
|
||||
)
|
||||
|
||||
@ -123,21 +127,45 @@ class DealRepository(
|
||||
rows: list[tuple[Deal, int, int]] = (await self.session.execute(stmt)).all()
|
||||
return rows, total_items
|
||||
|
||||
async def get_by_group_id(self, group_id: int) -> list[Deal]:
|
||||
stmt = (
|
||||
select(Deal)
|
||||
.where(Deal.group_id == group_id, Deal.is_deleted.is_(False))
|
||||
.options(joinedload(Deal.status), joinedload(Deal.board))
|
||||
)
|
||||
result = await self.session.execute(stmt)
|
||||
return list(result.scalars().all())
|
||||
|
||||
async def get_by_ids(self, deal_ids: list[int]) -> list[Deal]:
|
||||
stmt = (
|
||||
select(Deal)
|
||||
.where(Deal.id.in_(deal_ids), Deal.is_deleted.is_(False))
|
||||
.options(joinedload(Deal.status), joinedload(Deal.board))
|
||||
)
|
||||
result = await self.session.execute(stmt)
|
||||
return list(result.scalars().all())
|
||||
|
||||
def _process_get_by_id_stmt(self, stmt: Select) -> Select:
|
||||
return stmt.options(joinedload(Deal.status), joinedload(Deal.board))
|
||||
|
||||
async def update_status(self, deal: Deal, status_id: int):
|
||||
if deal.status_id == status_id:
|
||||
return
|
||||
|
||||
deal.status_history.append(
|
||||
DealStatusHistory(
|
||||
from_status_id=deal.status_id,
|
||||
to_status_id=status_id,
|
||||
)
|
||||
)
|
||||
deal.status_id = status_id
|
||||
|
||||
async def update(self, deal: Deal, data: UpdateDealSchema) -> Deal:
|
||||
fields = ["lexorank", "name", "board_id"]
|
||||
deal = await self._apply_update_data_to_model(deal, data, False, fields)
|
||||
|
||||
if data.status_id and deal.status_id != data.status_id:
|
||||
deal.status_history.append(
|
||||
DealStatusHistory(
|
||||
from_status_id=deal.status_id,
|
||||
to_status_id=data.status_id,
|
||||
)
|
||||
)
|
||||
deal.status_id = data.status_id
|
||||
if data.status_id:
|
||||
await self.update_status(deal, data.status_id)
|
||||
|
||||
self.session.add(deal)
|
||||
await self.session.commit()
|
||||
|
||||
Reference in New Issue
Block a user