307 lines
11 KiB
Python
307 lines
11 KiB
Python
import uuid
|
|
|
|
from datetime import date
|
|
from functools import reduce
|
|
|
|
import barker.schemas.modifier_category as schemas
|
|
|
|
from barker.schemas.product_link import ProductLink as ProductLinkSchema
|
|
from fastapi import APIRouter, Depends, HTTPException, Security, status
|
|
from sqlalchemy import and_, or_, select
|
|
from sqlalchemy.exc import SQLAlchemyError
|
|
from sqlalchemy.orm import Session, contains_eager, joinedload
|
|
|
|
from ..core.security import get_current_active_user as get_user
|
|
from ..db.session import SessionFuture
|
|
from ..models.menu_category import MenuCategory
|
|
from ..models.modifier_category import ModifierCategory
|
|
from ..models.product import Product
|
|
from ..models.product_version import ProductVersion
|
|
from ..schemas.user_token import UserToken
|
|
from . import effective_date
|
|
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.post("", response_model=schemas.ModifierCategory)
|
|
def save(
|
|
data: schemas.ModifierCategoryIn,
|
|
date_: date = Depends(effective_date),
|
|
user: UserToken = Security(get_user, scopes=["modifiers"]),
|
|
) -> schemas.ModifierCategory:
|
|
try:
|
|
with SessionFuture() as db:
|
|
item = ModifierCategory(
|
|
name=data.name,
|
|
minimum=data.minimum,
|
|
maximum=None if data.maximum == 0 else data.maximum,
|
|
is_active=data.is_active,
|
|
)
|
|
db.add(item)
|
|
add_products(item, data.menu_categories, db)
|
|
db.commit()
|
|
return modifier_category_info(item, date_, db=db)
|
|
except SQLAlchemyError as e:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
|
detail=str(e),
|
|
)
|
|
|
|
|
|
@router.put("/{id_}", response_model=schemas.ModifierCategory)
|
|
def update_route(
|
|
id_: uuid.UUID,
|
|
data: schemas.ModifierCategoryIn,
|
|
date_: date = Depends(effective_date),
|
|
user: UserToken = Security(get_user, scopes=["modifiers"]),
|
|
) -> schemas.ModifierCategory:
|
|
try:
|
|
with SessionFuture() as db:
|
|
item: ModifierCategory = db.execute(select(ModifierCategory).where(ModifierCategory.id == id_)).scalar_one()
|
|
item.name = data.name
|
|
item.minimum = data.minimum
|
|
item.maximum = None if data.maximum == 0 else data.maximum
|
|
item.is_active = data.is_active
|
|
add_products(item, data.menu_categories, db)
|
|
db.commit()
|
|
return modifier_category_info(item, date_, db=db)
|
|
except SQLAlchemyError as e:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
|
detail=str(e),
|
|
)
|
|
|
|
|
|
@router.delete("/{id_}", response_model=schemas.ModifierCategoryBlank)
|
|
def delete_route(
|
|
id_: uuid.UUID,
|
|
date_: date = Depends(effective_date),
|
|
user: UserToken = Security(get_user, scopes=["modifiers"]),
|
|
) -> schemas.ModifierCategoryBlank:
|
|
try:
|
|
with SessionFuture() as db:
|
|
item: ModifierCategory = db.execute(select(ModifierCategory).where(ModifierCategory.id == id_)).scalar_one()
|
|
db.delete(item)
|
|
db.commit()
|
|
return modifier_category_blank(date_, db=db)
|
|
except SQLAlchemyError as e:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
|
detail=str(e),
|
|
)
|
|
|
|
|
|
@router.get("", response_model=schemas.ModifierCategoryBlank)
|
|
def show_blank(
|
|
date_: date = Depends(effective_date),
|
|
user: UserToken = Security(get_user, scopes=["modifiers"]),
|
|
) -> schemas.ModifierCategoryBlank:
|
|
with SessionFuture() as db:
|
|
return modifier_category_blank(date_, db=db)
|
|
|
|
|
|
@router.get("/list")
|
|
def show_list(date_: date = Depends(effective_date), user: UserToken = Depends(get_user)):
|
|
with SessionFuture() as db:
|
|
list_ = (
|
|
db.execute(select(ModifierCategory).order_by(ModifierCategory.sort_order).order_by(ModifierCategory.name))
|
|
.scalars()
|
|
.all()
|
|
)
|
|
menu_categories = (
|
|
db.execute(
|
|
select(MenuCategory)
|
|
.join(MenuCategory.products)
|
|
.where(
|
|
and_(
|
|
or_(
|
|
ProductVersion.valid_from == None, # noqa: E711
|
|
ProductVersion.valid_from <= date_,
|
|
),
|
|
or_(
|
|
ProductVersion.valid_till == None, # noqa: E711
|
|
ProductVersion.valid_till >= date_,
|
|
),
|
|
)
|
|
)
|
|
.order_by(MenuCategory.sort_order, ProductVersion.sort_order, ProductVersion.name)
|
|
.options(
|
|
joinedload(MenuCategory.products, innerjoin=True),
|
|
contains_eager(MenuCategory.products),
|
|
)
|
|
)
|
|
.unique()
|
|
.scalars()
|
|
.all()
|
|
)
|
|
modifier_categories = []
|
|
for item in list_:
|
|
products = [x.id for x in item.products]
|
|
modifier_category = {
|
|
"id": item.id,
|
|
"name": item.name,
|
|
"minimum": item.minimum,
|
|
"maximum": item.maximum,
|
|
"isActive": item.is_active,
|
|
"menuCategories": [
|
|
{
|
|
"id": mc.id,
|
|
"name": mc.name,
|
|
"enabled": reduce(lambda x, y: x and (y.product_id in products), mc.products, True),
|
|
"products": [{"id": p.id, "name": p.name} for p in mc.products if p.product_id in products],
|
|
}
|
|
for mc in menu_categories
|
|
],
|
|
}
|
|
modifier_category["menuCategories"] = [
|
|
i for i in modifier_category["menuCategories"] if len(i["products"]) > 0
|
|
]
|
|
modifier_categories.append(modifier_category)
|
|
return modifier_categories
|
|
|
|
|
|
@router.get("/for-product/{id_}")
|
|
def for_product(
|
|
id_: uuid.UUID,
|
|
date_: date = Depends(effective_date),
|
|
user: UserToken = Security(get_user),
|
|
):
|
|
with SessionFuture() as db:
|
|
product: Product = db.execute(select(Product).where(Product.id == id_)).scalar_one()
|
|
|
|
return [
|
|
{
|
|
"id": item.id,
|
|
"name": item.name,
|
|
"minimum": item.minimum,
|
|
"maximum": item.maximum,
|
|
"isActive": item.is_active,
|
|
"modifiers": [
|
|
{"id": m.id, "name": m.name, "price": m.price} for m in item.modifiers if m.is_active is True
|
|
],
|
|
}
|
|
for item in product.modifier_categories
|
|
if item.is_active is True
|
|
]
|
|
|
|
|
|
@router.get("/{id_}", response_model=schemas.ModifierCategory)
|
|
def show_id(
|
|
id_: uuid.UUID,
|
|
date_: date = Depends(effective_date),
|
|
user: UserToken = Security(get_user, scopes=["modifiers"]),
|
|
) -> schemas.ModifierCategory:
|
|
with SessionFuture() as db:
|
|
item: ModifierCategory = db.execute(select(ModifierCategory).where(ModifierCategory.id == id_)).scalar_one()
|
|
return modifier_category_info(item, date_, db=db)
|
|
|
|
|
|
def modifier_category_info(item: ModifierCategory, date_: date, db: Session) -> schemas.ModifierCategory:
|
|
menu_categories = (
|
|
db.execute(
|
|
select(MenuCategory)
|
|
.join(MenuCategory.products)
|
|
.where(
|
|
and_(
|
|
or_(
|
|
ProductVersion.valid_from == None, # noqa: E711
|
|
ProductVersion.valid_from <= date_,
|
|
),
|
|
or_(
|
|
ProductVersion.valid_till == None, # noqa: E711
|
|
ProductVersion.valid_till >= date_,
|
|
),
|
|
)
|
|
)
|
|
.order_by(MenuCategory.sort_order, ProductVersion.sort_order, ProductVersion.name)
|
|
.options(
|
|
joinedload(MenuCategory.products, innerjoin=True),
|
|
contains_eager(MenuCategory.products),
|
|
)
|
|
)
|
|
.unique()
|
|
.scalars()
|
|
.all()
|
|
)
|
|
products = [p.id for p in item.products]
|
|
return schemas.ModifierCategory(
|
|
id=item.id,
|
|
name=item.name,
|
|
minimum=item.minimum,
|
|
maximum=item.maximum,
|
|
isActive=item.is_active,
|
|
menuCategories=[
|
|
schemas.MenuCategoryLink(
|
|
id=mc.id,
|
|
name=mc.name,
|
|
enabled=False,
|
|
products=[
|
|
ProductLinkSchema(
|
|
id=p.product_id,
|
|
name=p.name,
|
|
enabled=True if p.product_id in products else False,
|
|
)
|
|
for p in mc.products
|
|
],
|
|
)
|
|
for mc in menu_categories
|
|
],
|
|
sortOrder=item.sort_order,
|
|
)
|
|
|
|
|
|
def modifier_category_blank(date_: date, db: Session) -> schemas.ModifierCategoryBlank:
|
|
menu_categories = (
|
|
db.execute(
|
|
select(MenuCategory)
|
|
.join(MenuCategory.products)
|
|
.where(
|
|
and_(
|
|
or_(
|
|
ProductVersion.valid_from == None, # noqa: E711
|
|
ProductVersion.valid_from <= date_,
|
|
),
|
|
or_(
|
|
ProductVersion.valid_till == None, # noqa: E711
|
|
ProductVersion.valid_till >= date_,
|
|
),
|
|
)
|
|
)
|
|
.order_by(MenuCategory.sort_order, ProductVersion.sort_order, ProductVersion.name)
|
|
.options(
|
|
joinedload(MenuCategory.products, innerjoin=True),
|
|
contains_eager(MenuCategory.products),
|
|
)
|
|
)
|
|
.unique()
|
|
.scalars()
|
|
.all()
|
|
)
|
|
return schemas.ModifierCategoryBlank(
|
|
name="",
|
|
minimum=0,
|
|
maximum=0,
|
|
isActive=True,
|
|
menuCategories=[
|
|
schemas.MenuCategoryLink(
|
|
id=mc.id,
|
|
name=mc.name,
|
|
enabled=False,
|
|
products=[ProductLinkSchema(id=p.product_id, name=p.name, enabled=False) for p in mc.products],
|
|
)
|
|
for mc in menu_categories
|
|
],
|
|
)
|
|
|
|
|
|
def add_products(modifier_category: ModifierCategory, menu_categories: list[schemas.MenuCategoryLink], db: Session):
|
|
for mc in menu_categories:
|
|
for p in mc.products:
|
|
old = next((x for x in modifier_category.products if x.id == p.id_), None)
|
|
if p.enabled and old is None:
|
|
product_object = db.execute(select(Product).where(Product.id == p.id_)).scalar_one()
|
|
modifier_category.products.append(product_object)
|
|
elif not p.enabled and old:
|
|
modifier_category.products.remove(old)
|