Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions app/src/api/notifications.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { api } from './client';
export type Notification = { id: number; message: string; priority: string; group: string; created_at: string };
export async function createNotification(message: string, priority?: string, group?: string): Promise<Notification> { return api('/notifications', { method: 'POST', body: { message, priority, group } }); }
export async function listNotifications(priority?: string): Promise<Notification[]> { return api('/notifications' + (priority ? '?priority=' + priority : '')); }
export async function getGrouped(): Promise<{ groups: Record<string, Notification[]>; group_count: number }> { return api('/notifications/grouped'); }
2 changes: 2 additions & 0 deletions packages/backend/app/routes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from .categories import bp as categories_bp
from .docs import bp as docs_bp
from .dashboard import bp as dashboard_bp
from .notifications import bp as notifications_bp


def register_routes(app: Flask):
Expand All @@ -18,3 +19,4 @@ def register_routes(app: Flask):
app.register_blueprint(categories_bp, url_prefix="/categories")
app.register_blueprint(docs_bp, url_prefix="/docs")
app.register_blueprint(dashboard_bp, url_prefix="/dashboard")
app.register_blueprint(notifications_bp, url_prefix="/notifications")
54 changes: 54 additions & 0 deletions packages/backend/app/routes/notifications.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
"""Notification priority and grouping system."""
from datetime import datetime
from flask import Blueprint, jsonify, request
from flask_jwt_extended import jwt_required, get_jwt_identity
from ..extensions import db
from ..models import AuditLog
bp = Blueprint("notifications", __name__)

PRIORITIES = {"critical": 1, "high": 2, "medium": 3, "low": 4, "info": 5}

@bp.post("")
@jwt_required()
def create_notification():
uid = int(get_jwt_identity())
data = request.get_json() or {}
message = (data.get("message") or "").strip()
priority = (data.get("priority") or "medium").strip().lower()
group = (data.get("group") or "general").strip()
if not message: return jsonify(error="message required"), 400
if priority not in PRIORITIES: return jsonify(error="invalid priority"), 400
entry = AuditLog(user_id=uid, action=f"notif:{priority}:{group}:{message}")
db.session.add(entry)
db.session.commit()
return jsonify(id=entry.id, message=message, priority=priority, group=group, created_at=entry.created_at.isoformat()), 201

@bp.get("")
@jwt_required()
def list_notifications():
uid = int(get_jwt_identity())
priority = request.args.get("priority")
group = request.args.get("group")
q = db.session.query(AuditLog).filter_by(user_id=uid).filter(AuditLog.action.like("notif:%"))
if priority: q = q.filter(AuditLog.action.like(f"notif:{priority}:%"))
if group: q = q.filter(AuditLog.action.like(f"notif:%:{group}:%"))
entries = q.order_by(AuditLog.created_at.desc()).limit(50).all()
results = []
for e in entries:
parts = e.action.split(":", 3)
if len(parts) >= 4:
results.append({"id": e.id, "priority": parts[1], "group": parts[2], "message": parts[3], "created_at": e.created_at.isoformat()})
return jsonify(results)

@bp.get("/grouped")
@jwt_required()
def grouped():
uid = int(get_jwt_identity())
entries = db.session.query(AuditLog).filter_by(user_id=uid).filter(AuditLog.action.like("notif:%")).order_by(AuditLog.created_at.desc()).limit(100).all()
groups = {}
for e in entries:
parts = e.action.split(":", 3)
if len(parts) >= 4:
g = parts[2]
groups.setdefault(g, []).append({"id": e.id, "priority": parts[1], "message": parts[3], "created_at": e.created_at.isoformat()})
return jsonify(groups=groups, group_count=len(groups))
20 changes: 20 additions & 0 deletions packages/backend/tests/test_notifications.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
def test_auth(client): assert client.get("/notifications").status_code in (401, 422)
def test_create(client, auth_header):
r = client.post("/notifications", json={"message": "Bill due", "priority": "high", "group": "bills"}, headers=auth_header)
assert r.status_code == 201
assert r.get_json()["priority"] == "high"
def test_list(client, auth_header):
client.post("/notifications", json={"message": "Test", "priority": "low"}, headers=auth_header)
r = client.get("/notifications", headers=auth_header)
assert r.status_code == 200
assert len(r.get_json()) >= 1
def test_grouped(client, auth_header):
client.post("/notifications", json={"message": "A", "group": "bills"}, headers=auth_header)
client.post("/notifications", json={"message": "B", "group": "alerts"}, headers=auth_header)
r = client.get("/notifications/grouped", headers=auth_header)
assert r.status_code == 200
assert r.get_json()["group_count"] >= 1
def test_filter_priority(client, auth_header):
client.post("/notifications", json={"message": "Urgent", "priority": "critical"}, headers=auth_header)
r = client.get("/notifications?priority=critical", headers=auth_header)
assert all(n["priority"] == "critical" for n in r.get_json())