31 lines
890 B
Python
31 lines
890 B
Python
from sqlmodel import Session, select
|
|
|
|
from app.models import Dish, DishShare
|
|
|
|
|
|
def dish_for_barcode(session: Session, barcode: str) -> Dish | None:
|
|
if not barcode.startswith("dish-"):
|
|
return None
|
|
return session.exec(select(Dish).where(Dish.product_barcode == barcode)).first()
|
|
|
|
|
|
def user_can_access_dish(session: Session, dish: Dish, user_id: int) -> bool:
|
|
if dish.user_id == user_id:
|
|
return True
|
|
return (
|
|
session.exec(
|
|
select(DishShare).where(
|
|
DishShare.dish_id == dish.id,
|
|
DishShare.shared_with_user_id == user_id,
|
|
)
|
|
).first()
|
|
is not None
|
|
)
|
|
|
|
|
|
def product_barcode_accessible(session: Session, barcode: str, user_id: int) -> bool:
|
|
dish = dish_for_barcode(session, barcode)
|
|
if dish is None:
|
|
return True
|
|
return user_can_access_dish(session, dish, user_id)
|