admin.nomadro.com gets sidebar L1/L2 social ops (paginated lists + entity detail); API adds soft-delete archives/stats and shared nomadro_pb for social/coffee/notify/work. Co-authored-by: Cursor <cursoragent@cursor.com>
157 lines
4.4 KiB
Python
157 lines
4.4 KiB
Python
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
|
|
from fastapi import APIRouter, HTTPException, Query
|
|
from pydantic import BaseModel, Field
|
|
|
|
from coffee_svc import store
|
|
|
|
router = APIRouter(tags=["coffee"])
|
|
|
|
|
|
class SchedulePatch(BaseModel):
|
|
availability: list[dict[str, Any]] | None = None
|
|
timezone: str | None = None
|
|
name: str | None = None
|
|
|
|
|
|
class EventTypeIn(BaseModel):
|
|
slug: str = Field(min_length=1, max_length=80)
|
|
title: str = Field(min_length=1, max_length=200)
|
|
description: str = ""
|
|
length_minutes: int = Field(default=30, ge=5, le=480)
|
|
slot_interval: int = Field(default=15, ge=5, le=60)
|
|
locations: list[dict[str, Any]] = Field(default_factory=list)
|
|
active: bool = True
|
|
buffer_before: int = 0
|
|
buffer_after: int = 0
|
|
|
|
|
|
class BookingIn(BaseModel):
|
|
username: str = Field(min_length=1, max_length=80)
|
|
event_slug: str = Field(min_length=1, max_length=80)
|
|
start: str = Field(min_length=1, max_length=40)
|
|
attendee_name: str = Field(min_length=1, max_length=120)
|
|
attendee_email: str | None = None
|
|
attendee_device: str | None = None
|
|
location: str | None = None
|
|
notes: str = ""
|
|
|
|
|
|
@router.get("/health")
|
|
def health():
|
|
return {"ok": True, "service": "nomadro-coffee", "inspiredBy": ["cal.com"]}
|
|
|
|
|
|
@router.get("/hosts")
|
|
def hosts():
|
|
return {"ok": True, "hosts": store.list_hosts()}
|
|
|
|
|
|
@router.get("/hosts/{username}")
|
|
def host(username: str):
|
|
h = store.get_host(username)
|
|
if not h:
|
|
raise HTTPException(404, "host_not_found")
|
|
return {"ok": True, "host": h}
|
|
|
|
|
|
@router.get("/hosts/{username}/schedule")
|
|
def schedule(username: str):
|
|
s = store.get_schedule(username)
|
|
if not s:
|
|
raise HTTPException(404, "schedule_not_found")
|
|
return {"ok": True, "schedule": s}
|
|
|
|
|
|
@router.patch("/hosts/{username}/schedule")
|
|
def patch_schedule(username: str, body: SchedulePatch):
|
|
try:
|
|
s = store.update_schedule(
|
|
username,
|
|
availability=body.availability,
|
|
timezone_name=body.timezone,
|
|
name=body.name,
|
|
)
|
|
except ValueError as e:
|
|
raise HTTPException(400, str(e)) from e
|
|
return {"ok": True, "schedule": s}
|
|
|
|
|
|
@router.get("/hosts/{username}/event-types/{slug}")
|
|
def event_type(username: str, slug: str):
|
|
e = store.get_event_type(username, slug)
|
|
if not e:
|
|
raise HTTPException(404, "event_type_not_found")
|
|
return {"ok": True, "event_type": e}
|
|
|
|
|
|
@router.post("/hosts/{username}/event-types")
|
|
def create_event_type(username: str, body: EventTypeIn):
|
|
try:
|
|
e = store.upsert_event_type(username, body.model_dump())
|
|
except ValueError as e:
|
|
raise HTTPException(400, str(e)) from e
|
|
return {"ok": True, "event_type": e}
|
|
|
|
|
|
@router.get("/hosts/{username}/event-types/{slug}/slots")
|
|
def slots(
|
|
username: str,
|
|
slug: str,
|
|
day: str = Query(..., description="YYYY-MM-DD in host local timezone"),
|
|
):
|
|
try:
|
|
return store.available_slots(username, slug, day)
|
|
except ValueError as e:
|
|
raise HTTPException(400, str(e)) from e
|
|
|
|
|
|
@router.get("/bookings")
|
|
def bookings(
|
|
host: str | None = Query(default=None),
|
|
status: str | None = Query(default=None),
|
|
):
|
|
return {"ok": True, "bookings": store.list_bookings(host_username=host, status=status)}
|
|
|
|
|
|
@router.get("/bookings/{booking_id}")
|
|
def booking(booking_id: str):
|
|
b = store.get_booking(booking_id)
|
|
if not b:
|
|
raise HTTPException(404, "booking_not_found")
|
|
return {"ok": True, "booking": b}
|
|
|
|
|
|
@router.post("/bookings")
|
|
def book(body: BookingIn):
|
|
try:
|
|
b = store.create_booking(body.model_dump())
|
|
except ValueError as e:
|
|
code = 409 if str(e) == "slot_unavailable" else 400
|
|
raise HTTPException(code, str(e)) from e
|
|
# Notify host (subscriber = host_id) — best-effort
|
|
try:
|
|
from social_svc.notify_bridge import notify_coffee_invite
|
|
|
|
host = b.get("host") or {}
|
|
to_id = str(b.get("host_id") or host.get("username") or "")
|
|
if to_id:
|
|
notify_coffee_invite(
|
|
to_id,
|
|
str(body.attendee_name or "游民"),
|
|
str(b.get("title") or "咖啡聊"),
|
|
)
|
|
except Exception:
|
|
pass
|
|
return {"ok": True, "booking": b}
|
|
|
|
|
|
@router.post("/bookings/{booking_id}/cancel")
|
|
def cancel(booking_id: str):
|
|
b = store.cancel_booking(booking_id)
|
|
if not b:
|
|
raise HTTPException(404, "booking_not_found")
|
|
return {"ok": True, "booking": b}
|