fix: Kompetenzkataloge im Sync nachreichen

This commit is contained in:
admin committed 2026-09-20 00:03:45 +02:00
1 parent 0f2a25feb5
commit 22eceb3ac1
7 files changed
+188 -4

No files matched your search

+64 -2
View File
@@ -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);
}
}
}