Files
Crm-Backend/modules/fulfillment_base/services/deal_product.py

73 lines
3.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from fastapi import HTTPException
from sqlalchemy.ext.asyncio import AsyncSession
from modules.fulfillment_base.models import DealProduct
from modules.fulfillment_base.repositories import (
DealProductRepository,
ServicesKitRepository,
ProductServiceRepository,
)
from modules.fulfillment_base.schemas.deal_product import *
from services.mixins import ServiceGetAllMixin
class DealProductService(ServiceGetAllMixin[DealProduct, DealProductSchema]):
schema_class = DealProductSchema
entity_not_found_msg = "Связь товара со сделкой не найдена"
def __init__(self, session: AsyncSession):
self.repository = DealProductRepository(session)
async def create(
self, request: CreateDealProductRequest
) -> CreateDealProductResponse:
await self.repository.create(request.entity)
deal_product = await self.repository.get_by_id(
request.entity.deal_id, request.entity.product_id
)
return CreateDealProductResponse(
entity=DealProductSchema.model_validate(deal_product),
message="Товар добавлен в сделку",
)
async def update(
self, deal_id: int, product_id: int, data: UpdateDealProductRequest
) -> UpdateDealProductResponse:
entity = await self.repository.get_by_id(deal_id, product_id)
if not entity:
raise HTTPException(status_code=404, detail=self.entity_not_found_msg)
await self.repository.update(entity, data.entity)
return UpdateDealProductResponse(message="Товар сделки обновлен")
async def delete(self, deal_id: int, product_id: int) -> DeleteDealProductResponse:
entity = await self.repository.get_by_id(deal_id, product_id)
if not entity:
raise HTTPException(status_code=404, detail=self.entity_not_found_msg)
await self.repository.delete(entity)
return DeleteDealProductResponse(message="Товар удален из сделки")
async def add_services_kit(
self, request: DealProductAddKitRequest
) -> DealProductAddKitResponse:
services_kit_repo = ServicesKitRepository(self.repository.session)
services_kit = await services_kit_repo.get_by_id(request.kit_id)
if not services_kit:
raise HTTPException(status_code=404, detail="Набор услуг не найден")
deal_product = await self.repository.get_by_id(
request.deal_id, request.product_id
)
if not deal_product:
raise HTTPException(status_code=404, detail=self.entity_not_found_msg)
product_service_repo = ProductServiceRepository(self.repository.session)
await product_service_repo.delete_product_services(
request.deal_id, [request.product_id]
)
await self.repository.add_services_kit(deal_product, services_kit)
return DealProductAddKitResponse(message="Комплект добавлен в товар")