- Neues Modell ProjektUser (projekt_users) für projektbezogene Sichtbarkeit
- Migration V01_09 legt Tabelle an (keine Initial-Befüllung)
- CRUD-Logik für Projekt-User-Zuordnungen mit Cross-DB-User-Auflösung
- Serverseitige Sichtbarkeits-Filterung: Projekt-Liste/Get filtert nach
projekt_users-Zuordnung für Nicht-SysAdmin/Nicht-AppAdmin
- Auto-Assignment: Ersteller wird bei Projektanlage automatisch zugeordnet
- Sichtbarkeits-Filterung auf CallLog und Kontaktadresse erweitert
(Liste und Einzelabruf)
- Schreibschutz: Alle Schreib-Endpunkte (update/delete) für Projekt,
Kontaktadresse und CallLog prüfen Projekt-Zuordnung vor der Ausführung
- Hilfsfunktionen _get_visible_user_id() und _require_projekt_access()
zur Kapselung der wiederkehrenden Prüfungen
- REST-Endpunkte /xhr/projektuser/{list,add,remove};
Schreib-Endpunkte mit @authorisation_required([]) als Erweiterungspunkt
- Client: Neues Modell projekt_user.ts, Listen-Komponente mit Modal
(Select-Widget), Button in Projekt-Maske, Route registriert
- Build aktualisiert (tsc + rollup)
- AGENTS.md: Vorgaben für projektbezogene Sichtbarkeit dokumentiert
Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
151 lines
5.7 KiB
Python
151 lines
5.7 KiB
Python
"""
|
|
Geschäftslogik für die Entität CallLog (Gesprächsnotiz).
|
|
|
|
Diese Funktionen kapseln den DB-Zugriff und die fachlichen Validierungen.
|
|
Aufgerufen werden sie aus crm/routes.py; die Autorisierungsprüfung erfolgt
|
|
dort über den Decorator authorisation_required.
|
|
|
|
:author: Mistral Vibe <vibe@mistral.ai>
|
|
:date: 2026-09-04
|
|
:version: 1.0
|
|
"""
|
|
from db import dbapp
|
|
from crm.model import CallLog, Projekt, ProjektUser
|
|
|
|
from auth.model.errors import ApplicationError, HtmlStatusCode
|
|
|
|
session = dbapp.session
|
|
|
|
|
|
def get_call_logs(projekt_id: int | None = None, user_id: int | None = None):
|
|
"""
|
|
Liefert Gesprächsnotizen, sortiert nach Datum absteigend.
|
|
|
|
Ist projekt_id gesetzt, werden nur die Notizen dieses Projekts
|
|
zurückgegeben (serverseitige Filterung). Ohne projekt_id werden
|
|
alle Notizen geliefert.
|
|
|
|
Ist user_id gesetzt, werden nur Notizen von Projekten geliefert, denen
|
|
der Nutzer über projekt_users zugeordnet ist (Sichtbarkeits-Filterung).
|
|
SysAdmin und AppAdmin übergeben user_id=None und sehen alle Notizen.
|
|
"""
|
|
query = CallLog.query
|
|
if projekt_id is not None:
|
|
query = query.filter(CallLog.projekt_id == projekt_id)
|
|
if user_id is not None:
|
|
# Nur Notizen von Projekten, denen der Nutzer zugeordnet ist.
|
|
query = query.filter(
|
|
CallLog.projekt_id.in_(
|
|
dbapp.session.query(ProjektUser.projekt_id).filter(
|
|
ProjektUser.user_id == user_id
|
|
)
|
|
)
|
|
)
|
|
return query.order_by(CallLog.datum.desc()).all()
|
|
|
|
|
|
def get_call_log(call_log_id: int, user_id: int | None = None) -> CallLog:
|
|
"""
|
|
Liefert eine einzelne Gesprächsnotiz anhand ihrer ID.
|
|
Wirft ApplicationError (404), falls die Notiz nicht existiert.
|
|
|
|
Ist user_id gesetzt, wird zusätzlich geprüft, ob der Nutzer dem
|
|
Projekt der Notiz zugeordnet ist; sonst wird 404 geliefert.
|
|
"""
|
|
call_log = CallLog.query.get(call_log_id)
|
|
if call_log is None:
|
|
raise ApplicationError(
|
|
"Gesprächsnotiz mit der ID {} wurde nicht gefunden.".format(call_log_id),
|
|
htmlstatus=HtmlStatusCode.Not_Found,
|
|
)
|
|
if user_id is not None:
|
|
zuordnung = ProjektUser.query.filter(
|
|
ProjektUser.projekt_id == call_log.projekt_id,
|
|
ProjektUser.user_id == user_id,
|
|
).first()
|
|
if zuordnung is None:
|
|
raise ApplicationError(
|
|
"Gesprächsnotiz mit der ID {} wurde nicht gefunden.".format(call_log_id),
|
|
htmlstatus=HtmlStatusCode.Not_Found,
|
|
)
|
|
return call_log
|
|
|
|
|
|
def create_call_log(data: dict) -> CallLog:
|
|
"""
|
|
Legt eine neue Gesprächsnotiz an. Die vom Client übergebene ID wird
|
|
ignoriert; die ID wird serverseitig per Auto-Increment vergeben.
|
|
|
|
- data: dict mit den Feldern projekt_id, datum, uhrzeit_beginn, dauer,
|
|
betreff, name_ansprechpartner, telefon, notiz,
|
|
wiedervorlage_datum, erledigt
|
|
"""
|
|
projekt_id = data.get("projekt_id")
|
|
if projekt_id is None:
|
|
raise ApplicationError(
|
|
"Es muss ein Projekt ausgewählt werden.",
|
|
htmlstatus=HtmlStatusCode.Bad_Request,
|
|
)
|
|
projekt = Projekt.query.get(int(projekt_id))
|
|
if projekt is None:
|
|
raise ApplicationError(
|
|
"Das ausgewählte Projekt existiert nicht.",
|
|
htmlstatus=HtmlStatusCode.Bad_Request,
|
|
)
|
|
call_log = CallLog()
|
|
call_log.projekt_id = int(projekt_id)
|
|
call_log.datum = (data.get("datum") or "").strip() or None
|
|
call_log.uhrzeit_beginn = (data.get("uhrzeit_beginn") or "").strip() or None
|
|
call_log.dauer = (data.get("dauer") or "").strip() or None
|
|
call_log.betreff = (data.get("betreff") or "").strip() or None
|
|
call_log.name_ansprechpartner = (data.get("name_ansprechpartner") or "").strip() or None
|
|
call_log.telefon = (data.get("telefon") or "").strip() or None
|
|
call_log.notiz = (data.get("notiz") or "").strip() or None
|
|
call_log.wiedervorlage_datum = (data.get("wiedervorlage_datum") or "").strip() or None
|
|
call_log.erledigt = bool(data.get("erledigt", False))
|
|
session.add(call_log)
|
|
session.commit()
|
|
return call_log
|
|
|
|
|
|
def update_call_log(data: dict) -> CallLog:
|
|
"""
|
|
Aktualisiert eine bestehende Gesprächsnotiz. Die ID im data-Dict
|
|
bestimmt den zu ändernden Datensatz.
|
|
"""
|
|
call_log = get_call_log(data["id"])
|
|
projekt_id = data.get("projekt_id")
|
|
if projekt_id is None:
|
|
raise ApplicationError(
|
|
"Es muss ein Projekt ausgewählt werden.",
|
|
htmlstatus=HtmlStatusCode.Bad_Request,
|
|
)
|
|
projekt = Projekt.query.get(int(projekt_id))
|
|
if projekt is None:
|
|
raise ApplicationError(
|
|
"Das ausgewählte Projekt existiert nicht.",
|
|
htmlstatus=HtmlStatusCode.Bad_Request,
|
|
)
|
|
call_log.projekt_id = int(projekt_id)
|
|
call_log.datum = (data.get("datum") or "").strip() or None
|
|
call_log.uhrzeit_beginn = (data.get("uhrzeit_beginn") or "").strip() or None
|
|
call_log.dauer = (data.get("dauer") or "").strip() or None
|
|
call_log.betreff = (data.get("betreff") or "").strip() or None
|
|
call_log.name_ansprechpartner = (data.get("name_ansprechpartner") or "").strip() or None
|
|
call_log.telefon = (data.get("telefon") or "").strip() or None
|
|
call_log.notiz = (data.get("notiz") or "").strip() or None
|
|
call_log.wiedervorlage_datum = (data.get("wiedervorlage_datum") or "").strip() or None
|
|
call_log.erledigt = bool(data.get("erledigt", False))
|
|
session.commit()
|
|
return call_log
|
|
|
|
|
|
def delete_call_log(call_log_id: int):
|
|
"""
|
|
Löscht eine Gesprächsnotiz anhand ihrer ID.
|
|
Wirft ApplicationError (404), falls die Notiz nicht existiert.
|
|
"""
|
|
call_log = get_call_log(call_log_id)
|
|
session.delete(call_log)
|
|
session.commit()
|