Files
xenia-pos-local/local_backend/models/notification.py
T
bonaminandClaude Opus 5.5 5a21f67b42 feat(backend): platform notifications service with audiences, dedupe, read state and first producers
Platform v2 step 1.11 (APPS_PLATFORM section 10).
- notifications + notification_reads (revision 0010). Text = i18n key notification.<key> + params,
  rendered in the reader's language (en, el added).
- core/notifications.py: notify(key, severity, audience, params, dedupe_key, ttl/expires_at, fold)
  never raises; audience all_managers | perm:<key> | user:<id>; a live duplicate only bumps
  count/last_at (no unread reset, no event). New ones emit WS platform_notification {id} to the
  audience's users (current apps ignore it; phase 2 manager shows it).
- API (core, /api/platform/notifications; /api/notifications is chat's): list with unread count,
  POST /{id}/read, POST /read-all.
- Producers: license.limit_reached (device pairing/reassign/over-limit device, per module/limit/day,
  owners), license.expiring (14/7/3/1 days, from apply_license, owners), support.access_used (each
  use, core.settings.manage), print.failed_final (abandoned retry job, per printer/hour, managers).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-13 11:13:37 +02:00

42 lines
2.0 KiB
Python

"""Platform notifications (APPS_PLATFORM §10, plan step 1.11).
System messages to staff: license limits and expiry, support access used, print
failures (later: Ergani, close-day reminders). NOT staff chat or manager messages
(models/message.py, F-030), which stay separate.
- The text is an i18n key (`notification.<key>` in core/i18n/*.json) + params, rendered
in the reader's language when listed.
- audience: "all_managers" (holders of core.dashboard.access) | "perm:<key>" | "user:<id>".
- dedupe_key: while a row with the same key is still live (expires_at NULL or future),
notify() bumps its `count` / `last_at` instead of adding a row.
Written through core/notifications.py only.
"""
from sqlalchemy import JSON, Column, ForeignKey, Integer, String
from core.timeutil import UTCDateTime, utcnow
from database import Base
class Notification(Base):
__tablename__ = "notifications"
__table_args__ = {"sqlite_autoincrement": True}
id = Column(Integer, primary_key=True, autoincrement=True)
key = Column(String, nullable=False, index=True) # e.g. "license.limit_reached"
severity = Column(String, nullable=False, default="info") # info | warning | critical
audience = Column(String, nullable=False, index=True)
params = Column(JSON, nullable=True)
dedupe_key = Column(String, nullable=True, index=True)
count = Column(Integer, nullable=False, default=1) # occurrences folded in by dedupe
created_at = Column(UTCDateTime, nullable=False, default=utcnow, index=True)
last_at = Column(UTCDateTime, nullable=False, default=utcnow)
expires_at = Column(UTCDateTime, nullable=True) # hidden (and dedupe ends) after this
class NotificationRead(Base):
__tablename__ = "notification_reads"
notification_id = Column(Integer, ForeignKey("notifications.id", ondelete="CASCADE"), primary_key=True)
user_id = Column(Integer, primary_key=True) # no FK: the support account (id 0) can read too
read_at = Column(UTCDateTime, nullable=False, default=utcnow)