@studious-lms/server 1.2.53 → 1.3.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 (477) hide show
  1. package/.coderabbit.yaml +9 -0
  2. package/.env.example +9 -1
  3. package/dist/index.d.ts.map +1 -1
  4. package/dist/index.js +102 -8
  5. package/dist/index.js.map +1 -1
  6. package/dist/lib/config/env.d.ts +21 -0
  7. package/dist/lib/config/env.d.ts.map +1 -1
  8. package/dist/lib/config/env.js +8 -2
  9. package/dist/lib/config/env.js.map +1 -1
  10. package/dist/lib/fileUpload.d.ts.map +1 -1
  11. package/dist/lib/fileUpload.js +2 -2
  12. package/dist/lib/fileUpload.js.map +1 -1
  13. package/dist/lib/googleCloudStorage.d.ts +6 -0
  14. package/dist/lib/googleCloudStorage.d.ts.map +1 -1
  15. package/dist/lib/googleCloudStorage.js +19 -2
  16. package/dist/lib/googleCloudStorage.js.map +1 -1
  17. package/dist/lib/pusher.d.ts +4 -1
  18. package/dist/lib/pusher.d.ts.map +1 -1
  19. package/dist/lib/pusher.js +6 -3
  20. package/dist/lib/pusher.js.map +1 -1
  21. package/dist/lib/redis.d.ts +5 -0
  22. package/dist/lib/redis.d.ts.map +1 -0
  23. package/dist/lib/redis.js +53 -0
  24. package/dist/lib/redis.js.map +1 -0
  25. package/dist/lib/thumbnailGenerator.d.ts +0 -21
  26. package/dist/lib/thumbnailGenerator.d.ts.map +1 -1
  27. package/dist/lib/thumbnailGenerator.js +157 -160
  28. package/dist/lib/thumbnailGenerator.js.map +1 -1
  29. package/dist/middleware/auth.d.ts.map +1 -1
  30. package/dist/middleware/auth.js +33 -95
  31. package/dist/middleware/auth.js.map +1 -1
  32. package/dist/models/agenda.d.ts +97 -0
  33. package/dist/models/agenda.d.ts.map +1 -0
  34. package/dist/models/agenda.js +40 -0
  35. package/dist/models/agenda.js.map +1 -0
  36. package/dist/models/announcement.d.ts +223 -0
  37. package/dist/models/announcement.d.ts.map +1 -0
  38. package/dist/models/announcement.js +120 -0
  39. package/dist/models/announcement.js.map +1 -0
  40. package/dist/models/assignment.d.ts +1292 -0
  41. package/dist/models/assignment.d.ts.map +1 -0
  42. package/dist/models/assignment.js +309 -0
  43. package/dist/models/assignment.js.map +1 -0
  44. package/dist/models/attendance.d.ts +180 -0
  45. package/dist/models/attendance.d.ts.map +1 -0
  46. package/dist/models/attendance.js +188 -0
  47. package/dist/models/attendance.js.map +1 -0
  48. package/dist/models/auth.d.ts +153 -0
  49. package/dist/models/auth.d.ts.map +1 -0
  50. package/dist/models/auth.js +217 -0
  51. package/dist/models/auth.js.map +1 -0
  52. package/dist/models/class.d.ts +439 -0
  53. package/dist/models/class.d.ts.map +1 -0
  54. package/dist/models/class.js +546 -0
  55. package/dist/models/class.js.map +1 -0
  56. package/dist/models/comment.d.ts +171 -0
  57. package/dist/models/comment.d.ts.map +1 -0
  58. package/dist/models/comment.js +138 -0
  59. package/dist/models/comment.js.map +1 -0
  60. package/dist/models/conversation.d.ts +164 -0
  61. package/dist/models/conversation.d.ts.map +1 -0
  62. package/dist/models/conversation.js +175 -0
  63. package/dist/models/conversation.js.map +1 -0
  64. package/dist/models/event.d.ts +295 -0
  65. package/dist/models/event.d.ts.map +1 -0
  66. package/dist/models/event.js +145 -0
  67. package/dist/models/event.js.map +1 -0
  68. package/dist/models/file.d.ts +536 -0
  69. package/dist/models/file.d.ts.map +1 -0
  70. package/dist/models/file.js +126 -0
  71. package/dist/models/file.js.map +1 -0
  72. package/dist/models/folder.d.ts +295 -0
  73. package/dist/models/folder.d.ts.map +1 -0
  74. package/dist/models/folder.js +202 -0
  75. package/dist/models/folder.js.map +1 -0
  76. package/dist/models/labChat.d.ts +243 -0
  77. package/dist/models/labChat.d.ts.map +1 -0
  78. package/dist/models/labChat.js +204 -0
  79. package/dist/models/labChat.js.map +1 -0
  80. package/dist/models/marketing.d.ts +72 -0
  81. package/dist/models/marketing.d.ts.map +1 -0
  82. package/dist/models/marketing.js +26 -0
  83. package/dist/models/marketing.js.map +1 -0
  84. package/dist/models/message.d.ts +100 -0
  85. package/dist/models/message.d.ts.map +1 -0
  86. package/dist/models/message.js +131 -0
  87. package/dist/models/message.js.map +1 -0
  88. package/dist/models/newtonChat.d.ts +72 -0
  89. package/dist/models/newtonChat.d.ts.map +1 -0
  90. package/dist/models/newtonChat.js +61 -0
  91. package/dist/models/newtonChat.js.map +1 -0
  92. package/dist/models/notification.d.ts +65 -0
  93. package/dist/models/notification.d.ts.map +1 -0
  94. package/dist/models/notification.js +46 -0
  95. package/dist/models/notification.js.map +1 -0
  96. package/dist/models/section.d.ts +102 -0
  97. package/dist/models/section.d.ts.map +1 -0
  98. package/dist/models/section.js +83 -0
  99. package/dist/models/section.js.map +1 -0
  100. package/dist/models/user.d.ts +39 -0
  101. package/dist/models/user.d.ts.map +1 -0
  102. package/dist/models/user.js +38 -0
  103. package/dist/models/user.js.map +1 -0
  104. package/dist/models/worksheet.d.ts +460 -0
  105. package/dist/models/worksheet.d.ts.map +1 -0
  106. package/dist/models/worksheet.js +200 -0
  107. package/dist/models/worksheet.js.map +1 -0
  108. package/dist/pipelines/aiLabChat.d.ts +21 -0
  109. package/dist/pipelines/aiLabChat.d.ts.map +1 -0
  110. package/dist/pipelines/aiLabChat.js +460 -0
  111. package/dist/pipelines/aiLabChat.js.map +1 -0
  112. package/dist/pipelines/aiNewtonChat.d.ts +30 -0
  113. package/dist/pipelines/aiNewtonChat.d.ts.map +1 -0
  114. package/dist/pipelines/aiNewtonChat.js +289 -0
  115. package/dist/pipelines/aiNewtonChat.js.map +1 -0
  116. package/dist/pipelines/gradeWorksheet.d.ts +30 -0
  117. package/dist/pipelines/gradeWorksheet.d.ts.map +1 -0
  118. package/dist/pipelines/gradeWorksheet.js +252 -0
  119. package/dist/pipelines/gradeWorksheet.js.map +1 -0
  120. package/dist/routers/_app.d.ts +1393 -1267
  121. package/dist/routers/_app.d.ts.map +1 -1
  122. package/dist/routers/agenda.d.ts +22 -22
  123. package/dist/routers/agenda.d.ts.map +1 -1
  124. package/dist/routers/agenda.js +4 -65
  125. package/dist/routers/agenda.js.map +1 -1
  126. package/dist/routers/announcement.d.ts +16 -16
  127. package/dist/routers/announcement.d.ts.map +1 -1
  128. package/dist/routers/announcement.js +37 -446
  129. package/dist/routers/announcement.js.map +1 -1
  130. package/dist/routers/assignment.d.ts +300 -378
  131. package/dist/routers/assignment.d.ts.map +1 -1
  132. package/dist/routers/assignment.js +78 -1868
  133. package/dist/routers/assignment.js.map +1 -1
  134. package/dist/routers/attendance.d.ts +19 -9
  135. package/dist/routers/attendance.d.ts.map +1 -1
  136. package/dist/routers/attendance.js +7 -264
  137. package/dist/routers/attendance.js.map +1 -1
  138. package/dist/routers/auth.d.ts +2 -2
  139. package/dist/routers/auth.d.ts.map +1 -1
  140. package/dist/routers/auth.js +29 -354
  141. package/dist/routers/auth.js.map +1 -1
  142. package/dist/routers/class.d.ts +139 -68
  143. package/dist/routers/class.d.ts.map +1 -1
  144. package/dist/routers/class.js +82 -1052
  145. package/dist/routers/class.js.map +1 -1
  146. package/dist/routers/comment.d.ts +6 -42
  147. package/dist/routers/comment.d.ts.map +1 -1
  148. package/dist/routers/comment.js +24 -244
  149. package/dist/routers/comment.js.map +1 -1
  150. package/dist/routers/conversation.d.ts +45 -7
  151. package/dist/routers/conversation.d.ts.map +1 -1
  152. package/dist/routers/conversation.js +19 -327
  153. package/dist/routers/conversation.js.map +1 -1
  154. package/dist/routers/event.d.ts +36 -36
  155. package/dist/routers/event.d.ts.map +1 -1
  156. package/dist/routers/event.js +13 -433
  157. package/dist/routers/event.js.map +1 -1
  158. package/dist/routers/file.d.ts +2 -2
  159. package/dist/routers/file.d.ts.map +1 -1
  160. package/dist/routers/file.js +9 -323
  161. package/dist/routers/file.js.map +1 -1
  162. package/dist/routers/folder.d.ts +21 -14
  163. package/dist/routers/folder.d.ts.map +1 -1
  164. package/dist/routers/folder.js +34 -745
  165. package/dist/routers/folder.js.map +1 -1
  166. package/dist/routers/labChat.d.ts +11 -10
  167. package/dist/routers/labChat.d.ts.map +1 -1
  168. package/dist/routers/labChat.js +19 -570
  169. package/dist/routers/labChat.js.map +1 -1
  170. package/dist/routers/marketing.d.ts +1 -1
  171. package/dist/routers/marketing.d.ts.map +1 -1
  172. package/dist/routers/marketing.js +7 -56
  173. package/dist/routers/marketing.js.map +1 -1
  174. package/dist/routers/message.d.ts +2 -2
  175. package/dist/routers/message.d.ts.map +1 -1
  176. package/dist/routers/message.js +27 -522
  177. package/dist/routers/message.js.map +1 -1
  178. package/dist/routers/newtonChat.d.ts +1 -1
  179. package/dist/routers/newtonChat.d.ts.map +1 -1
  180. package/dist/routers/newtonChat.js +7 -246
  181. package/dist/routers/newtonChat.js.map +1 -1
  182. package/dist/routers/notifications.d.ts +4 -4
  183. package/dist/routers/notifications.d.ts.map +1 -1
  184. package/dist/routers/notifications.js +18 -83
  185. package/dist/routers/notifications.js.map +1 -1
  186. package/dist/routers/section.d.ts +4 -4
  187. package/dist/routers/section.d.ts.map +1 -1
  188. package/dist/routers/section.js +14 -286
  189. package/dist/routers/section.js.map +1 -1
  190. package/dist/routers/user.d.ts +1 -1
  191. package/dist/routers/user.d.ts.map +1 -1
  192. package/dist/routers/user.js +32 -207
  193. package/dist/routers/user.js.map +1 -1
  194. package/dist/routers/worksheet.d.ts +51 -38
  195. package/dist/routers/worksheet.d.ts.map +1 -1
  196. package/dist/routers/worksheet.js +79 -394
  197. package/dist/routers/worksheet.js.map +1 -1
  198. package/dist/seedDatabase.d.ts +1 -1
  199. package/dist/server/pipelines/gradeWorksheet.d.ts +6 -6
  200. package/dist/server/pipelines/gradeWorksheet.d.ts.map +1 -1
  201. package/dist/server/pipelines/gradeWorksheet.js +12 -5
  202. package/dist/server/pipelines/gradeWorksheet.js.map +1 -1
  203. package/dist/services/agenda.d.ts +100 -0
  204. package/dist/services/agenda.d.ts.map +1 -0
  205. package/dist/services/agenda.js +21 -0
  206. package/dist/services/agenda.js.map +1 -0
  207. package/dist/services/announcement.d.ts +135 -0
  208. package/dist/services/announcement.d.ts.map +1 -0
  209. package/dist/services/announcement.js +223 -0
  210. package/dist/services/announcement.js.map +1 -0
  211. package/dist/services/assignment.d.ts +1462 -0
  212. package/dist/services/assignment.d.ts.map +1 -0
  213. package/dist/services/assignment.js +898 -0
  214. package/dist/services/assignment.js.map +1 -0
  215. package/dist/services/attendance.d.ts +93 -0
  216. package/dist/services/attendance.d.ts.map +1 -0
  217. package/dist/services/attendance.js +61 -0
  218. package/dist/services/attendance.js.map +1 -0
  219. package/dist/services/auth.d.ts +68 -0
  220. package/dist/services/auth.d.ts.map +1 -0
  221. package/dist/services/auth.js +218 -0
  222. package/dist/services/auth.js.map +1 -0
  223. package/dist/services/class.d.ts +621 -0
  224. package/dist/services/class.d.ts.map +1 -0
  225. package/dist/services/class.js +474 -0
  226. package/dist/services/class.js.map +1 -0
  227. package/dist/services/comment.d.ts +100 -0
  228. package/dist/services/comment.d.ts.map +1 -0
  229. package/dist/services/comment.js +83 -0
  230. package/dist/services/comment.js.map +1 -0
  231. package/dist/services/conversation.d.ts +159 -0
  232. package/dist/services/conversation.d.ts.map +1 -0
  233. package/dist/services/conversation.js +138 -0
  234. package/dist/services/conversation.js.map +1 -0
  235. package/dist/services/event.d.ts +216 -0
  236. package/dist/services/event.d.ts.map +1 -0
  237. package/dist/services/event.js +168 -0
  238. package/dist/services/event.js.map +1 -0
  239. package/dist/services/file.d.ts +74 -0
  240. package/dist/services/file.d.ts.map +1 -0
  241. package/dist/services/file.js +133 -0
  242. package/dist/services/file.js.map +1 -0
  243. package/dist/services/folder.d.ts +239 -0
  244. package/dist/services/folder.d.ts.map +1 -0
  245. package/dist/services/folder.js +248 -0
  246. package/dist/services/folder.js.map +1 -0
  247. package/dist/services/labChat.d.ts +165 -0
  248. package/dist/services/labChat.d.ts.map +1 -0
  249. package/dist/services/labChat.js +289 -0
  250. package/dist/services/labChat.js.map +1 -0
  251. package/dist/services/marketing.d.ts +50 -0
  252. package/dist/services/marketing.d.ts.map +1 -0
  253. package/dist/services/marketing.js +32 -0
  254. package/dist/services/marketing.js.map +1 -0
  255. package/dist/services/message.d.ts +95 -0
  256. package/dist/services/message.d.ts.map +1 -0
  257. package/dist/services/message.js +350 -0
  258. package/dist/services/message.js.map +1 -0
  259. package/dist/services/newtonChat.d.ts +22 -0
  260. package/dist/services/newtonChat.d.ts.map +1 -0
  261. package/dist/services/newtonChat.js +174 -0
  262. package/dist/services/newtonChat.js.map +1 -0
  263. package/dist/services/notification.d.ts +65 -0
  264. package/dist/services/notification.d.ts.map +1 -0
  265. package/dist/services/notification.js +33 -0
  266. package/dist/services/notification.js.map +1 -0
  267. package/dist/services/section.d.ts +53 -0
  268. package/dist/services/section.d.ts.map +1 -0
  269. package/dist/services/section.js +199 -0
  270. package/dist/services/section.js.map +1 -0
  271. package/dist/services/user.d.ts +48 -0
  272. package/dist/services/user.d.ts.map +1 -0
  273. package/dist/services/user.js +141 -0
  274. package/dist/services/user.js.map +1 -0
  275. package/dist/services/worksheet.d.ts +239 -0
  276. package/dist/services/worksheet.d.ts.map +1 -0
  277. package/dist/services/worksheet.js +235 -0
  278. package/dist/services/worksheet.js.map +1 -0
  279. package/dist/utils/aiUser.d.ts +1 -3
  280. package/dist/utils/aiUser.d.ts.map +1 -1
  281. package/dist/utils/aiUser.js +6 -5
  282. package/dist/utils/aiUser.js.map +1 -1
  283. package/dist/utils/email.d.ts +3 -0
  284. package/dist/utils/email.d.ts.map +1 -1
  285. package/dist/utils/email.js +7 -4
  286. package/dist/utils/email.js.map +1 -1
  287. package/dist/utils/generateInviteCode.d.ts +1 -2
  288. package/dist/utils/generateInviteCode.d.ts.map +1 -1
  289. package/dist/utils/generateInviteCode.js +3 -4
  290. package/dist/utils/generateInviteCode.js.map +1 -1
  291. package/dist/utils/inference.d.ts +3 -0
  292. package/dist/utils/inference.d.ts.map +1 -1
  293. package/dist/utils/inference.js +7 -4
  294. package/dist/utils/inference.js.map +1 -1
  295. package/dist/utils/logger.d.ts +3 -0
  296. package/dist/utils/logger.d.ts.map +1 -1
  297. package/dist/utils/logger.js +5 -2
  298. package/dist/utils/logger.js.map +1 -1
  299. package/dist/utils/prismaErrorHandler.d.ts.map +1 -1
  300. package/dist/utils/prismaErrorHandler.js +5 -2
  301. package/dist/utils/prismaErrorHandler.js.map +1 -1
  302. package/dist/utils/prismaWrapper.d.ts +1 -0
  303. package/dist/utils/prismaWrapper.d.ts.map +1 -1
  304. package/dist/utils/prismaWrapper.js +6 -2
  305. package/dist/utils/prismaWrapper.js.map +1 -1
  306. package/docker-compose.yml +5 -0
  307. package/package.json +4 -3
  308. package/src/index.ts +119 -12
  309. package/src/lib/config/env.ts +6 -0
  310. package/src/lib/fileUpload.ts +0 -1
  311. package/src/lib/googleCloudStorage.ts +17 -0
  312. package/src/lib/pusher.ts +5 -1
  313. package/src/lib/redis.ts +56 -0
  314. package/src/lib/thumbnailGenerator.ts +170 -168
  315. package/src/middleware/auth.ts +80 -137
  316. package/src/models/agenda.ts +46 -0
  317. package/src/models/announcement.ts +134 -0
  318. package/src/models/assignment.ts +322 -0
  319. package/src/models/attendance.ts +208 -0
  320. package/src/models/auth.ts +247 -0
  321. package/src/models/class.ts +598 -0
  322. package/src/models/comment.ts +152 -0
  323. package/src/models/conversation.ts +200 -0
  324. package/src/models/event.ts +177 -0
  325. package/src/models/file.ts +129 -0
  326. package/src/models/folder.ts +225 -0
  327. package/src/models/labChat.ts +213 -0
  328. package/src/models/marketing.ts +45 -0
  329. package/src/models/message.ts +153 -0
  330. package/src/models/newtonChat.ts +70 -0
  331. package/src/models/notification.ts +54 -0
  332. package/src/models/section.ts +98 -0
  333. package/src/models/user.ts +47 -0
  334. package/src/models/worksheet.ts +294 -0
  335. package/src/{server/pipelines → pipelines}/aiLabChat.ts +11 -7
  336. package/src/{server/pipelines → pipelines}/aiNewtonChat.ts +9 -5
  337. package/src/{server/pipelines → pipelines}/gradeWorksheet.ts +25 -14
  338. package/src/routers/agenda.ts +3 -66
  339. package/src/routers/announcement.ts +54 -495
  340. package/src/routers/assignment.ts +126 -2018
  341. package/src/routers/attendance.ts +15 -276
  342. package/src/routers/auth.ts +79 -442
  343. package/src/routers/class.ts +263 -1187
  344. package/src/routers/comment.ts +61 -288
  345. package/src/routers/conversation.ts +51 -360
  346. package/src/routers/event.ts +50 -481
  347. package/src/routers/file.ts +45 -368
  348. package/src/routers/folder.ts +107 -836
  349. package/src/routers/labChat.ts +29 -605
  350. package/src/routers/marketing.ts +35 -77
  351. package/src/routers/message.ts +45 -571
  352. package/src/routers/newtonChat.ts +17 -278
  353. package/src/routers/notifications.ts +32 -82
  354. package/src/routers/section.ts +46 -330
  355. package/src/routers/user.ts +49 -227
  356. package/src/routers/worksheet.ts +215 -503
  357. package/src/services/agenda.ts +21 -0
  358. package/src/services/announcement.ts +290 -0
  359. package/src/services/assignment.ts +1198 -0
  360. package/src/services/attendance.ts +85 -0
  361. package/src/services/auth.ts +277 -0
  362. package/src/services/class.ts +622 -0
  363. package/src/services/comment.ts +106 -0
  364. package/src/services/conversation.ts +213 -0
  365. package/src/services/event.ts +231 -0
  366. package/src/services/file.ts +167 -0
  367. package/src/services/folder.ts +316 -0
  368. package/src/services/labChat.ts +352 -0
  369. package/src/services/marketing.ts +57 -0
  370. package/src/services/message.ts +461 -0
  371. package/src/services/newtonChat.ts +222 -0
  372. package/src/services/notification.ts +50 -0
  373. package/src/services/section.ts +283 -0
  374. package/src/services/user.ts +172 -0
  375. package/src/services/worksheet.ts +358 -0
  376. package/src/utils/aiUser.ts +4 -3
  377. package/src/utils/email.ts +5 -3
  378. package/src/utils/generateInviteCode.ts +1 -3
  379. package/src/utils/inference.ts +5 -2
  380. package/src/utils/logger.ts +3 -1
  381. package/src/utils/prismaErrorHandler.ts +3 -0
  382. package/src/utils/prismaWrapper.ts +4 -0
  383. package/tests/globalSetup.ts +62 -0
  384. package/tests/helpers.ts +22 -0
  385. package/tests/middleware/security.test.ts +42 -0
  386. package/tests/routers/agenda.test.ts +138 -0
  387. package/tests/routers/announcement.test.ts +490 -0
  388. package/tests/routers/assignment.test.ts +837 -0
  389. package/tests/{attendance.test.ts → routers/attendance.test.ts} +6 -14
  390. package/tests/routers/auth.test.ts +171 -0
  391. package/tests/{class.test.ts → routers/class.test.ts} +131 -85
  392. package/tests/routers/comment.test.ts +126 -0
  393. package/tests/routers/conversation.test.ts +145 -0
  394. package/tests/{event.test.ts → routers/event.test.ts} +93 -32
  395. package/tests/routers/folder.test.ts +178 -0
  396. package/tests/routers/labChat.test.ts +115 -0
  397. package/tests/routers/marketing.test.ts +59 -0
  398. package/tests/routers/message.test.ts +123 -0
  399. package/tests/routers/notification.test.ts +69 -0
  400. package/tests/{section.test.ts → routers/section.test.ts} +5 -13
  401. package/tests/server/rateLimit.test.ts +73 -0
  402. package/tests/setup.ts +18 -92
  403. package/tests/user.test.ts +9 -31
  404. package/tests/utils/aiUser.test.ts +22 -0
  405. package/tests/utils/generateInviteCode.test.ts +24 -0
  406. package/tests/utils/logger.test.ts +74 -0
  407. package/tests/utils/prismaErrorHandler.test.ts +101 -0
  408. package/tests/utils/prismaWrapper.test.ts +82 -0
  409. package/tests/worksheet.test.ts +181 -0
  410. package/vitest.config.ts +6 -3
  411. package/vitest.unit.config.ts +21 -0
  412. package/TODO.md +0 -2
  413. package/coverage/base.css +0 -224
  414. package/coverage/block-navigation.js +0 -87
  415. package/coverage/clover.xml +0 -12110
  416. package/coverage/coverage-final.json +0 -44
  417. package/coverage/favicon.png +0 -0
  418. package/coverage/index.html +0 -221
  419. package/coverage/prettify.css +0 -1
  420. package/coverage/prettify.js +0 -2
  421. package/coverage/server/index.html +0 -116
  422. package/coverage/server/src/exportType.ts.html +0 -109
  423. package/coverage/server/src/index.html +0 -161
  424. package/coverage/server/src/index.ts.html +0 -1702
  425. package/coverage/server/src/instrument.ts.html +0 -130
  426. package/coverage/server/src/lib/config/env.ts.html +0 -448
  427. package/coverage/server/src/lib/config/index.html +0 -116
  428. package/coverage/server/src/lib/fileUpload.ts.html +0 -1138
  429. package/coverage/server/src/lib/googleCloudStorage.ts.html +0 -334
  430. package/coverage/server/src/lib/index.html +0 -206
  431. package/coverage/server/src/lib/jsonConversion.ts.html +0 -2323
  432. package/coverage/server/src/lib/jsonStyles.ts.html +0 -193
  433. package/coverage/server/src/lib/notificationHandler.ts.html +0 -193
  434. package/coverage/server/src/lib/pusher.ts.html +0 -121
  435. package/coverage/server/src/lib/thumbnailGenerator.ts.html +0 -592
  436. package/coverage/server/src/middleware/auth.ts.html +0 -646
  437. package/coverage/server/src/middleware/index.html +0 -146
  438. package/coverage/server/src/middleware/logging.ts.html +0 -244
  439. package/coverage/server/src/middleware/security.ts.html +0 -271
  440. package/coverage/server/src/routers/_app.ts.html +0 -232
  441. package/coverage/server/src/routers/agenda.ts.html +0 -319
  442. package/coverage/server/src/routers/announcement.ts.html +0 -3481
  443. package/coverage/server/src/routers/assignment.ts.html +0 -7633
  444. package/coverage/server/src/routers/attendance.ts.html +0 -1030
  445. package/coverage/server/src/routers/auth.ts.html +0 -1081
  446. package/coverage/server/src/routers/class.ts.html +0 -3535
  447. package/coverage/server/src/routers/comment.ts.html +0 -991
  448. package/coverage/server/src/routers/conversation.ts.html +0 -982
  449. package/coverage/server/src/routers/event.ts.html +0 -1609
  450. package/coverage/server/src/routers/file.ts.html +0 -1144
  451. package/coverage/server/src/routers/folder.ts.html +0 -2797
  452. package/coverage/server/src/routers/index.html +0 -386
  453. package/coverage/server/src/routers/labChat.ts.html +0 -3073
  454. package/coverage/server/src/routers/marketing.ts.html +0 -340
  455. package/coverage/server/src/routers/message.ts.html +0 -1912
  456. package/coverage/server/src/routers/notifications.ts.html +0 -364
  457. package/coverage/server/src/routers/section.ts.html +0 -1120
  458. package/coverage/server/src/routers/user.ts.html +0 -862
  459. package/coverage/server/src/routers/worksheet.ts.html +0 -1729
  460. package/coverage/server/src/trpc.ts.html +0 -397
  461. package/coverage/server/src/types/index.html +0 -116
  462. package/coverage/server/src/types/trpc.ts.html +0 -127
  463. package/coverage/server/src/utils/aiUser.ts.html +0 -280
  464. package/coverage/server/src/utils/email.ts.html +0 -121
  465. package/coverage/server/src/utils/generateInviteCode.ts.html +0 -106
  466. package/coverage/server/src/utils/index.html +0 -206
  467. package/coverage/server/src/utils/inference.ts.html +0 -709
  468. package/coverage/server/src/utils/logger.ts.html +0 -664
  469. package/coverage/server/src/utils/prismaErrorHandler.ts.html +0 -907
  470. package/coverage/server/src/utils/prismaWrapper.ts.html +0 -355
  471. package/coverage/server/vitest.config.ts.html +0 -196
  472. package/coverage/sort-arrow-sprite.png +0 -0
  473. package/coverage/sorter.js +0 -210
  474. package/src/lib/notificationHandler.ts +0 -36
  475. package/tests/announcement.test.ts +0 -164
  476. package/tests/assignment.test.ts +0 -296
  477. package/tests/auth.test.ts +0 -48
@@ -4,9 +4,31 @@ import { TRPCError } from "@trpc/server";
4
4
  import { prisma } from "../lib/prisma.js";
5
5
  import { createDirectUploadFiles, type DirectUploadFile, confirmDirectUpload, updateUploadProgress, type UploadedFile } from "../lib/fileUpload.js";
6
6
  import { deleteFile } from "../lib/googleCloudStorage.js";
7
- import { sendNotifications } from "../lib/notificationHandler.js";
8
7
  import { logger } from "../utils/logger.js";
9
- import { gradeWorksheetPipeline } from "../server/pipelines/gradeWorksheet.js";
8
+ import { gradeWorksheetPipeline } from "../pipelines/gradeWorksheet.js";
9
+ import {
10
+ assignmentExists,
11
+ getDueToday,
12
+ getAssignment,
13
+ getSubmission,
14
+ getSubmissionById,
15
+ getSubmissions,
16
+ createAssignmentRecord,
17
+ updateAssignmentRecord,
18
+ deleteAssignmentRecord,
19
+ updateSubmissionRecord,
20
+ updateSubmissionAsTeacherRecord,
21
+ attachAssignmentToEventRecord,
22
+ detachAssignmentFromEventRecord,
23
+ getAvailableEventsForAssignment,
24
+ attachMarkSchemeRecord,
25
+ detachMarkSchemeRecord,
26
+ attachGradingBoundaryRecord,
27
+ detachGradingBoundaryRecord,
28
+ reorderAssignmentRecord,
29
+ moveAssignmentRecord,
30
+ } from "../services/assignment.js";
31
+ import { sendToMultiple } from "../services/notification.js";
10
32
 
11
33
  // DEPRECATED: This schema is no longer used - files are uploaded directly to GCS
12
34
  // Use directFileSchema instead
@@ -149,2065 +171,151 @@ const updateUploadProgressSchema = z.object({
149
171
  progress: z.number().min(0).max(100),
150
172
  });
151
173
 
152
- // Helper function to get unified list of sections and assignments for a class
153
- async function getUnifiedList(tx: any, classId: string) {
154
- const [sections, assignments] = await Promise.all([
155
- tx.section.findMany({
156
- where: { classId },
157
- select: { id: true, order: true },
158
- }),
159
- tx.assignment.findMany({
160
- where: { classId },
161
- select: { id: true, order: true },
162
- }),
163
- ]);
164
-
165
- // Combine and sort by order
166
- const unified = [
167
- ...sections.map((s: any) => ({ id: s.id, order: s.order, type: 'section' as const })),
168
- ...assignments.map((a: any) => ({ id: a.id, order: a.order, type: 'assignment' as const })),
169
- ].sort((a, b) => (a.order ?? Number.MAX_SAFE_INTEGER) - (b.order ?? Number.MAX_SAFE_INTEGER));
170
-
171
- return unified;
172
- }
173
-
174
- // Helper function to normalize unified list to 1..n
175
- // Updated to batch updates to prevent timeouts with large lists
176
- async function normalizeUnifiedList(tx: any, classId: string, orderedItems: Array<{ id: string; type: 'section' | 'assignment' }>) {
177
- const BATCH_SIZE = 10; // Process 10 items at a time to avoid overwhelming the transaction
178
-
179
- // Group items by type for more efficient updates
180
- const sections: Array<{ id: string; order: number }> = [];
181
- const assignments: Array<{ id: string; order: number }> = [];
182
-
183
- orderedItems.forEach((item, index) => {
184
- const orderData = { id: item.id, order: index + 1 };
185
- if (item.type === 'section') {
186
- sections.push(orderData);
187
- } else {
188
- assignments.push(orderData);
189
- }
190
- });
191
-
192
- // Process updates in batches
193
- const processBatch = async (items: Array<{ id: string; order: number }>, type: 'section' | 'assignment') => {
194
- for (let i = 0; i < items.length; i += BATCH_SIZE) {
195
- const batch = items.slice(i, i + BATCH_SIZE);
196
- await Promise.all(
197
- batch.map(item => {
198
- if (type === 'section') {
199
- return tx.section.update({ where: { id: item.id }, data: { order: item.order } });
200
- } else {
201
- return tx.assignment.update({ where: { id: item.id }, data: { order: item.order } });
202
- }
203
- })
204
- );
205
- }
206
- };
207
-
208
- // Process sections and assignments sequentially to avoid transaction overload
209
- await processBatch(sections, 'section');
210
- await processBatch(assignments, 'assignment');
211
- }
212
-
213
174
  export const assignmentRouter = createTRPCRouter({
214
- // Reorder an assignment within the unified list (sections + assignments)
215
175
  reorder: protectedTeacherProcedure
216
176
  .input(z.object({
217
177
  classId: z.string(),
218
178
  movedId: z.string(),
219
- // One of: place at start/end of unified list, or relative to targetId (can be section or assignment)
220
179
  position: z.enum(['start', 'end', 'before', 'after']),
221
- targetId: z.string().optional(), // Can be a section ID or assignment ID
222
- }))
223
- .mutation(async ({ ctx, input }) => {
224
- const { classId, movedId, position, targetId } = input;
225
-
226
- const moved = await prisma.assignment.findFirst({
227
- where: { id: movedId, classId },
228
- select: { id: true, classId: true },
229
- });
230
-
231
- if (!moved) {
232
- throw new TRPCError({ code: 'NOT_FOUND', message: 'Assignment not found' });
233
- }
234
-
235
- if ((position === 'before' || position === 'after') && !targetId) {
236
- throw new TRPCError({ code: 'BAD_REQUEST', message: 'targetId required for before/after' });
237
- }
238
-
239
- const result = await prisma.$transaction(async (tx) => {
240
- const unified = await getUnifiedList(tx, classId);
241
-
242
- // Find moved item and target in unified list
243
- const movedIdx = unified.findIndex(item => item.id === movedId && item.type === 'assignment');
244
- if (movedIdx === -1) {
245
- throw new TRPCError({ code: 'NOT_FOUND', message: 'Assignment not found in unified list' });
246
- }
247
-
248
- // Build list without moved item
249
- const withoutMoved = unified.filter(item => !(item.id === movedId && item.type === 'assignment'));
250
-
251
- let next: Array<{ id: string; type: 'section' | 'assignment' }> = [];
252
-
253
- if (position === 'start') {
254
- next = [{ id: movedId, type: 'assignment' }, ...withoutMoved.map(item => ({ id: item.id, type: item.type }))];
255
- } else if (position === 'end') {
256
- next = [...withoutMoved.map(item => ({ id: item.id, type: item.type })), { id: movedId, type: 'assignment' }];
257
- } else {
258
- const targetIdx = withoutMoved.findIndex(item => item.id === targetId);
259
- if (targetIdx === -1) {
260
- throw new TRPCError({ code: 'BAD_REQUEST', message: 'targetId not found in unified list' });
261
- }
262
- if (position === 'before') {
263
- next = [
264
- ...withoutMoved.slice(0, targetIdx).map(item => ({ id: item.id, type: item.type })),
265
- { id: movedId, type: 'assignment' },
266
- ...withoutMoved.slice(targetIdx).map(item => ({ id: item.id, type: item.type })),
267
- ];
268
- } else {
269
- next = [
270
- ...withoutMoved.slice(0, targetIdx + 1).map(item => ({ id: item.id, type: item.type })),
271
- { id: movedId, type: 'assignment' },
272
- ...withoutMoved.slice(targetIdx + 1).map(item => ({ id: item.id, type: item.type })),
273
- ];
274
- }
275
- }
276
-
277
- // Normalize to 1..n
278
- await normalizeUnifiedList(tx, classId, next);
279
-
280
- return tx.assignment.findUnique({ where: { id: movedId } });
281
- }, {
282
- maxWait: 10000, // 10 seconds max wait time
283
- timeout: 30000, // 30 seconds timeout for reordering operations
284
- });
285
-
286
- return result;
287
- }),
288
- order: protectedTeacherProcedure
289
- .input(z.object({
290
- id: z.string(),
291
- classId: z.string(),
292
- order: z.number(),
180
+ targetId: z.string().optional(),
293
181
  }))
294
- .mutation(async ({ ctx, input }) => {
295
- // Deprecated: prefer `reorder`. For backward-compatibility, set the order then normalize unified list.
296
- const { id, order } = input;
297
-
298
- const current = await prisma.assignment.findUnique({
299
- where: { id },
300
- select: { id: true, classId: true },
301
- });
302
-
303
- if (!current) {
304
- throw new TRPCError({ code: 'NOT_FOUND', message: 'Assignment not found' });
305
- }
306
-
307
- const updated = await prisma.$transaction(async (tx) => {
308
- await tx.assignment.update({ where: { id }, data: { order } });
309
-
310
- // Normalize entire unified list
311
- const unified = await getUnifiedList(tx, current.classId);
312
- await normalizeUnifiedList(tx, current.classId, unified.map(item => ({ id: item.id, type: item.type })));
313
-
314
- return tx.assignment.findUnique({ where: { id } });
315
- }, {
316
- maxWait: 10000, // 10 seconds max wait time
317
- timeout: 30000, // 30 seconds timeout for reordering operations
318
- });
319
-
320
- return updated;
321
- }),
182
+ .mutation(({ ctx, input }) =>
183
+ reorderAssignmentRecord(ctx.user!.id, {
184
+ classId: input.classId,
185
+ movedId: input.movedId,
186
+ position: input.position,
187
+ targetId: input.targetId,
188
+ })
189
+ ),
322
190
 
323
191
  exists: protectedClassMemberProcedure
324
- .input(z.object({
325
- id: z.string(),
326
- }))
327
- .query(async ({ ctx, input }) => {
328
- if (!ctx.user) {
329
- throw new TRPCError({ code: 'UNAUTHORIZED', message: 'User must be authenticated' });
330
- }
331
- const assignment = await prisma.assignment.findUnique({
332
- where: { id: input.id },
333
- });
334
-
335
- return assignment ? true : false;
336
- }),
192
+ .input(z.object({ id: z.string() }))
193
+ .query(({ input }) => assignmentExists(input.id)),
337
194
  move: protectedTeacherProcedure
338
195
  .input(z.object({
339
196
  id: z.string(),
340
197
  classId: z.string(),
341
198
  targetSectionId: z.string().nullable().optional(),
342
199
  }))
343
- .mutation(async ({ ctx, input }) => {
344
- const { id } = input;
345
- const targetSectionId = (input.targetSectionId ?? null) || null; // normalize empty string to null
346
-
347
- const moved = await prisma.assignment.findUnique({
348
- where: { id },
349
- select: { id: true, classId: true, sectionId: true },
350
- });
351
-
352
- if (!moved) {
353
- throw new TRPCError({ code: 'NOT_FOUND', message: 'Assignment not found' });
354
- }
355
-
356
- const updated = await prisma.$transaction(async (tx) => {
357
- // Update sectionId first
358
- await tx.assignment.update({ where: { id }, data: { sectionId: targetSectionId } });
359
-
360
- // The unified list ordering remains the same, just the assignment's sectionId changed
361
- // No need to reorder since we're keeping the same position in the unified list
362
- // If frontend wants to change position, they should call reorder after move
363
-
364
- return tx.assignment.findUnique({ where: { id } });
365
- }, {
366
- maxWait: 5000, // 5 seconds max wait time
367
- timeout: 10000, // 10 seconds timeout
368
- });
369
-
370
- return updated;
371
- }),
200
+ .mutation(({ ctx, input }) =>
201
+ moveAssignmentRecord(ctx.user!.id, {
202
+ id: input.id,
203
+ classId: input.classId,
204
+ targetSectionId: (input.targetSectionId ?? null) || null,
205
+ })
206
+ ),
372
207
 
373
208
  create: protectedTeacherProcedure
374
209
  .input(createAssignmentSchema)
375
- .mutation(async ({ ctx, input }) => {
376
- const { classId, id, title, instructions, dueDate, files, existingFileIds, aiPolicyLevel, acceptFiles, acceptExtendedResponse, acceptWorksheet, worksheetIds, gradeWithAI, studentIds, maxGrade, graded, weight, sectionId, type, markSchemeId, gradingBoundaryId, inProgress } = input;
377
-
378
- if (!ctx.user) {
379
- throw new TRPCError({
380
- code: "UNAUTHORIZED",
381
- message: "User must be authenticated",
382
- });
383
- }
384
-
385
- // Pre-fetch data needed for the transaction (outside transaction scope)
386
- const [classData, rubricData] = await Promise.all([
387
- prisma.class.findUnique({
388
- where: { id: classId },
389
- include: {
390
- students: {
391
- select: { id: true }
392
- }
393
- }
394
- }),
395
- markSchemeId ? prisma.markScheme.findUnique({
396
- where: { id: markSchemeId },
397
- select: {
398
- structured: true,
399
- }
400
- }) : null
401
- ]);
402
-
403
- if (!classData) {
404
- throw new TRPCError({
405
- code: "NOT_FOUND",
406
- message: "Class not found",
407
- });
408
- }
409
-
410
- // Calculate max grade outside transaction
411
- let computedMaxGrade = maxGrade;
412
- if (markSchemeId && rubricData) {
413
- const parsedRubric = JSON.parse(rubricData.structured || "{}");
414
- computedMaxGrade = parsedRubric.criteria.reduce((acc: number, criterion: any) => {
415
- const maxPoints = Math.max(...criterion.levels.map((level: any) => level.points));
416
- return acc + maxPoints;
417
- }, 0);
418
- }
419
-
420
- // Prepare submission data outside transaction
421
- const submissionData = studentIds && studentIds.length > 0
422
- ? studentIds.map((studentId) => ({
423
- student: { connect: { id: studentId } }
424
- }))
425
- : classData.students.map((student) => ({
426
- student: { connect: { id: student.id } }
427
- }));
428
-
429
- const teacherId = ctx.user.id;
430
-
431
- // Minimal transaction - only for critical operations
432
- const assignment = await prisma.$transaction(async (tx) => {
433
- // Create assignment with order 0 (will be at top)
434
- const created = await tx.assignment.create({
435
- data: {
436
- ...(id && { id }),
437
- title,
438
- instructions,
439
- dueDate: new Date(dueDate),
440
- maxGrade: markSchemeId ? computedMaxGrade : maxGrade,
441
- graded,
442
- weight,
443
- type,
444
- ...(aiPolicyLevel && { aiPolicyLevel }),
445
- acceptFiles,
446
- acceptExtendedResponse,
447
- acceptWorksheet,
448
- ...(worksheetIds && {
449
- worksheets: {
450
- connect: worksheetIds.map(id => ({ id }))
451
- }
452
- }),
453
- gradeWithAI,
454
- ...(studentIds && {
455
- assignedTo: {
456
- connect: studentIds.map(id => ({ id }))
457
- }
458
- }),
459
- order: 0, // Set to 0 to place at top
460
- inProgress: inProgress || false,
461
- class: {
462
- connect: { id: classId }
463
- },
464
- ...(sectionId && {
465
- section: {
466
- connect: { id: sectionId }
467
- }
468
- }),
469
- ...(markSchemeId && {
470
- markScheme: {
471
- connect: { id: markSchemeId }
472
- }
473
- }),
474
- ...(gradingBoundaryId && {
475
- gradingBoundary: {
476
- connect: { id: gradingBoundaryId }
477
- }
478
- }),
479
- submissions: {
480
- create: submissionData
481
- },
482
- teacher: {
483
- connect: { id: teacherId }
484
- }
485
- },
486
- select: {
487
- id: true,
488
- title: true,
489
- instructions: true,
490
- dueDate: true,
491
- maxGrade: true,
492
- graded: true,
493
- weight: true,
494
- type: true,
495
- attachments: {
496
- select: {
497
- id: true,
498
- name: true,
499
- type: true,
500
- }
501
- },
502
- section: {
503
- select: {
504
- id: true,
505
- name: true
506
- }
507
- },
508
- teacher: {
509
- select: {
510
- id: true,
511
- username: true
512
- }
513
- },
514
- class: {
515
- select: {
516
- id: true,
517
- name: true
518
- }
519
- }
520
- }
521
- });
522
-
523
- // Optimized reordering - only increment existing items by 1
524
- // This is much faster than reordering everything
525
- await tx.assignment.updateMany({
526
- where: {
527
- classId: classId,
528
- id: { not: created.id },
529
- },
530
- data: {
531
- order: { increment: 1 }
532
- }
533
- });
534
-
535
- await tx.section.updateMany({
536
- where: {
537
- classId: classId,
538
- },
539
- data: {
540
- order: { increment: 1 }
541
- }
542
- });
543
-
544
- // Update the created assignment to have order 1
545
- await tx.assignment.update({
546
- where: { id: created.id },
547
- data: { order: 1 }
548
- });
549
-
550
- return created;
551
- }, {
552
- maxWait: 10000, // Increased to 10 seconds max wait time
553
- timeout: 20000, // Increased to 20 seconds timeout for safety
554
- });
555
-
556
- // Handle file operations outside transaction
557
- const fileOperations: Promise<any>[] = [];
558
-
559
- // Process direct upload files
560
- if (files && files.length > 0) {
561
- fileOperations.push(
562
- createDirectUploadFiles(files, ctx.user.id, undefined, assignment.id)
563
- .then(uploadedFiles => {
564
- if (uploadedFiles.length > 0) {
565
- return prisma.assignment.update({
566
- where: { id: assignment.id },
567
- data: {
568
- attachments: {
569
- create: uploadedFiles.map(file => ({
570
- name: file.name,
571
- type: file.type,
572
- size: file.size,
573
- path: file.path,
574
- }))
575
- }
576
- }
577
- });
578
- }
579
- })
580
- );
581
- }
582
-
583
- // Connect existing files
584
- if (existingFileIds && existingFileIds.length > 0) {
585
- fileOperations.push(
586
- prisma.assignment.update({
587
- where: { id: assignment.id },
588
- data: {
589
- attachments: {
590
- connect: existingFileIds.map(fileId => ({ id: fileId }))
591
- }
592
- }
593
- })
594
- );
595
- }
596
-
597
- // Execute file operations in parallel
598
- await Promise.all(fileOperations);
599
-
600
- // Send notifications asynchronously (non-blocking)
601
- sendNotifications(classData.students.map(student => student.id), {
602
- title: `🔔 New assignment for ${classData.name}`,
603
- content:
604
- `The assignment "${title}" has been created in ${classData.name}.\n
605
- Due date: ${new Date(dueDate).toLocaleDateString()}.
606
- [Link to assignment](/class/${classId}/assignments/${assignment.id})`
607
- }).catch(error => {
608
- logger.error('Failed to send assignment notifications:', error);
609
- });
610
-
611
- return assignment;
612
- }),
210
+ .mutation(({ ctx, input }) => createAssignmentRecord(ctx.user!.id, input)),
613
211
  update: protectedTeacherProcedure
614
212
  .input(updateAssignmentSchema)
615
- .mutation(async ({ ctx, input }) => {
616
- const { id, title, instructions, dueDate, files, existingFileIds, worksheetIds, aiPolicyLevel, maxGrade, graded, weight, sectionId, type, inProgress, acceptFiles, acceptExtendedResponse, acceptWorksheet, gradeWithAI, studentIds } = input;
617
-
618
- if (!ctx.user) {
619
- throw new TRPCError({
620
- code: "UNAUTHORIZED",
621
- message: "User must be authenticated",
622
- });
623
- }
624
-
625
- // Pre-fetch all necessary data outside transaction
626
- const [assignment, classData] = await Promise.all([
627
- prisma.assignment.findFirst({
628
- where: {
629
- id,
630
- teacherId: ctx.user.id,
631
- },
632
- include: {
633
- attachments: {
634
- select: {
635
- id: true,
636
- name: true,
637
- type: true,
638
- path: true,
639
- size: true,
640
- uploadStatus: true,
641
- thumbnail: {
642
- select: {
643
- path: true
644
- }
645
- }
646
- },
647
- },
648
- class: {
649
- select: {
650
- id: true,
651
- name: true
652
- }
653
- },
654
- markScheme: true,
655
- },
656
- }),
657
- prisma.class.findFirst({
658
- where: {
659
- assignments: {
660
- some: { id }
661
- }
662
- },
663
- include: {
664
- students: {
665
- select: { id: true }
666
- }
667
- }
668
- })
669
- ]);
670
-
671
- if (!assignment) {
672
- throw new TRPCError({
673
- code: "NOT_FOUND",
674
- message: "Assignment not found",
675
- });
676
- }
677
-
678
- // Prepare submission data outside transaction if needed
679
- const submissionData = studentIds && studentIds.length > 0
680
- ? studentIds.map((studentId) => ({
681
- student: { connect: { id: studentId } }
682
- }))
683
- : classData?.students.map((student) => ({
684
- student: { connect: { id: student.id } }
685
- }));
686
-
687
- // Handle file deletion operations outside transaction
688
- const fileDeletionPromises: Promise<void>[] = [];
689
- if (input.removedAttachments && input.removedAttachments.length > 0) {
690
- const filesToDelete = assignment.attachments.filter((file) =>
691
- input.removedAttachments!.includes(file.id)
692
- );
693
-
694
- filesToDelete.forEach((file) => {
695
- if (file.uploadStatus === 'COMPLETED') {
696
- fileDeletionPromises.push(
697
- deleteFile(file.path).catch(error => {
698
- console.warn(`Failed to delete file ${file.path}:`, error);
699
- })
700
- );
701
- if (file.thumbnail?.path) {
702
- fileDeletionPromises.push(
703
- deleteFile(file.thumbnail.path).catch(error => {
704
- console.warn(`Failed to delete thumbnail ${file.thumbnail!.path}:`, error);
705
- })
706
- );
707
- }
708
- }
709
- });
710
- }
711
-
712
- // Execute file deletions in parallel
713
- await Promise.all(fileDeletionPromises);
714
-
715
- // Prepare file upload operations
716
- let uploadedFiles: UploadedFile[] = [];
717
- if (files && files.length > 0) {
718
- uploadedFiles = await createDirectUploadFiles(files, ctx.user.id, undefined, input.id);
719
- }
720
-
721
- // Minimal transaction for database update
722
- const updatedAssignment = await prisma.$transaction(async (tx) => {
723
- return tx.assignment.update({
724
- where: { id },
725
- data: {
726
- ...(title && { title }),
727
- ...(instructions && { instructions }),
728
- ...(dueDate && { dueDate: new Date(dueDate) }),
729
- ...(maxGrade && { maxGrade }),
730
- ...(graded !== undefined && { graded }),
731
- ...(weight && { weight }),
732
- ...(type && { type }),
733
- ...(inProgress !== undefined && { inProgress }),
734
- ...(acceptFiles !== undefined && { acceptFiles }),
735
- ...(acceptExtendedResponse !== undefined && { acceptExtendedResponse }),
736
- ...(acceptWorksheet !== undefined && { acceptWorksheet }),
737
- ...(gradeWithAI !== undefined && { gradeWithAI }),
738
- ...(studentIds && {
739
- assignedTo: {
740
- set: [], // Clear existing
741
- connect: studentIds.map(id => ({ id }))
742
- }
743
- }),
744
- ...(submissionData && {
745
- submissions: {
746
- deleteMany: {}, // Clear existing submissions
747
- create: submissionData
748
- }
749
- }),
750
- ...(aiPolicyLevel !== undefined && { aiPolicyLevel }),
751
- ...(sectionId !== undefined && {
752
- section: sectionId ? {
753
- connect: { id: sectionId }
754
- } : {
755
- disconnect: true
756
- }
757
- }),
758
- ...(worksheetIds && {
759
- worksheets: {
760
- set: [], // Clear existing
761
- connect: worksheetIds.map(id => ({ id }))
762
- }
763
- }),
764
- ...(uploadedFiles.length > 0 && {
765
- attachments: {
766
- create: uploadedFiles.map(file => ({
767
- name: file.name,
768
- type: file.type,
769
- size: file.size,
770
- path: file.path,
771
- ...(file.thumbnailId && {
772
- thumbnail: {
773
- connect: { id: file.thumbnailId }
774
- }
775
- })
776
- }))
777
- }
778
- }),
779
- ...(existingFileIds && existingFileIds.length > 0 && {
780
- attachments: {
781
- connect: existingFileIds.map(fileId => ({ id: fileId }))
782
- }
783
- }),
784
- ...(input.removedAttachments && input.removedAttachments.length > 0 && {
785
- attachments: {
786
- deleteMany: {
787
- id: { in: input.removedAttachments }
788
- }
789
- }
790
- }),
791
- },
792
- select: {
793
- id: true,
794
- title: true,
795
- instructions: true,
796
- dueDate: true,
797
- maxGrade: true,
798
- graded: true,
799
- weight: true,
800
- type: true,
801
- createdAt: true,
802
- markSchemeId: true,
803
- submissions: {
804
- select: {
805
- student: {
806
- select: {
807
- id: true,
808
- username: true
809
- }
810
- }
811
- }
812
- },
813
- attachments: {
814
- select: {
815
- id: true,
816
- name: true,
817
- type: true,
818
- thumbnail: true,
819
- size: true,
820
- path: true,
821
- uploadedAt: true,
822
- thumbnailId: true,
823
- }
824
- },
825
- section: true,
826
- teacher: true,
827
- class: true
828
- }
829
- });
830
- }, {
831
- maxWait: 5000, // 5 seconds max wait time
832
- timeout: 10000, // 10 seconds timeout
833
- });
834
-
835
- // Handle rubric max grade calculation outside transaction
836
- if (updatedAssignment.markSchemeId) {
837
- prisma.markScheme.findUnique({
838
- where: { id: updatedAssignment.markSchemeId },
839
- select: {
840
- structured: true,
841
- }
842
- }).then(rubric => {
843
- if (rubric) {
844
- const parsedRubric = JSON.parse(rubric.structured || "{}");
845
- const computedMaxGrade = parsedRubric.criteria?.reduce((acc: number, criterion: any) => {
846
- const maxPoints = Math.max(...criterion.levels.map((level: any) => level.points));
847
- return acc + maxPoints;
848
- }, 0) || 0;
849
-
850
- return prisma.assignment.update({
851
- where: { id },
852
- data: {
853
- maxGrade: computedMaxGrade,
854
- }
855
- });
856
- }
857
- }).catch(error => {
858
- logger.error('Failed to update max grade from rubric:', error);
859
- });
860
- }
861
-
862
- return updatedAssignment;
863
- }),
213
+ .mutation(({ ctx, input }) => updateAssignmentRecord(ctx.user!.id, input)),
864
214
 
865
215
  delete: protectedProcedure
866
216
  .input(deleteAssignmentSchema)
867
- .mutation(async ({ ctx, input }) => {
868
- const { id, classId } = input;
869
-
870
- if (!ctx.user) {
871
- throw new TRPCError({
872
- code: "UNAUTHORIZED",
873
- message: "User must be authenticated",
874
- });
875
- }
876
-
877
- // Get the assignment with all related files
878
- const assignment = await prisma.assignment.findFirst({
879
- where: {
880
- id,
881
- teacherId: ctx.user.id,
882
- },
883
- include: {
884
- attachments: {
885
- include: {
886
- thumbnail: true
887
- }
888
- },
889
- submissions: {
890
- include: {
891
- attachments: {
892
- include: {
893
- thumbnail: true
894
- }
895
- },
896
- annotations: {
897
- include: {
898
- thumbnail: true
899
- }
900
- }
901
- }
902
- }
903
- }
904
- });
217
+ .mutation(({ ctx, input }) =>
218
+ deleteAssignmentRecord(ctx.user!.id, input.id, input.classId)
219
+ ),
905
220
 
906
- if (!assignment) {
907
- throw new TRPCError({
908
- code: "NOT_FOUND",
909
- message: "Assignment not found",
910
- });
911
- }
221
+ get: protectedClassMemberProcedure
222
+ .input(getAssignmentSchema)
223
+ .query(({ input }) => getAssignment(input.id, input.classId)),
912
224
 
913
- // Delete all files from storage
914
- const filesToDelete = [
915
- ...assignment.attachments,
916
- ...assignment.submissions.flatMap(sub => [...sub.attachments, ...sub.annotations])
917
- ];
918
-
919
- // Delete files from storage (only if they were actually uploaded)
920
- await Promise.all(filesToDelete.map(async (file) => {
921
- try {
922
- // Only delete from GCS if the file was successfully uploaded
923
- if (file.uploadStatus === 'COMPLETED') {
924
- // Delete the main file
925
- await deleteFile(file.path);
926
-
927
- // Delete thumbnail if it exists
928
- if (file.thumbnail) {
929
- await deleteFile(file.thumbnail.path);
930
- }
931
- }
932
- } catch (error) {
933
- console.warn(`Failed to delete file ${file.path}:`, error);
934
- }
935
- }));
936
-
937
- // Delete the assignment (this will cascade delete all related records)
938
- await prisma.assignment.delete({
939
- where: { id },
940
- });
225
+ getSubmission: protectedClassMemberProcedure
226
+ .input(z.object({ assignmentId: z.string(), classId: z.string() }))
227
+ .query(({ ctx, input }) =>
228
+ getSubmission(input.assignmentId, ctx.user!.id)
229
+ ),
941
230
 
942
- return {
943
- id,
944
- };
945
- }),
231
+ getSubmissionById: protectedClassMemberProcedure
232
+ .input(z.object({ classId: z.string(), submissionId: z.string() }))
233
+ .query(({ ctx, input }) =>
234
+ getSubmissionById(input.submissionId, input.classId, ctx.user!.id)
235
+ ),
946
236
 
947
- get: protectedClassMemberProcedure
948
- .input(getAssignmentSchema)
949
- .query(async ({ ctx, input }) => {
950
- const { id, classId } = input;
237
+ updateSubmission: protectedClassMemberProcedure
238
+ .input(submissionSchema)
239
+ .mutation(({ ctx, input }) =>
240
+ updateSubmissionRecord(ctx.user!.id, {
241
+ submissionId: input.submissionId,
242
+ assignmentId: input.assignmentId,
243
+ classId: input.classId,
244
+ submit: input.submit,
245
+ newAttachments: input.newAttachments,
246
+ extendedResponse: input.extendedResponse,
247
+ existingFileIds: input.existingFileIds,
248
+ removedAttachments: input.removedAttachments,
249
+ })
250
+ ),
951
251
 
952
- if (!ctx.user) {
953
- throw new TRPCError({
954
- code: "UNAUTHORIZED",
955
- message: "User must be authenticated",
956
- });
957
- }
252
+ getSubmissions: protectedTeacherProcedure
253
+ .input(z.object({ assignmentId: z.string(), classId: z.string() }))
254
+ .query(({ ctx, input }) => getSubmissions(input.assignmentId, ctx.user!.id)),
958
255
 
959
- const assignment = await prisma.assignment.findUnique({
960
- where: {
961
- id,
962
- // classId,
963
- },
964
- include: {
965
- submissions: {
966
- select: {
967
- student: {
968
- select: {
969
- id: true,
970
- username: true
971
- }
972
- }
973
- }
974
- },
975
- attachments: {
976
- select: {
977
- id: true,
978
- name: true,
979
- type: true,
980
- size: true,
981
- path: true,
982
- uploadedAt: true,
983
- thumbnailId: true,
984
- }
985
- },
986
- section: {
987
- select: {
988
- id: true,
989
- name: true,
990
- }
991
- },
992
- teacher: {
993
- select: {
994
- id: true,
995
- username: true
996
- }
997
- },
998
- worksheets: {
999
- select: {
1000
- id: true,
1001
- name: true,
1002
- }
1003
- },
1004
- class: {
1005
- select: {
1006
- id: true,
1007
- name: true
1008
- }
1009
- },
1010
- eventAttached: {
1011
- select: {
1012
- id: true,
1013
- name: true,
1014
- startTime: true,
1015
- endTime: true,
1016
- location: true,
1017
- remarks: true,
1018
- color: true,
1019
- }
1020
- },
1021
- markScheme: {
1022
- select: {
1023
- id: true,
1024
- structured: true,
1025
- }
1026
- },
1027
- gradingBoundary: {
1028
- select: {
1029
- id: true,
1030
- structured: true,
1031
- }
1032
- }
1033
- }
1034
- });
256
+ updateSubmissionAsTeacher: protectedTeacherProcedure
257
+ .input(updateSubmissionSchema)
258
+ .mutation(({ ctx, input }) =>
259
+ updateSubmissionAsTeacherRecord(ctx.user!.id, {
260
+ submissionId: input.submissionId,
261
+ assignmentId: input.assignmentId,
262
+ classId: input.classId,
263
+ return: input.return,
264
+ gradeReceived: input.gradeReceived,
265
+ newAttachments: input.newAttachments,
266
+ existingFileIds: input.existingFileIds,
267
+ removedAttachments: input.removedAttachments,
268
+ rubricGrades: input.rubricGrades,
269
+ feedback: input.feedback,
270
+ })
271
+ ),
1035
272
 
1036
- if (!assignment) {
1037
- throw new TRPCError({
1038
- code: "NOT_FOUND",
1039
- message: "Assignment not found",
1040
- });
1041
- }
273
+ attachToEvent: protectedTeacherProcedure
274
+ .input(z.object({ assignmentId: z.string(), eventId: z.string() }))
275
+ .mutation(({ ctx, input }) =>
276
+ attachAssignmentToEventRecord(ctx.user!.id, input.assignmentId, input.eventId)
277
+ ),
1042
278
 
1043
- const sections = await prisma.section.findMany({
1044
- where: {
1045
- classId: assignment.classId,
1046
- },
1047
- select: {
1048
- id: true,
1049
- name: true,
1050
- },
1051
- });
279
+ detachEvent: protectedTeacherProcedure
280
+ .input(z.object({ assignmentId: z.string() }))
281
+ .mutation(({ ctx, input }) =>
282
+ detachAssignmentFromEventRecord(ctx.user!.id, input.assignmentId)
283
+ ),
1052
284
 
1053
- return { ...assignment, sections };
1054
- }),
285
+ getAvailableEvents: protectedTeacherProcedure
286
+ .input(z.object({ assignmentId: z.string() }))
287
+ .query(({ ctx, input }) =>
288
+ getAvailableEventsForAssignment(ctx.user!.id, input.assignmentId)
289
+ ),
1055
290
 
1056
- getSubmission: protectedClassMemberProcedure
291
+ dueToday: protectedProcedure
292
+ .query(() => getDueToday()),
293
+ attachMarkScheme: protectedTeacherProcedure
1057
294
  .input(z.object({
1058
295
  assignmentId: z.string(),
1059
- classId: z.string(),
296
+ markSchemeId: z.string().nullable(),
1060
297
  }))
1061
- .query(async ({ ctx, input }) => {
1062
- if (!ctx.user) {
1063
- throw new TRPCError({
1064
- code: "UNAUTHORIZED",
1065
- message: "User must be authenticated",
1066
- });
1067
- }
1068
-
1069
- const { assignmentId } = input;
1070
-
1071
- const submission = await prisma.submission.findFirst({
1072
- where: {
1073
- assignmentId,
1074
- studentId: ctx.user.id,
1075
- },
1076
- include: {
1077
- attachments: true,
1078
- student: {
1079
- select: {
1080
- id: true,
1081
- username: true,
1082
- profile: true,
1083
- },
1084
- },
1085
- assignment: {
1086
- include: {
1087
- class: true,
1088
- markScheme: {
1089
- select: {
1090
- id: true,
1091
- structured: true,
1092
- }
1093
- },
1094
- worksheets: {
1095
- select: {
1096
- id: true,
1097
- name: true,
1098
- }
1099
- },
1100
- gradingBoundary: {
1101
- select: {
1102
- id: true,
1103
- structured: true,
1104
- }
1105
- }
1106
- },
1107
- },
1108
- annotations: true,
1109
- },
1110
- });
1111
-
1112
- if (!submission) {
1113
- // Create a new submission if it doesn't exist
1114
- return await prisma.submission.create({
1115
- data: {
1116
- assignment: {
1117
- connect: { id: assignmentId },
1118
- },
1119
- student: {
1120
- connect: { id: ctx.user.id },
1121
- },
1122
- },
1123
- include: {
1124
- attachments: true,
1125
- annotations: true,
1126
- student: {
1127
- select: {
1128
- id: true,
1129
- username: true,
1130
- },
1131
- },
1132
- assignment: {
1133
- include: {
1134
- class: true,
1135
- markScheme: {
1136
- select: {
1137
- id: true,
1138
- structured: true,
1139
- }
1140
- },
1141
- gradingBoundary: {
1142
- select: {
1143
- id: true,
1144
- structured: true,
1145
- }
1146
- }
1147
- },
1148
- },
1149
- },
1150
- });
1151
- }
1152
-
1153
- return {
1154
- ...submission,
1155
- late: submission.assignment.dueDate < new Date(),
1156
- };
1157
- }),
1158
-
1159
- getSubmissionById: protectedClassMemberProcedure
1160
- .input(z.object({
1161
- classId: z.string(),
1162
- submissionId: z.string(),
1163
- }))
1164
- .query(async ({ ctx, input }) => {
1165
- const { submissionId, classId } = input;
1166
-
1167
- const submission = await prisma.submission.findFirst({
1168
- where: {
1169
- id: submissionId,
1170
- assignment: {
1171
- classId,
1172
- class: {
1173
- OR: [
1174
- {
1175
- teachers: {
1176
- some: {
1177
- id: ctx.user?.id
1178
- }
1179
- }
1180
- },
1181
- {
1182
- students: {
1183
- some: {
1184
- id: ctx.user?.id
1185
- }
1186
- }
1187
- }
1188
- ],
1189
- }
1190
- },
1191
- },
1192
- include: {
1193
- attachments: true,
1194
- annotations: true,
1195
- student: {
1196
- select: {
1197
- id: true,
1198
- username: true,
1199
- profile: true,
1200
- },
1201
- },
1202
- assignment: {
1203
- include: {
1204
- class: true,
1205
- markScheme: {
1206
- select: {
1207
- id: true,
1208
- structured: true,
1209
- }
1210
- },
1211
- gradingBoundary: {
1212
- select: {
1213
- id: true,
1214
- structured: true,
1215
- }
1216
- },
1217
- worksheets: {
1218
- select: {
1219
- id: true,
1220
- name: true,
1221
- }
1222
- }
1223
- },
1224
- },
1225
- },
1226
- });
1227
-
1228
- if (!submission) {
1229
- throw new TRPCError({
1230
- code: "NOT_FOUND",
1231
- message: "Submission not found",
1232
- });
1233
- }
1234
-
1235
- return {
1236
- ...submission,
1237
- late: submission.assignment.dueDate < new Date(),
1238
- };
1239
- }),
1240
-
1241
- updateSubmission: protectedClassMemberProcedure
1242
- .input(submissionSchema)
1243
- .mutation(async ({ ctx, input }) => {
1244
- if (!ctx.user) {
1245
- throw new TRPCError({
1246
- code: "UNAUTHORIZED",
1247
- message: "User must be authenticated",
1248
- });
1249
- }
1250
-
1251
- const { submissionId, submit, newAttachments, existingFileIds, removedAttachments, extendedResponse } = input;
1252
-
1253
- const submission = await prisma.submission.findFirst({
1254
- where: {
1255
- id: submissionId,
1256
- OR: [
1257
- {
1258
- student: {
1259
- id: ctx.user.id,
1260
- },
1261
- },
1262
- {
1263
- assignment: {
1264
- class: {
1265
- teachers: {
1266
- some: {
1267
- id: ctx.user.id,
1268
- },
1269
- },
1270
- },
1271
- },
1272
- },
1273
- ],
1274
- },
1275
- include: {
1276
- attachments: {
1277
- include: {
1278
- thumbnail: true
1279
- }
1280
- },
1281
- assignment: {
1282
- include: {
1283
- class: true,
1284
- markScheme: {
1285
- select: {
1286
- id: true,
1287
- structured: true,
1288
- }
1289
- },
1290
- gradingBoundary: {
1291
- select: {
1292
- id: true,
1293
- structured: true,
1294
- }
1295
- }
1296
- },
1297
- },
1298
- },
1299
- });
1300
-
1301
- if (!submission) {
1302
- throw new TRPCError({
1303
- code: "NOT_FOUND",
1304
- message: "Submission not found",
1305
- });
1306
- }
1307
-
1308
- if (submit !== undefined) {
1309
- // Toggle submission status
1310
- if (submission.assignment.acceptWorksheet && submission.assignment.gradeWithAI) {
1311
-
1312
- // Grade the submission with AI
1313
- const worksheetResponses = await prisma.studentWorksheetResponse.findMany({
1314
- where: {
1315
- submissionId: submission.id,
1316
- },
1317
- });
1318
-
1319
- for (const worksheetResponse of worksheetResponses) {
1320
- // Run it in the background, non-blocking
1321
- gradeWorksheetPipeline(worksheetResponse.id);
1322
- }
1323
- }
1324
- return await prisma.submission.update({
1325
- where: { id: submission.id },
1326
- data: {
1327
- submitted: !submission.submitted,
1328
- submittedAt: new Date(),
1329
- },
1330
- include: {
1331
- attachments: true,
1332
- student: {
1333
- select: {
1334
- id: true,
1335
- username: true,
1336
- },
1337
- },
1338
- assignment: {
1339
- include: {
1340
- class: true,
1341
- markScheme: {
1342
- select: {
1343
- id: true,
1344
- structured: true,
1345
- }
1346
- },
1347
- gradingBoundary: {
1348
- select: {
1349
- id: true,
1350
- structured: true,
1351
- }
1352
- }
1353
- },
1354
- },
1355
- },
1356
- });
1357
- }
1358
-
1359
- let uploadedFiles: UploadedFile[] = [];
1360
- if (newAttachments && newAttachments.length > 0) {
1361
- // Store files in a class and assignment specific directory
1362
- uploadedFiles = await createDirectUploadFiles(newAttachments, ctx.user.id, undefined, undefined, submission.id);
1363
- }
1364
-
1365
- // Update submission with new file attachments
1366
- if (uploadedFiles.length > 0) {
1367
- await prisma.submission.update({
1368
- where: { id: submission.id },
1369
- data: {
1370
- attachments: {
1371
- create: uploadedFiles.map(file => ({
1372
- name: file.name,
1373
- type: file.type,
1374
- size: file.size,
1375
- path: file.path,
1376
- ...(file.thumbnailId && {
1377
- thumbnail: {
1378
- connect: { id: file.thumbnailId }
1379
- }
1380
- })
1381
- }))
1382
- }
1383
- }
1384
- });
1385
- }
1386
-
1387
- // Connect existing files if provided
1388
- if (existingFileIds && existingFileIds.length > 0) {
1389
- await prisma.submission.update({
1390
- where: { id: submission.id },
1391
- data: {
1392
- attachments: {
1393
- connect: existingFileIds.map(fileId => ({ id: fileId }))
1394
- }
1395
- }
1396
- });
1397
- }
1398
-
1399
- // Delete removed attachments if any
1400
- if (removedAttachments && removedAttachments.length > 0) {
1401
- const filesToDelete = submission.attachments.filter((file) =>
1402
- removedAttachments.includes(file.id)
1403
- );
1404
-
1405
- // Delete files from storage (only if they were actually uploaded)
1406
- await Promise.all(filesToDelete.map(async (file) => {
1407
- try {
1408
- // Only delete from GCS if the file was successfully uploaded
1409
- if (file.uploadStatus === 'COMPLETED') {
1410
- // Delete the main file
1411
- await deleteFile(file.path);
1412
-
1413
- // Delete thumbnail if it exists
1414
- if (file.thumbnail?.path) {
1415
- await deleteFile(file.thumbnail.path);
1416
- }
1417
- }
1418
- } catch (error) {
1419
- console.warn(`Failed to delete file ${file.path}:`, error);
1420
- }
1421
- }));
1422
- }
1423
-
1424
- // Update submission with attachments
1425
- return await prisma.submission.update({
1426
- where: { id: submission.id },
1427
- data: {
1428
- ...(removedAttachments && removedAttachments.length > 0 && {
1429
- attachments: {
1430
- deleteMany: {
1431
- id: { in: removedAttachments },
1432
- },
1433
- },
1434
- }),
1435
- ...(extendedResponse !== undefined && { extendedResponse }),
1436
- },
1437
- include: {
1438
- attachments: {
1439
- include: {
1440
- thumbnail: true
1441
- }
1442
- },
1443
- student: {
1444
- select: {
1445
- id: true,
1446
- username: true,
1447
- },
1448
- },
1449
- assignment: {
1450
- include: {
1451
- class: true,
1452
- markScheme: {
1453
- select: {
1454
- id: true,
1455
- structured: true,
1456
- }
1457
- },
1458
- gradingBoundary: {
1459
- select: {
1460
- id: true,
1461
- structured: true,
1462
- }
1463
- }
1464
- },
1465
- },
1466
- },
1467
- });
1468
- }),
1469
-
1470
- getSubmissions: protectedTeacherProcedure
1471
- .input(z.object({
1472
- assignmentId: z.string(),
1473
- classId: z.string(),
1474
- }))
1475
- .query(async ({ ctx, input }) => {
1476
- if (!ctx.user) {
1477
- throw new TRPCError({
1478
- code: "UNAUTHORIZED",
1479
- message: "User must be authenticated",
1480
- });
1481
- }
1482
-
1483
- const { assignmentId } = input;
1484
-
1485
- const submissions = await prisma.submission.findMany({
1486
- where: {
1487
- assignment: {
1488
- id: assignmentId,
1489
- class: {
1490
- teachers: {
1491
- some: { id: ctx.user.id },
1492
- },
1493
- },
1494
- },
1495
- },
1496
- include: {
1497
- attachments: {
1498
- include: {
1499
- thumbnail: true
1500
- }
1501
- },
1502
- student: {
1503
- select: {
1504
- id: true,
1505
- username: true,
1506
- profile: {
1507
- select: {
1508
- displayName: true,
1509
- profilePicture: true,
1510
- profilePictureThumbnail: true,
1511
- },
1512
- },
1513
- },
1514
- },
1515
- assignment: {
1516
- include: {
1517
- class: true,
1518
- markScheme: {
1519
- select: {
1520
- id: true,
1521
- structured: true,
1522
- }
1523
- },
1524
- gradingBoundary: {
1525
- select: {
1526
- id: true,
1527
- structured: true,
1528
- }
1529
- }
1530
- },
1531
- },
1532
- },
1533
- });
1534
-
1535
- return submissions.map(submission => ({
1536
- ...submission,
1537
- late: submission.assignment.dueDate < new Date(),
1538
- }));
1539
- }),
1540
-
1541
- updateSubmissionAsTeacher: protectedTeacherProcedure
1542
- .input(updateSubmissionSchema)
1543
- .mutation(async ({ ctx, input }) => {
1544
- if (!ctx.user) {
1545
- throw new TRPCError({
1546
- code: "UNAUTHORIZED",
1547
- message: "User must be authenticated",
1548
- });
1549
- }
1550
-
1551
- const { submissionId, return: returnSubmission, gradeReceived, newAttachments, existingFileIds, removedAttachments, rubricGrades, feedback } = input;
1552
-
1553
- const submission = await prisma.submission.findFirst({
1554
- where: {
1555
- id: submissionId,
1556
- assignment: {
1557
- class: {
1558
- teachers: {
1559
- some: { id: ctx.user.id },
1560
- },
1561
- },
1562
- },
1563
- },
1564
- include: {
1565
- attachments: {
1566
- include: {
1567
- thumbnail: true
1568
- }
1569
- },
1570
- annotations: {
1571
- include: {
1572
- thumbnail: true
1573
- }
1574
- },
1575
- assignment: {
1576
- include: {
1577
- class: true,
1578
- markScheme: {
1579
- select: {
1580
- id: true,
1581
- structured: true,
1582
- }
1583
- },
1584
- gradingBoundary: {
1585
- select: {
1586
- id: true,
1587
- structured: true,
1588
- }
1589
- }
1590
- },
1591
- },
1592
- },
1593
- });
1594
-
1595
- if (!submission) {
1596
- throw new TRPCError({
1597
- code: "NOT_FOUND",
1598
- message: "Submission not found",
1599
- });
1600
- }
1601
-
1602
- if (returnSubmission !== undefined) {
1603
- // Toggle return status
1604
- return await prisma.submission.update({
1605
- where: { id: submissionId },
1606
- data: {
1607
- returned: !submission.returned,
1608
- },
1609
- include: {
1610
- attachments: true,
1611
- student: {
1612
- select: {
1613
- id: true,
1614
- username: true,
1615
- profile: {
1616
- select: {
1617
- displayName: true,
1618
- profilePicture: true,
1619
- profilePictureThumbnail: true,
1620
- },
1621
- },
1622
- },
1623
- },
1624
- assignment: {
1625
- include: {
1626
- class: true,
1627
- markScheme: {
1628
- select: {
1629
- id: true,
1630
- structured: true,
1631
- }
1632
- },
1633
- gradingBoundary: {
1634
- select: {
1635
- id: true,
1636
- structured: true,
1637
- }
1638
- }
1639
- },
1640
- },
1641
- },
1642
- });
1643
- }
1644
-
1645
- // NOTE: Teacher annotation files are now handled via direct upload endpoints
1646
- // Use getAnnotationUploadUrls and confirmAnnotationUpload endpoints instead
1647
- // The newAttachments field is deprecated for annotations
1648
- if (newAttachments && newAttachments.length > 0) {
1649
- throw new TRPCError({
1650
- code: "BAD_REQUEST",
1651
- message: "Direct file upload is deprecated. Use getAnnotationUploadUrls endpoint instead.",
1652
- });
1653
- }
1654
-
1655
- // Connect existing files if provided
1656
- if (existingFileIds && existingFileIds.length > 0) {
1657
- await prisma.submission.update({
1658
- where: { id: submission.id },
1659
- data: {
1660
- annotations: {
1661
- connect: existingFileIds.map(fileId => ({ id: fileId }))
1662
- }
1663
- }
1664
- });
1665
- }
1666
-
1667
- // Delete removed attachments if any
1668
- if (removedAttachments && removedAttachments.length > 0) {
1669
- const filesToDelete = submission.annotations.filter((file) =>
1670
- removedAttachments.includes(file.id)
1671
- );
1672
-
1673
- // Delete files from storage (only if they were actually uploaded)
1674
- await Promise.all(filesToDelete.map(async (file) => {
1675
- try {
1676
- // Only delete from GCS if the file was successfully uploaded
1677
- if (file.uploadStatus === 'COMPLETED') {
1678
- // Delete the main file
1679
- await deleteFile(file.path);
1680
-
1681
- // Delete thumbnail if it exists
1682
- if (file.thumbnail?.path) {
1683
- await deleteFile(file.thumbnail.path);
1684
- }
1685
- }
1686
- } catch (error) {
1687
- console.warn(`Failed to delete file ${file.path}:`, error);
1688
- }
1689
- }));
1690
- }
1691
-
1692
- // Update submission with grade and attachments
1693
- return await prisma.submission.update({
1694
- where: { id: submissionId },
1695
- data: {
1696
- ...(gradeReceived !== undefined && { gradeReceived }),
1697
- ...(rubricGrades && { rubricState: JSON.stringify(rubricGrades) }),
1698
- ...(feedback && { teacherComments: feedback }),
1699
- ...(removedAttachments && removedAttachments.length > 0 && {
1700
- annotations: {
1701
- deleteMany: {
1702
- id: { in: removedAttachments },
1703
- },
1704
- },
1705
- }),
1706
- ...(returnSubmission as unknown as boolean && { returned: returnSubmission }),
1707
- },
1708
- include: {
1709
- attachments: {
1710
- include: {
1711
- thumbnail: true
1712
- }
1713
- },
1714
- annotations: {
1715
- include: {
1716
- thumbnail: true
1717
- }
1718
- },
1719
- student: {
1720
- select: {
1721
- id: true,
1722
- username: true,
1723
- profile: {
1724
- select: {
1725
- displayName: true,
1726
- profilePicture: true,
1727
- profilePictureThumbnail: true,
1728
- },
1729
- },
1730
- },
1731
- },
1732
- assignment: {
1733
- include: {
1734
- class: true,
1735
- markScheme: {
1736
- select: {
1737
- id: true,
1738
- structured: true,
1739
- }
1740
- },
1741
- gradingBoundary: {
1742
- select: {
1743
- id: true,
1744
- structured: true,
1745
- }
1746
- }
1747
- },
1748
- },
1749
- },
1750
- });
1751
- }),
1752
-
1753
- attachToEvent: protectedTeacherProcedure
1754
- .input(z.object({
1755
- assignmentId: z.string(),
1756
- eventId: z.string(),
1757
- }))
1758
- .mutation(async ({ ctx, input }) => {
1759
- if (!ctx.user) {
1760
- throw new TRPCError({
1761
- code: "UNAUTHORIZED",
1762
- message: "User must be authenticated",
1763
- });
1764
- }
1765
-
1766
- const { assignmentId, eventId } = input;
1767
-
1768
- // Check if assignment exists and user is a teacher of the class
1769
- const assignment = await prisma.assignment.findFirst({
1770
- where: {
1771
- id: assignmentId,
1772
- class: {
1773
- teachers: {
1774
- some: { id: ctx.user.id },
1775
- },
1776
- },
1777
- },
1778
- include: {
1779
- class: true,
1780
- },
1781
- });
1782
-
1783
- if (!assignment) {
1784
- throw new TRPCError({
1785
- code: "NOT_FOUND",
1786
- message: "Assignment not found or you are not authorized",
1787
- });
1788
- }
1789
-
1790
- // Check if event exists and belongs to the same class
1791
- const event = await prisma.event.findFirst({
1792
- where: {
1793
- id: eventId,
1794
- classId: assignment.classId,
1795
- },
1796
- });
1797
-
1798
- if (!event) {
1799
- throw new TRPCError({
1800
- code: "NOT_FOUND",
1801
- message: "Event not found or does not belong to the same class",
1802
- });
1803
- }
1804
-
1805
- // Attach assignment to event
1806
- const updatedAssignment = await prisma.assignment.update({
1807
- where: { id: assignmentId },
1808
- data: {
1809
- eventAttached: {
1810
- connect: { id: eventId }
1811
- }
1812
- },
1813
- include: {
1814
- attachments: {
1815
- select: {
1816
- id: true,
1817
- name: true,
1818
- type: true,
1819
- }
1820
- },
1821
- section: {
1822
- select: {
1823
- id: true,
1824
- name: true
1825
- }
1826
- },
1827
- teacher: {
1828
- select: {
1829
- id: true,
1830
- username: true
1831
- }
1832
- },
1833
- eventAttached: {
1834
- select: {
1835
- id: true,
1836
- name: true,
1837
- startTime: true,
1838
- endTime: true,
1839
- color: true,
1840
- }
1841
- }
1842
- }
1843
- });
1844
-
1845
- return { assignment: updatedAssignment };
1846
- }),
1847
-
1848
- detachEvent: protectedTeacherProcedure
1849
- .input(z.object({
1850
- assignmentId: z.string(),
1851
- }))
1852
- .mutation(async ({ ctx, input }) => {
1853
- if (!ctx.user) {
1854
- throw new TRPCError({
1855
- code: "UNAUTHORIZED",
1856
- message: "User must be authenticated",
1857
- });
1858
- }
1859
-
1860
- const { assignmentId } = input;
1861
-
1862
- // Check if assignment exists and user is a teacher of the class
1863
- const assignment = await prisma.assignment.findFirst({
1864
- where: {
1865
- id: assignmentId,
1866
- class: {
1867
- teachers: {
1868
- some: { id: ctx.user.id },
1869
- },
1870
- },
1871
- },
1872
- });
1873
-
1874
- if (!assignment) {
1875
- throw new TRPCError({
1876
- code: "NOT_FOUND",
1877
- message: "Assignment not found or you are not authorized",
1878
- });
1879
- }
1880
-
1881
- // Detach assignment from event
1882
- const updatedAssignment = await prisma.assignment.update({
1883
- where: { id: assignmentId },
1884
- data: {
1885
- eventAttached: {
1886
- disconnect: true
1887
- }
1888
- },
1889
- include: {
1890
- attachments: {
1891
- select: {
1892
- id: true,
1893
- name: true,
1894
- type: true,
1895
- }
1896
- },
1897
- section: {
1898
- select: {
1899
- id: true,
1900
- name: true
1901
- }
1902
- },
1903
- teacher: {
1904
- select: {
1905
- id: true,
1906
- username: true
1907
- }
1908
- },
1909
- eventAttached: {
1910
- select: {
1911
- id: true,
1912
- name: true,
1913
- startTime: true,
1914
- endTime: true,
1915
- }
1916
- }
1917
- }
1918
- });
1919
-
1920
- return { assignment: updatedAssignment };
1921
- }),
1922
-
1923
- getAvailableEvents: protectedTeacherProcedure
1924
- .input(z.object({
1925
- assignmentId: z.string(),
1926
- }))
1927
- .query(async ({ ctx, input }) => {
1928
- if (!ctx.user) {
1929
- throw new TRPCError({
1930
- code: "UNAUTHORIZED",
1931
- message: "User must be authenticated",
1932
- });
1933
- }
1934
-
1935
- const { assignmentId } = input;
1936
-
1937
- // Get the assignment to find the class
1938
- const assignment = await prisma.assignment.findFirst({
1939
- where: {
1940
- id: assignmentId,
1941
- class: {
1942
- teachers: {
1943
- some: { id: ctx.user.id },
1944
- },
1945
- },
1946
- },
1947
- select: { classId: true }
1948
- });
1949
-
1950
- if (!assignment) {
1951
- throw new TRPCError({
1952
- code: "NOT_FOUND",
1953
- message: "Assignment not found or you are not authorized",
1954
- });
1955
- }
1956
-
1957
- // Get all events for the class that don't already have this assignment attached
1958
- const events = await prisma.event.findMany({
1959
- where: {
1960
- classId: assignment.classId,
1961
- assignmentsAttached: {
1962
- none: {
1963
- id: assignmentId
1964
- }
1965
- }
1966
- },
1967
- select: {
1968
- id: true,
1969
- name: true,
1970
- startTime: true,
1971
- endTime: true,
1972
- location: true,
1973
- remarks: true,
1974
- color: true,
1975
- },
1976
- orderBy: {
1977
- startTime: 'asc'
1978
- }
1979
- });
1980
-
1981
- return { events };
1982
- }),
1983
-
1984
- dueToday: protectedProcedure
1985
- .query(async ({ ctx }) => {
1986
- if (!ctx.user) {
1987
- throw new TRPCError({
1988
- code: "UNAUTHORIZED",
1989
- message: "User must be authenticated",
1990
- });
1991
- }
1992
-
1993
- const assignments = await prisma.assignment.findMany({
1994
- where: {
1995
- dueDate: {
1996
- equals: new Date(),
1997
- },
1998
- },
1999
- select: {
2000
- id: true,
2001
- title: true,
2002
- dueDate: true,
2003
- type: true,
2004
- graded: true,
2005
- maxGrade: true,
2006
- class: {
2007
- select: {
2008
- id: true,
2009
- name: true,
2010
- }
2011
- }
2012
- }
2013
- });
2014
-
2015
- return assignments.map(assignment => ({
2016
- ...assignment,
2017
- dueDate: assignment.dueDate.toISOString(),
2018
- }));
2019
- }),
2020
- attachMarkScheme: protectedTeacherProcedure
2021
- .input(z.object({
2022
- assignmentId: z.string(),
2023
- markSchemeId: z.string().nullable(),
2024
- }))
2025
- .mutation(async ({ ctx, input }) => {
2026
- const { assignmentId, markSchemeId } = input;
2027
-
2028
- const assignment = await prisma.assignment.findFirst({
2029
- where: {
2030
- id: assignmentId,
2031
- },
2032
- });
2033
-
2034
- if (!assignment) {
2035
- throw new TRPCError({
2036
- code: "NOT_FOUND",
2037
- message: "Assignment not found",
2038
- });
2039
- }
2040
-
2041
- // If markSchemeId is provided, verify it exists
2042
- if (markSchemeId) {
2043
- const markScheme = await prisma.markScheme.findFirst({
2044
- where: {
2045
- id: markSchemeId,
2046
- },
2047
- });
2048
-
2049
- if (!markScheme) {
2050
- throw new TRPCError({
2051
- code: "NOT_FOUND",
2052
- message: "Mark scheme not found",
2053
- });
2054
- }
2055
- }
2056
-
2057
- const updatedAssignment = await prisma.assignment.update({
2058
- where: { id: assignmentId },
2059
- data: {
2060
- markScheme: markSchemeId ? {
2061
- connect: { id: markSchemeId },
2062
- } : {
2063
- disconnect: true,
2064
- },
2065
- },
2066
- include: {
2067
- attachments: true,
2068
- section: true,
2069
- teacher: true,
2070
- eventAttached: true,
2071
- markScheme: true,
2072
- },
2073
- });
2074
-
2075
- return updatedAssignment;
2076
- }),
298
+ .mutation(({ ctx, input }) =>
299
+ attachMarkSchemeRecord(ctx.user!.id, input.assignmentId, input.markSchemeId)
300
+ ),
2077
301
  detachMarkScheme: protectedTeacherProcedure
2078
- .input(z.object({
2079
- assignmentId: z.string(),
2080
- }))
2081
- .mutation(async ({ ctx, input }) => {
2082
- const { assignmentId } = input;
2083
-
2084
- const assignment = await prisma.assignment.findFirst({
2085
- where: {
2086
- id: assignmentId,
2087
- },
2088
- });
2089
-
2090
- if (!assignment) {
2091
- throw new TRPCError({
2092
- code: "NOT_FOUND",
2093
- message: "Assignment not found",
2094
- });
2095
- }
2096
-
2097
- const updatedAssignment = await prisma.assignment.update({
2098
- where: { id: assignmentId },
2099
- data: {
2100
- markScheme: {
2101
- disconnect: true,
2102
- },
2103
- },
2104
- include: {
2105
- attachments: true,
2106
- section: true,
2107
- teacher: true,
2108
- eventAttached: true,
2109
- markScheme: true,
2110
- },
2111
- });
2112
-
2113
- return updatedAssignment;
2114
- }),
302
+ .input(z.object({ assignmentId: z.string() }))
303
+ .mutation(({ ctx, input }) =>
304
+ detachMarkSchemeRecord(ctx.user!.id, input.assignmentId)
305
+ ),
2115
306
  attachGradingBoundary: protectedTeacherProcedure
2116
307
  .input(z.object({
2117
308
  assignmentId: z.string(),
2118
309
  gradingBoundaryId: z.string().nullable(),
2119
310
  }))
2120
- .mutation(async ({ ctx, input }) => {
2121
- const { assignmentId, gradingBoundaryId } = input;
2122
-
2123
- const assignment = await prisma.assignment.findFirst({
2124
- where: {
2125
- id: assignmentId,
2126
- },
2127
- });
2128
-
2129
- if (!assignment) {
2130
- throw new TRPCError({
2131
- code: "NOT_FOUND",
2132
- message: "Assignment not found",
2133
- });
2134
- }
2135
-
2136
- // If gradingBoundaryId is provided, verify it exists
2137
- if (gradingBoundaryId) {
2138
- const gradingBoundary = await prisma.gradingBoundary.findFirst({
2139
- where: {
2140
- id: gradingBoundaryId,
2141
- },
2142
- });
2143
-
2144
- if (!gradingBoundary) {
2145
- throw new TRPCError({
2146
- code: "NOT_FOUND",
2147
- message: "Grading boundary not found",
2148
- });
2149
- }
2150
- }
2151
-
2152
- const updatedAssignment = await prisma.assignment.update({
2153
- where: { id: assignmentId },
2154
- data: {
2155
- gradingBoundary: gradingBoundaryId ? {
2156
- connect: { id: gradingBoundaryId },
2157
- } : {
2158
- disconnect: true,
2159
- },
2160
- },
2161
- include: {
2162
- attachments: true,
2163
- section: true,
2164
- teacher: true,
2165
- eventAttached: true,
2166
- gradingBoundary: true,
2167
- },
2168
- });
2169
-
2170
- return updatedAssignment;
2171
- }),
311
+ .mutation(({ ctx, input }) =>
312
+ attachGradingBoundaryRecord(ctx.user!.id, input.assignmentId, input.gradingBoundaryId)
313
+ ),
2172
314
  detachGradingBoundary: protectedTeacherProcedure
2173
- .input(z.object({
2174
- classId: z.string(),
2175
- assignmentId: z.string(),
2176
- }))
2177
- .mutation(async ({ ctx, input }) => {
2178
- const { assignmentId } = input;
2179
-
2180
- const assignment = await prisma.assignment.findFirst({
2181
- where: {
2182
- id: assignmentId,
2183
- },
2184
- });
2185
-
2186
- if (!assignment) {
2187
- throw new TRPCError({
2188
- code: "NOT_FOUND",
2189
- message: "Assignment not found",
2190
- });
2191
- }
2192
-
2193
- const updatedAssignment = await prisma.assignment.update({
2194
- where: { id: assignmentId },
2195
- data: {
2196
- gradingBoundary: {
2197
- disconnect: true,
2198
- },
2199
- },
2200
- include: {
2201
- attachments: true,
2202
- section: true,
2203
- teacher: true,
2204
- eventAttached: true,
2205
- gradingBoundary: true,
2206
- },
2207
- });
2208
-
2209
- return updatedAssignment;
2210
- }),
315
+ .input(z.object({ classId: z.string(), assignmentId: z.string() }))
316
+ .mutation(({ ctx, input }) =>
317
+ detachGradingBoundaryRecord(ctx.user!.id, input.assignmentId)
318
+ ),
2211
319
 
2212
320
  // New direct upload endpoints
2213
321
  getAssignmentUploadUrls: protectedTeacherProcedure