Platform v2 step 1.1 (APPS_PLATFORM D-11, §7, D-24). - core/timeutil.py: utcnow, venue_tz (venue.timezone, cached, invalidated on change - no restart), to_venue/venue_date/venue_strftime, parse_api_datetime (offset -> UTC; naive = venue-local; date = venue midnight), business_date_of /business_date_window (rollover hour, default 06:00), LocalClock + is_within, UTCDateTime column type (naive writes raise, reads are aware UTC), iso_z - every model column is UTCDateTime; Pydantic UTCDatetime emits "...Z" - business_days.business_date set at open; revision 0002 adds it (backfilled) and makes sync_events.created_at a UTCDateTime (AUTOINCREMENT restated) - tz.py deleted; system.timezone setting replaced by venue.timezone (validated) + business_day.rollover_hour; manager timezone picker writes venue.timezone - fixes: price-group schedules/time_range used the container's zone (happy hour bug); traffic report bucketed by UTC hour; reports' "date" meant the UTC calendar day, now the business date; reservations stored venue-local naive times (no-show check 2-3 h off) and listed by func.date; schedule week used UTC midnights; fiscal file names used container-local time - seed.py/demo_seed.py create store_manager with role permissions (regression from step 1.0: v1's startup ALTER list used to rename/grant role 'manager') - tzdata dependency; guard tests/guards/test_no_naive_time.py (AST scan); tests/test_timeutil.py covers the §7.3 DST cases and the API checks Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
104 lines
4.9 KiB
Python
104 lines
4.9 KiB
Python
from sqlalchemy import Column, Integer, String, Boolean, Float, ForeignKey, Text
|
|
from core.timeutil import UTCDateTime, utcnow
|
|
from sqlalchemy.orm import relationship
|
|
from database import Base
|
|
|
|
|
|
|
|
|
|
class User(Base):
|
|
__tablename__ = "users"
|
|
|
|
id = Column(Integer, primary_key=True, index=True)
|
|
username = Column(String, unique=True, nullable=False, index=True)
|
|
pin_hash = Column(String, nullable=False)
|
|
password_hash = Column(String, nullable=True)
|
|
email = Column(String, nullable=True)
|
|
# Role is a label + default-permission template. Valid values: see roles.py
|
|
role = Column(String, nullable=False)
|
|
is_active = Column(Boolean, default=True, nullable=False)
|
|
full_name = Column(String, nullable=True)
|
|
nickname = Column(String, nullable=True)
|
|
mobile_phone = Column(String, nullable=True)
|
|
note = Column(String, nullable=True)
|
|
avatar_url = Column(String, nullable=True)
|
|
created_at = Column(UTCDateTime, default=utcnow)
|
|
# Phase 2B — payroll
|
|
hourly_rate = Column(Float, nullable=True)
|
|
|
|
# ── Access permissions (which sites/areas this user can enter) ────────────
|
|
perm_access_dashboard = Column(Boolean, default=False, nullable=False)
|
|
perm_access_waiter_app = Column(Boolean, default=True, nullable=False)
|
|
perm_access_kds = Column(Boolean, default=False, nullable=False)
|
|
|
|
# ── Order action permissions ──────────────────────────────────────────────
|
|
perm_cancel_orders = Column(Boolean, default=False, nullable=False)
|
|
perm_apply_discounts = Column(Boolean, default=False, nullable=False)
|
|
perm_modify_prices = Column(Boolean, default=False, nullable=False)
|
|
perm_open_orders = Column(Boolean, default=True, nullable=False)
|
|
perm_close_orders = Column(Boolean, default=True, nullable=False)
|
|
|
|
# ── Management permissions ────────────────────────────────────────────────
|
|
perm_view_reports = Column(Boolean, default=False, nullable=False)
|
|
perm_manage_staff = Column(Boolean, default=False, nullable=False)
|
|
perm_manage_tables = Column(Boolean, default=False, nullable=False)
|
|
perm_manage_menu = Column(Boolean, default=False, nullable=False)
|
|
perm_manage_settings = Column(Boolean, default=False, nullable=False)
|
|
|
|
# Per-waiter app settings + favorites (JSON blob, synced from PWA)
|
|
waiter_settings = Column(Text, nullable=True)
|
|
|
|
orders_opened = relationship("Order", foreign_keys="Order.opened_by", back_populates="opener")
|
|
orders_closed = relationship("Order", foreign_keys="Order.closed_by", back_populates="closer")
|
|
order_items = relationship("OrderItem", foreign_keys="OrderItem.added_by", back_populates="added_by_user")
|
|
items_paid = relationship("OrderItem", foreign_keys="OrderItem.paid_by", back_populates="paid_by_user")
|
|
order_assignments = relationship("OrderWaiter", back_populates="waiter")
|
|
zone_assignments = relationship("WaiterZone", back_populates="waiter", cascade="all, delete-orphan")
|
|
|
|
primary_assignments = relationship(
|
|
"AssistantAssignment",
|
|
foreign_keys="AssistantAssignment.primary_waiter_id",
|
|
back_populates="primary_waiter",
|
|
)
|
|
assistant_assignments = relationship(
|
|
"AssistantAssignment",
|
|
foreign_keys="AssistantAssignment.assistant_waiter_id",
|
|
back_populates="assistant_waiter",
|
|
)
|
|
|
|
@property
|
|
def can_cancel_orders(self):
|
|
"""Back-compat alias used by orders router."""
|
|
return self.perm_cancel_orders
|
|
|
|
@property
|
|
def is_superadmin(self):
|
|
return self.role == "superadmin"
|
|
|
|
|
|
class WaiterZone(Base):
|
|
"""Maps a waiter to a table group they are allowed to operate in.
|
|
If a waiter has NO rows here, they see NOTHING.
|
|
A sentinel row with group_id=NULL means 'all zones'."""
|
|
__tablename__ = "waiter_zones"
|
|
|
|
id = Column(Integer, primary_key=True, index=True)
|
|
waiter_id = Column(Integer, ForeignKey("users.id"), nullable=False)
|
|
group_id = Column(Integer, ForeignKey("table_groups.id"), nullable=True) # NULL = all zones
|
|
assigned_at = Column(UTCDateTime, default=utcnow)
|
|
|
|
waiter = relationship("User", back_populates="zone_assignments")
|
|
group = relationship("TableGroup", back_populates="waiter_zones")
|
|
|
|
|
|
class AssistantAssignment(Base):
|
|
__tablename__ = "assistant_assignments"
|
|
|
|
id = Column(Integer, primary_key=True, index=True)
|
|
primary_waiter_id = Column(Integer, ForeignKey("users.id"), nullable=False)
|
|
assistant_waiter_id = Column(Integer, ForeignKey("users.id"), nullable=False)
|
|
assigned_at = Column(UTCDateTime, default=utcnow)
|
|
|
|
primary_waiter = relationship("User", foreign_keys=[primary_waiter_id], back_populates="primary_assignments")
|
|
assistant_waiter = relationship("User", foreign_keys=[assistant_waiter_id], back_populates="assistant_assignments")
|