namespace SOR.Support.Handlers;
using Microsoft.EntityFrameworkCore;
using SOR.Database.Contexts;
using SOR.Database.Entities.Support;
using SOR.Networking.Abstractions.Messaging;
using SOR.Networking.Abstractions.Transport;
using SOR.Shared.Networking;
///
/// support.reply_bug_report — lets the submitter reply to their own support thread.
///
internal sealed class ReplyBugReportHandler : IMessageHandler
{
private readonly IMessageSerializer _serializer;
private readonly SupportDbContext _supportDb;
public ReplyBugReportHandler(IMessageSerializer serializer, SupportDbContext supportDb)
{
_serializer = serializer;
_supportDb = supportDb;
}
public string MessageType => "support.reply_bug_report";
public async Task HandleAsync(
IClientMessage message,
AuthenticatedConnection connection,
CancellationToken cancellationToken)
{
var req = _serializer.DeserializePayload(message.Payload);
var accountId = Guid.Parse(connection.UserId);
var content = req.Content?.Trim() ?? string.Empty;
if (string.IsNullOrWhiteSpace(content))
return ServerMessage.Error(message.Seq, "INVALID_PAYLOAD", "Reply content is required.");
var report = await _supportDb.BugReports
.FirstOrDefaultAsync(r => r.Id == req.BugReportId, cancellationToken);
if (report is null)
return ServerMessage.Error(message.Seq, "NOT_FOUND", "Bug report not found.");
if (report.SubmitterAccountId != accountId)
return ServerMessage.Error(message.Seq, "FORBIDDEN", "You can only reply to your own bug report.");
var response = new BugReportResponse
{
BugReportId = report.Id,
AdminAccountId = accountId,
AdminDisplayName = connection.DisplayName,
Content = content[..Math.Min(content.Length, 4000)],
CreatedAt = DateTimeOffset.UtcNow,
};
report.UpdatedAt = DateTimeOffset.UtcNow;
if (report.Status is "Resolved" or "Closed")
report.Status = "InProgress";
_supportDb.BugReportResponses.Add(response);
await _supportDb.SaveChangesAsync(cancellationToken);
return ServerMessage.Reply("support.reply_bug_report.ack", message.Seq,
new ReplyBugReportResponse(new BugReportResponseDto(
response.Id,
response.AdminAccountId,
response.AdminDisplayName,
response.Content,
response.CreatedAt)));
}
}