fix: Kompetenzkataloge im Sync nachreichen
This commit is contained in:
1 parent
0f2a25feb5
commit
22eceb3ac1
7 files changed
+188
-4
No files matched your search
@@ -145,11 +145,38 @@ public class EventApplier(LiteDbContext db, byte[] syncKey, HttpClient? http = n
|
||||
Simple<SchoolHoliday>(context => context.SchoolHolidays);
|
||||
Simple<SupervisionDuty>(context => context.SupervisionDuties);
|
||||
Simple<SubstitutionEntry>(context => context.SubstitutionEntries);
|
||||
Simple<CompetencyDomain>(context => context.CompetencyDomains);
|
||||
Simple<CompetencyCatalogVersion>(context => context.CompetencyCatalogVersions);
|
||||
Simple<Vorgang>(context => context.Vorgaenge);
|
||||
Simple<UntisHubJobState>(context => context.UntisHubJobStates);
|
||||
|
||||
handlers[nameof(CompetencyCatalogVersion)] = (context, operation, entityId, json) =>
|
||||
{
|
||||
if (operation == "Delete")
|
||||
{
|
||||
context.CompetencyCatalogVersions.Delete(new Guid(entityId));
|
||||
return;
|
||||
}
|
||||
|
||||
var version = JsonSerializer.Deserialize<CompetencyCatalogVersion>(json)!;
|
||||
context.CompetencyCatalogVersions.Upsert(version);
|
||||
NormalizeCompetencyDefaults(context, version.SubjectId, version.Id);
|
||||
};
|
||||
handlers[nameof(CompetencyDomain)] = (context, operation, entityId, json) =>
|
||||
{
|
||||
Guid? subjectId;
|
||||
if (operation == "Delete")
|
||||
{
|
||||
subjectId = context.CompetencyDomains.FindById(new Guid(entityId))?.SubjectId;
|
||||
context.CompetencyDomains.Delete(new Guid(entityId));
|
||||
}
|
||||
else
|
||||
{
|
||||
var domain = JsonSerializer.Deserialize<CompetencyDomain>(json)!;
|
||||
subjectId = domain.SubjectId;
|
||||
context.CompetencyDomains.Upsert(domain);
|
||||
}
|
||||
if (subjectId is Guid id) NormalizeCompetencyDefaults(context, id);
|
||||
};
|
||||
|
||||
// Kaskaden-Fälle: dieselben internen LiteDbContext-Hilfsmethoden wie die jeweiligen
|
||||
// Repositories, damit die Kaskade nur an einer Stelle im Code existiert.
|
||||
handlers[nameof(LearningGroup)] = (context, operation, entityId, json) =>
|
||||
@@ -177,4 +204,39 @@ public class EventApplier(LiteDbContext db, byte[] syncKey, HttpClient? http = n
|
||||
|
||||
return handlers;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Alte, auf mehreren Geräten unabhängig erzeugte Standardversionen heißen identisch
|
||||
/// „Bestehender Kompetenzkatalog“, besitzen aber unterschiedliche IDs. Bei diesem speziellen
|
||||
/// Migrationsfall gewinnt deterministisch die Version mit den meisten Bereichen. Bei bewusst
|
||||
/// verschieden benannten Versionen gilt dagegen die zuletzt empfangene Standard-Auswahl.
|
||||
/// Direkte Collection-Schreibzugriffe verhindern dabei Sync-Ping-Pong.
|
||||
/// </summary>
|
||||
private static void NormalizeCompetencyDefaults(LiteDbContext context, Guid subjectId,
|
||||
Guid? incomingDefaultId = null)
|
||||
{
|
||||
var defaults = context.CompetencyCatalogVersions
|
||||
.Find(v => v.SubjectId == subjectId && v.IsDefault).ToList();
|
||||
if (defaults.Count <= 1) return;
|
||||
|
||||
CompetencyCatalogVersion winner;
|
||||
if (defaults.Select(v => v.Name.Trim()).Distinct(StringComparer.OrdinalIgnoreCase).Count() == 1)
|
||||
{
|
||||
winner = defaults
|
||||
.OrderByDescending(v => context.CompetencyDomains.Count(d => d.VersionId == v.Id))
|
||||
.ThenBy(v => v.Id)
|
||||
.First();
|
||||
}
|
||||
else
|
||||
{
|
||||
winner = defaults.FirstOrDefault(v => v.Id == incomingDefaultId)
|
||||
?? defaults.OrderBy(v => v.Id).First();
|
||||
}
|
||||
|
||||
foreach (var version in defaults.Where(v => v.Id != winner.Id))
|
||||
{
|
||||
version.IsDefault = false;
|
||||
context.CompetencyCatalogVersions.Upsert(version);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -102,7 +102,13 @@ public class EventQueue : IDisposable
|
||||
/// sie an, solange er sie unter der aktuellen userId selbst noch nicht kennt.</summary>
|
||||
public void ResetKnownServerSeqs() => _entityVersions.DeleteAll();
|
||||
|
||||
internal bool IsMaintenanceCompleted(string id) => _meta.FindById(MaintenanceKey(id)) is not null;
|
||||
|
||||
internal void MarkMaintenanceCompleted(string id) =>
|
||||
_meta.Upsert(new SyncMeta { Id = MaintenanceKey(id), Value = 1, Timestamp = DateTime.UtcNow });
|
||||
|
||||
private static string EntityVersionKey(string entityType, string entityId) => $"{entityType}:{entityId}";
|
||||
private static string MaintenanceKey(string id) => $"maintenance:{id}";
|
||||
|
||||
// ── Anhang-Warteliste (getrennt von der JSON-Ereignis-Outbox, siehe AttachmentSyncer) ────
|
||||
public void QueueAttachmentUpload(string storageId)
|
||||
|
||||
@@ -13,6 +13,8 @@ namespace LehrerApp.Sync;
|
||||
/// </summary>
|
||||
public class SyncEventPublisher(EventQueue queue, string deviceId, byte[] syncKey, AppLogger? logger = null)
|
||||
{
|
||||
private const string CompetencyCatalogBackfillId = "competency-catalog-v1";
|
||||
|
||||
public void Publish(string entityType, string entityId, string operation, object? payload)
|
||||
{
|
||||
var encrypted = payload is null ? "" : SyncCrypto.EncryptObject(payload, syncKey);
|
||||
@@ -29,4 +31,31 @@ public class SyncEventPublisher(EventQueue queue, string deviceId, byte[] syncKe
|
||||
foreach (var attachment in withAttachments.Attachments)
|
||||
queue.QueueAttachmentUpload(attachment.StorageId);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Veröffentlicht bestehende Kompetenzkataloge einmalig als vollständige Snapshots. Das
|
||||
/// repariert Daten, die vor aktivierter Synchronisation oder während einer Migration direkt
|
||||
/// in LiteDB geschrieben wurden und deshalb nie den Repository-OnChange-Hook durchlaufen
|
||||
/// haben. Der Marker liegt gerätespezifisch in dessen lokaler EventQueue.
|
||||
/// </summary>
|
||||
public int BackfillCompetencyCatalog(LiteDbContext db)
|
||||
{
|
||||
if (queue.IsMaintenanceCompleted(CompetencyCatalogBackfillId)) return 0;
|
||||
|
||||
var count = 0;
|
||||
foreach (var version in db.CompetencyCatalogVersions.FindAll())
|
||||
{
|
||||
Publish(nameof(CompetencyCatalogVersion), version.Id.ToString(), "Save", version);
|
||||
count++;
|
||||
}
|
||||
foreach (var domain in db.CompetencyDomains.FindAll())
|
||||
{
|
||||
Publish(nameof(CompetencyDomain), domain.Id.ToString(), "Save", domain);
|
||||
count++;
|
||||
}
|
||||
|
||||
queue.MarkMaintenanceCompleted(CompetencyCatalogBackfillId);
|
||||
logger?.Info($"Sync: einmaliger Kompetenzkatalog-Backfill abgeschlossen ({count} Ereignis(se)).");
|
||||
return count;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user