feat: groups
This commit is contained in:
@ -3,3 +3,4 @@ from .deal import DealRepository as DealRepository
|
||||
from .project import ProjectRepository as ProjectRepository
|
||||
from .status import StatusRepository as StatusRepository
|
||||
from .built_in_module import BuiltInModuleRepository as BuiltInModuleRepository
|
||||
from .deal_group import DealGroupRepository as DealGroupRepository
|
||||
|
||||
@ -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()
|
||||
|
||||
52
repositories/deal_group.py
Normal file
52
repositories/deal_group.py
Normal file
@ -0,0 +1,52 @@
|
||||
from models import DealGroup, Deal
|
||||
from repositories import DealRepository
|
||||
from repositories.mixins import *
|
||||
from schemas.deal_group import UpdateDealGroupSchema
|
||||
|
||||
|
||||
class DealGroupRepository(
|
||||
BaseRepository,
|
||||
RepGetByIdMixin[DealGroup],
|
||||
RepUpdateMixin[DealGroup, UpdateDealGroupSchema],
|
||||
):
|
||||
entity_class = DealGroup
|
||||
|
||||
async def create(self, deals: list[Deal], lexorank: str) -> DealGroup:
|
||||
group = DealGroup(deals=deals, lexorank=lexorank)
|
||||
self.session.add(group)
|
||||
await self.session.commit()
|
||||
await self.session.refresh(group)
|
||||
return group
|
||||
|
||||
async def update(self, entity: DealGroup, data: UpdateDealGroupSchema) -> DealGroup:
|
||||
if data.status_id:
|
||||
deal_repo = DealRepository(self.session)
|
||||
deals = await deal_repo.get_by_group_id(entity.id)
|
||||
for deal in deals:
|
||||
await deal_repo.update_status(deal, data.status_id)
|
||||
del data.status_id
|
||||
return await self._apply_update_data_to_model(entity, data, True)
|
||||
|
||||
async def update_group_deals(
|
||||
self, group_id: int, old_deals: list[Deal], new_deals: list[Deal]
|
||||
):
|
||||
old_set = set(old_deals)
|
||||
new_set = set(new_deals)
|
||||
deals_to_remove = old_set - new_set
|
||||
deals_to_add = new_set - old_set
|
||||
|
||||
for deal in deals_to_remove:
|
||||
deal.group_id = None
|
||||
for deal in deals_to_add:
|
||||
deal.group_id = group_id
|
||||
|
||||
self.session.add_all([*deals_to_remove, *deals_to_add])
|
||||
await self.session.commit()
|
||||
|
||||
async def delete(self, group_id: int) -> None:
|
||||
deal_repo = DealRepository(self.session)
|
||||
deals = await deal_repo.get_by_group_id(group_id)
|
||||
for deal in deals:
|
||||
deal.is_deleted = True
|
||||
self.session.add(deal)
|
||||
await self.session.commit()
|
||||
Reference in New Issue
Block a user