Pay-then-profile join desk with live card, S3 upload fallback, hydration-safe drafts; document on test9 README. Co-authored-by: Cursor <cursoragent@cursor.com>
296 lines
8.2 KiB
Python
296 lines
8.2 KiB
Python
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
|
|
from fastapi import APIRouter, HTTPException, Query
|
|
from pydantic import BaseModel, Field
|
|
|
|
from notify_svc import store
|
|
|
|
router = APIRouter(tags=["notify"])
|
|
|
|
|
|
class SubscriberIn(BaseModel):
|
|
subscriber_id: str = Field(min_length=1, max_length=120)
|
|
email: str | None = None
|
|
phone: str | None = None
|
|
first_name: str | None = None
|
|
last_name: str | None = None
|
|
avatar: str | None = None
|
|
locale: str | None = "zh"
|
|
data: dict[str, Any] | None = None
|
|
|
|
|
|
class TopicIn(BaseModel):
|
|
key: str = Field(min_length=1, max_length=80)
|
|
name: str = Field(default="", max_length=200)
|
|
|
|
|
|
class TopicMemberIn(BaseModel):
|
|
subscriber_id: str = Field(min_length=1, max_length=120)
|
|
|
|
|
|
class StepIn(BaseModel):
|
|
id: str | None = None
|
|
type: str = "in_app"
|
|
name: str = ""
|
|
position: float | None = None
|
|
active: bool = True
|
|
template: dict[str, Any] = Field(default_factory=dict)
|
|
filters: list[Any] = Field(default_factory=list)
|
|
|
|
|
|
class WorkflowIn(BaseModel):
|
|
workflow_id: str = Field(min_length=1, max_length=120)
|
|
name: str = Field(min_length=1, max_length=200)
|
|
description: str = ""
|
|
tags: list[str] = Field(default_factory=list)
|
|
steps: list[StepIn] = Field(default_factory=list)
|
|
|
|
|
|
class WorkflowPatch(BaseModel):
|
|
name: str | None = None
|
|
description: str | None = None
|
|
active: bool | None = None
|
|
tags: list[str] | None = None
|
|
steps: list[StepIn] | None = None
|
|
|
|
|
|
class TriggerIn(BaseModel):
|
|
workflow_id: str = Field(min_length=1, max_length=120)
|
|
subscriber_id: str | None = None
|
|
topic_key: str | None = None
|
|
payload: dict[str, Any] = Field(default_factory=dict)
|
|
|
|
|
|
class PrefsIn(BaseModel):
|
|
channels: dict[str, Any] = Field(default_factory=dict)
|
|
|
|
|
|
class MessagePatch(BaseModel):
|
|
read: bool | None = None
|
|
archived: bool | None = None
|
|
deleted: bool | None = None
|
|
|
|
|
|
class ReadAllIn(BaseModel):
|
|
subscriber_id: str = "guest"
|
|
category: str | None = None
|
|
|
|
|
|
@router.get("/health")
|
|
def health():
|
|
return {"ok": True, "service": "nomadro-notify", "inspiredBy": ["novu"]}
|
|
|
|
|
|
@router.get("/subscribers")
|
|
def subscribers():
|
|
return {"ok": True, "subscribers": store.list_subscribers()}
|
|
|
|
|
|
@router.get("/subscribers/{subscriber_id}")
|
|
def subscriber(subscriber_id: str):
|
|
s = store.get_subscriber(subscriber_id)
|
|
if not s:
|
|
raise HTTPException(404, "subscriber_not_found")
|
|
return {"ok": True, "subscriber": s}
|
|
|
|
|
|
@router.post("/subscribers")
|
|
def create_subscriber(body: SubscriberIn):
|
|
try:
|
|
s = store.upsert_subscriber(body.model_dump())
|
|
except ValueError as e:
|
|
raise HTTPException(400, str(e)) from e
|
|
return {"ok": True, "subscriber": s}
|
|
|
|
|
|
@router.put("/subscribers/{subscriber_id}")
|
|
def update_subscriber(subscriber_id: str, body: SubscriberIn):
|
|
data = body.model_dump()
|
|
data["subscriber_id"] = subscriber_id
|
|
try:
|
|
s = store.upsert_subscriber(data)
|
|
except ValueError as e:
|
|
raise HTTPException(400, str(e)) from e
|
|
return {"ok": True, "subscriber": s}
|
|
|
|
|
|
@router.delete("/subscribers/{subscriber_id}")
|
|
def remove_subscriber(subscriber_id: str):
|
|
if not store.delete_subscriber(subscriber_id):
|
|
raise HTTPException(404, "subscriber_not_found")
|
|
return {"ok": True}
|
|
|
|
|
|
@router.get("/subscribers/{subscriber_id}/preferences")
|
|
def get_prefs(subscriber_id: str):
|
|
p = store.get_preferences(subscriber_id)
|
|
if not p:
|
|
raise HTTPException(404, "subscriber_not_found")
|
|
return {"ok": True, "preferences": p}
|
|
|
|
|
|
@router.put("/subscribers/{subscriber_id}/preferences")
|
|
def put_prefs(subscriber_id: str, body: PrefsIn):
|
|
try:
|
|
p = store.set_preferences(subscriber_id, body.channels)
|
|
except ValueError as e:
|
|
raise HTTPException(404, str(e)) from e
|
|
return {"ok": True, "preferences": p}
|
|
|
|
|
|
@router.get("/topics")
|
|
def topics():
|
|
return {"ok": True, "topics": store.list_topics()}
|
|
|
|
|
|
@router.post("/topics")
|
|
def create_topic(body: TopicIn):
|
|
try:
|
|
t = store.create_topic(body.key, body.name)
|
|
except ValueError as e:
|
|
raise HTTPException(400, str(e)) from e
|
|
return {"ok": True, "topic": t}
|
|
|
|
|
|
@router.post("/topics/{topic_key}/subscribers")
|
|
def topic_add(topic_key: str, body: TopicMemberIn):
|
|
try:
|
|
t = store.add_topic_subscriber(topic_key, body.subscriber_id)
|
|
except ValueError as e:
|
|
raise HTTPException(400, str(e)) from e
|
|
return {"ok": True, "topic": t}
|
|
|
|
|
|
@router.delete("/topics/{topic_key}/subscribers/{subscriber_id}")
|
|
def topic_remove(topic_key: str, subscriber_id: str):
|
|
if not store.remove_topic_subscriber(topic_key, subscriber_id):
|
|
raise HTTPException(404, "not_found")
|
|
return {"ok": True}
|
|
|
|
|
|
@router.get("/workflows")
|
|
def workflows():
|
|
return {"ok": True, "workflows": store.list_workflows()}
|
|
|
|
|
|
@router.get("/workflows/{workflow_id}")
|
|
def workflow(workflow_id: str):
|
|
w = store.get_workflow(workflow_id)
|
|
if not w:
|
|
raise HTTPException(404, "workflow_not_found")
|
|
return {"ok": True, "workflow": w}
|
|
|
|
|
|
@router.post("/workflows")
|
|
def create_workflow(body: WorkflowIn):
|
|
try:
|
|
w = store.create_workflow(
|
|
body.workflow_id,
|
|
body.name,
|
|
body.description,
|
|
[s.model_dump() for s in body.steps] if body.steps else None,
|
|
body.tags,
|
|
)
|
|
except ValueError as e:
|
|
raise HTTPException(400, str(e)) from e
|
|
return {"ok": True, "workflow": w}
|
|
|
|
|
|
@router.patch("/workflows/{workflow_id}")
|
|
def patch_workflow(workflow_id: str, body: WorkflowPatch):
|
|
patch = body.model_dump(exclude_unset=True)
|
|
if "steps" in patch and patch["steps"] is not None:
|
|
patch["steps"] = [s if isinstance(s, dict) else s for s in patch["steps"]]
|
|
w = store.update_workflow(workflow_id, patch)
|
|
if not w:
|
|
raise HTTPException(404, "workflow_not_found")
|
|
return {"ok": True, "workflow": w}
|
|
|
|
|
|
@router.delete("/workflows/{workflow_id}")
|
|
def remove_workflow(workflow_id: str):
|
|
if not store.delete_workflow(workflow_id):
|
|
raise HTTPException(404, "workflow_not_found")
|
|
return {"ok": True}
|
|
|
|
|
|
@router.post("/events/trigger")
|
|
def trigger(body: TriggerIn):
|
|
try:
|
|
result = store.trigger_event(
|
|
workflow_id=body.workflow_id,
|
|
subscriber_id=body.subscriber_id,
|
|
topic_key=body.topic_key,
|
|
payload=body.payload,
|
|
)
|
|
except ValueError as e:
|
|
raise HTTPException(400, str(e)) from e
|
|
return {"ok": True, **result}
|
|
|
|
|
|
@router.get("/inbox")
|
|
def inbox(
|
|
subscriber_id: str = Query(default="guest"),
|
|
include_archived: bool = False,
|
|
category: str | None = Query(default=None),
|
|
):
|
|
return {
|
|
"ok": True,
|
|
"subscriber_id": subscriber_id,
|
|
"unread": store.unread_count(subscriber_id),
|
|
"unread_by_category": store.unread_by_category(subscriber_id),
|
|
"messages": store.list_inbox(
|
|
subscriber_id,
|
|
include_archived=include_archived,
|
|
category=category,
|
|
),
|
|
}
|
|
|
|
|
|
@router.get("/inbox/unread")
|
|
def inbox_unread(subscriber_id: str = Query(default="guest")):
|
|
return {
|
|
"ok": True,
|
|
"subscriber_id": subscriber_id,
|
|
"unread": store.unread_count(subscriber_id),
|
|
"unread_by_category": store.unread_by_category(subscriber_id),
|
|
}
|
|
|
|
|
|
@router.post("/inbox/read-all")
|
|
def inbox_read_all(body: ReadAllIn):
|
|
n = store.mark_all_read(body.subscriber_id, body.category)
|
|
return {
|
|
"ok": True,
|
|
"updated": n,
|
|
"unread": store.unread_count(body.subscriber_id),
|
|
"unread_by_category": store.unread_by_category(body.subscriber_id),
|
|
}
|
|
|
|
|
|
@router.patch("/inbox/{message_id}")
|
|
def patch_inbox(message_id: str, body: MessagePatch):
|
|
m = store.patch_message(message_id, body.model_dump(exclude_unset=True))
|
|
if not m:
|
|
raise HTTPException(404, "message_not_found")
|
|
return {"ok": True, "message": m}
|
|
|
|
|
|
@router.delete("/inbox/{message_id}")
|
|
def delete_inbox(message_id: str):
|
|
if not store.delete_message(message_id):
|
|
raise HTTPException(404, "message_not_found")
|
|
return {"ok": True}
|
|
|
|
|
|
@router.get("/activity")
|
|
def activity(limit: int = Query(default=50, ge=1, le=200)):
|
|
return {"ok": True, "activity": store.list_activity(limit)}
|
|
|
|
|
|
@router.get("/integrations")
|
|
def integrations():
|
|
return {"ok": True, "integrations": store.list_integrations()}
|