79 lines
3.0 KiB
Python
79 lines
3.0 KiB
Python
from models import Attribute, AttributeValue, AttributeLabel
|
|
from repositories import AttributeRepository, DealRepository
|
|
from schemas.attribute import *
|
|
from services.mixins import *
|
|
|
|
|
|
class AttributeService(
|
|
ServiceCrudMixin[
|
|
Attribute, AttributeSchema, CreateAttributeRequest, UpdateAttributeRequest
|
|
]
|
|
):
|
|
schema_class = AttributeSchema
|
|
|
|
def __init__(self, session: AsyncSession):
|
|
self.repository = AttributeRepository(session)
|
|
|
|
async def update_attribute_label(
|
|
self, request: UpdateAttributeLabelRequest
|
|
) -> UpdateAttributeLabelResponse:
|
|
await self.repository.create_or_update_attribute_label(
|
|
request.module_id, request.attribute_id, request.label
|
|
)
|
|
return UpdateAttributeLabelResponse(
|
|
message="Название атрибута в модуле изменено"
|
|
)
|
|
|
|
async def get_attribute_types(self) -> GetAllAttributeTypesResponse:
|
|
types = await self.repository.get_attribute_types()
|
|
return GetAllAttributeTypesResponse(
|
|
items=[AttributeTypeSchema.model_validate(t) for t in types]
|
|
)
|
|
|
|
async def get_deal_module_attributes(
|
|
self, deal_id: int, module_id: int
|
|
) -> GetDealModuleAttributesResponse:
|
|
deal_attributes: list[
|
|
tuple[Attribute, AttributeValue, AttributeLabel]
|
|
] = await self.repository.get_deal_module_attributes(deal_id, module_id)
|
|
|
|
attributes = []
|
|
for attr, attr_value, attr_label in deal_attributes:
|
|
select_schema = (
|
|
AttributeSelectSchema.model_validate(attr.select)
|
|
if attr.select
|
|
else None
|
|
)
|
|
|
|
attribute = DealModuleAttributeSchema(
|
|
attribute_id=attr.id,
|
|
label=attr_label.label if attr_label else attr.label,
|
|
original_label=attr.label,
|
|
value=attr_value.value if attr_value else None,
|
|
type=AttributeTypeSchema.model_validate(attr.type),
|
|
select=select_schema,
|
|
default_value=attr.default_value,
|
|
description=attr.description,
|
|
is_applicable_to_group=attr.is_applicable_to_group,
|
|
is_nullable=attr.is_nullable,
|
|
)
|
|
attributes.append(attribute)
|
|
|
|
return GetDealModuleAttributesResponse(attributes=attributes)
|
|
|
|
async def update_deal_module_attributes(
|
|
self, deal_id: int, module_id: int, request: UpdateDealModuleAttributesRequest
|
|
) -> UpdateDealModuleAttributesResponse:
|
|
deal_repo = DealRepository(self.repository.session)
|
|
deal = await deal_repo.get_by_id(deal_id)
|
|
if deal.group_id:
|
|
deals = await deal_repo.get_by_group_id(deal.group_id)
|
|
else:
|
|
deals = [deal]
|
|
|
|
group_deal_ids = [d.id for d in deals]
|
|
await self.repository.update_or_create_deals_attribute_values(
|
|
deal_id, group_deal_ids, module_id, request.attributes
|
|
)
|
|
return UpdateDealModuleAttributesResponse(message="Успешно сохранено")
|