@studious-lms/server 1.20.0 → 1.22.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (117) hide show
  1. package/Dockerfile +5 -2
  2. package/dist/models/agenda.d.ts +6 -0
  3. package/dist/models/agenda.d.ts.map +1 -1
  4. package/dist/models/announcement.d.ts +2 -0
  5. package/dist/models/announcement.d.ts.map +1 -1
  6. package/dist/models/assignment.d.ts +27 -0
  7. package/dist/models/assignment.d.ts.map +1 -1
  8. package/dist/models/auth.d.ts +4 -0
  9. package/dist/models/auth.d.ts.map +1 -1
  10. package/dist/models/class.d.ts +14 -0
  11. package/dist/models/class.d.ts.map +1 -1
  12. package/dist/models/event.d.ts +11 -0
  13. package/dist/models/event.d.ts.map +1 -1
  14. package/dist/models/file.d.ts +19 -0
  15. package/dist/models/file.d.ts.map +1 -1
  16. package/dist/models/folder.d.ts +4 -0
  17. package/dist/models/folder.d.ts.map +1 -1
  18. package/dist/models/gradebook.d.ts +180 -0
  19. package/dist/models/gradebook.d.ts.map +1 -0
  20. package/dist/models/gradebook.js +88 -0
  21. package/dist/models/gradebook.js.map +1 -0
  22. package/dist/models/labChat.d.ts +4 -0
  23. package/dist/models/labChat.d.ts.map +1 -1
  24. package/dist/models/newtonChat.d.ts +2 -0
  25. package/dist/models/newtonChat.d.ts.map +1 -1
  26. package/dist/models/school.d.ts +2 -0
  27. package/dist/models/school.d.ts.map +1 -1
  28. package/dist/models/section.d.ts +3 -0
  29. package/dist/models/section.d.ts.map +1 -1
  30. package/dist/models/worksheet.d.ts +6 -0
  31. package/dist/models/worksheet.d.ts.map +1 -1
  32. package/dist/routers/_app.d.ts +1359 -37
  33. package/dist/routers/_app.d.ts.map +1 -1
  34. package/dist/routers/_app.js +4 -2
  35. package/dist/routers/_app.js.map +1 -1
  36. package/dist/routers/admin.d.ts +164 -0
  37. package/dist/routers/admin.d.ts.map +1 -1
  38. package/dist/routers/admin.js +23 -4
  39. package/dist/routers/admin.js.map +1 -1
  40. package/dist/routers/agenda.d.ts +6 -0
  41. package/dist/routers/agenda.d.ts.map +1 -1
  42. package/dist/routers/assignment.d.ts +102 -1
  43. package/dist/routers/assignment.d.ts.map +1 -1
  44. package/dist/routers/assignment.js +180 -2
  45. package/dist/routers/assignment.js.map +1 -1
  46. package/dist/routers/class.d.ts +17 -0
  47. package/dist/routers/class.d.ts.map +1 -1
  48. package/dist/routers/event.d.ts +4 -0
  49. package/dist/routers/event.d.ts.map +1 -1
  50. package/dist/routers/file.d.ts +66 -0
  51. package/dist/routers/file.d.ts.map +1 -1
  52. package/dist/routers/file.js +111 -2
  53. package/dist/routers/file.js.map +1 -1
  54. package/dist/routers/gradebook.d.ts +299 -0
  55. package/dist/routers/gradebook.d.ts.map +1 -0
  56. package/dist/routers/gradebook.js +107 -0
  57. package/dist/routers/gradebook.js.map +1 -0
  58. package/dist/routers/school.d.ts +4 -0
  59. package/dist/routers/school.d.ts.map +1 -1
  60. package/dist/routers/section.d.ts +1 -1
  61. package/dist/routers/worksheet.d.ts +3 -1
  62. package/dist/routers/worksheet.d.ts.map +1 -1
  63. package/dist/services/adminInstitution.d.ts +51 -0
  64. package/dist/services/adminInstitution.d.ts.map +1 -1
  65. package/dist/services/adminInstitution.js +52 -2
  66. package/dist/services/adminInstitution.js.map +1 -1
  67. package/dist/services/adminMetrics.d.ts +17 -0
  68. package/dist/services/adminMetrics.d.ts.map +1 -0
  69. package/dist/services/adminMetrics.js +37 -0
  70. package/dist/services/adminMetrics.js.map +1 -0
  71. package/dist/services/adminRunJob.d.ts +9 -0
  72. package/dist/services/adminRunJob.d.ts.map +1 -1
  73. package/dist/services/adminRunJob.js +18 -2
  74. package/dist/services/adminRunJob.js.map +1 -1
  75. package/dist/services/adminUser.d.ts +67 -0
  76. package/dist/services/adminUser.d.ts.map +1 -0
  77. package/dist/services/adminUser.js +87 -0
  78. package/dist/services/adminUser.js.map +1 -0
  79. package/dist/services/agenda.d.ts +6 -0
  80. package/dist/services/agenda.d.ts.map +1 -1
  81. package/dist/services/assignment.d.ts +27 -0
  82. package/dist/services/assignment.d.ts.map +1 -1
  83. package/dist/services/class.d.ts +17 -0
  84. package/dist/services/class.d.ts.map +1 -1
  85. package/dist/services/event.d.ts +4 -0
  86. package/dist/services/event.d.ts.map +1 -1
  87. package/dist/services/gradeCalculation.d.ts +70 -0
  88. package/dist/services/gradeCalculation.d.ts.map +1 -0
  89. package/dist/services/gradeCalculation.js +148 -0
  90. package/dist/services/gradeCalculation.js.map +1 -0
  91. package/dist/services/gradebook.d.ts +197 -0
  92. package/dist/services/gradebook.d.ts.map +1 -0
  93. package/dist/services/gradebook.js +263 -0
  94. package/dist/services/gradebook.js.map +1 -0
  95. package/dist/services/school.d.ts +4 -0
  96. package/dist/services/school.d.ts.map +1 -1
  97. package/dist/services/worksheet.d.ts +2 -0
  98. package/dist/services/worksheet.d.ts.map +1 -1
  99. package/docker-entrypoint.sh +7 -0
  100. package/package.json +1 -1
  101. package/prisma/migrations/20260708011456_add_file_annotations/migration.sql +35 -0
  102. package/prisma/migrations/20260708011629_add_gradebook_categories_columns_policies/migration.sql +78 -0
  103. package/prisma/migrations/20260723142314_make_file_annotation_submission_optional/migration.sql +2 -0
  104. package/prisma/schema.prisma +130 -38
  105. package/src/models/gradebook.ts +91 -0
  106. package/src/routers/_app.ts +2 -0
  107. package/src/routers/admin.ts +28 -0
  108. package/src/routers/assignment.ts +204 -0
  109. package/src/routers/file.ts +128 -0
  110. package/src/routers/gradebook.ts +173 -0
  111. package/src/services/adminInstitution.ts +52 -0
  112. package/src/services/adminMetrics.ts +46 -0
  113. package/src/services/adminRunJob.ts +24 -0
  114. package/src/services/adminUser.ts +93 -0
  115. package/src/services/gradeCalculation.ts +253 -0
  116. package/src/services/gradebook.ts +383 -0
  117. package/tests/unit/services/gradeCalculation.test.ts +206 -0
@@ -180,6 +180,30 @@ const confirmAnnotationUploadSchema = z.object({
180
180
  errorMessage: z.string().optional(),
181
181
  });
182
182
 
183
+ const listFileAnnotationsSchema = z.object({
184
+ classId: z.string(),
185
+ submissionId: z.string(),
186
+ fileId: z.string(),
187
+ });
188
+
189
+ const upsertFileAnnotationSchema = z.object({
190
+ classId: z.string(),
191
+ submissionId: z.string(),
192
+ fileId: z.string(),
193
+ id: z.string().optional(),
194
+ page: z.number().int().min(1),
195
+ type: z.enum(["HIGHLIGHT", "INK", "COMMENT", "TEXT"]),
196
+ geometry: z.record(z.string(), z.unknown()),
197
+ text: z.string().optional(),
198
+ color: z.string().optional(),
199
+ });
200
+
201
+ const deleteFileAnnotationSchema = z.object({
202
+ classId: z.string(),
203
+ submissionId: z.string(),
204
+ id: z.string(),
205
+ });
206
+
183
207
  const updateUploadProgressSchema = z.object({
184
208
  fileId: z.string(),
185
209
  progress: z.number().min(0).max(100),
@@ -966,6 +990,186 @@ export const assignmentRouter = createTRPCRouter({
966
990
  };
967
991
  }),
968
992
 
993
+ listFileAnnotations: protectedClassMemberProcedure
994
+ .input(listFileAnnotationsSchema)
995
+ .query(async ({ ctx, input }) => {
996
+ const { classId, submissionId, fileId } = input;
997
+
998
+ const submission = await prisma.submission.findFirst({
999
+ where: {
1000
+ id: submissionId,
1001
+ assignment: { classId },
1002
+ },
1003
+ select: {
1004
+ id: true,
1005
+ studentId: true,
1006
+ returned: true,
1007
+ assignment: {
1008
+ select: {
1009
+ class: {
1010
+ select: {
1011
+ teachers: { where: { id: ctx.user!.id }, select: { id: true } },
1012
+ },
1013
+ },
1014
+ },
1015
+ },
1016
+ },
1017
+ });
1018
+
1019
+ if (!submission) {
1020
+ throw new TRPCError({
1021
+ code: "NOT_FOUND",
1022
+ message: "Submission not found",
1023
+ });
1024
+ }
1025
+
1026
+ const isTeacher = submission.assignment.class.teachers.length > 0;
1027
+ const isOwningStudent = submission.studentId === ctx.user!.id;
1028
+
1029
+ if (!isTeacher && !(isOwningStudent && submission.returned)) {
1030
+ throw new TRPCError({
1031
+ code: "FORBIDDEN",
1032
+ message: "You don't have access to these annotations",
1033
+ });
1034
+ }
1035
+
1036
+ const annotations = await prisma.fileAnnotation.findMany({
1037
+ where: { submissionId, fileId },
1038
+ orderBy: { createdAt: "asc" },
1039
+ include: {
1040
+ author: {
1041
+ select: {
1042
+ id: true,
1043
+ username: true,
1044
+ profile: { select: { displayName: true } },
1045
+ },
1046
+ },
1047
+ },
1048
+ });
1049
+
1050
+ return { annotations };
1051
+ }),
1052
+
1053
+ upsertFileAnnotation: protectedTeacherProcedure
1054
+ .input(upsertFileAnnotationSchema)
1055
+ .mutation(async ({ ctx, input }) => {
1056
+ const { classId, submissionId, fileId, id, page, type, geometry, text, color } = input;
1057
+
1058
+ const submission = await prisma.submission.findFirst({
1059
+ where: {
1060
+ id: submissionId,
1061
+ assignment: {
1062
+ classId,
1063
+ class: {
1064
+ teachers: {
1065
+ some: { id: ctx.user!.id },
1066
+ },
1067
+ },
1068
+ },
1069
+ },
1070
+ select: { id: true },
1071
+ });
1072
+
1073
+ if (!submission) {
1074
+ throw new TRPCError({
1075
+ code: "NOT_FOUND",
1076
+ message: "Submission not found or you are not a teacher of this class",
1077
+ });
1078
+ }
1079
+
1080
+ const file = await prisma.file.findFirst({
1081
+ where: {
1082
+ id: fileId,
1083
+ OR: [{ submissionId }, { annotationId: submissionId }],
1084
+ },
1085
+ select: { id: true },
1086
+ });
1087
+
1088
+ if (!file) {
1089
+ throw new TRPCError({
1090
+ code: "NOT_FOUND",
1091
+ message: "File does not belong to this submission",
1092
+ });
1093
+ }
1094
+
1095
+ if (id) {
1096
+ const existing = await prisma.fileAnnotation.findFirst({
1097
+ where: { id, submissionId, fileId },
1098
+ select: { id: true },
1099
+ });
1100
+
1101
+ if (!existing) {
1102
+ throw new TRPCError({
1103
+ code: "NOT_FOUND",
1104
+ message: "Annotation not found",
1105
+ });
1106
+ }
1107
+
1108
+ const annotation = await prisma.fileAnnotation.update({
1109
+ where: { id },
1110
+ data: {
1111
+ page,
1112
+ type,
1113
+ geometry: geometry as object,
1114
+ text: text ?? null,
1115
+ color: color ?? null,
1116
+ },
1117
+ });
1118
+
1119
+ return { annotation };
1120
+ }
1121
+
1122
+ const annotation = await prisma.fileAnnotation.create({
1123
+ data: {
1124
+ fileId,
1125
+ submissionId,
1126
+ authorId: ctx.user!.id,
1127
+ page,
1128
+ type,
1129
+ geometry: geometry as object,
1130
+ text: text ?? null,
1131
+ color: color ?? null,
1132
+ },
1133
+ });
1134
+
1135
+ return { annotation };
1136
+ }),
1137
+
1138
+ deleteFileAnnotation: protectedTeacherProcedure
1139
+ .input(deleteFileAnnotationSchema)
1140
+ .mutation(async ({ ctx, input }) => {
1141
+ const { classId, submissionId, id } = input;
1142
+
1143
+ const annotation = await prisma.fileAnnotation.findFirst({
1144
+ where: {
1145
+ id,
1146
+ submissionId,
1147
+ submission: {
1148
+ assignment: {
1149
+ classId,
1150
+ class: {
1151
+ teachers: {
1152
+ some: { id: ctx.user!.id },
1153
+ },
1154
+ },
1155
+ },
1156
+ },
1157
+ },
1158
+ select: { id: true },
1159
+ });
1160
+
1161
+ if (!annotation) {
1162
+ throw new TRPCError({
1163
+ code: "NOT_FOUND",
1164
+ message: "Annotation not found or you are not a teacher of this class",
1165
+ });
1166
+ }
1167
+
1168
+ await prisma.fileAnnotation.delete({ where: { id } });
1169
+
1170
+ return { success: true };
1171
+ }),
1172
+
969
1173
  updateUploadProgress: protectedProcedure
970
1174
  .input(updateUploadProgressSchema)
971
1175
  .mutation(async ({ ctx, input }) => {
@@ -1,5 +1,7 @@
1
1
  import { z } from "zod";
2
+ import { TRPCError } from "@trpc/server";
2
3
  import { createTRPCRouter, protectedProcedure } from "../trpc.js";
4
+ import { prisma } from "../lib/prisma.js";
3
5
  import {
4
6
  deleteFileGeneric,
5
7
  getFileSignedUrl,
@@ -7,6 +9,31 @@ import {
7
9
  renameFileScoped,
8
10
  } from "../services/file.js";
9
11
 
12
+ const upsertAnnotationSchema = z.object({
13
+ fileId: z.string(),
14
+ id: z.string().optional(),
15
+ page: z.number().int().min(1),
16
+ type: z.enum(["HIGHLIGHT", "INK", "COMMENT", "TEXT"]),
17
+ geometry: z.record(z.string(), z.unknown()),
18
+ text: z.string().optional(),
19
+ color: z.string().optional(),
20
+ });
21
+
22
+ /** @throws NOT_FOUND unless the file exists and is owned by the user. */
23
+ async function requireOwnedFile(userId: string, fileId: string) {
24
+ const file = await prisma.file.findFirst({
25
+ where: { id: fileId, userId },
26
+ select: { id: true },
27
+ });
28
+
29
+ if (!file) {
30
+ throw new TRPCError({
31
+ code: "NOT_FOUND",
32
+ message: "File not found or you don't own it",
33
+ });
34
+ }
35
+ }
36
+
10
37
  /**
11
38
  * File router. Backed by the same unified scope-aware permission model as the
12
39
  * folder router. The class files page and the cloud-storage page (personal /
@@ -55,4 +82,105 @@ export const fileRouter = createTRPCRouter({
55
82
  .mutation(({ ctx, input }) =>
56
83
  deleteFileGeneric(ctx.user!.id, input.fileId)
57
84
  ),
85
+
86
+ // PDF annotations on files the user owns (personal cloud storage). The
87
+ // submission review flow uses the annotation endpoints on the assignment
88
+ // router instead; these are owner-only and carry no submission context.
89
+ listAnnotations: protectedProcedure
90
+ .input(z.object({ fileId: z.string() }))
91
+ .query(async ({ ctx, input }) => {
92
+ await requireOwnedFile(ctx.user!.id, input.fileId);
93
+
94
+ const annotations = await prisma.fileAnnotation.findMany({
95
+ where: { fileId: input.fileId, submissionId: null },
96
+ orderBy: { createdAt: "asc" },
97
+ include: {
98
+ author: {
99
+ select: {
100
+ id: true,
101
+ username: true,
102
+ profile: { select: { displayName: true } },
103
+ },
104
+ },
105
+ },
106
+ });
107
+
108
+ return { annotations };
109
+ }),
110
+
111
+ upsertAnnotation: protectedProcedure
112
+ .input(upsertAnnotationSchema)
113
+ .mutation(async ({ ctx, input }) => {
114
+ const { fileId, id, page, type, geometry, text, color } = input;
115
+
116
+ await requireOwnedFile(ctx.user!.id, fileId);
117
+
118
+ if (id) {
119
+ const existing = await prisma.fileAnnotation.findFirst({
120
+ where: { id, fileId, submissionId: null, authorId: ctx.user!.id },
121
+ select: { id: true },
122
+ });
123
+
124
+ if (!existing) {
125
+ throw new TRPCError({
126
+ code: "NOT_FOUND",
127
+ message: "Annotation not found",
128
+ });
129
+ }
130
+
131
+ const annotation = await prisma.fileAnnotation.update({
132
+ where: { id },
133
+ data: {
134
+ page,
135
+ type,
136
+ geometry: geometry as object,
137
+ text: text ?? null,
138
+ color: color ?? null,
139
+ },
140
+ });
141
+
142
+ return { annotation };
143
+ }
144
+
145
+ const annotation = await prisma.fileAnnotation.create({
146
+ data: {
147
+ fileId,
148
+ authorId: ctx.user!.id,
149
+ page,
150
+ type,
151
+ geometry: geometry as object,
152
+ text: text ?? null,
153
+ color: color ?? null,
154
+ },
155
+ });
156
+
157
+ return { annotation };
158
+ }),
159
+
160
+ deleteAnnotation: protectedProcedure
161
+ .input(z.object({ fileId: z.string(), id: z.string() }))
162
+ .mutation(async ({ ctx, input }) => {
163
+ await requireOwnedFile(ctx.user!.id, input.fileId);
164
+
165
+ const annotation = await prisma.fileAnnotation.findFirst({
166
+ where: {
167
+ id: input.id,
168
+ fileId: input.fileId,
169
+ submissionId: null,
170
+ authorId: ctx.user!.id,
171
+ },
172
+ select: { id: true },
173
+ });
174
+
175
+ if (!annotation) {
176
+ throw new TRPCError({
177
+ code: "NOT_FOUND",
178
+ message: "Annotation not found",
179
+ });
180
+ }
181
+
182
+ await prisma.fileAnnotation.delete({ where: { id: input.id } });
183
+
184
+ return { success: true };
185
+ }),
58
186
  });
@@ -0,0 +1,173 @@
1
+ import { z } from "zod";
2
+ import {
3
+ createTRPCRouter,
4
+ protectedTeacherProcedure,
5
+ protectedClassMemberProcedure,
6
+ } from "../trpc.js";
7
+ import {
8
+ listGradeCategories,
9
+ createGradeCategory,
10
+ updateGradeCategory,
11
+ deleteGradeCategory,
12
+ assignAssignmentCategory,
13
+ listCustomGradeColumns,
14
+ createCustomGradeColumn,
15
+ updateCustomGradeColumn,
16
+ deleteCustomGradeColumn,
17
+ setCustomGradeEntry,
18
+ getGradePolicies,
19
+ updateGradePolicies,
20
+ getComputedGradebook,
21
+ exportGradebookCsv,
22
+ } from "../services/gradebook.js";
23
+
24
+ const roundingModeSchema = z.enum(["NONE", "HALF_UP", "FLOOR", "CEILING"]);
25
+
26
+ export const gradebookRouter = createTRPCRouter({
27
+ listCategories: protectedClassMemberProcedure
28
+ .input(z.object({ classId: z.string() }))
29
+ .query(({ input }) => listGradeCategories(input.classId)),
30
+
31
+ createCategory: protectedTeacherProcedure
32
+ .input(
33
+ z.object({
34
+ classId: z.string(),
35
+ name: z.string().min(1),
36
+ weight: z.number().min(0),
37
+ dropLowest: z.number().int().min(0).optional(),
38
+ position: z.number().int().min(0).optional(),
39
+ })
40
+ )
41
+ .mutation(({ input }) => {
42
+ const { classId, ...data } = input;
43
+ return createGradeCategory(classId, data);
44
+ }),
45
+
46
+ updateCategory: protectedTeacherProcedure
47
+ .input(
48
+ z.object({
49
+ classId: z.string(),
50
+ categoryId: z.string(),
51
+ name: z.string().min(1).optional(),
52
+ weight: z.number().min(0).optional(),
53
+ dropLowest: z.number().int().min(0).optional(),
54
+ position: z.number().int().min(0).optional(),
55
+ })
56
+ )
57
+ .mutation(({ input }) => {
58
+ const { classId, categoryId, ...data } = input;
59
+ return updateGradeCategory(categoryId, classId, data);
60
+ }),
61
+
62
+ deleteCategory: protectedTeacherProcedure
63
+ .input(z.object({ classId: z.string(), categoryId: z.string() }))
64
+ .mutation(({ input }) =>
65
+ deleteGradeCategory(input.categoryId, input.classId)
66
+ ),
67
+
68
+ assignCategory: protectedTeacherProcedure
69
+ .input(
70
+ z.object({
71
+ classId: z.string(),
72
+ assignmentId: z.string(),
73
+ gradeCategoryId: z.string().nullable(),
74
+ })
75
+ )
76
+ .mutation(({ input }) =>
77
+ assignAssignmentCategory(
78
+ input.classId,
79
+ input.assignmentId,
80
+ input.gradeCategoryId
81
+ )
82
+ ),
83
+
84
+ listColumns: protectedClassMemberProcedure
85
+ .input(z.object({ classId: z.string() }))
86
+ .query(({ input }) => listCustomGradeColumns(input.classId)),
87
+
88
+ createColumn: protectedTeacherProcedure
89
+ .input(
90
+ z.object({
91
+ classId: z.string(),
92
+ name: z.string().min(1),
93
+ maxPoints: z.number().positive(),
94
+ gradeCategoryId: z.string().nullable().optional(),
95
+ position: z.number().int().min(0).optional(),
96
+ })
97
+ )
98
+ .mutation(({ input }) => {
99
+ const { classId, ...data } = input;
100
+ return createCustomGradeColumn(classId, data);
101
+ }),
102
+
103
+ updateColumn: protectedTeacherProcedure
104
+ .input(
105
+ z.object({
106
+ classId: z.string(),
107
+ columnId: z.string(),
108
+ name: z.string().min(1).optional(),
109
+ maxPoints: z.number().positive().optional(),
110
+ gradeCategoryId: z.string().nullable().optional(),
111
+ position: z.number().int().min(0).optional(),
112
+ })
113
+ )
114
+ .mutation(({ input }) => {
115
+ const { classId, columnId, ...data } = input;
116
+ return updateCustomGradeColumn(columnId, classId, data);
117
+ }),
118
+
119
+ deleteColumn: protectedTeacherProcedure
120
+ .input(z.object({ classId: z.string(), columnId: z.string() }))
121
+ .mutation(({ input }) =>
122
+ deleteCustomGradeColumn(input.columnId, input.classId)
123
+ ),
124
+
125
+ setEntry: protectedTeacherProcedure
126
+ .input(
127
+ z.object({
128
+ classId: z.string(),
129
+ columnId: z.string(),
130
+ studentId: z.string(),
131
+ points: z.number().min(0).nullable(),
132
+ note: z.string().nullable().optional(),
133
+ })
134
+ )
135
+ .mutation(({ input }) =>
136
+ setCustomGradeEntry(
137
+ input.classId,
138
+ input.columnId,
139
+ input.studentId,
140
+ input.points,
141
+ input.note
142
+ )
143
+ ),
144
+
145
+ getPolicies: protectedClassMemberProcedure
146
+ .input(z.object({ classId: z.string() }))
147
+ .query(({ input }) => getGradePolicies(input.classId)),
148
+
149
+ updatePolicies: protectedTeacherProcedure
150
+ .input(
151
+ z.object({
152
+ classId: z.string(),
153
+ treatMissingAsZero: z.boolean().optional(),
154
+ gradeRoundingMode: roundingModeSchema.optional(),
155
+ })
156
+ )
157
+ .mutation(({ input }) => {
158
+ const { classId, ...data } = input;
159
+ return updateGradePolicies(classId, data);
160
+ }),
161
+
162
+ getComputed: protectedClassMemberProcedure
163
+ .input(z.object({ classId: z.string() }))
164
+ .query(({ ctx, input }) =>
165
+ getComputedGradebook(input.classId, ctx.user?.id ?? "")
166
+ ),
167
+
168
+ exportCsv: protectedTeacherProcedure
169
+ .input(z.object({ classId: z.string() }))
170
+ .query(({ ctx, input }) =>
171
+ exportGradebookCsv(input.classId, ctx.user?.id ?? "")
172
+ ),
173
+ });
@@ -1,4 +1,5 @@
1
1
  import type { Prisma } from '@prisma/client';
2
+ import { TRPCError } from '@trpc/server';
2
3
  import { prisma } from '../lib/prisma.js';
3
4
 
4
5
  export async function listSchoolsForAdmin(input: {
@@ -45,6 +46,57 @@ export async function listSchoolsForAdmin(input: {
45
46
  };
46
47
  }
47
48
 
49
+ export async function getSchoolForAdmin(id: string) {
50
+ const school = await prisma.school.findUnique({
51
+ where: { id },
52
+ include: {
53
+ _count: {
54
+ select: { memberships: true, classes: true, departments: true },
55
+ },
56
+ memberships: {
57
+ orderBy: { createdAt: 'desc' },
58
+ take: 100,
59
+ select: {
60
+ id: true,
61
+ role: true,
62
+ status: true,
63
+ createdAt: true,
64
+ user: {
65
+ select: { id: true, username: true, email: true, verified: true },
66
+ },
67
+ },
68
+ },
69
+ classes: {
70
+ orderBy: { name: 'asc' },
71
+ take: 100,
72
+ select: {
73
+ id: true,
74
+ name: true,
75
+ subject: true,
76
+ section: true,
77
+ color: true,
78
+ department: { select: { id: true, name: true } },
79
+ _count: { select: { students: true, teachers: true } },
80
+ },
81
+ },
82
+ departments: {
83
+ orderBy: { name: 'asc' },
84
+ select: {
85
+ id: true,
86
+ name: true,
87
+ color: true,
88
+ _count: { select: { classes: true, members: true } },
89
+ },
90
+ },
91
+ },
92
+ });
93
+
94
+ if (!school) {
95
+ throw new TRPCError({ code: 'NOT_FOUND', message: 'School not found' });
96
+ }
97
+ return school;
98
+ }
99
+
48
100
  export async function listDepartmentsForAdmin(input: {
49
101
  page: number;
50
102
  pageSize: number;
@@ -0,0 +1,46 @@
1
+ import { prisma } from '../lib/prisma.js';
2
+
3
+ export async function getOverviewMetricsForAdmin() {
4
+ const dayAgo = new Date(Date.now() - 24 * 60 * 60 * 1000);
5
+
6
+ const [
7
+ schools,
8
+ users,
9
+ verifiedUsers,
10
+ classes,
11
+ departments,
12
+ runJobsByStatus,
13
+ failedRunJobsLast24h,
14
+ earlyAccessRequests,
15
+ pendingDevProgramApplications,
16
+ ] = await Promise.all([
17
+ prisma.school.count(),
18
+ prisma.user.count(),
19
+ prisma.user.count({ where: { verified: true } }),
20
+ prisma.class.count(),
21
+ prisma.department.count(),
22
+ prisma.runJob.groupBy({ by: ['status'], _count: { _all: true } }),
23
+ prisma.runJob.count({
24
+ where: { status: 'FAILED', createdAt: { gte: dayAgo } },
25
+ }),
26
+ prisma.earlyAccessRequest.count(),
27
+ prisma.schoolDevelopementProgram.count({ where: { status: 'PENDING' } }),
28
+ ]);
29
+
30
+ return {
31
+ schools,
32
+ users,
33
+ verifiedUsers,
34
+ classes,
35
+ departments,
36
+ runJobs: {
37
+ byStatus: runJobsByStatus.map((row) => ({
38
+ status: row.status,
39
+ count: row._count._all,
40
+ })),
41
+ failedLast24h: failedRunJobsLast24h,
42
+ },
43
+ earlyAccessRequests,
44
+ pendingDevProgramApplications,
45
+ };
46
+ }
@@ -1,6 +1,7 @@
1
1
  import type { Prisma, RunJobStatus, RunJobType } from '@prisma/client';
2
2
  import { TRPCError } from '@trpc/server';
3
3
  import { prisma } from '../lib/prisma.js';
4
+ import { enqueueRunJob, type JobInputs } from '../workers/queues.js';
4
5
 
5
6
  export async function listRunJobsForAdmin(input: {
6
7
  page: number;
@@ -44,3 +45,26 @@ export async function getRunJobForAdmin(id: string) {
44
45
  }
45
46
  return job;
46
47
  }
48
+
49
+ /**
50
+ * Re-enqueue a FAILED or CANCELLED job as a fresh RunJob row with the same
51
+ * type/input. The original row is left untouched for audit purposes.
52
+ */
53
+ export async function retryRunJobForAdmin(id: string) {
54
+ const job = await getRunJobForAdmin(id);
55
+
56
+ if (job.status !== 'FAILED' && job.status !== 'CANCELLED') {
57
+ throw new TRPCError({
58
+ code: 'PRECONDITION_FAILED',
59
+ message: `Only FAILED or CANCELLED jobs can be retried (status: ${job.status})`,
60
+ });
61
+ }
62
+
63
+ const { runJobId, bullJobId } = await enqueueRunJob(
64
+ job.type,
65
+ job.input as JobInputs[typeof job.type],
66
+ { classId: job.classId, userId: job.userId },
67
+ );
68
+
69
+ return { runJobId, bullJobId, retriedFrom: job.id };
70
+ }