From bdda63089f4fb74e628ee978bf1fc4fe6c964fcd Mon Sep 17 00:00:00 2001 From: MarcUs7i <96580944+MarcUs7i@users.noreply.github.com> Date: Mon, 28 Apr 2025 15:32:26 +0200 Subject: [PATCH] Improved discord webhook management --- .../Services/DiscordNotificationService.cs | 28 ++++++++++++---- .../Services/QuestionManagementService.cs | 32 +++++++++++++++---- 2 files changed, 46 insertions(+), 14 deletions(-) diff --git a/Server/Server/Services/DiscordNotificationService.cs b/Server/Server/Services/DiscordNotificationService.cs index 37c99de..97f6f2e 100644 --- a/Server/Server/Services/DiscordNotificationService.cs +++ b/Server/Server/Services/DiscordNotificationService.cs @@ -6,9 +6,10 @@ namespace WritingServer.Services; public interface IDiscordNotificationService { Task SendStartupNotification(TimeSpan startupTime, long memoryUsageMb); - Task SendResponseNotification(string username, string questionSetName, string questionText, string responseText); + Task SendResponseNotification(string username, string questionSetName, + Dictionary response); Task SendExceptionNotification(string path); - Task SendDiscordMessage(string content); + Task SendDiscordMessage(string content, string username); } public class DiscordNotificationService : IDiscordNotificationService @@ -27,10 +28,15 @@ public class DiscordNotificationService : IDiscordNotificationService await SendDiscordMessage($"Backend warmed up!\nTook {startupTime.TotalSeconds:F2} seconds.\nMemory usage: {memoryUsageMb} MB"); } - public async Task SendResponseNotification(string username, string questionSetName, string questionText, string responseText) + public async Task SendResponseNotification(string username, string questionSetName, + Dictionary response) { - var message = $"New Response:\n**User:** {username}\n**Question Set:** {questionSetName}\n**Question:** {questionText}\n**Response:** {responseText}"; - await SendDiscordMessage(message); + var message = $"**{questionSetName}**\n**Question:**\n"; + foreach (var item in response) + { + message += $"**{item.Key}** {item.Value}\n"; + } + await SendDiscordMessage(message, username); } public async Task SendExceptionNotification(string path) @@ -39,14 +45,22 @@ public class DiscordNotificationService : IDiscordNotificationService await SendDiscordMessage(message); } - public async Task SendDiscordMessage(string content) + public async Task SendDiscordMessage(string content, string? username = null) { if (string.IsNullOrEmpty(_webhookUrl)) { return; } - var payload = new { content }; + object payload; + if (!string.IsNullOrEmpty(username)) + { + payload = new { content, username }; + } + else + { + payload = new { content }; + } var json = JsonSerializer.Serialize(payload); var stringContent = new StringContent(json, Encoding.UTF8, "application/json"); diff --git a/Server/Server/Services/QuestionManagementService.cs b/Server/Server/Services/QuestionManagementService.cs index 3a0219b..cbf7c59 100644 --- a/Server/Server/Services/QuestionManagementService.cs +++ b/Server/Server/Services/QuestionManagementService.cs @@ -210,15 +210,33 @@ public class QuestionManagementService : IQuestionManagementService await _responsesCollection.InsertOneAsync(response); - // Discord webhook - var question = await GetQuestionByIdAsync(questionId); string questionSetId = questionId.Split('_')[0]; var questionSet = await GetQuestionSetByIdAsync(questionSetId); - await _discordNotificationService.SendResponseNotification( - userName, - questionSet?.QuestionSetName ?? "Unknown Set", - question?.QuestionText ?? "Unknown Question", - responseText); + + var allQuestionsInSet = await GetQuestionsInSetAsync(questionSetId); + var userResponses = await _responsesCollection + .Find(r => r.UserName == userName && r.QuestionId.StartsWith(questionSetId + "_")) + .ToListAsync(); + + var answeredQuestionIds = userResponses.Select(r => r.QuestionId).ToHashSet(); + + if (allQuestionsInSet.Questions.All(q => answeredQuestionIds.Contains(q.QuestionId))) + { + var responseDict = new Dictionary(); + foreach (var q in allQuestionsInSet.Questions.OrderBy(q => q.QuestionOrder)) + { + var userResponse = userResponses.FirstOrDefault(r => r.QuestionId == q.QuestionId); + if (userResponse != null) + { + responseDict[q.QuestionText] = userResponse.ResponseText; + } + } + + await _discordNotificationService.SendResponseNotification( + userName, + questionSet?.QuestionSetName ?? "Unknown Set", + responseDict); + } return true; }