using LiteDB;
using LehrerApp.Sync.Models;
namespace LehrerApp.Api;
///
/// Speichert Klartext-Events der WebApp/Companion im EventStore.
/// Diese werden vom Desktop-Client beim nächsten Pull abgeholt
/// und wie normale Events angewendet – nur ohne Entschlüsselung.
///
/// Technisch gesehen ist das nur eine dünne Schicht über dem
/// bestehenden EventStore – Plain-Events werden einfach mit
/// DeviceType.Companion markiert und landen im selben Stream.
///
public class PlainEventStore
{
private readonly EventStore _eventStore;
public PlainEventStore(EventStore eventStore)
{
_eventStore = eventStore;
}
public PlainPushResponse Push(string userId, List events)
{
// Plain-Events in normale SyncEvents umwandeln
// DeviceType.Companion → Desktop-Client entschlüsselt nicht
var syncEvents = events.Select(e => new SyncEvent
{
EventId = e.EventId,
DeviceId = e.DeviceId,
DeviceType = DeviceType.Companion, // ← Signal: kein Decrypt nötig
Timestamp = e.Timestamp,
SequenceNr = 0, // vom EventStore vergeben
EntityType = e.EntityType,
EntityId = e.EntityId,
Operation = e.Operation,
Payload = e.Payload, // Klartext JSON
}).ToList();
var result = _eventStore.Push(userId, syncEvents);
return new PlainPushResponse
{
Success = result.Success,
ServerSequenceNr = result.ServerSequenceNr,
RejectedEventIds = result.ConflictingEventIds,
};
}
}