- CRM -> Customers: search ignoring case and Greek accents (and part of the phone), visits / spent / last visit, the card (contact, notes, open tab, every visit with what was on a tab), new/edit/remove - Inventory -> Throwaways: log (product, quantity with a comma, reason, note) with its cost, today's list (delete today only), a period's totals by reason and product, every entry; Inventory -> Stock: an honest 'not available yet' - Notes: notes (pinned first, edit in place) and to-dos (urgent first, done folded away, who did it kept) - backend: crm.manage / inventory.manage / notes.use (the customer search also with the dashboard key, for orders); spending counted by services/sales.py (refunds off, tab lines in); null clears a field; names/notes not blank, priorities checked; waste ?date_from=&date_to= venue dates; the Greek reason labels left the API; live customers.changed / waste.changed / notes.changed - the overview's notes card needs notes.use and refreshes live - tests: test_customers_waste_notes.py (4), crm.test.mjs (3), e2e manager.mjs (customers, throwaways, notes, stock, Greek) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
175 lines
5.8 KiB
Python
175 lines
5.8 KiB
Python
from core.timeutil import utcnow
|
|
from datetime import datetime, timezone
|
|
from fastapi import APIRouter, Depends, status
|
|
from sqlalchemy.orm import Session
|
|
from typing import List
|
|
|
|
from database import get_db
|
|
from models.notes import SiteNote, SiteTodo
|
|
from models.user import User
|
|
from schemas.notes import NoteCreate, NoteUpdate, NoteOut, TodoCreate, TodoUpdate, TodoOut
|
|
from core.access.deps import require
|
|
from core.errors import XeniaError
|
|
from services.ws_bus import broadcast_sync
|
|
|
|
router = APIRouter()
|
|
|
|
USE = Depends(require("notes.use"))
|
|
PRIORITIES = ("normal", "high")
|
|
|
|
|
|
def _changed():
|
|
broadcast_sync("notes.changed", {})
|
|
|
|
|
|
def _text(body: str | None) -> str:
|
|
if not (body or "").strip():
|
|
raise XeniaError("validation.missing", field="body")
|
|
return body.strip()
|
|
|
|
|
|
|
|
|
|
def _note_out(n: SiteNote) -> dict:
|
|
name = None
|
|
if n.created_by:
|
|
name = n.created_by.full_name or n.created_by.username
|
|
return {
|
|
"id": n.id,
|
|
"body": n.body,
|
|
"is_pinned": n.is_pinned,
|
|
"created_by_id": n.created_by_id,
|
|
"created_by_name": name,
|
|
"created_at": n.created_at,
|
|
"updated_at": n.updated_at,
|
|
}
|
|
|
|
|
|
def _todo_out(t: SiteTodo) -> dict:
|
|
creator_name = None
|
|
if t.created_by:
|
|
creator_name = t.created_by.full_name or t.created_by.username
|
|
done_name = None
|
|
if t.done_by:
|
|
done_name = t.done_by.full_name or t.done_by.username
|
|
return {
|
|
"id": t.id,
|
|
"body": t.body,
|
|
"is_done": t.is_done,
|
|
"done_at": t.done_at,
|
|
"done_by_id": t.done_by_id,
|
|
"done_by_name": done_name,
|
|
"created_by_id": t.created_by_id,
|
|
"created_by_name": creator_name,
|
|
"created_at": t.created_at,
|
|
"priority": t.priority,
|
|
}
|
|
|
|
|
|
# ── Notes ─────────────────────────────────────────────────────────────────────
|
|
|
|
@router.get("/", response_model=List[NoteOut])
|
|
def list_notes(db: Session = Depends(get_db), user: User = USE):
|
|
notes = db.query(SiteNote).order_by(
|
|
SiteNote.is_pinned.desc(), SiteNote.updated_at.desc()
|
|
).all()
|
|
return [_note_out(n) for n in notes]
|
|
|
|
|
|
@router.post("/", response_model=NoteOut, status_code=status.HTTP_201_CREATED)
|
|
def create_note(body: NoteCreate, db: Session = Depends(get_db), user: User = USE):
|
|
note = SiteNote(body=_text(body.body), is_pinned=body.is_pinned, created_by_id=user.id)
|
|
db.add(note)
|
|
db.commit()
|
|
db.refresh(note)
|
|
_changed()
|
|
return _note_out(note)
|
|
|
|
|
|
@router.put("/{note_id}", response_model=NoteOut)
|
|
def update_note(note_id: int, body: NoteUpdate, db: Session = Depends(get_db), user: User = USE):
|
|
note = db.query(SiteNote).filter(SiteNote.id == note_id).first()
|
|
if not note:
|
|
raise XeniaError("note.not_found")
|
|
if body.body is not None:
|
|
note.body = _text(body.body)
|
|
if body.is_pinned is not None:
|
|
note.is_pinned = body.is_pinned
|
|
db.commit()
|
|
db.refresh(note)
|
|
_changed()
|
|
return _note_out(note)
|
|
|
|
|
|
@router.delete("/{note_id}", status_code=status.HTTP_204_NO_CONTENT)
|
|
def delete_note(note_id: int, db: Session = Depends(get_db), user: User = USE):
|
|
note = db.query(SiteNote).filter(SiteNote.id == note_id).first()
|
|
if not note:
|
|
raise XeniaError("note.not_found")
|
|
db.delete(note)
|
|
db.commit()
|
|
_changed()
|
|
|
|
|
|
# ── Todos ─────────────────────────────────────────────────────────────────────
|
|
|
|
@router.get("/todos", response_model=List[TodoOut])
|
|
def list_todos(done: bool = None, db: Session = Depends(get_db), user: User = USE):
|
|
q = db.query(SiteTodo)
|
|
if done is not None:
|
|
q = q.filter(SiteTodo.is_done == done)
|
|
# undone high-priority first, then undone normal, then done (by created_at desc)
|
|
todos = q.order_by(
|
|
SiteTodo.is_done.asc(),
|
|
SiteTodo.priority.desc(),
|
|
SiteTodo.created_at.desc(),
|
|
).all()
|
|
return [_todo_out(t) for t in todos]
|
|
|
|
|
|
@router.post("/todos", response_model=TodoOut, status_code=status.HTTP_201_CREATED)
|
|
def create_todo(body: TodoCreate, db: Session = Depends(get_db), user: User = USE):
|
|
if body.priority not in PRIORITIES:
|
|
raise XeniaError("validation.choice", field="priority", value=body.priority)
|
|
todo = SiteTodo(body=_text(body.body), priority=body.priority, created_by_id=user.id)
|
|
db.add(todo)
|
|
db.commit()
|
|
db.refresh(todo)
|
|
_changed()
|
|
return _todo_out(todo)
|
|
|
|
|
|
@router.put("/todos/{todo_id}", response_model=TodoOut)
|
|
def update_todo(todo_id: int, body: TodoUpdate, db: Session = Depends(get_db), user: User = USE):
|
|
todo = db.query(SiteTodo).filter(SiteTodo.id == todo_id).first()
|
|
if not todo:
|
|
raise XeniaError("todo.not_found")
|
|
if body.body is not None:
|
|
todo.body = _text(body.body)
|
|
if body.priority is not None:
|
|
if body.priority not in PRIORITIES:
|
|
raise XeniaError("validation.choice", field="priority", value=body.priority)
|
|
todo.priority = body.priority
|
|
if body.is_done is not None:
|
|
todo.is_done = body.is_done
|
|
if body.is_done and not todo.done_at:
|
|
todo.done_at = utcnow()
|
|
todo.done_by_id = user.id
|
|
elif not body.is_done:
|
|
todo.done_at = None
|
|
todo.done_by_id = None
|
|
db.commit()
|
|
db.refresh(todo)
|
|
_changed()
|
|
return _todo_out(todo)
|
|
|
|
|
|
@router.delete("/todos/{todo_id}", status_code=status.HTTP_204_NO_CONTENT)
|
|
def delete_todo(todo_id: int, db: Session = Depends(get_db), user: User = USE):
|
|
todo = db.query(SiteTodo).filter(SiteTodo.id == todo_id).first()
|
|
if not todo:
|
|
raise XeniaError("todo.not_found")
|
|
db.delete(todo)
|
|
db.commit()
|
|
_changed()
|