From faa2743b847b1d239b87bbe6195f4805d9666155 Mon Sep 17 00:00:00 2001 From: biss Date: Sat, 14 Nov 2026 20:53:48 +0800 Subject: [PATCH] Fix assessment workflow and physical admin data scope --- .../ComprehensiveAssessmentsController.cs | 25 ++++++++---- .../Controllers/PhysicalFitnessController.cs | 2 +- .../Auth/CurrentUserDataScope.cs | 5 ++- ...ComprehensiveAssessmentsControllerTests.cs | 39 +++++++++++++++++-- .../CounselorDataScopeTests.cs | 20 +++++++++- tests/Jiaowu.Api.Tests/DataScopeTests.cs | 4 ++ .../PhysicalFitnessControllerTests.cs | 21 +++++++++- web/src/i18n/messages/en-US/assessment.ts | 4 ++ web/src/i18n/messages/zh-CN/assessment.ts | 4 ++ web/src/views/ApprovalCenterView.vue | 4 +- .../views/ComprehensiveAssessmentsView.vue | 28 ++++++++++++- 11 files changed, 135 insertions(+), 21 deletions(-) diff --git a/src/Jiaowu.Api/Controllers/ComprehensiveAssessmentsController.cs b/src/Jiaowu.Api/Controllers/ComprehensiveAssessmentsController.cs index 724893c..10abed7 100644 --- a/src/Jiaowu.Api/Controllers/ComprehensiveAssessmentsController.cs +++ b/src/Jiaowu.Api/Controllers/ComprehensiveAssessmentsController.cs @@ -1253,12 +1253,7 @@ public sealed class ComprehensiveAssessmentsController( { page = Math.Max(page, 1); pageSize = Math.Clamp(pageSize, 10, 100); - var query = ApplyDeclarationScope(db.ComprehensiveAssessmentDeclarations.AsNoTracking()) - .Where(x => x.Status == ComprehensiveAssessmentDeclarationStatus.Submitted || x.Status == ComprehensiveAssessmentDeclarationStatus.CounselorApproved); - var current = currentUserDataScope.Current; - query = current.Scope == DataScope.Class && current.IsInRole(SystemRoles.Counselor) - ? query.Where(x => x.Status == ComprehensiveAssessmentDeclarationStatus.Submitted) - : query.Where(x => x.Status == ComprehensiveAssessmentDeclarationStatus.CounselorApproved); + var query = PendingDeclarationScope(db.ComprehensiveAssessmentDeclarations.AsNoTracking()); if (batchId.HasValue) query = query.Where(x => x.ComprehensiveAssessmentBatchId == batchId); if (!string.IsNullOrWhiteSpace(keyword)) { @@ -1278,10 +1273,10 @@ public sealed class ComprehensiveAssessmentsController( [Authorize(Roles = Reviewers)] public async Task ReviewDeclaration(Guid id, ReviewAssessmentDeclarationRequest request, CancellationToken token) { - var item = await ApplyDeclarationScope(db.ComprehensiveAssessmentDeclarations).Include(x => x.Dimension).FirstOrDefaultAsync(x => x.Id == id, token); + var item = await PendingDeclarationScope(db.ComprehensiveAssessmentDeclarations).Include(x => x.Dimension).FirstOrDefaultAsync(x => x.Id == id, token); if (item is null) return NotFound(); var current = currentUserDataScope.Current; - var counselorStage = current.Scope == DataScope.Class && current.IsInRole(SystemRoles.Counselor); + var counselorStage = item.Status == ComprehensiveAssessmentDeclarationStatus.Submitted; if (counselorStage) { if (item.Status != ComprehensiveAssessmentDeclarationStatus.Submitted) return ConflictProblem("材料不在辅导员初审阶段。"); @@ -1471,6 +1466,20 @@ public sealed class ComprehensiveAssessmentsController( return query; } + private IQueryable PendingDeclarationScope(IQueryable query) + { + var current = currentUserDataScope.Current; + var counselor = current.IsInRole(SystemRoles.Counselor); + var schoolReviewer = current.IsInRole(SystemRoles.SuperAdmin) || + current.IsInRole(SystemRoles.AcademicAdmin) || current.IsInRole(SystemRoles.StudentAffairsAdmin); + var collegeReviewer = current.IsInRole(SystemRoles.CollegeAdmin) && current.CollegeId.HasValue; + return query.Where(x => + (counselor && x.Status == ComprehensiveAssessmentDeclarationStatus.Submitted && + x.Student!.AdministrativeClass!.CounselorUserId == current.UserId) || + ((schoolReviewer || (collegeReviewer && x.Student!.AdministrativeClass!.Major!.CollegeId == current.CollegeId)) && + x.Status == ComprehensiveAssessmentDeclarationStatus.CounselorApproved)); + } + private IQueryable ApplyAppealScope(IQueryable query) { var current = currentUserDataScope.Current; diff --git a/src/Jiaowu.Api/Controllers/PhysicalFitnessController.cs b/src/Jiaowu.Api/Controllers/PhysicalFitnessController.cs index 24db851..33420a2 100644 --- a/src/Jiaowu.Api/Controllers/PhysicalFitnessController.cs +++ b/src/Jiaowu.Api/Controllers/PhysicalFitnessController.cs @@ -516,7 +516,7 @@ public sealed class PhysicalFitnessController(AppDbContext db, ICurrentUserDataS private IQueryable ApplyStudentScope(IQueryable source) { var current = scope.Current; - if (current.Scope == DataScope.All) return source; + if (current.IsInRole(SystemRoles.PhysicalEducationAdmin) || current.Scope == DataScope.All) return source; if (current.Scope == DataScope.College) return source.Where(x => x.AdministrativeClass!.Major!.CollegeId == current.RestrictedCollegeId); if (current.Scope == DataScope.Class) diff --git a/src/Jiaowu.Api/Infrastructure/Auth/CurrentUserDataScope.cs b/src/Jiaowu.Api/Infrastructure/Auth/CurrentUserDataScope.cs index 2b6310c..269c6a6 100644 --- a/src/Jiaowu.Api/Infrastructure/Auth/CurrentUserDataScope.cs +++ b/src/Jiaowu.Api/Infrastructure/Auth/CurrentUserDataScope.cs @@ -60,6 +60,9 @@ public sealed class CurrentUserDataScope(IHttpContextAccessor httpContextAccesso public static class EffectiveDataScopeResolver { + // Physical education administrators have school-wide scope only in the + // physical-fitness domain, not in unrelated academic or student data. public static DataScope Resolve(IEnumerable roles) => - RolePermissionCatalog.ResolveDataScope(roles); + RolePermissionCatalog.ResolveDataScope(roles.Where(role => + !string.Equals(role, SystemRoles.PhysicalEducationAdmin, StringComparison.OrdinalIgnoreCase))); } diff --git a/tests/Jiaowu.Api.Tests/ComprehensiveAssessmentsControllerTests.cs b/tests/Jiaowu.Api.Tests/ComprehensiveAssessmentsControllerTests.cs index ccb723c..b772126 100644 --- a/tests/Jiaowu.Api.Tests/ComprehensiveAssessmentsControllerTests.cs +++ b/tests/Jiaowu.Api.Tests/ComprehensiveAssessmentsControllerTests.cs @@ -253,6 +253,35 @@ public sealed class ComprehensiveAssessmentsControllerTests Assert.Equal("本班材料", page.Items.Single().Title); } + [Fact] + public async Task CounselorWithCollegeRole_CanReviewAssignedInitialAndCollegeFinalStages() + { + await using var fixture = await Fixture.CreateAsync(includeSecondStudent: true, secondClass: true); + var assigned = Declaration(fixture.Batch.Id, fixture.Student.Id, fixture.DeclarationDimension.Id, "本班初审"); + var other = Declaration(fixture.Batch.Id, fixture.OtherStudent!.Id, fixture.DeclarationDimension.Id, "其他班初审"); + var final = Declaration(fixture.Batch.Id, fixture.OtherStudent.Id, fixture.DeclarationDimension.Id, "学院复核"); + final.Status = ComprehensiveAssessmentDeclarationStatus.CounselorApproved; + fixture.Db.ComprehensiveAssessmentDeclarations.AddRange(assigned, other, final); + await fixture.Db.SaveChangesAsync(); + var scope = new TestScope(fixture.CounselorScope.Current.UserId, fixture.Batch.CollegeId, + DataScope.College, SystemRoles.Counselor, SystemRoles.CollegeAdmin); + var controller = new ComprehensiveAssessmentsController(fixture.Db, scope); + + var response = Assert.IsType(await controller.PendingDeclarations(token: CancellationToken.None)); + var page = Assert.IsType>(response.Value); + Assert.Equal(2, page.Total); + Assert.Contains(page.Items, x => x.Id == assigned.Id); + Assert.Contains(page.Items, x => x.Id == final.Id); + Assert.DoesNotContain(page.Items, x => x.Id == other.Id); + + var request = new ReviewAssessmentDeclarationRequest(true, null, null); + Assert.IsType(await controller.ReviewDeclaration(other.Id, request, CancellationToken.None)); + Assert.IsType(await controller.ReviewDeclaration(assigned.Id, request, CancellationToken.None)); + Assert.Equal(ComprehensiveAssessmentDeclarationStatus.CounselorApproved, assigned.Status); + Assert.IsType(await controller.ReviewDeclaration(final.Id, request, CancellationToken.None)); + Assert.Equal(ComprehensiveAssessmentDeclarationStatus.Approved, final.Status); + } + [Fact] public async Task LiveResults_ArePagedAndLimitedToTheCounselorsAssignedClasses() { @@ -277,7 +306,9 @@ public sealed class ComprehensiveAssessmentsControllerTests await fixture.Db.SaveChangesAsync(); var counselorController = new ComprehensiveAssessmentsController( - fixture.Db, fixture.CounselorScope); + fixture.Db, new TestScope(fixture.CounselorScope.Current.UserId, null, + EffectiveDataScopeResolver.Resolve([SystemRoles.Counselor, SystemRoles.PhysicalEducationAdmin]), + SystemRoles.Counselor, SystemRoles.PhysicalEducationAdmin)); var counselorResponse = Assert.IsType(await counselorController.GetLiveResults( fixture.Batch.Id, token: CancellationToken.None)); var counselorPage = Assert.IsType>( @@ -322,7 +353,7 @@ public sealed class ComprehensiveAssessmentsControllerTests Assert.Empty(page.Items); var reviewResponse = await controller.ReviewDeclaration(declaration.Id, new ReviewAssessmentDeclarationRequest(true, 5, "学院复核"), CancellationToken.None); - Assert.IsType(reviewResponse); + Assert.IsType(reviewResponse); } [Fact] @@ -544,8 +575,8 @@ public sealed class ComprehensiveAssessmentsControllerTests protected override bool ShouldRetryOn(Exception exception) => false; } - private sealed class TestScope(Guid userId, Guid? collegeId, DataScope scope, string role) : ICurrentUserDataScope + private sealed class TestScope(Guid userId, Guid? collegeId, DataScope scope, params string[] roles) : ICurrentUserDataScope { - public CurrentUserScope Current { get; } = new(userId, "测试用户", collegeId, scope, new HashSet([role])); + public CurrentUserScope Current { get; } = new(userId, "测试用户", collegeId, scope, new HashSet(roles)); } } diff --git a/tests/Jiaowu.Api.Tests/CounselorDataScopeTests.cs b/tests/Jiaowu.Api.Tests/CounselorDataScopeTests.cs index 097d843..1e941d9 100644 --- a/tests/Jiaowu.Api.Tests/CounselorDataScopeTests.cs +++ b/tests/Jiaowu.Api.Tests/CounselorDataScopeTests.cs @@ -4,6 +4,7 @@ using Jiaowu.Api.Controllers; using Jiaowu.Api.Domain.Academic; using Jiaowu.Api.Domain.Identity; using Jiaowu.Api.Infrastructure.Auth; +using Jiaowu.Api.Infrastructure.Caching; using Jiaowu.Api.Infrastructure.Persistence; using Microsoft.AspNetCore.Mvc; using Microsoft.Data.Sqlite; @@ -13,6 +14,21 @@ namespace Jiaowu.Api.Tests; public sealed class CounselorDataScopeTests { + [Fact] + public async Task PhysicalEducationAdminCounselor_CourseCatalogExcludesUnrelatedCourses() + { + await using var fixture = await CounselorFixture.CreateAsync(); + var course = await fixture.Db.Courses.SingleAsync(); + fixture.Db.Courses.Add(new Course { Code = "UNRELATED", Name = "其他课程", CollegeId = course.CollegeId, + Credits = 2, TotalHours = 32, Nature = CourseNature.MajorRequired }); + await fixture.Db.SaveChangesAsync(); + var controller = new CoursesController(fixture.Db, fixture.Scope, NoOpAppCache.Instance); + var result = await controller.Get(cancellationToken: CancellationToken.None); + var page = Assert.IsType>(Assert.IsType(result.Result).Value); + var json = JsonSerializer.Serialize(page.Items); + Assert.Contains("COURSE-1", json); + Assert.DoesNotContain("UNRELATED", json); + } [Fact] public async Task Counselor_message_scope_contains_only_managed_class_students() { @@ -138,8 +154,8 @@ public sealed class CounselorDataScopeTests userId, "测试辅导员", collegeId, - DataScope.Class, - new HashSet([SystemRoles.Counselor])); + EffectiveDataScopeResolver.Resolve([SystemRoles.Counselor, SystemRoles.PhysicalEducationAdmin]), + new HashSet([SystemRoles.Counselor, SystemRoles.PhysicalEducationAdmin])); } public CurrentUserScope Current { get; } diff --git a/tests/Jiaowu.Api.Tests/DataScopeTests.cs b/tests/Jiaowu.Api.Tests/DataScopeTests.cs index 7a36f3a..0474273 100644 --- a/tests/Jiaowu.Api.Tests/DataScopeTests.cs +++ b/tests/Jiaowu.Api.Tests/DataScopeTests.cs @@ -14,6 +14,10 @@ public sealed class DataScopeTests [InlineData(SystemRoles.CollegeAdmin, SystemRoles.Counselor, DataScope.College)] [InlineData(SystemRoles.SuperAdmin, SystemRoles.CollegeAdmin, DataScope.All)] [InlineData(SystemRoles.AcademicAdmin, SystemRoles.Student, DataScope.All)] + [InlineData(SystemRoles.Counselor, SystemRoles.PhysicalEducationAdmin, DataScope.Class)] + [InlineData(SystemRoles.Teacher, SystemRoles.PhysicalEducationAdmin, DataScope.Self)] + [InlineData(SystemRoles.CollegeAdmin, SystemRoles.PhysicalEducationAdmin, DataScope.College)] + [InlineData(SystemRoles.PhysicalEducationAdmin, null, DataScope.Self)] public void Resolver_UsesHighestScope( string firstRole, string? secondRole, diff --git a/tests/Jiaowu.Api.Tests/PhysicalFitnessControllerTests.cs b/tests/Jiaowu.Api.Tests/PhysicalFitnessControllerTests.cs index c7cde51..d00bbd3 100644 --- a/tests/Jiaowu.Api.Tests/PhysicalFitnessControllerTests.cs +++ b/tests/Jiaowu.Api.Tests/PhysicalFitnessControllerTests.cs @@ -73,6 +73,23 @@ public sealed class PhysicalFitnessControllerTests Assert.Equal(1, visibleCount); } + [Fact] + public async Task PhysicalEducationAdminWithCounselorRole_CanSeeSchoolWidePhysicalRecords() + { + await using var fixture = await Fixture.CreateAsync(includeSecondCollege: true); + fixture.Db.PhysicalTestRecords.AddRange( + new PhysicalTestRecord { PhysicalTestBatchId = fixture.Batch.Id, StudentId = fixture.Student.Id, GradeLevel = 1, Status = PhysicalTestRecordStatus.Exempt, SpecialReason = "测试" }, + new PhysicalTestRecord { PhysicalTestBatchId = fixture.Batch.Id, StudentId = fixture.OtherStudent!.Id, GradeLevel = 1, Status = PhysicalTestRecordStatus.Exempt, SpecialReason = "测试" }); + await fixture.Db.SaveChangesAsync(); + var scope = new TestScope(null, EffectiveDataScopeResolver.Resolve([SystemRoles.Counselor, SystemRoles.PhysicalEducationAdmin]), + SystemRoles.Counselor, SystemRoles.PhysicalEducationAdmin); + var controller = new PhysicalFitnessController(fixture.Db, scope); + var action = Assert.IsType(await controller.GetRecords(fixture.Batch.Id, pageSize: 30, ct: CancellationToken.None)); + var json = System.Text.Json.JsonSerializer.Serialize(action.Value); + Assert.Contains(fixture.Student.StudentNumber, json); + Assert.Contains(fixture.OtherStudent.StudentNumber, json); + } + [Fact] public async Task Publish_RejectsEmptyBatch() { @@ -182,9 +199,9 @@ public sealed class PhysicalFitnessControllerTests } } - private sealed class TestScope(Guid? collegeId, DataScope dataScope, string role) : ICurrentUserDataScope + private sealed class TestScope(Guid? collegeId, DataScope dataScope, params string[] roles) : ICurrentUserDataScope { public CurrentUserScope Current { get; } = new( - Guid.NewGuid(), "测试用户", collegeId, dataScope, new HashSet([role])); + Guid.NewGuid(), "测试用户", collegeId, dataScope, new HashSet(roles)); } } diff --git a/web/src/i18n/messages/en-US/assessment.ts b/web/src/i18n/messages/en-US/assessment.ts index 9f6f7d6..9acd463 100644 --- a/web/src/i18n/messages/en-US/assessment.ts +++ b/web/src/i18n/messages/en-US/assessment.ts @@ -18,6 +18,10 @@ export default { withdrawn: "Withdrawn", planPublished: "Plan published", evidenceOpens: "Evidence opens", + milestoneCompleted: "Milestone completed", + milestoneTimeReached: "Scheduled time reached; check the workflow for completion", + milestoneUpcoming: "Scheduled time not yet reached", + milestoneLegend: "Filled: completed · Outline: scheduled time reached", evidenceCloses: "Evidence closes", counselorReviewDeadline: "Counselor review deadline", collegeReviewDeadline: "College review deadline", diff --git a/web/src/i18n/messages/zh-CN/assessment.ts b/web/src/i18n/messages/zh-CN/assessment.ts index e254c85..267a102 100644 --- a/web/src/i18n/messages/zh-CN/assessment.ts +++ b/web/src/i18n/messages/zh-CN/assessment.ts @@ -18,6 +18,10 @@ export default { withdrawn: "已撤回", planPublished: "方案发布", evidenceOpens: "材料开放", + milestoneCompleted: "节点已完成", + milestoneTimeReached: "节点时间已到,实际操作状态以业务记录为准", + milestoneUpcoming: "节点时间未到", + milestoneLegend: "实心:已完成 · 空心:时间已到", evidenceCloses: "材料截止", counselorReviewDeadline: "辅导员初审截止", collegeReviewDeadline: "学院复核截止", diff --git a/web/src/views/ApprovalCenterView.vue b/web/src/views/ApprovalCenterView.vue index b13d5a4..ea0df83 100644 --- a/web/src/views/ApprovalCenterView.vue +++ b/web/src/views/ApprovalCenterView.vue @@ -1157,7 +1157,7 @@ onMounted(async () => { $router.push({ path: approvalRoute(item.type), query: - item.type === 'ClassroomReservation' ? { tab: 'review' } : {}, + item.type === 'ClassroomReservation' ? { tab: 'review' } : item.type === 'AssessmentDeclaration' ? { tab: 'reviews' } : item.type === 'AssessmentAppeal' ? { tab: 'appeals' } : {}, }) " >{{ t("approvalBatch.view") }} { $router.push({ path: approvalRoute(item.type), query: - item.type === 'ClassroomReservation' ? { tab: 'review' } : {}, + item.type === 'ClassroomReservation' ? { tab: 'review' } : item.type === 'AssessmentDeclaration' ? { tab: 'reviews' } : item.type === 'AssessmentAppeal' ? { tab: 'appeals' } : {}, }) " > diff --git a/web/src/views/ComprehensiveAssessmentsView.vue b/web/src/views/ComprehensiveAssessmentsView.vue index 0dc033b..579daba 100644 --- a/web/src/views/ComprehensiveAssessmentsView.vue +++ b/web/src/views/ComprehensiveAssessmentsView.vue @@ -2,7 +2,8 @@ import { getDisplayLocale } from "../i18n"; import { useI18n } from "vue-i18n"; const { t } = useI18n(); -import { computed, onMounted, reactive, ref } from "vue"; +import { computed, onMounted, onUnmounted, reactive, ref, watch } from "vue"; +import { useRoute } from "vue-router"; import { ElMessage, ElMessageBox } from "element-plus"; import { Clock, @@ -20,6 +21,7 @@ import { useAuthStore } from "../stores/auth"; import ApprovalAttachmentPicker from "../components/ApprovalAttachmentPicker.vue"; const auth = useAuthStore(); +const route = useRoute(); const roles = computed(() => auth.user?.roles ?? []); const isStudent = computed( () => @@ -50,6 +52,9 @@ const canPublish = computed(() => ), ); const activeTab = ref("batches"); +watch(() => route.query.tab, (tab) => { + if (tab === "reviews" || tab === "appeals") activeTab.value = tab; +}, { immediate: true }); const loading = ref(false); const saving = ref(false); const schemes = ref([]); @@ -316,6 +321,11 @@ const scheduleLabels = computed>(() => ({ 10: t("assessmentScript.publicReviewEnds"), 11: t("assessmentScript.finalPublication"), })); +const scheduleNow = ref(Date.now()); +let scheduleClock: ReturnType | undefined; +const scheduleNodeArrived = (node: any) => + Number.isFinite(new Date(node.scheduledAt).getTime()) && + new Date(node.scheduledAt).getTime() <= scheduleNow.value; const formatDate = (value?: string) => value ? new Date(value).toLocaleString(getDisplayLocale(), { @@ -1099,8 +1109,10 @@ async function reviewAppeal(row: any, approved: boolean) { } onMounted(async () => { + scheduleClock = setInterval(() => { scheduleNow.value = Date.now(); }, 60_000); await Promise.all([load(), loadTerms()]); }); +onUnmounted(() => { if (scheduleClock) clearInterval(scheduleClock); });