feat: staff and the schedule on v2; managers give staff passwords (platform-v2 step 6.11, KI-014)
- staff: PUT /api/waiters/{id}/password (set, change, or take away; 6+ chars)
so the password sign-in mode works for everyone; PUT /{id}/pin with the PIN in
the body (was in the address: reset-pin?pin= removed); PINs 4-6 digits when
adding; a password can be given when adding; has_password on staff; editing
clears emptied fields and keeps usernames unique; delete only without history
(orders, items, shifts) and never yourself
- schedule: times checked (schedule.bad_time), one person's overlapping shifts
refused (schedule.overlap, past midnight understood), everyone active can be
scheduled (the kitchen too), changes need core.staff.manage
- manager: src/pages/staff (list with search and filters, person dialog with
photo, PIN / password dialog, zones) and src/pages/schedule (by person, by
hour with opening hours, shift dialog with clash check, view settings kept in
the browser); the never-used "automatic start/close" switches dropped; v1
StaffTab and SchedulePage removed; ApprovalsPage loads with useLiveQuery
- tests: test_staff_v2.py (done-when on a paired screen), staff.test.mjs, e2e
manager.mjs (add, password, sign-in on a paired till, a shift and a clash)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -39,6 +39,21 @@ def _guard_superadmin_target(target: User, acting_user: User):
|
||||
raise XeniaError("staff.superadmin_protected")
|
||||
|
||||
|
||||
PIN_LEN = (4, 6)
|
||||
PASSWORD_MIN = 6
|
||||
|
||||
|
||||
def _check_pin(pin: str) -> str:
|
||||
"""A PIN is 4 to 6 digits (the phones' and screens' PIN pad)."""
|
||||
if not isinstance(pin, str) or not pin.isdigit() or not PIN_LEN[0] <= len(pin) <= PIN_LEN[1]:
|
||||
raise XeniaError("staff.pin_invalid", min=PIN_LEN[0], max=PIN_LEN[1])
|
||||
return pin
|
||||
|
||||
|
||||
def _hash(secret: str) -> str:
|
||||
return bcrypt.hashpw(secret.encode(), bcrypt.gensalt()).decode()
|
||||
|
||||
|
||||
def _guard_grant(acting_user: User, keys) -> None:
|
||||
"""A non-superadmin may only give keys they hold themselves (no self-escalation via staff)."""
|
||||
if acting_user.role == "superadmin":
|
||||
@@ -152,10 +167,13 @@ def create_staff(body: UserCreate, db: Session = Depends(get_db), user: User = D
|
||||
if db.query(User).filter(User.username == body.username).first():
|
||||
raise XeniaError("staff.username_taken", status=400)
|
||||
|
||||
pin_hash = bcrypt.hashpw(body.pin.encode(), bcrypt.gensalt()).decode()
|
||||
pin_hash = _hash(_check_pin(body.pin))
|
||||
if body.password is not None and len(body.password) < PASSWORD_MIN:
|
||||
raise XeniaError("auth.password_too_short", min=PASSWORD_MIN)
|
||||
new_user = User(
|
||||
username=body.username,
|
||||
pin_hash=pin_hash,
|
||||
password_hash=_hash(body.password) if body.password else None, # 6.11: for the password sign-in mode
|
||||
role=body.role,
|
||||
is_active=body.is_active,
|
||||
full_name=body.full_name,
|
||||
@@ -192,8 +210,18 @@ def update_staff(waiter_id: int, body: UserUpdate, db: Session = Depends(get_db)
|
||||
|
||||
if body.language is not None:
|
||||
body.language = normalize_language(body.language) or ""
|
||||
data = body.model_dump(exclude_none=True)
|
||||
# 6.11: fields sent as null are cleared (phone, email, note, nickname, pay); the required
|
||||
# ones (username, role, active) are only changed when given
|
||||
data = body.model_dump(exclude_unset=True)
|
||||
for field, value in data.items():
|
||||
if value is None and field in ("username", "role", "is_active", "language"):
|
||||
continue
|
||||
if field == "username":
|
||||
value = value.strip()
|
||||
if not value:
|
||||
raise XeniaError("staff.username_needed")
|
||||
if db.query(User).filter(User.username == value, User.id != target.id).first():
|
||||
raise XeniaError("staff.username_taken", status=409)
|
||||
setattr(target, field, value)
|
||||
if target.language == "":
|
||||
target.language = None # "" means inherit
|
||||
@@ -203,13 +231,40 @@ def update_staff(waiter_id: int, body: UserUpdate, db: Session = Depends(get_db)
|
||||
return target
|
||||
|
||||
|
||||
@router.put("/{waiter_id}/reset-pin")
|
||||
def reset_pin(waiter_id: int, pin: str, db: Session = Depends(get_db), user: User = Depends(require_staff_manager)):
|
||||
class PinBody(BaseModel):
|
||||
pin: str
|
||||
|
||||
|
||||
class PasswordBody(BaseModel):
|
||||
password: str | None = None # None = take the password away (PIN sign-in only)
|
||||
|
||||
|
||||
@router.put("/{waiter_id}/pin")
|
||||
def set_pin(waiter_id: int, body: PinBody, db: Session = Depends(get_db), user: User = Depends(require_staff_manager)):
|
||||
"""A new PIN for someone (6.11: in the body, never in the address; 4-6 digits)."""
|
||||
target = _user_or_404(waiter_id, db)
|
||||
_guard_superadmin_target(target, user)
|
||||
target.pin_hash = bcrypt.hashpw(pin.encode(), bcrypt.gensalt()).decode()
|
||||
target.pin_hash = _hash(_check_pin(body.pin))
|
||||
db.commit()
|
||||
return {"status": "pin reset"}
|
||||
return {"ok": True}
|
||||
|
||||
|
||||
@router.put("/{waiter_id}/password")
|
||||
def set_password(waiter_id: int, body: PasswordBody, db: Session = Depends(get_db), user: User = Depends(require_staff_manager)):
|
||||
"""Give someone a password, change it, or take it away (6.11, KI-014): what the password
|
||||
sign-in mode (phones, paired screens, the manager) checks. Their open sessions stay."""
|
||||
target = _user_or_404(waiter_id, db)
|
||||
_guard_superadmin_target(target, user)
|
||||
if body.password is None:
|
||||
if target.id == user.id:
|
||||
raise XeniaError("staff.password_own") # your own: Profile → change password
|
||||
target.password_hash = None
|
||||
else:
|
||||
if len(body.password) < PASSWORD_MIN:
|
||||
raise XeniaError("auth.password_too_short", min=PASSWORD_MIN)
|
||||
target.password_hash = _hash(body.password)
|
||||
db.commit()
|
||||
return {"ok": True, "has_password": target.password_hash is not None}
|
||||
|
||||
|
||||
@router.put("/{waiter_id}/block")
|
||||
@@ -226,8 +281,17 @@ def toggle_block(waiter_id: int, db: Session = Depends(get_db), user: User = Dep
|
||||
|
||||
@router.delete("/{waiter_id}", status_code=status.HTTP_204_NO_CONTENT)
|
||||
def delete_staff(waiter_id: int, db: Session = Depends(get_db), user: User = Depends(require_staff_manager)):
|
||||
"""Only someone with no history (no orders, items or shifts): the reports keep who did
|
||||
what, so anyone else is blocked instead (6.11)."""
|
||||
from models.order import Order, OrderItem
|
||||
target = _user_or_404(waiter_id, db)
|
||||
_guard_superadmin_target(target, user)
|
||||
if target.id == user.id:
|
||||
raise XeniaError("staff.delete_self")
|
||||
if (db.query(Order.id).filter(Order.opened_by == target.id).first() or db.query(OrderItem.id).filter(OrderItem.added_by == target.id).first()
|
||||
or db.query(WaiterShift.id).filter(WaiterShift.waiter_id == target.id).first()):
|
||||
raise XeniaError("staff.has_history", name=target.full_name or target.username)
|
||||
db.query(WaiterZone).filter(WaiterZone.waiter_id == target.id).delete()
|
||||
db.delete(target)
|
||||
db.commit()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user