Bugfix for AdminView

This commit is contained in:
Rhywden
2026-08-26 06:40:16 +02:00
parent cf145c299c
commit 78656d7165
+66 -19
View File
@@ -129,9 +129,10 @@ public sealed class AdminEffects(IMoodleService moodleService, ILdapService ldap
{
await cache.RemoveAsync(MoodleUsersCacheKey);
}
catch (RedisException)
catch (RedisException exception)
{
ReportMoodleDeletionFailure(dispatcher, "Moodle deletion did not start because the cache could not be invalidated.");
dispatcher.Dispatch(new MoodleUsersDeletionFailed());
snackbar.Add($"Moodle deletion did not start because the cache could not be invalidated: {exception.Message}", Severity.Error);
return;
}
@@ -162,37 +163,83 @@ public sealed class AdminEffects(IMoodleService moodleService, ILdapService ldap
await moodleService.DeleteUsers(userIds);
return true;
}
catch (HttpRequestException)
catch (HttpRequestException exception)
{
ReportMoodleDeletionFailure(dispatcher, deletedUserCount, totalUserCount);
await ReportMoodleDeletionFailure(dispatcher, exception, userIds, deletedUserCount, totalUserCount);
}
catch (InvalidOperationException)
catch (InvalidOperationException exception)
{
ReportMoodleDeletionFailure(dispatcher, deletedUserCount, totalUserCount);
await ReportMoodleDeletionFailure(dispatcher, exception, userIds, deletedUserCount, totalUserCount);
}
catch (TaskCanceledException)
catch (TaskCanceledException exception)
{
ReportMoodleDeletionFailure(dispatcher, deletedUserCount, totalUserCount);
await ReportMoodleDeletionFailure(dispatcher, exception, userIds, deletedUserCount, totalUserCount);
}
catch (JsonException)
catch (JsonException exception)
{
ReportMoodleDeletionFailure(dispatcher, deletedUserCount, totalUserCount);
await ReportMoodleDeletionFailure(dispatcher, exception, userIds, deletedUserCount, totalUserCount);
}
return false;
}
private void ReportMoodleDeletionFailure(IDispatcher dispatcher, int deletedUserCount, int totalUserCount)
{
ReportMoodleDeletionFailure(
dispatcher,
$"Moodle deletion stopped after deleting {deletedUserCount} of {totalUserCount} user(s). Later users were not processed.");
}
private void ReportMoodleDeletionFailure(IDispatcher dispatcher, string message)
private async Task ReportMoodleDeletionFailure(
IDispatcher dispatcher,
Exception exception,
IReadOnlyCollection<int> failedUserIds,
int deletedUserCount,
int totalUserCount)
{
dispatcher.Dispatch(new MoodleUsersDeletionFailed());
snackbar.Add(message, Severity.Error);
var reconciliationMessage = await ReconcileMoodleUsersAfterDeletionFailure(dispatcher, failedUserIds);
snackbar.Add(
$"Moodle deletion stopped after confirming {deletedUserCount} of {totalUserCount} user(s). Reason: {exception.Message}.{reconciliationMessage}",
Severity.Error);
}
private async Task<string> ReconcileMoodleUsersAfterDeletionFailure(
IDispatcher dispatcher,
IReadOnlyCollection<int> failedUserIds)
{
try
{
var users = await moodleService.GetAllUsers();
dispatcher.Dispatch(new SetMoodleUsers(users));
try
{
await cache.SetStringAsync(
MoodleUsersCacheKey,
JsonSerializer.Serialize(users),
new DistributedCacheEntryOptions { SlidingExpiration = TimeSpan.FromMinutes(10) });
}
catch (RedisException exception)
{
return $" The user list was refreshed from Moodle, but Redis could not be updated: {exception.Message}";
}
var deletedFromFailedRequest = failedUserIds.Count(userId => !users.Any(user => user.Id == userId));
return deletedFromFailedRequest == 0
? " The user list was refreshed from Moodle."
: $" The user list was refreshed from Moodle; {deletedFromFailedRequest} user(s) from the failed request were deleted server-side.";
}
catch (HttpRequestException exception)
{
return $" The user list could not be refreshed: {exception.Message}";
}
catch (InvalidOperationException exception)
{
return $" The user list could not be refreshed: {exception.Message}";
}
catch (TaskCanceledException exception)
{
return $" The user list could not be refreshed: {exception.Message}";
}
catch (JsonException exception)
{
return $" The user list could not be refreshed: {exception.Message}";
}
}
[EffectMethod]