fix: removed crap, category on service and deal

This commit is contained in:
2024-09-27 04:50:01 +03:00
parent 5df64d4916
commit 91cf44f3ae
10 changed files with 258 additions and 64 deletions

View File

@@ -130,6 +130,14 @@ class DealService(BaseService):
user,
deadline=request.acceptance_date,
comment=request.comment)
# add category if specified
if request.category:
deal_category = DealPriceCategory(
deal_id=deal.id,
category_id=request.category.id
)
self.session.add(deal_category)
await self.session.commit()
return DealQuickCreateResponse(deal_id=deal.id)
@@ -1004,7 +1012,7 @@ class DealService(BaseService):
for product in deal.products:
total_one_product = sum((service.price for service in product.services))
total = total_one_product * product.quantity
totals.append({ "total_one_product": total_one_product, "total": total })
totals.append({"total_one_product": total_one_product, "total": total})
return totals

View File

@@ -3,7 +3,8 @@ from typing import Union
from sqlalchemy import select, update, insert, delete
from sqlalchemy.orm import joinedload
from models import Service, ServiceCategory, ServicePriceRange, ServicesKit, services_kit_services
from models import Service, ServiceCategory, ServicePriceRange, ServicesKit, services_kit_services, \
ServiceCategoryPrice, ServicePriceCategory
from services.base import BaseService
from schemas.service import *
@@ -27,6 +28,7 @@ class ServiceService(BaseService):
del service_dict['id']
del service_dict['category']
del service_dict['price_ranges']
del service_dict['category_prices']
service = Service(**service_dict)
self.session.add(service)
await self.session.flush()
@@ -37,7 +39,15 @@ class ServiceService(BaseService):
del price_range_dict['id']
price_range_obj = ServicePriceRange(**price_range_dict)
self.session.add(price_range_obj)
category_prices = request.service.category_prices
for category_price in category_prices:
category_price_dict = category_price.model_dump()
category_price_dict['service_id'] = service.id
category_price_dict['category_id'] = category_price.category.id
del category_price_dict['category']
category_price_obj = ServiceCategoryPrice(**category_price_dict)
self.session.add(category_price_obj)
await self.session.commit()
return ServiceCreateResponse(ok=True, message="Услуга успешно создана")
except Exception as e:
@@ -54,6 +64,7 @@ class ServiceService(BaseService):
service_dict['category_id'] = raw_service.category.id
del service_dict['category']
del service_dict['price_ranges']
del service_dict['category_prices']
await self.session.execute(
update(Service)
.where(Service.id == raw_service.id)
@@ -81,6 +92,26 @@ class ServiceService(BaseService):
del price_range_dict['id']
price_range_obj = ServicePriceRange(**price_range_dict)
self.session.add(price_range_obj)
# deleting previouse category prices
stmt = (
delete(
ServiceCategoryPrice
).where(
ServiceCategoryPrice.service_id == service.id
)
)
await self.session.execute(stmt)
await self.session.flush()
# inserting new category prices
for category_price in raw_service.category_prices:
category_price_dict = category_price.dict()
category_price_dict['service_id'] = raw_service.id
category_price_dict['category_id'] = category_price.category.id
del category_price_dict['category']
category_price_obj = ServiceCategoryPrice(**category_price_dict)
self.session.add(category_price_obj)
await self.session.commit()
return ServiceUpdateResponse(ok=True, message="Услуга успешно обновлена")
except Exception as e:
@@ -205,3 +236,60 @@ class ServiceService(BaseService):
except Exception as e:
return UpdateServicesKitResponse(ok=False, message=str(e))
async def get_all_price_categories(self) -> GetAllPriceCategoriesResponse:
query = await (self.session
.scalars(select(ServicePriceCategory)
.order_by(ServicePriceCategory.id)))
price_categories = []
for category in query.all():
price_categories.append(ServicePriceCategorySchema.model_validate(category))
return GetAllPriceCategoriesResponse(price_categories=price_categories)
async def create_price_category(self, request: CreatePriceCategoryRequest) -> ServiceCreateCategoryResponse:
try:
raw_category = request.name
category = ServicePriceCategory(name=raw_category)
self.session.add(category)
await self.session.commit()
return ServiceCreateCategoryResponse(ok=True, message="Категория цен успешно создана")
except Exception as e:
return ServiceCreateCategoryResponse(ok=False, message=f"Неудалось создать категорию цен, ошибка: {e}")
async def update_price_category(self, request: UpdatePriceCategoryRequest) -> ServiceUpdateResponse:
try:
raw_category = request.name
category = await (self.session.get(ServicePriceCategory, request.id))
if not category:
return ServiceUpdateResponse(ok=False, message="Категория цен не найдена")
await self.session.execute(
update(ServicePriceCategory)
.where(ServicePriceCategory.id == request.id)
.values(name=raw_category)
)
await self.session.commit()
return ServiceUpdateResponse(ok=True, message="Категория цен успешно обновлена")
except Exception as e:
return ServiceUpdateResponse(ok=False, message=f"Неудалось обновить категорию цен, ошибка: {e}")
async def delete_price_category(self, request: DeletePriceCategoryRequest) -> ServiceDeleteResponse:
try:
category = await (
self.session
.scalar(
select(
ServicePriceCategory
)
.filter(
ServicePriceCategory.id == request.id
)
)
)
if not category:
return ServiceDeleteResponse(ok=False, message="Категория цен не найдена")
await self.session.delete(category)
await self.session.commit()
return ServiceDeleteResponse(ok=True, message="Категория цен успешно удалена")
except Exception as e:
return ServiceDeleteResponse(ok=False, message=f"Неудалось удалить категорию цен, ошибка: {e}")