Integrate leave and discipline approval tasks

This commit is contained in:
biss committed 2026-12-01 10:14:54 +08:00
1 parent 167af46687
commit 784b5b76f2
6 files changed
+80 -7

No files matched your search

@@ -891,6 +891,40 @@ public sealed class ApprovalsController(
var current = scope.Current;
var isManager = current.IsInRole(SystemRoles.SuperAdmin) || current.IsInRole(SystemRoles.AcademicAdmin) || current.IsInRole(SystemRoles.CollegeAdmin);
if (isManager)
{
var leaves = db.StudentLeaves.AsNoTracking().Where(x => x.Status == StudentLeaveStatus.Submitted);
if (current.Scope != DataScope.All)
{
var collegeId = current.RestrictedCollegeId;
leaves = collegeId.HasValue ? leaves.Where(x => x.Student!.AdministrativeClass!.Major!.CollegeId == collegeId) : leaves.Where(x => false);
}
items.AddRange(await leaves.WhereApprovalBefore(x => x.CreatedAt, cutoff).OrderByApprovalTime(x => x.CreatedAt, oldestFirst).ThenBy(x => x.Id).Take(take)
.Select(x => new ApprovalItem(x.Id, "StudentLeave", "学生请假", x.Student!.Name, x.Reason, x.CreatedAt, x.Student.AdministrativeClass!.Major!.College!.Name)).ToListAsync(ct));
AddCount("StudentLeave", await leaves.WhereApprovalBefore(x => x.CreatedAt, cutoff).CountAsync(ct));
}
{
var school = current.IsInRole(SystemRoles.StudentAffairsAdmin) || current.IsInRole(SystemRoles.AcademicAdmin) || current.IsInRole(SystemRoles.SuperAdmin);
var college = current.IsInRole(SystemRoles.CollegeAdmin) || current.IsInRole(SystemRoles.SuperAdmin);
var disciplines = db.StudentDisciplines.AsNoTracking().Where(x => (college && x.State == StudentDisciplineState.Submitted) || (school && x.State == StudentDisciplineState.CollegeApproved));
if (!current.IsInRole(SystemRoles.StudentAffairsAdmin))
{
if (current.IsInRole(SystemRoles.SuperAdmin) || current.IsInRole(SystemRoles.AcademicAdmin))
{
if (current.Scope != DataScope.All) disciplines = disciplines.Where(x => false);
}
else if (current.Scope == DataScope.College && current.IsInRole(SystemRoles.CollegeAdmin))
{
var collegeId = current.RestrictedCollegeId;
disciplines = disciplines.Where(x => x.Student!.AdministrativeClass!.Major!.CollegeId == collegeId);
}
else disciplines = disciplines.Where(x => false);
}
items.AddRange(await disciplines.WhereApprovalBefore(x => x.SubmittedAt ?? x.CreatedAt, cutoff).OrderByApprovalTime(x => x.SubmittedAt ?? x.CreatedAt, oldestFirst).ThenBy(x => x.Id).Take(take)
.Select(x => new ApprovalItem(x.Id, "StudentDiscipline", "学生处分", x.Student!.Name, x.DisciplineType, x.SubmittedAt ?? x.CreatedAt, x.Student.AdministrativeClass!.Major!.College!.Name, x.State == StudentDisciplineState.Submitted ? "college" : "final")).ToListAsync(ct));
AddCount("StudentDiscipline", await disciplines.WhereApprovalBefore(x => x.SubmittedAt ?? x.CreatedAt, cutoff).CountAsync(ct));
}
if (isManager)
{
var super = current.IsInRole(SystemRoles.SuperAdmin);
var college = current.IsInRole(SystemRoles.CollegeAdmin);
@@ -69,8 +69,8 @@ public sealed class StudentLeavesController(AppDbContext db, ICurrentUserDataSco
[HttpGet("pending")]
[Authorize(Roles = Managers)]
public async Task<IActionResult> Pending(CancellationToken ct) => Ok(await Scoped()
.Where(x => x.Status == StudentLeaveStatus.Submitted)
public async Task<IActionResult> Pending(CancellationToken ct, Guid? id = null) => Ok(await Scoped()
.Where(x => x.Status == StudentLeaveStatus.Submitted && (id == null || x.Id == id))
.OrderBy(x => x.CreatedAt).Take(200)
.Select(x => new { x.Id, StudentName = x.Student!.Name, x.Student.StudentNumber, x.Type, x.StartAt, x.EndAt, x.Reason, x.CreatedAt })
.ToListAsync(ct));
@@ -1695,6 +1695,26 @@ public sealed class ApprovalsControllerTests
Assert.Equal(1, filteredPage.Counts["DeferredExam"]);
}
[Fact]
public async Task Pending_includes_leave_and_discipline_only_for_authorized_scope_and_stage()
{
await using var fixture = await ApprovalFixture.CreateAsync();
fixture.Db.StudentLeaves.Add(new StudentLeave { StudentId = fixture.Student.Id, Type = StudentLeaveType.Sick, StartAt = DateTime.UtcNow, EndAt = DateTime.UtcNow.AddDays(1), Reason = "病假" });
fixture.Db.StudentDisciplines.Add(new StudentDiscipline { StudentId = fixture.Student.Id, DisciplineType = "警告", Facts = "事实", Basis = "依据", DecisionNumber = "1", DecisionAuthority = "学院", DecisionDate = DateOnly.FromDateTime(DateTime.Today), EffectiveDate = DateOnly.FromDateTime(DateTime.Today), CreatedByUserId = Guid.NewGuid(), State = StudentDisciplineState.Submitted, SubmittedAt = DateTime.UtcNow });
await fixture.Db.SaveChangesAsync();
var manager = new ApprovalsController(fixture.Db, new ManagerDataScope());
var result = await manager.GetPending(1, 20, CancellationToken.None);
var page = Assert.IsType<ApprovalPendingResult>(Assert.IsType<OkObjectResult>(result.Result).Value);
Assert.Equal(1, page.Counts["StudentLeave"]);
Assert.Equal("college", Assert.Single(page.Items, x => x.Type == "StudentDiscipline").Stage);
var outsider = new ApprovalsController(fixture.Db, new CollegeDataScope(Guid.NewGuid()));
var otherResult = await outsider.GetPending(1, 20, CancellationToken.None);
var otherPage = Assert.IsType<ApprovalPendingResult>(Assert.IsType<OkObjectResult>(otherResult.Result).Value);
Assert.DoesNotContain(otherPage.Items, x => x.Type is "StudentLeave" or "StudentDiscipline");
}
[Fact]
public async Task MyCourses_IncludesPublishedScheduledTaskAfterCurrentTermChanges()
{
Loaded 3 of 6 files, more files were not shown because too many files have changed in this diff. Show more