from models import Attribute, AttributeValue, AttributeLabel from repositories import AttributeRepository 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: 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 attr.default_value, type=AttributeTypeSchema.model_validate(attr.type), default_value=attr.default_value, description=attr.description, is_applicable_to_group=attr.is_applicable_to_group, is_shown_on_dashboard=attr.is_shown_on_dashboard, is_highlight_if_expired=attr.is_highlight_if_expired, 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: await self.repository.update_or_create_deal_attribute_values( deal_id, module_id, request.attributes ) return UpdateDealModuleAttributesResponse(message="Успешно сохранено")