Files
Sternsinger/CMBNet/cmbnet/services/groups_service.py
T
2026-10-23 00:05:06 +02:00

96 lines
3.6 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)
if tenant_id not in cls._cache:
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_id': group['account_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:
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
return group.get('account_id') == account_id
@classmethod
def get_accounts_for_group(cls, group_id, tenant_id):
"""Gibt alle mit einer Gruppe verknüpften Accounts zurück"""
group = cls.get_by_id(tenant_id, group_id)
if not group or not group.get('account_id'):
return []
account = AccountModel.get_by_id(group['account_id'], tenant_id)
return [account] if account else []
@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)