BE-CAL-004: implement MinimumWorkload storage
- New model: minimum_workloads table with JSON config column (per-user) - Schemas: MinimumWorkloadConfig, MinimumWorkloadUpdate, MinimumWorkloadResponse - Service: CRUD operations + check_workload_warnings() entry point for BE-CAL-007 - API: GET/PUT/PATCH /calendar/workload-config (self + admin routes) - Migration: auto-create minimum_workloads table on startup - Registered calendar router in main.py
This commit is contained in:
66
app/models/minimum_workload.py
Normal file
66
app/models/minimum_workload.py
Normal file
@@ -0,0 +1,66 @@
|
||||
"""MinimumWorkload model — per-user workload threshold configuration.
|
||||
|
||||
Stores the minimum expected workload (in minutes) across four periods
|
||||
(daily / weekly / monthly / yearly) and three slot categories
|
||||
(work / on_call / entertainment). Values are advisory: when a
|
||||
calendar submission would leave the user below these thresholds, the
|
||||
system returns a *warning* but does not block the operation.
|
||||
|
||||
Storage decision (BE-CAL-004): independent table with a JSON column.
|
||||
This keeps the User model clean while giving each user exactly one
|
||||
configuration row. The JSON structure matches the design document:
|
||||
|
||||
{
|
||||
"daily": {"work": 0, "on_call": 0, "entertainment": 0},
|
||||
"weekly": {"work": 0, "on_call": 0, "entertainment": 0},
|
||||
"monthly": {"work": 0, "on_call": 0, "entertainment": 0},
|
||||
"yearly": {"work": 0, "on_call": 0, "entertainment": 0}
|
||||
}
|
||||
|
||||
All values are minutes in range [0, 65535].
|
||||
"""
|
||||
|
||||
from sqlalchemy import Column, Integer, ForeignKey, JSON, DateTime
|
||||
from sqlalchemy.orm import relationship
|
||||
from sqlalchemy.sql import func
|
||||
|
||||
from app.core.config import Base
|
||||
|
||||
|
||||
# Default configuration — all thresholds zeroed out (no warnings).
|
||||
DEFAULT_WORKLOAD_CONFIG: dict = {
|
||||
"daily": {"work": 0, "on_call": 0, "entertainment": 0},
|
||||
"weekly": {"work": 0, "on_call": 0, "entertainment": 0},
|
||||
"monthly": {"work": 0, "on_call": 0, "entertainment": 0},
|
||||
"yearly": {"work": 0, "on_call": 0, "entertainment": 0},
|
||||
}
|
||||
|
||||
PERIODS = ("daily", "weekly", "monthly", "yearly")
|
||||
CATEGORIES = ("work", "on_call", "entertainment")
|
||||
|
||||
|
||||
class MinimumWorkload(Base):
|
||||
"""Per-user minimum workload configuration."""
|
||||
|
||||
__tablename__ = "minimum_workloads"
|
||||
|
||||
id = Column(Integer, primary_key=True, index=True)
|
||||
|
||||
user_id = Column(
|
||||
Integer,
|
||||
ForeignKey("users.id"),
|
||||
nullable=False,
|
||||
unique=True,
|
||||
index=True,
|
||||
comment="One config row per user",
|
||||
)
|
||||
|
||||
config = Column(
|
||||
JSON,
|
||||
nullable=False,
|
||||
default=lambda: dict(DEFAULT_WORKLOAD_CONFIG),
|
||||
comment="Workload thresholds JSON — see module docstring for schema",
|
||||
)
|
||||
|
||||
created_at = Column(DateTime(timezone=True), server_default=func.now())
|
||||
updated_at = Column(DateTime(timezone=True), onupdate=func.now())
|
||||
Reference in New Issue
Block a user