public IActionResult SendFeedback([FromBody] SendFeedbackRequest request) { var allowedFeedbacks = _dbContext.ReportAllowedFeedbacks.Where(r => r.ReportId == request.ReportId && r.Feedback.IsEnabled).Select(f => f.FeedbackId).ToList(); var userId = User.GetUserId(); if (!userId.HasValue) { throw new HttpStatusException(HttpStatusCode.Unauthorized); } if (!allowedFeedbacks.Contains(request.FeedbackId)) { throw new HttpStatusException(HttpStatusCode.BadRequest, "Invalid feedback id"); } var existingFeedback = _dbContext.ReportFeedbacks.FirstOrDefault(rf => rf.UserId == userId && rf.ReportId == request.ReportId && rf.InvalidatedDate == null); if (existingFeedback != null) { // No change if (existingFeedback.FeedbackId == request.FeedbackId) { return(Ok()); } existingFeedback.InvalidatedByUserId = userId.Value; existingFeedback.InvalidatedDate = DateTime.UtcNow; existingFeedback.InvalidationReason = "Feedback changed"; } _dbContext.ReportFeedbacks.Add(new DbReportFeedback { FeedbackId = request.FeedbackId, ReportId = request.ReportId, UserId = userId.Value }); _dbContext.SaveChanges(); _dbContext.ProcessReport(request.ReportId); _dbContext.SaveChanges(); return(Ok()); }
public IActionResult RegisterPost([FromBody] RegisterPostRequest request) { var botId = GetBotId(); if (!botId.HasValue) { throw new HttpStatusException(HttpStatusCode.BadRequest, "Invalid or missing botId in claim"); } if (string.IsNullOrWhiteSpace(request.Title)) { throw new HttpStatusException(HttpStatusCode.BadRequest, "Title is required"); } if (string.IsNullOrWhiteSpace(request.ContentUrl)) { throw new HttpStatusException(HttpStatusCode.BadRequest, "ContentUrl is required"); } if (!request.ContentId.HasValue) { throw new HttpStatusException(HttpStatusCode.BadRequest, "ContentId is required"); } if (string.IsNullOrWhiteSpace(request.ContentSite)) { throw new HttpStatusException(HttpStatusCode.BadRequest, "ContentSite is required"); } if (string.IsNullOrWhiteSpace(request.ContentType)) { throw new HttpStatusException(HttpStatusCode.BadRequest, "ContentType is required"); } var report = new DbReport { AuthorName = request.AuthorName, AuthorReputation = request.AuthorReputation, DashboardId = botId.Value, Title = request.Title, ContentUrl = request.ContentUrl, ContentId = request.ContentId.Value, ContentSite = request.ContentSite, ContentType = request.ContentType, ContentCreationDate = request.ContentCreationDate?.ToUniversalTime(), DetectedDate = request.DetectedDate?.ToUniversalTime(), DetectionScore = request.DetectionScore, Feedbacks = new List <DbReportFeedback>(), ConflictExceptions = new List <DbConflictException>() }; var dashboard = _dbContext.Dashboards.FirstOrDefault(d => d.Id == botId); if (dashboard == null) { throw new HttpStatusException(HttpStatusCode.BadRequest, $"Dashboard with id {botId} does not exist"); } report.RequiredFeedback = request.RequiredFeedback ?? dashboard.RequiredFeedback; report.RequiredFeedbackConflicted = request.RequiredFeedbackConflicted ?? dashboard.RequiredFeedbackConflicted; var contentFragments = request.ContentFragments ?? Enumerable.Empty <RegisterPostContentFragment>(); var fragments = string.IsNullOrWhiteSpace(request.Content) ? contentFragments : new[] { new RegisterPostContentFragment { Content = request.Content, Name = "Original", Order = 0, RequiredScope = string.Empty } }.Concat(contentFragments.Select(cf => new RegisterPostContentFragment { Content = cf.Content, Name = cf.Name, Order = cf.Order + 1, RequiredScope = cf.RequiredScope })); foreach (var contentFragment in fragments) { var dbContentFragment = new DbContentFragment { Order = contentFragment.Order, Name = contentFragment.Name, Content = contentFragment.Content, RequiredScope = contentFragment.RequiredScope, Report = report }; _dbContext.ContentFragments.Add(dbContentFragment); } _dbContext.Reports.Add(report); if (request.AllowedFeedback?.Any() ?? false) { var feedbackTypes = _dbContext.Feedbacks.Where(f => f.DashboardId == botId && request.AllowedFeedback.Contains(f.Name)).ToDictionary(f => f.Name, f => f.Id); foreach (var allowedFeedback in request.AllowedFeedback) { if (feedbackTypes.ContainsKey(allowedFeedback)) { _dbContext.ReportAllowedFeedbacks.Add(new DbReportAllowedFeedback { FeedbackId = feedbackTypes[allowedFeedback], Report = report }); } else { throw new HttpStatusException(HttpStatusCode.BadRequest, $"Feedback '{allowedFeedback}' not registered for bot"); } } } if (request.Reasons?.Any() ?? false) { var reasons = _dbContext.Reasons.Where(f => f.DashboardId == botId).ToDictionary(f => f.Name, f => f); foreach (var reason in request.Reasons ?? Enumerable.Empty <RegisterPostReason>()) { DbReason dbReason; if (reasons.ContainsKey(reason.ReasonName)) { dbReason = reasons[reason.ReasonName]; } else { dbReason = new DbReason { Name = reason.ReasonName, DashboardId = botId.Value }; _dbContext.Reasons.Add(dbReason); } _dbContext.ReportReasons.Add(new DbReportReason { Confidence = reason.Confidence, Tripped = reason.Tripped ?? false, Reason = dbReason, Report = report }); } } foreach (var attribute in request.Attributes ?? Enumerable.Empty <RegisterPostAttribute>()) { _dbContext.ReportAttributes.Add(new DbReportAttribute { Name = attribute.Key, Value = attribute.Value, Report = report }); } _dbContext.SaveChanges(); _dbContext.ProcessReport(report.Id); _dbContext.SaveChanges(); return(Json(report.Id)); }