Files
Sternsinger/CMBNet/cmbnet/services/groups_service.py
T

101 lines
3.9 KiB
Python

"""
Groups Service - Geschäftslogik für Gruppen mit In-Memory-Caching
"""
from cmbnet.models.groups import GroupModel
from cmbnet.models.streets import StreetModel
from cmbnet.models.accounts import AccountModel
from cmbnet.db import get_db
class GroupsService:
# In-Memory Cache: groups_cache[tenant_id][group_id] = group_dict
_cache = {}
@classmethod
def load_from_db(cls, tenant_id):
"""Lädt alle Gruppen eines Mandanten aus der Datenbank in den Cache"""
groups = GroupModel.get_all_by_tenant(tenant_id)
memberships = GroupModel.get_account_ids_by_group(tenant_id)
# Vollständig neu aufbauen, damit gelöschte Gruppen nicht im Cache
# zurückbleiben.
cls._cache[tenant_id] = {}
for group in groups:
cls._cache[tenant_id][group['id']] = {
'id': group['id'],
'tenant_id': group['tenant_id'],
'aliasname': group['aliasname'],
'state': group['state'],
'last_lat': group['last_lat'],
'last_lon': group['last_lon'],
'last_pos_at': group['last_pos_at'],
'account_ids': memberships.get(group['id'], []),
'created_at': group['created_at'],
'updated_at': group['updated_at']
}
return cls._cache[tenant_id]
@classmethod
def get_all(cls, tenant_id, force_reload=False):
"""Gibt alle Gruppen für einen Mandanten zurück (aus Cache)"""
if force_reload or tenant_id not in cls._cache or not cls._cache[tenant_id]:
cls.load_from_db(tenant_id)
return cls._cache.get(tenant_id, {})
@classmethod
def get_by_id(cls, tenant_id, group_id):
"""Gibt eine spezifische Gruppe aus dem Cache zurück"""
if tenant_id not in cls._cache or not cls._cache[tenant_id]:
cls.load_from_db(tenant_id)
return cls._cache.get(tenant_id, {}).get(group_id)
@classmethod
def invalidate(cls, tenant_id):
"""Invalidiert den Cache für einen Mandanten (erzwingt Neuladen)"""
if tenant_id in cls._cache:
cls._cache[tenant_id] = {}
@classmethod
def invalidate_all(cls):
"""Invalidiert den gesamten Cache"""
cls._cache = {}
@classmethod
def is_account_linked(cls, account_id, group_id, tenant_id):
"""Prüft, ob ein Account mit einer Gruppe verknüpft ist"""
group = cls.get_by_id(tenant_id, group_id)
if not group:
return False
account_ids = {int(value) for value in group.get('account_ids', [])}
return int(account_id) in account_ids
@classmethod
def get_accounts_for_group(cls, group_id, tenant_id):
"""Gibt alle mit einer Gruppe verknüpften Accounts zurück"""
account_ids = GroupModel.get_account_ids_for_group(group_id, tenant_id)
accounts = []
for account_id in account_ids:
account = AccountModel.get_by_id(account_id, tenant_id)
if account:
accounts.append(account)
return accounts
@classmethod
def get_streets_for_group(cls, group_id, tenant_id):
"""Gibt alle mit einer Gruppe verknüpften Straßen zurück"""
return StreetModel.get_by_group_id(group_id, tenant_id)
@classmethod
def update_cache_entry(cls, tenant_id, group_id, data):
"""Aktualisiert einen Eintrag im Cache.
Der Cache wird vor dem Aktualisieren vollständig geladen, damit ein
gezieltes Update (z. B. nach Code 200/300) keinen Teil-Cache erzeugt,
der anschließend beim Initialload unvollständige Gruppendaten liefert.
"""
if tenant_id not in cls._cache or not cls._cache[tenant_id]:
cls.load_from_db(tenant_id)
if group_id not in cls._cache[tenant_id]:
cls._cache[tenant_id][group_id] = {}
cls._cache[tenant_id][group_id].update(data)