- Added new API endpoints for admin features: `GET /api/admin/me`, `GET /api/admin/users`, and `PATCH /api/admin/duties/:id` to manage user duties. - Introduced `UserForAdmin` and `AdminDutyReassignBody` schemas for handling admin-related data. - Updated documentation to include Mini App design guidelines and admin panel functionalities. - Enhanced tests for admin API to ensure proper access control and functionality. - Improved error handling and localization for admin actions.
92 lines
2.2 KiB
Python
92 lines
2.2 KiB
Python
"""Pydantic schemas for API request/response and validation."""
|
|
|
|
from typing import Literal
|
|
|
|
from pydantic import BaseModel, ConfigDict
|
|
|
|
# Allowed duty event types; API maps unknown DB values to "duty".
|
|
DUTY_EVENT_TYPES = ("duty", "unavailable", "vacation")
|
|
|
|
|
|
class UserBase(BaseModel):
|
|
"""Base user fields (full_name, username, first/last name)."""
|
|
|
|
full_name: str
|
|
username: str | None = None
|
|
first_name: str | None = None
|
|
last_name: str | None = None
|
|
|
|
|
|
class UserCreate(UserBase):
|
|
"""User creation payload including Telegram user id."""
|
|
|
|
telegram_user_id: int
|
|
|
|
|
|
class UserInDb(UserBase):
|
|
"""User as stored in DB (includes id and telegram_user_id)."""
|
|
|
|
id: int
|
|
telegram_user_id: int
|
|
|
|
model_config = ConfigDict(from_attributes=True)
|
|
|
|
|
|
class DutyBase(BaseModel):
|
|
"""Duty fields: user_id, start_at, end_at (UTC ISO 8601 with Z)."""
|
|
|
|
user_id: int
|
|
start_at: str # UTC, ISO 8601 with Z
|
|
end_at: str # UTC, ISO 8601 with Z
|
|
|
|
|
|
class DutyCreate(DutyBase):
|
|
"""Duty creation payload."""
|
|
|
|
pass
|
|
|
|
|
|
class DutyInDb(DutyBase):
|
|
"""Duty as stored in DB (includes id)."""
|
|
|
|
id: int
|
|
|
|
model_config = ConfigDict(from_attributes=True)
|
|
|
|
|
|
class DutyWithUser(DutyInDb):
|
|
"""Duty with full_name, event_type, and optional contact fields for calendar display.
|
|
|
|
event_type: only these values are returned; unknown DB values are mapped to "duty" in the API.
|
|
phone and username are exposed only to authenticated Mini App users (role-gated).
|
|
"""
|
|
|
|
full_name: str
|
|
event_type: Literal["duty", "unavailable", "vacation"] = "duty"
|
|
phone: str | None = None
|
|
username: str | None = None
|
|
|
|
model_config = ConfigDict(from_attributes=True)
|
|
|
|
|
|
class UserForAdmin(BaseModel):
|
|
"""User summary for admin dropdown: id, full_name, username, role_id."""
|
|
|
|
id: int
|
|
full_name: str
|
|
username: str | None = None
|
|
role_id: int | None = None
|
|
|
|
|
|
class AdminDutyReassignBody(BaseModel):
|
|
"""Request body for PATCH /api/admin/duties/:id — reassign duty to another user."""
|
|
|
|
user_id: int
|
|
|
|
|
|
class CalendarEvent(BaseModel):
|
|
"""External calendar event (e.g. holiday) for a single day."""
|
|
|
|
date: str # YYYY-MM-DD
|
|
summary: str
|