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
4 changes: 2 additions & 2 deletions RikiPath.Application/Requests/StudyCourses/CourseRequests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ namespace RikiPath.Application.Requests.StudyCourses;
public sealed record SaveCoursePlan(string Name, int CertificateLevelId, int DurationMonths, int SessionsPerWeek,
int? SessionMinutes, int? TotalSessions, int? RetakeClaimDays, decimal Price, bool IsPublished, int Revision = 1,
string SessionType = "Individual", int Capacity = 1, DateTime? TargetExamDate = null);
public sealed record PurchaseCourse(int? PlanId, Guid IdempotencyKey, int? CohortId = null, string? EntitlementReference = null);
public sealed record PurchaseCourse(Guid IdempotencyKey, int CohortId, string? EntitlementReference = null);
public sealed record EnrollCohortRequest(Guid IdempotencyKey, string? EntitlementReference = null);
public sealed record CancelCohortEnrollmentRequest(string? Reason = null);
public sealed record ActivateCourse(int MentorId, DateTime StartsAt);
Expand All @@ -29,7 +29,7 @@ public sealed record SaveRetakeSupportPlan(
public sealed record GrantCohortEntitlementRequest(int LearnerId, string SourceType = "AdminManualGrant",
string? EntitlementReference = null, string? Note = null, DateTime? ExpiresAtUtc = null);
public sealed record RevokeCohortEntitlementRequest(string Reason);
public sealed record SaveStudyCohort(int PlanId, string Name, int MentorId, int Capacity,
public sealed record SaveStudyCohort(string Name, int MentorId, int Capacity,
List<int> AvailabilityIds, bool Publish, int Revision = 1, int? SourceProposalId = null);
public sealed record AdminCohortSessionTime(DateTime StartsAtUtc, DateTime EndsAtUtc);
public sealed record CreateAdminCohortRequest(string Name, string Level, int MentorId, int Capacity, decimal Price,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public class LearningPathResponse
public int Version { get; set; }
public string Status { get; set; } = "ACTIVE";
public string Horizon { get; set; } = "long_term";
/// <summary>Số ngày trong chặng ôn AI được tạo, tối đa bảy ngày.</summary>
public int PlanDurationDays { get; set; }
public DateTime? TargetExamDate { get; set; }
public int? DaysUntilExam { get; set; }
Expand All @@ -30,6 +31,7 @@ public class LearningPathResponse
public int? CertificateTypeId { get; set; }
public string CertificateTypeName { get; set; } = "JLPT";
public int? TargetCertificateLevelId { get; set; }
/// <summary>Số ngày học chi tiết của chặng hiện tại; ước tính tới kỳ thi nằm ở RecommendedStudyDays.</summary>
public int TotalStudyDays { get; set; }
public DateTime? DetailedPlanEndDate { get; set; }
public int? SuggestedDailyStudyMinutes { get; set; }
Expand Down
12 changes: 6 additions & 6 deletions RikiPath.Application/Services/CohortPreparationZipService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,17 @@ public async Task<ApiResponse<CohortPreparationZipResponse>> GetAsync(int cohort
{
var actor = claims.GetUserClaim();
var cohort = await uow.StudyCohorts.Query().AsNoTracking()
.Where(c => c.Id == cohortId).Select(c => new { c.MentorId, c.Status }).FirstOrDefaultAsync(ct);
.Where(c => c.Id == cohortId).Select(c => new { c.MentorId }).FirstOrDefaultAsync(ct);
if (cohort is null) return ApiResponse<CohortPreparationZipResponse>.NotFound("Không tìm thấy lớp học.");
var authorized = actor.Role switch
{
Role.Admin => true,
Role.Mentor => cohort.MentorId == actor.Id,
Role.Learner => cohort.Status is "Published" or "Completed"
&& await uow.StudyCourses.Query().AsNoTracking().AnyAsync(c =>
c.CohortId == cohortId && c.LearnerId == actor.Id
&& (c.PaymentStatus == "Paid" || c.PaymentStatus == "PartiallyRefunded")
&& (c.Status == "Ready" || c.Status == "Active" || c.Status == "Completed"), ct),
Role.Learner => await uow.StudyCourses.Query().AsNoTracking().AnyAsync(c =>
c.CohortId == cohortId && c.LearnerId == actor.Id
&& (c.PaymentStatus == "Paid" || c.PaymentStatus == "PartiallyRefunded")
&& (c.Status == "Ready" || c.Status == "Active" || c.Status == "Completed"
|| c.Status == "Transferred"), ct),
_ => false
};
if (!authorized) return ApiResponse<CohortPreparationZipResponse>.Forbidden("Bạn không có quyền tải tài liệu lớp.");
Expand Down
22 changes: 16 additions & 6 deletions RikiPath.Application/Services/HomeworkService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ public async Task<List<HomeworkLearner>> GetLearnersAsync(CancellationToken ct,
{
var mentorId = claims.GetUserClaim().Id;
var enrollments = await unit.StudyCourses.Query()
.Where(c => c.CohortId != null && (c.PaymentStatus == "Paid" || c.PaymentStatus == "PartiallyRefunded") && c.Status == "Active"
.Where(c => c.CohortId != null && (c.PaymentStatus == "Paid" || c.PaymentStatus == "PartiallyRefunded")
&& (c.Status == "Active" || c.Status == "Completed")
&& c.Cohort!.MentorId == mentorId && !c.Learner.IsDeleted)
.OrderByDescending(c => c.StartsAt ?? c.Cohort!.StartsAt)
.ThenByDescending(c => c.Id)
Expand All @@ -59,14 +60,22 @@ public async Task<List<HomeworkLearner>> GetLearnersAsync(CancellationToken ct,
.OrderBy(c => c.LearnerName).ToList();
}

public async Task<List<HomeworkCatalogItem>> GetCatalogAsync(string? level, string? search, int page, CancellationToken ct)
public async Task<List<HomeworkCatalogItem>> GetCatalogAsync(string? level, string? search, int page, CancellationToken ct,
string? skill = null)
{
var query = VisibleLessons();
if (!string.IsNullOrWhiteSpace(level))
{
var code = level.Trim().ToUpperInvariant();
query = query.Where(lesson => lesson.CertificateLevel.Code == code);
}
if (!string.IsNullOrWhiteSpace(skill))
{
var code = skill.Trim().ToUpperInvariant();
if (code is not ("VOCABULARY" or "KANJI" or "GRAMMAR" or "READING" or "LISTENING" or "WRITING" or "SPEAKING"))
return [];
query = query.Where(lesson => lesson.LanguageSkill.Code == code);
}
if (!string.IsNullOrWhiteSpace(search))
{
var term = search.Trim().ToLowerInvariant();
Expand Down Expand Up @@ -121,8 +130,8 @@ public async Task<ApiResponse<List<int>>> CreateCohortAssignmentsAsync(int cohor
return ApiResponse<List<int>>.NotFound("Không tìm thấy lớp nhóm.");
if (cohort.MentorId != user.Id)
return ApiResponse<List<int>>.Fail("Bạn chỉ được giao bài tập cho lớp nhóm do mình phụ trách.", HttpStatusCode.Forbidden);
if (cohort.Status is "Cancelled" or "Completed" or "Draft")
return ApiResponse<List<int>>.Fail("Lớp học đã kết thúc, đã hủy hoặc chưa mở; không thể giao bài tập.", HttpStatusCode.Conflict);
if (cohort.Status is "Cancelled" or "Draft")
return ApiResponse<List<int>>.Fail("Lớp học đã bị hủy hoặc chưa mở; không thể giao bài tập.", HttpStatusCode.Conflict);

var validSessions = cohort.Sessions
.Where(s => s.Availability is not null
Expand All @@ -133,12 +142,13 @@ public async Task<ApiResponse<List<int>>> CreateCohortAssignmentsAsync(int cohor
.ToList();
var firstValidSessionStartUtc = BusinessDateHelper.EnsureUtc(
validSessions.FirstOrDefault()?.Availability.StartTime ?? cohort.StartsAt);
var eligibleEnrollmentStatus = cohort.Status == "Completed" ? "Completed" : "Active";
var activeEnrollments = cohort.Enrollments
.Where(e => e.PaymentStatus is "Paid" or "PartiallyRefunded" && e.Status == "Active")
.Where(e => e.PaymentStatus is "Paid" or "PartiallyRefunded" && e.Status == eligibleEnrollmentStatus)
.ToList();
var confirmedLearnerCount = activeEnrollments.Select(e => e.LearnerId).Distinct().Count();
if (confirmedLearnerCount < 1)
return ApiResponse<List<int>>.Fail("Lớp học chưa có học viên ghi danh chính thức để bắt đầu.", HttpStatusCode.Conflict);
return ApiResponse<List<int>>.Fail("Lớp học chưa có học viên đủ điều kiện nhận bài tập.", HttpStatusCode.Conflict);

var hasDeliveredSession = activeEnrollments
.SelectMany(e => e.Sessions)
Expand Down
12 changes: 10 additions & 2 deletions RikiPath.Application/Services/LearningPathPlanningPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,16 @@ public static int AvailableStudyDays(DateTime today, DateTime examDate)
public static DateTime SuggestedExamDate(DateTime today, int studyDays)
{
var earliest = today.Date.AddDays(studyDays + RestDays);
if (BusinessDateHelper.IsJlptExamMonth(earliest)) return earliest;
return new DateTime(earliest.Year, earliest.Month < 7 ? 7 : 12, 1);
static DateTime FirstSunday(int year, int month)
{
var first = new DateTime(year, month, 1);
return first.AddDays((7 - (int)first.DayOfWeek) % 7);
}

var july = FirstSunday(earliest.Year, 7);
if (earliest <= july) return july;
var december = FirstSunday(earliest.Year, 12);
return earliest <= december ? december : FirstSunday(earliest.Year + 1, 7);
}

public static (int Min, int Max) ResolveDailyMinutes(int? fixedMinutes, int? min, int? max,
Expand Down
16 changes: 8 additions & 8 deletions RikiPath.Application/Services/LearningPathService.Assessment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,12 @@ private static string BuildStudyTimeAdvice(List<LearningPathStudyTimeScenario> s
return "Cần ngày thi và bằng chứng của đủ bốn kỹ năng để so sánh thời lượng. Giữ kế hoạch trong khoảng phút đã chọn.";
if (scenarios.Single(x => x.MinutesPerDay == minimum).CanMeetRequestedExamDate == true)
return $"Mức {minimum} phút/ngày có thể đủ về thời gian theo ước tính; phần tăng đến {maximum} phút là tùy chọn. Kết quả thi còn phụ thuộc chất lượng ôn luyện và tiến bộ thực tế.";
if (suggested is { } minutes && minutes <= maximum)
return $"Mức {minimum} phút/ngày chưa đủ theo ước tính. Lộ trình dùng {minutes} phút/ngày trong khoảng thời gian bạn đã chọn để hướng tới ngày thi gốc. " +
"Cần duy trì đều mức này; tăng số phút học không bảo đảm chắc chắn đỗ.";
return suggested.HasValue
? $"Mức {minimum} phút/ngày chưa đủ theo ước tính. Nếu có thể duy trì khoảng {suggested} phút/ngày, mục tiêu có thể khả thi hơn trong quỹ thời gian còn lại. " +
(suggested > maximum ? "Mức này vượt khoảng thời gian đã chọn; chỉ áp dụng khi bạn đồng ý điều chỉnh. " : "Mức này nằm trong khoảng thời gian bạn đã chọn. ") +
? $"Mức {minimum} phút/ngày chưa đủ theo ước tính. Muốn kịp ngày thi gốc có thể cần khoảng {suggested} phút/ngày, " +
$"vượt mức tối đa {maximum} phút bạn đã chọn. Lịch thay thế vẫn dùng mức đã chọn; chỉ tăng khi bạn đồng ý điều chỉnh. " +
"Không thể khẳng định chắc chắn đỗ chỉ bằng cách tăng số phút học."
: "Theo dữ liệu hiện tại chưa tìm được mức học tối đa 480 phút/ngày có đủ cơ sở để kết luận kịp lịch. Cần bổ sung đánh giá hoặc cân nhắc ngày thi/mục tiêu khác.";
}
Expand All @@ -54,14 +57,11 @@ private static string SerializeLessonContent(Lesson lesson)
=> JsonSerializer.Serialize(new
{
kanji = (lesson.LessonKanjis ?? []).Where(x => !x.Kanji.IsDeleted)
.Select(x => new { x.Kanji.Id, x.Kanji.Character, x.Kanji.Meaning, x.Kanji.SinoVietnamese,
x.Kanji.OnYomi, x.Kanji.KunYomi, x.Kanji.StrokeCount }),
.Select(x => new { x.Kanji.Character, x.Kanji.Meaning }).Take(3),
vocabulary = (lesson.LessonVocabularies ?? []).Where(x => !x.Vocabulary.IsDeleted)
.Select(x => new { x.Vocabulary.Id, x.Vocabulary.Word, x.Vocabulary.Reading, x.Vocabulary.Meaning,
x.Vocabulary.ExampleSentence, x.Vocabulary.ExampleSentenceMeaning }),
.Select(x => new { x.Vocabulary.Word, x.Vocabulary.Reading, x.Vocabulary.Meaning }).Take(3),
grammar = (lesson.LessonGrammars ?? []).Where(x => !x.GrammarPattern.IsDeleted)
.Select(x => new { x.GrammarPattern.Id, x.GrammarPattern.Title, x.GrammarPattern.Structure,
x.GrammarPattern.UsageNotes, x.GrammarPattern.ExampleSentence, x.GrammarPattern.ExampleSentenceMeaning })
.Select(x => new { x.GrammarPattern.Title, x.GrammarPattern.Structure }).Take(3)
}, StoredPathJsonOptions);

private static List<LearningPathPhaseResponse> BuildPhases(DateTime today, int days)
Expand Down
Loading
Loading