Fix category data missing id & bad owner id

This commit is contained in:
Peter Vacho 2024-12-29 17:28:59 +01:00
parent 187a04e0fc
commit e225151c48
Signed by: school
GPG key ID: 8CFC3837052871B4

View file

@ -10,7 +10,7 @@ from src.api.auth.dependencies import LoggedInDep
from src.db.models.category import Category
from src.db.models.event import Event
from src.db.models.user import User
from src.utils.db import MissingIdError, expr, update_document
from src.utils.db import MissingIdError, UnfetchedLinkError, expr, update_document
from src.utils.logging import get_logger
from .auth import CurrentUserDep
@ -42,6 +42,7 @@ class _BaseCategoryData(BaseModel):
class CategoryData(_BaseCategoryData):
"""Data about a category sent to the user."""
id: PydanticObjectId
owner_user_id: PydanticObjectId
created_at: datetime
@ -51,10 +52,16 @@ class CategoryData(_BaseCategoryData):
if category.id is None:
raise MissingIdError(category)
if isinstance(category.user, Link):
raise UnfetchedLinkError(category.user)
if category.user.id is None:
raise MissingIdError(category.user)
return cls(
id=category.id,
name=category.name,
color=Color(category.color),
owner_user_id=category.id,
owner_user_id=category.user.id,
created_at=category.created_at,
)