feat(manager): customers, throwaways, stock and notes on v2 (platform-v2 step 6.13b)

- 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>
This commit is contained in:
2026-11-19 18:36:27 +02:00
co-authored by Claude Opus 5.5
parent 07c9a0aecf
commit bde125bf09
28 changed files with 1356 additions and 1244 deletions
@@ -0,0 +1,199 @@
// Notes and to-dos (plan step 6.13b; replaces the v1 NotesPage): the managers' shared notes
// (pinned ones first; tap to edit, Ctrl+Enter saves, Esc cancels) and to-dos (urgent first; tick
// when done, who did it is kept; the done ones folded away). The overview's notes card shows the
// same. Live: `notes.changed`.
import { useState } from 'react'
import { useMutation, useQueryClient } from '@tanstack/react-query'
import toast from 'react-hot-toast'
import { Check, ChevronDown, Pin, PinOff, Plus, Trash2 } from 'lucide-react'
import { errorMessage } from '@xenia/api'
import { useFormat, useTranslation } from '@xenia/i18n'
import { Button, Dialog, IconButton, Input, Switch, Textarea, cx } from '@xenia/ui'
import client from '../../api/client'
import { useLiveQuery } from '../../live/useLiveQuery'
import { Panel } from '../reports/kit/parts'
import { splitNotes, splitTodos } from './notesLogic'
const LIVE = ['notes.changed']
function useChange() {
const { t } = useTranslation('manager')
const qc = useQueryClient()
return (fn, done) => ({
mutationFn: fn,
onSuccess: () => { qc.invalidateQueries({ queryKey: ['notes'] }); qc.invalidateQueries({ queryKey: ['todos'] }); if (done) toast.success(done) },
onError: err => toast.error(errorMessage(err, t)),
})
}
export default function NotesPage() {
const { t } = useTranslation('manager')
const { data: notes = [] } = useLiveQuery({ queryKey: ['notes'], queryFn: () => client.get('/api/notes/').then(r => r.data), events: LIVE })
const { data: todos = [] } = useLiveQuery({ queryKey: ['todos'], queryFn: () => client.get('/api/notes/todos').then(r => r.data), events: LIVE })
const [removing, setRemoving] = useState(null) // { kind: 'note' | 'todo', item }
const change = useChange()
const remove = useMutation(change(({ kind, item }) => client.delete(kind === 'note' ? `/api/notes/${item.id}` : `/api/notes/todos/${item.id}`), t('notes.deleted')))
const { pinned, rest } = splitNotes(notes)
const { open, done } = splitTodos(todos)
return (
<div className="h-full overflow-y-auto p-4 lg:p-6" data-testid="notes-page">
<div className="max-w-6xl mx-auto grid gap-4">
<header>
<h1 className="text-title font-semibold text-ink">{t('notes.title')}</h1>
<p className="text-label text-ink-2">{t('notes.subtitle', { notes: notes.length, todos: open.length })}</p>
</header>
<div className="grid gap-4 lg:grid-cols-2 items-start">
<Panel title={t('notes.notes')} testId="notes-column">
<div className="grid gap-3">
<NewNote />
{pinned.length > 0 && <Label>{t('notes.pinned')}</Label>}
{pinned.map(n => <NoteCard key={n.id} note={n} onDelete={() => setRemoving({ kind: 'note', item: n })} />)}
{pinned.length > 0 && rest.length > 0 && <Label>{t('notes.others')}</Label>}
{rest.map(n => <NoteCard key={n.id} note={n} onDelete={() => setRemoving({ kind: 'note', item: n })} />)}
{!notes.length && <p className="text-label text-ink-3">{t('notes.noNotes')}</p>}
</div>
</Panel>
<Panel title={t('notes.todos')} testId="todos-column">
<div className="grid gap-2">
<NewTodo />
{open.map(x => <TodoRow key={x.id} todo={x} onDelete={() => setRemoving({ kind: 'todo', item: x })} />)}
{!todos.length && <p className="text-label text-ink-3">{t('notes.noTodos')}</p>}
{done.length > 0 && <DoneTodos done={done} onDelete={x => setRemoving({ kind: 'todo', item: x })} />}
</div>
</Panel>
</div>
</div>
{removing && (
<Dialog open size="sm" onClose={() => setRemoving(null)} title={t(removing.kind === 'note' ? 'notes.deleteNote' : 'notes.deleteTodo')}
description={removing.item.body.length > 120 ? `${removing.item.body.slice(0, 120)}…` : removing.item.body}
footer={<>
<Button variant="secondary" onClick={() => setRemoving(null)}>{t('notes.back')}</Button>
<Button variant="danger" loading={remove.isPending} onClick={() => remove.mutate(removing, { onSuccess: () => setRemoving(null) })} data-testid="notes-delete-confirm">{t('notes.deleteYes')}</Button>
</>} />
)}
</div>
)
}
function Label({ children }) {
return <span className="text-caption font-semibold uppercase tracking-wide text-ink-3">{children}</span>
}
function NewNote() {
const { t } = useTranslation('manager')
const [body, setBody] = useState('')
const [pin, setPin] = useState(false)
const change = useChange()
const add = useMutation(change(() => client.post('/api/notes/', { body: body.trim(), is_pinned: pin })))
const submit = () => { if (body.trim()) add.mutate(undefined, { onSuccess: () => { setBody(''); setPin(false) } }) }
return (
<div className="grid gap-2 rounded-card border border-dashed border-line p-3">
<Textarea rows={3} value={body} onChange={e => setBody(e.target.value)} placeholder={t('notes.newNote')} aria-label={t('notes.newNote')}
onKeyDown={e => { if (e.key === 'Enter' && (e.ctrlKey || e.metaKey)) submit() }} data-testid="note-new" />
<div className="flex items-center gap-3">
<Switch checked={pin} onChange={setPin} label={t('notes.pinIt')} />
<span className="flex-1" />
<Button size="sm" icon={<Plus size={14} />} disabled={!body.trim()} loading={add.isPending} onClick={submit} data-testid="note-add">{t('notes.addNote')}</Button>
</div>
</div>
)
}
function NoteCard({ note, onDelete }) {
const { t } = useTranslation('manager')
const { formatDateTime } = useFormat()
const [draft, setDraft] = useState(null)
const change = useChange()
const save = useMutation(change(body => client.put(`/api/notes/${note.id}`, body)))
const finish = () => {
const text = draft?.trim()
if (text && text !== note.body) save.mutate({ body: text })
setDraft(null)
}
return (
<div className={cx('rounded-card border p-3 grid gap-2', note.is_pinned ? 'border-warning bg-warning-soft' : 'border-line bg-surface')} data-testid="note-card">
{draft != null ? (
<Textarea rows={4} autoFocus value={draft} onChange={e => setDraft(e.target.value)} onBlur={finish} aria-label={t('notes.edit')}
onKeyDown={e => { if (e.key === 'Escape') setDraft(null); if (e.key === 'Enter' && (e.ctrlKey || e.metaKey)) finish() }} />
) : (
<button type="button" className="text-left text-body text-ink whitespace-pre-wrap break-words" onClick={() => setDraft(note.body)} title={t('notes.tapToEdit')}>{note.body}</button>
)}
<div className="flex items-center gap-2">
<span className="flex-1 text-caption text-ink-3">{note.created_by_name} · {formatDateTime(note.updated_at ?? note.created_at, 'datetimeShort')}</span>
<IconButton size="sm" variant="ghost" label={t(note.is_pinned ? 'notes.unpin' : 'notes.pin')} onClick={() => save.mutate({ is_pinned: !note.is_pinned })}>
{note.is_pinned ? <PinOff size={15} /> : <Pin size={15} />}
</IconButton>
<IconButton size="sm" variant="ghost" label={t('notes.deleteNote')} onClick={onDelete}><Trash2 size={15} /></IconButton>
</div>
</div>
)
}
function NewTodo() {
const { t } = useTranslation('manager')
const [body, setBody] = useState('')
const [urgent, setUrgent] = useState(false)
const change = useChange()
const add = useMutation(change(() => client.post('/api/notes/todos', { body: body.trim(), priority: urgent ? 'high' : 'normal' })))
const submit = () => { if (body.trim()) add.mutate(undefined, { onSuccess: () => { setBody(''); setUrgent(false) } }) }
return (
<div className="grid gap-2 rounded-card border border-dashed border-line p-3">
<Input value={body} onChange={e => setBody(e.target.value)} placeholder={t('notes.newTodo')} aria-label={t('notes.newTodo')}
onKeyDown={e => { if (e.key === 'Enter') submit() }} data-testid="todo-new" />
<div className="flex items-center gap-3">
<Switch checked={urgent} onChange={setUrgent} label={t('notes.urgent')} />
<span className="flex-1" />
<Button size="sm" icon={<Plus size={14} />} disabled={!body.trim()} loading={add.isPending} onClick={submit} data-testid="todo-add">{t('notes.addTodo')}</Button>
</div>
</div>
)
}
function TodoRow({ todo, onDelete }) {
const { t } = useTranslation('manager')
const { formatDateTime } = useFormat()
const [draft, setDraft] = useState(null)
const change = useChange()
const save = useMutation(change(body => client.put(`/api/notes/todos/${todo.id}`, body)))
const finish = () => {
const text = draft?.trim()
if (text && text !== todo.body) save.mutate({ body: text })
setDraft(null)
}
const urgent = todo.priority === 'high' && !todo.is_done
return (
<div className={cx('flex items-start gap-3 rounded-card border p-2.5', urgent ? 'border-danger bg-danger-soft' : 'border-line bg-surface', todo.is_done && 'opacity-70')} data-testid="todo-row">
<button type="button" role="checkbox" aria-checked={todo.is_done} aria-label={t(todo.is_done ? 'notes.undo' : 'notes.markDone', { what: todo.body })}
onClick={() => save.mutate({ is_done: !todo.is_done })} data-testid="todo-done"
className={cx('mt-0.5 w-5 h-5 shrink-0 rounded border-2 grid place-items-center', todo.is_done ? 'bg-success border-success text-success-fg' : urgent ? 'border-danger' : 'border-line')}>
{todo.is_done && <Check size={13} aria-hidden="true" />}
</button>
<span className="flex-1 min-w-0 grid gap-0.5">
{draft != null ? (
<Input autoFocus value={draft} onChange={e => setDraft(e.target.value)} onBlur={finish} aria-label={t('notes.edit')}
onKeyDown={e => { if (e.key === 'Enter') finish(); if (e.key === 'Escape') setDraft(null) }} />
) : (
<button type="button" disabled={todo.is_done} onClick={() => setDraft(todo.body)} className={cx('text-left text-body break-words', todo.is_done ? 'line-through text-ink-3' : 'text-ink')}>
{urgent && <span className="text-caption font-semibold text-danger-ink mr-1.5">{t('notes.urgentTag')}</span>}{todo.body}
</button>
)}
<span className="text-caption text-ink-3">
{todo.created_by_name} · {formatDateTime(todo.created_at, 'datetimeShort')}
{todo.is_done && todo.done_by_name && <span className="text-success-ink"> · {t('notes.doneBy', { name: todo.done_by_name, time: formatDateTime(todo.done_at, 'datetimeShort') })}</span>}
</span>
</span>
<IconButton size="sm" variant="ghost" label={t('notes.deleteTodo')} onClick={onDelete}><Trash2 size={15} /></IconButton>
</div>
)
}
function DoneTodos({ done, onDelete }) {
const { t } = useTranslation('manager')
const [open, setOpen] = useState(false)
return (<>
<button type="button" onClick={() => setOpen(o => !o)} aria-expanded={open} className="flex items-center gap-1 text-label font-semibold text-ink-2 hover:text-ink mt-1" data-testid="todos-done-toggle">
<ChevronDown size={15} className={cx('transition-transform', open && 'rotate-180')} aria-hidden="true" />{t('notes.doneCount', { count: done.length })}
</button>
{open && done.map(x => <TodoRow key={x.id} todo={x} onDelete={() => onDelete(x)} />)}
</>)
}
@@ -0,0 +1,15 @@
// The order of the Notes page (plan step 6.13b), unit-tested in tests/crm.test.mjs.
/** Pinned notes first, then the rest; each newest-edited first. */
export function splitNotes(notes) {
const newest = (a, b) => String(b.updated_at ?? b.created_at).localeCompare(String(a.updated_at ?? a.created_at))
return { pinned: notes.filter(n => n.is_pinned).sort(newest), rest: notes.filter(n => !n.is_pinned).sort(newest) }
}
/** To-dos not done (urgent first, then newest) and done ones (most recently done first). */
export function splitTodos(todos) {
const open = todos.filter(t => !t.is_done).sort((a, b) => (a.priority === 'high' ? 0 : 1) - (b.priority === 'high' ? 0 : 1)
|| String(b.created_at).localeCompare(String(a.created_at)))
const done = todos.filter(t => t.is_done).sort((a, b) => String(b.done_at ?? '').localeCompare(String(a.done_at ?? '')))
return { open, done }
}