sync-docs-and-fixes
This commit is contained in:
@@ -3,6 +3,7 @@ import "server-only"
|
||||
import { cache } from "react"
|
||||
import { and, count, desc, eq, inArray, isNull, lte, or, sql } from "drizzle-orm"
|
||||
|
||||
import { auth } from "@/auth"
|
||||
import { db } from "@/shared/db"
|
||||
import {
|
||||
classEnrollments,
|
||||
@@ -11,7 +12,9 @@ import {
|
||||
homeworkAssignmentTargets,
|
||||
homeworkAssignments,
|
||||
homeworkSubmissions,
|
||||
roles,
|
||||
users,
|
||||
usersToRoles,
|
||||
} from "@/shared/db/schema"
|
||||
|
||||
import type {
|
||||
@@ -550,17 +553,20 @@ export const getHomeworkSubmissionDetails = cache(async (submissionId: string):
|
||||
})
|
||||
|
||||
export const getDemoStudentUser = cache(async (): Promise<{ id: string; name: string } | null> => {
|
||||
const student = await db.query.users.findFirst({
|
||||
where: eq(users.role, "student"),
|
||||
orderBy: (u, { asc }) => [asc(u.createdAt)],
|
||||
})
|
||||
if (student) return { id: student.id, name: student.name || "Student" }
|
||||
const session = await auth()
|
||||
const userId = String(session?.user?.id ?? "").trim()
|
||||
if (!userId) return null
|
||||
|
||||
const anyUser = await db.query.users.findFirst({
|
||||
orderBy: (u, { asc }) => [asc(u.createdAt)],
|
||||
})
|
||||
if (!anyUser) return null
|
||||
return { id: anyUser.id, name: anyUser.name || "User" }
|
||||
const [student] = await db
|
||||
.select({ id: users.id, name: users.name })
|
||||
.from(users)
|
||||
.innerJoin(usersToRoles, eq(usersToRoles.userId, users.id))
|
||||
.innerJoin(roles, eq(usersToRoles.roleId, roles.id))
|
||||
.where(and(eq(users.id, userId), eq(roles.name, "student")))
|
||||
.limit(1)
|
||||
|
||||
if (!student) return null
|
||||
return { id: student.id, name: student.name || "Student" }
|
||||
})
|
||||
|
||||
const toStudentProgressStatus = (v: string | null | undefined): StudentHomeworkProgressStatus => {
|
||||
@@ -592,19 +598,23 @@ export const getStudentHomeworkAssignments = cache(async (studentId: string): Pr
|
||||
const assignmentIds = assignments.map((a) => a.id)
|
||||
const submissions = await db.query.homeworkSubmissions.findMany({
|
||||
where: and(eq(homeworkSubmissions.studentId, studentId), inArray(homeworkSubmissions.assignmentId, assignmentIds)),
|
||||
orderBy: [desc(homeworkSubmissions.createdAt)],
|
||||
orderBy: [desc(homeworkSubmissions.updatedAt)],
|
||||
})
|
||||
|
||||
const attemptsByAssignmentId = new Map<string, number>()
|
||||
const latestByAssignmentId = new Map<string, (typeof submissions)[number]>()
|
||||
const latestSubmittedByAssignmentId = new Map<string, (typeof submissions)[number]>()
|
||||
|
||||
for (const s of submissions) {
|
||||
attemptsByAssignmentId.set(s.assignmentId, (attemptsByAssignmentId.get(s.assignmentId) ?? 0) + 1)
|
||||
if (!latestByAssignmentId.has(s.assignmentId)) latestByAssignmentId.set(s.assignmentId, s)
|
||||
if (s.status === "submitted" || s.status === "graded") {
|
||||
if (!latestSubmittedByAssignmentId.has(s.assignmentId)) latestSubmittedByAssignmentId.set(s.assignmentId, s)
|
||||
}
|
||||
}
|
||||
|
||||
return assignments.map((a) => {
|
||||
const latest = latestByAssignmentId.get(a.id) ?? null
|
||||
const latest = latestSubmittedByAssignmentId.get(a.id) ?? latestByAssignmentId.get(a.id) ?? null
|
||||
const attemptsUsed = attemptsByAssignmentId.get(a.id) ?? 0
|
||||
|
||||
const item: StudentHomeworkAssignmentListItem = {
|
||||
|
||||
Reference in New Issue
Block a user