Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions RikiPath.Application/IServices/IContentApprovalService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Http;
using RikiPath.Application.Common;
using RikiPath.Application.Requests.Content;
using RikiPath.Application.Responses;
using RikiPath.Application.Responses.Content;
Expand All @@ -23,8 +24,8 @@ Task<ApiResponse<int>> SubmitImportRequestsAsync(
/// <summary>
/// Lấy danh sách các yêu cầu đang chờ duyệt (Pending Review).
/// </summary>
Task<ApiResponse<List<PendingApprovalItemResponse>>> GetPendingApprovalsAsync(
ImportEntityType? entityType, CancellationToken cancellationToken = default);
Task<ApiResponse<PagedResult<PendingApprovalItemResponse>>> GetPendingApprovalsAsync(
ImportEntityType? entityType, int page, int pageSize, CancellationToken cancellationToken = default);

/// <summary>
/// Duyệt hoặc từ chối một yêu cầu duyệt nội dung cụ thể.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Task<ListeningAudioUploadResponse> UploadAsync(
Stream audio, string fileName, int authorId, CancellationToken ct);
Task<ContentAudioTranscriptionResponse?> QueueAsync(string audioUrl, int? authorId, CancellationToken ct);
Task QueuePayloadAsync(JsonElement payload, int authorId, CancellationToken ct);
Task QueuePayloadsAsync(IReadOnlyList<string> payloadJsons, int authorId, CancellationToken ct);
Task<ContentAudioTranscriptionResponse?> GetAsync(string audioUrl, CancellationToken ct);
Task<IReadOnlyList<ContentAudioTranscriptionResponse>> ListMineAsync(int authorId, CancellationToken ct);
Task<ApiResponse<ContentAudioTranscriptionResponse>> RetryAsync(string audioUrl, CancellationToken ct);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ namespace RikiPath.Application.Responses.Content
public class ImportConfirmResponse
{
public Guid BatchId { get; set; }
/// <summary>Tổng số bản nháp đã lưu trong BatchId này, kể cả các phần gửi trước.</summary>
public int DraftsCreatedCount { get; set; }

/// <summary>Số dòng server phòng thủ bỏ qua (Action không phải New/Modified, hoặc thiếu PayloadJson) - lẽ ra FE đã lọc trước khi gửi.</summary>
/// <summary>Số dòng bị bỏ qua trong request hiện tại, gồm cả các dòng đã lưu trước đó.</summary>
public int SkippedCount { get; set; }
}
}
70 changes: 51 additions & 19 deletions RikiPath.Application/Services/ContentApprovalService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using RikiPath.Application.Handlers;
using RikiPath.Application.Common;
using RikiPath.Application.IClients;
using RikiPath.Application.IServices;
using RikiPath.Application.Requests.Content;
Expand Down Expand Up @@ -87,21 +88,29 @@ public Task<ApiResponse<int>> SubmitImportRequestsAsync(
System.Net.HttpStatusCode.Gone));
}

public async Task<ApiResponse<List<PendingApprovalItemResponse>>> GetPendingApprovalsAsync(
ImportEntityType? entityType, CancellationToken cancellationToken)
public async Task<ApiResponse<PagedResult<PendingApprovalItemResponse>>> GetPendingApprovalsAsync(
ImportEntityType? entityType, int page, int pageSize, CancellationToken cancellationToken)
{
try
{
var query = unitOfWork.ContentApprovalRequests.Query()
page = Math.Max(1, page);
pageSize = Math.Clamp(pageSize, 1, 100);
var query = unitOfWork.ContentApprovalRequests.Query().AsNoTracking()
.Include(r => r.SubmittedBy)
.Where(r => !r.IsDeleted && r.Status == ContentApprovalStatus.PendingReview);

if (entityType.HasValue)
query = query.Where(r => r.EntityType == entityType.Value);

var requests = await query.OrderByDescending(r => r.SubmittedAt).ToListAsync(cancellationToken);
var total = await query.CountAsync(cancellationToken);
var offset = (int)Math.Min((long)(page - 1) * pageSize, int.MaxValue);
var requests = await query.OrderByDescending(r => r.SubmittedAt).ThenByDescending(r => r.Id)
.Skip(offset).Take(pageSize).ToListAsync(cancellationToken);
if (requests.Count == 0)
return ApiResponse<List<PendingApprovalItemResponse>>.Success([]);
return ApiResponse<PagedResult<PendingApprovalItemResponse>>.Success(new()
{
Page = page, PageSize = pageSize, TotalCount = total,
});

var builderDict = diffBuilders.ToDictionary(b => b.EntityType);
var responseList = new List<PendingApprovalItemResponse>();
Expand Down Expand Up @@ -145,11 +154,17 @@ public async Task<ApiResponse<List<PendingApprovalItemResponse>>> GetPendingAppr

await AttachLessonRelatedContentAsync(requests, responseList, cancellationToken);
await AttachMockTestPassagesAsync(requests, responseList, cancellationToken);
return ApiResponse<List<PendingApprovalItemResponse>>.Success(responseList);
var requestOrder = requests.Select((request, index) => (request.Id, index))
.ToDictionary(pair => pair.Id, pair => pair.index);
return ApiResponse<PagedResult<PendingApprovalItemResponse>>.Success(new()
{
Page = page, PageSize = pageSize, TotalCount = total,
Items = responseList.OrderBy(item => requestOrder[item.RequestId]).ToList(),
});
}
catch (Exception ex)
{
return ApiResponse<List<PendingApprovalItemResponse>>.Fail($"Không thể lấy danh sách chờ duyệt: {ex.Message}");
return ApiResponse<PagedResult<PendingApprovalItemResponse>>.Fail($"Không thể lấy danh sách chờ duyệt: {ex.Message}");
}
}

Expand Down Expand Up @@ -270,9 +285,14 @@ private async Task AttachLessonRelatedContentAsync(
if (lessons.Count == 0) return;

var lessonKeys = lessons.Values.Select(p => LessonKey(p.Title, p.CertificateLevelCode)).ToHashSet(StringComparer.OrdinalIgnoreCase);
var relatedRequests = await unitOfWork.ContentApprovalRequests.Query()
.Where(r => !r.IsDeleted && (r.Status == ContentApprovalStatus.Approved
|| r.Status == ContentApprovalStatus.PendingReview)
var batchIds = requests.Where(r => r.EntityType == ImportEntityType.Lesson)
.Select(r => r.BatchId).Where(id => id != Guid.Empty).Distinct().ToArray();
var manualAuthorIds = requests.Where(r => r.EntityType == ImportEntityType.Lesson && r.BatchId == Guid.Empty)
.Select(r => r.SubmittedByUserId).Distinct().ToArray();
var relatedRequests = await unitOfWork.ContentApprovalRequests.Query().AsNoTracking()
.Where(r => !r.IsDeleted && (batchIds.Contains(r.BatchId) ||
(r.BatchId == Guid.Empty && manualAuthorIds.Contains(r.SubmittedByUserId)))
&& (r.Status == ContentApprovalStatus.Approved || r.Status == ContentApprovalStatus.PendingReview)
&& (r.EntityType == ImportEntityType.Kanji || r.EntityType == ImportEntityType.Grammar
|| r.EntityType == ImportEntityType.Vocabulary || r.EntityType == ImportEntityType.PracticeExercise))
.ToListAsync(cancellationToken);
Expand Down Expand Up @@ -336,12 +356,28 @@ private async Task AttachLessonRelatedContentAsync(
}).ToList();
}

var dbLessons = await unitOfWork.Lessons.Query()
.Include(l => l.CertificateLevel)
var lessonIds = requests.Where(r => r.EntityType == ImportEntityType.Lesson && r.TargetEntityId.HasValue)
.Select(r => r.TargetEntityId!.Value).Distinct().ToArray();
var lessonTitles = lessons.Values.Select(lesson => lesson.Title).Distinct().ToArray();
var levelIds = lessons.Values.Select(lesson => lesson.CertificateLevelId).Distinct().ToArray();
var dbLessons = await unitOfWork.Lessons.Query().AsNoTracking()
.Include(l => l.LessonKanjis!).ThenInclude(x => x.Kanji)
.Include(l => l.LessonGrammars!).ThenInclude(x => x.GrammarPattern)
.Include(l => l.LessonVocabularies!).ThenInclude(x => x.Vocabulary)
.Where(l => !l.IsDeleted)
.Where(l => !l.IsDeleted && (lessonIds.Contains(l.Id) ||
(lessonTitles.Contains(l.Title) && levelIds.Contains(l.CertificateLevelId))))
.AsSplitQuery()
.ToListAsync(cancellationToken);

var dbLessonIds = dbLessons.Select(lesson => lesson.Id).ToArray();
var exercises = await unitOfWork.PracticeExercises.Query().AsNoTracking()
.Where(exercise => !exercise.IsDeleted && exercise.LessonId.HasValue &&
dbLessonIds.Contains(exercise.LessonId.Value))
.Select(exercise => new
{
exercise.LessonId, exercise.Title, exercise.Description,
QuestionCount = exercise.Questions!.Count(question => !question.IsDeleted)
})
.ToListAsync(cancellationToken);

foreach (var response in responses.Where(x => x.EntityType == ImportEntityType.Lesson))
Expand All @@ -354,19 +390,15 @@ private async Task AttachLessonRelatedContentAsync(
AddDbRelated(response, "Kanji", dbLesson.LessonKanjis?.Where(x => x.Kanji is { IsDeleted: false }).Select(x => (x.Kanji.Character, x.Kanji.Meaning)));
AddDbRelated(response, "Grammar", dbLesson.LessonGrammars?.Where(x => x.GrammarPattern is { IsDeleted: false }).Select(x => (x.GrammarPattern.Title, x.GrammarPattern.Structure)));
AddDbRelated(response, "Vocabulary", dbLesson.LessonVocabularies?.Where(x => x.Vocabulary is { IsDeleted: false }).Select(x => ($"{x.Vocabulary.Word} ({x.Vocabulary.Reading})", x.Vocabulary.Meaning)));
var exercises = await unitOfWork.PracticeExercises.Query()
.Include(x => x.Questions!).ThenInclude(x => x.Options)
.Where(x => !x.IsDeleted && x.LessonId == dbLesson.Id)
.ToListAsync(cancellationToken);
foreach (var exercise in exercises)
foreach (var exercise in exercises.Where(item => item.LessonId == dbLesson.Id))
response.RelatedLessonContent.Add(new PendingLessonContentResponse
{
EntityType = "PracticeExercise",
Action = "Existing",
Status = "Approved",
RowKey = exercise.Title,
Fields = [new ApprovalFieldDiffResponse { Field = "description", Label = "Mô tả", NewValue = exercise.Description },
new ApprovalFieldDiffResponse { Field = "questions", Label = "Số câu hỏi", NewValue = (exercise.Questions?.Count(q => !q.IsDeleted) ?? 0).ToString() }]
new ApprovalFieldDiffResponse { Field = "questions", Label = "Số câu hỏi", NewValue = exercise.QuestionCount.ToString() }]
});
}
}
Expand Down
Loading
Loading