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

67 lines
2.6 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 DealProductService
from modules.fulfillment_base.repositories import ProductServiceRepository
from modules.fulfillment_base.schemas.product_service import *
class ProductServiceService:
schema_class = ProductServiceSchema
entity_not_found_msg = "Связь услуги с товаром не найдена"
def __init__(self, session: AsyncSession):
self.repository = ProductServiceRepository(session)
async def create(
self, request: CreateProductServiceRequest
) -> CreateProductServiceResponse:
await self.repository.create(request.entity)
deal_product = await self.repository.get_by_id(
request.entity.deal_id,
request.entity.product_id,
request.entity.service_id,
)
return CreateProductServiceResponse(
entity=ProductServiceSchema.model_validate(deal_product),
message="Услуга добавлена к товару",
)
async def update(
self,
deal_id: int,
product_id: int,
service_id: int,
data: UpdateProductServiceRequest,
) -> UpdateProductServiceResponse:
entity = await self.repository.get_by_id(deal_id, product_id, service_id)
if not entity:
raise HTTPException(status_code=404, detail=self.entity_not_found_msg)
await self.repository.update(entity, data.entity)
return UpdateProductServiceResponse(message="Услуга обновлена")
async def delete(
self, deal_id: int, product_id: int, service_id: int
) -> DeleteProductServiceResponse:
entity = await self.repository.get_by_id(deal_id, product_id, service_id)
if not entity:
raise HTTPException(status_code=404, detail=self.entity_not_found_msg)
await self.repository.delete(entity)
return DeleteProductServiceResponse(message="Товар удален из сделки")
async def duplicate_product_services(
self, request: ProductServicesDuplicateRequest
) -> ProductServicesDuplicateResponse:
services_to_copy: list[
DealProductService
] = await self.repository.get_product_services(
request.deal_id, request.source_deal_product_id
)
await self.repository.duplicate_services(
request.deal_id, request.target_deal_product_ids, services_to_copy
)
return ProductServicesDuplicateResponse(message="Услуги продублированы")