@studious-lms/server 1.1.24 → 1.2.26

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 (49) hide show
  1. package/dist/lib/fileUpload.d.ts +2 -2
  2. package/dist/lib/fileUpload.d.ts.map +1 -1
  3. package/dist/lib/fileUpload.js +76 -14
  4. package/dist/lib/googleCloudStorage.d.ts +7 -0
  5. package/dist/lib/googleCloudStorage.d.ts.map +1 -1
  6. package/dist/lib/googleCloudStorage.js +19 -0
  7. package/dist/lib/notificationHandler.d.ts +25 -0
  8. package/dist/lib/notificationHandler.d.ts.map +1 -0
  9. package/dist/lib/notificationHandler.js +28 -0
  10. package/dist/routers/_app.d.ts +818 -78
  11. package/dist/routers/_app.d.ts.map +1 -1
  12. package/dist/routers/announcement.d.ts +290 -3
  13. package/dist/routers/announcement.d.ts.map +1 -1
  14. package/dist/routers/announcement.js +896 -10
  15. package/dist/routers/assignment.d.ts +70 -4
  16. package/dist/routers/assignment.d.ts.map +1 -1
  17. package/dist/routers/assignment.js +265 -131
  18. package/dist/routers/auth.js +1 -1
  19. package/dist/routers/file.d.ts +2 -0
  20. package/dist/routers/file.d.ts.map +1 -1
  21. package/dist/routers/file.js +9 -6
  22. package/dist/routers/labChat.d.ts.map +1 -1
  23. package/dist/routers/labChat.js +13 -5
  24. package/dist/routers/notifications.d.ts +8 -8
  25. package/dist/routers/section.d.ts +16 -0
  26. package/dist/routers/section.d.ts.map +1 -1
  27. package/dist/routers/section.js +139 -30
  28. package/dist/seedDatabase.d.ts +2 -2
  29. package/dist/seedDatabase.d.ts.map +1 -1
  30. package/dist/seedDatabase.js +2 -1
  31. package/dist/utils/logger.d.ts +1 -0
  32. package/dist/utils/logger.d.ts.map +1 -1
  33. package/dist/utils/logger.js +27 -2
  34. package/package.json +2 -2
  35. package/prisma/migrations/20251109122857_annuoncements_comments/migration.sql +30 -0
  36. package/prisma/migrations/20251109135555_reactions_announcements_comments/migration.sql +35 -0
  37. package/prisma/schema.prisma +50 -0
  38. package/src/lib/fileUpload.ts +79 -14
  39. package/src/lib/googleCloudStorage.ts +19 -0
  40. package/src/lib/notificationHandler.ts +36 -0
  41. package/src/routers/announcement.ts +1007 -10
  42. package/src/routers/assignment.ts +230 -82
  43. package/src/routers/auth.ts +1 -1
  44. package/src/routers/file.ts +10 -7
  45. package/src/routers/labChat.ts +15 -6
  46. package/src/routers/section.ts +158 -36
  47. package/src/seedDatabase.ts +2 -1
  48. package/src/utils/logger.ts +29 -2
  49. package/tests/setup.ts +3 -9
@@ -2,6 +2,30 @@ import { z } from "zod";
2
2
  import { createTRPCRouter, protectedClassMemberProcedure, protectedTeacherProcedure, protectedProcedure } from "../trpc.js";
3
3
  import { prisma } from "../lib/prisma.js";
4
4
  import { TRPCError } from "@trpc/server";
5
+ import { sendNotifications } from "../lib/notificationHandler.js";
6
+ import { logger } from "../utils/logger.js";
7
+ import { createDirectUploadFiles, type UploadedFile, type DirectUploadFile, confirmDirectUpload } from "../lib/fileUpload.js";
8
+ import { deleteFile } from "../lib/googleCloudStorage.js";
9
+
10
+ // Schema for direct file uploads (no base64 data)
11
+ const directFileSchema = z.object({
12
+ name: z.string(),
13
+ type: z.string(),
14
+ size: z.number(),
15
+ });
16
+
17
+ // Schemas for file upload endpoints
18
+ const getAnnouncementUploadUrlsSchema = z.object({
19
+ announcementId: z.string(),
20
+ classId: z.string(),
21
+ files: z.array(directFileSchema),
22
+ });
23
+
24
+ const confirmAnnouncementUploadSchema = z.object({
25
+ fileId: z.string(),
26
+ uploadSuccess: z.boolean(),
27
+ errorMessage: z.string().optional(),
28
+ });
5
29
 
6
30
  const AnnouncementSelect = {
7
31
  id: true,
@@ -13,6 +37,18 @@ const AnnouncementSelect = {
13
37
  },
14
38
  remarks: true,
15
39
  createdAt: true,
40
+ modifiedAt: true,
41
+ attachments: {
42
+ select: {
43
+ id: true,
44
+ name: true,
45
+ type: true,
46
+ size: true,
47
+ path: true,
48
+ uploadedAt: true,
49
+ thumbnailId: true,
50
+ },
51
+ },
16
52
  };
17
53
 
18
54
  export const announcementRouter = createTRPCRouter({
@@ -25,42 +61,148 @@ export const announcementRouter = createTRPCRouter({
25
61
  where: {
26
62
  classId: input.classId,
27
63
  },
28
- select: AnnouncementSelect,
64
+ select: {
65
+ ...AnnouncementSelect,
66
+ _count: {
67
+ select: {
68
+ comments: true,
69
+ },
70
+ },
71
+ },
29
72
  orderBy: {
30
73
  createdAt: 'desc',
31
74
  },
32
75
  });
33
76
 
77
+ // Transform to include comment count
78
+ const announcementsWithCounts = announcements.map(announcement => ({
79
+ ...announcement,
80
+ commentCount: announcement._count.comments,
81
+ _count: undefined,
82
+ }));
83
+
84
+ return {
85
+ announcements: announcementsWithCounts,
86
+ };
87
+ }),
88
+
89
+ get: protectedClassMemberProcedure
90
+ .input(z.object({
91
+ id: z.string(),
92
+ classId: z.string(),
93
+ }))
94
+ .query(async ({ ctx, input }) => {
95
+ const announcement = await prisma.announcement.findUnique({
96
+ where: {
97
+ id: input.id,
98
+ classId: input.classId,
99
+ },
100
+ select: AnnouncementSelect,
101
+ });
102
+
103
+ if (!announcement) {
104
+ throw new TRPCError({
105
+ code: "NOT_FOUND",
106
+ message: "Announcement not found",
107
+ });
108
+ }
109
+
34
110
  return {
35
- announcements,
111
+ announcement,
36
112
  };
37
113
  }),
38
114
 
39
115
  create: protectedTeacherProcedure
40
116
  .input(z.object({
41
117
  classId: z.string(),
42
- remarks: z.string(),
118
+ remarks: z.string().min(1, "Remarks cannot be empty"),
119
+ files: z.array(directFileSchema).optional(),
120
+ existingFileIds: z.array(z.string()).optional(),
43
121
  }))
44
122
  .mutation(async ({ ctx, input }) => {
123
+ const { classId, remarks, files, existingFileIds } = input;
124
+
125
+ if (!ctx.user) {
126
+ throw new TRPCError({
127
+ code: "UNAUTHORIZED",
128
+ message: "User must be authenticated",
129
+ });
130
+ }
131
+
132
+ const classData = await prisma.class.findUnique({
133
+ where: { id: classId },
134
+ include: {
135
+ students: {
136
+ select: { id: true }
137
+ }
138
+ }
139
+ });
140
+
141
+ if (!classData) {
142
+ throw new TRPCError({
143
+ code: "NOT_FOUND",
144
+ message: "Class not found",
145
+ });
146
+ }
147
+
45
148
  const announcement = await prisma.announcement.create({
46
149
  data: {
47
- remarks: input.remarks,
150
+ remarks: remarks,
48
151
  teacher: {
49
152
  connect: {
50
- id: ctx.user?.id,
153
+ id: ctx.user.id,
51
154
  },
52
155
  },
53
156
  class: {
54
157
  connect: {
55
- id: input.classId,
158
+ id: classId,
56
159
  },
57
160
  },
58
161
  },
59
162
  select: AnnouncementSelect,
60
163
  });
61
164
 
165
+ // Handle file attachments
166
+ // NOTE: Files are now handled via direct upload endpoints
167
+ // The files field in the schema is for metadata only
168
+ // Actual file uploads should use getAnnouncementUploadUrls endpoint
169
+ // However, if files are provided here, we create the file records and return upload URLs
170
+ let directUploadFiles: DirectUploadFile[] = [];
171
+ if (files && files.length > 0) {
172
+ // Create direct upload files - this creates file records with upload URLs
173
+ // Files are automatically connected to the announcement via announcementId
174
+ directUploadFiles = await createDirectUploadFiles(files, ctx.user.id, undefined, undefined, undefined, announcement.id);
175
+ }
176
+
177
+ // Connect existing files if provided
178
+ if (existingFileIds && existingFileIds.length > 0) {
179
+ await prisma.announcement.update({
180
+ where: { id: announcement.id },
181
+ data: {
182
+ attachments: {
183
+ connect: existingFileIds.map(fileId => ({ id: fileId }))
184
+ }
185
+ }
186
+ });
187
+ }
188
+
189
+ // Fetch announcement with attachments
190
+ const announcementWithAttachments = await prisma.announcement.findUnique({
191
+ where: { id: announcement.id },
192
+ select: AnnouncementSelect,
193
+ });
194
+
195
+ sendNotifications(classData.students.map(student => student.id), {
196
+ title: `🔔 Announcement for ${classData.name}`,
197
+ content: remarks
198
+ }).catch(error => {
199
+ logger.error('Failed to send announcement notifications:', error);
200
+ });
201
+
62
202
  return {
63
- announcement,
203
+ announcement: announcementWithAttachments || announcement,
204
+ // Return upload URLs if files were provided
205
+ uploadFiles: directUploadFiles.length > 0 ? directUploadFiles : undefined,
64
206
  };
65
207
  }),
66
208
 
@@ -68,10 +210,19 @@ export const announcementRouter = createTRPCRouter({
68
210
  .input(z.object({
69
211
  id: z.string(),
70
212
  data: z.object({
71
- content: z.string(),
213
+ remarks: z.string().min(1, "Remarks cannot be empty").optional(),
214
+ files: z.array(directFileSchema).optional(),
215
+ existingFileIds: z.array(z.string()).optional(),
216
+ removedAttachments: z.array(z.string()).optional(),
72
217
  }),
73
218
  }))
74
219
  .mutation(async ({ ctx, input }) => {
220
+ if (!ctx.user) {
221
+ throw new TRPCError({
222
+ code: "UNAUTHORIZED",
223
+ message: "User must be authenticated",
224
+ });
225
+ }
75
226
 
76
227
  const announcement = await prisma.announcement.findUnique({
77
228
  where: { id: input.id },
@@ -81,6 +232,21 @@ export const announcementRouter = createTRPCRouter({
81
232
  teachers: true,
82
233
  },
83
234
  },
235
+ attachments: {
236
+ select: {
237
+ id: true,
238
+ name: true,
239
+ type: true,
240
+ path: true,
241
+ size: true,
242
+ uploadStatus: true,
243
+ thumbnail: {
244
+ select: {
245
+ path: true
246
+ }
247
+ }
248
+ },
249
+ },
84
250
  },
85
251
  });
86
252
 
@@ -91,14 +257,86 @@ export const announcementRouter = createTRPCRouter({
91
257
  });
92
258
  }
93
259
 
260
+ // Authorization check: user must be the creator OR a teacher in the class
261
+ const userId = ctx.user.id;
262
+ const isCreator = announcement.teacherId === userId;
263
+ const isClassTeacher = announcement.class.teachers.some(
264
+ (teacher) => teacher.id === userId
265
+ );
266
+
267
+ if (!isCreator && !isClassTeacher) {
268
+ throw new TRPCError({
269
+ code: "FORBIDDEN",
270
+ message: "Only the announcement creator or class teachers can update announcements",
271
+ });
272
+ }
273
+
274
+ // Handle file attachments
275
+ // NOTE: Files are now handled via direct upload endpoints
276
+ let directUploadFiles: DirectUploadFile[] = [];
277
+ if (input.data.files && input.data.files.length > 0) {
278
+ // Create direct upload files - this creates file records with upload URLs
279
+ // Files are automatically connected to the announcement via announcementId
280
+ directUploadFiles = await createDirectUploadFiles(input.data.files, userId, undefined, undefined, undefined, input.id);
281
+ }
282
+
283
+ // Delete removed attachments from storage before updating database
284
+ if (input.data.removedAttachments && input.data.removedAttachments.length > 0) {
285
+ const filesToDelete = announcement.attachments.filter((file) =>
286
+ input.data.removedAttachments!.includes(file.id)
287
+ );
288
+
289
+ // Delete files from storage (only if they were actually uploaded)
290
+ await Promise.all(filesToDelete.map(async (file) => {
291
+ try {
292
+ // Only delete from GCS if the file was successfully uploaded
293
+ if (file.uploadStatus === 'COMPLETED') {
294
+ // Delete the main file
295
+ await deleteFile(file.path);
296
+
297
+ // Delete thumbnail if it exists
298
+ if (file.thumbnail?.path) {
299
+ await deleteFile(file.thumbnail.path);
300
+ }
301
+ }
302
+ } catch (error) {
303
+ logger.warn(`Failed to delete file ${file.path}:`, {
304
+ error: error instanceof Error ? {
305
+ name: error.name,
306
+ message: error.message,
307
+ stack: error.stack,
308
+ } : error
309
+ });
310
+ }
311
+ }));
312
+ }
313
+
94
314
  const updatedAnnouncement = await prisma.announcement.update({
95
315
  where: { id: input.id },
96
316
  data: {
97
- remarks: input.data.content,
317
+ ...(input.data.remarks && { remarks: input.data.remarks }),
318
+ // Note: directUploadFiles are already connected via createDirectUploadFiles
319
+ ...(input.data.existingFileIds && input.data.existingFileIds.length > 0 && {
320
+ attachments: {
321
+ connect: input.data.existingFileIds.map(fileId => ({ id: fileId }))
322
+ }
323
+ }),
324
+ ...(input.data.removedAttachments && input.data.removedAttachments.length > 0 && {
325
+ attachments: {
326
+ deleteMany: {
327
+ id: { in: input.data.removedAttachments }
328
+ }
329
+ }
330
+ }),
98
331
  },
332
+ select: AnnouncementSelect,
99
333
  });
100
334
 
101
- return { announcement: updatedAnnouncement };
335
+ return {
336
+ announcement: updatedAnnouncement,
337
+ // Return upload URLs if new files were provided
338
+ uploadFiles: directUploadFiles.length > 0 ? directUploadFiles : undefined,
339
+ };
102
340
  }),
103
341
 
104
342
  delete: protectedProcedure
@@ -106,6 +344,12 @@ export const announcementRouter = createTRPCRouter({
106
344
  id: z.string(),
107
345
  }))
108
346
  .mutation(async ({ ctx, input }) => {
347
+ if (!ctx.user) {
348
+ throw new TRPCError({
349
+ code: "UNAUTHORIZED",
350
+ message: "User must be authenticated",
351
+ });
352
+ }
109
353
 
110
354
  const announcement = await prisma.announcement.findUnique({
111
355
  where: { id: input.id },
@@ -125,10 +369,763 @@ export const announcementRouter = createTRPCRouter({
125
369
  });
126
370
  }
127
371
 
372
+ // Authorization check: user must be the creator OR a teacher in the class
373
+ const userId = ctx.user.id;
374
+ const isCreator = announcement.teacherId === userId;
375
+ const isClassTeacher = announcement.class.teachers.some(
376
+ (teacher) => teacher.id === userId
377
+ );
378
+
379
+ if (!isCreator && !isClassTeacher) {
380
+ throw new TRPCError({
381
+ code: "FORBIDDEN",
382
+ message: "Only the announcement creator or class teachers can delete announcements",
383
+ });
384
+ }
385
+
128
386
  await prisma.announcement.delete({
129
387
  where: { id: input.id },
130
388
  });
131
389
 
132
390
  return { success: true };
133
391
  }),
392
+
393
+ getAnnouncementUploadUrls: protectedTeacherProcedure
394
+ .input(getAnnouncementUploadUrlsSchema)
395
+ .mutation(async ({ ctx, input }) => {
396
+ const { announcementId, classId, files } = input;
397
+
398
+ if (!ctx.user) {
399
+ throw new TRPCError({
400
+ code: "UNAUTHORIZED",
401
+ message: "You must be logged in to upload files",
402
+ });
403
+ }
404
+
405
+ // Verify user is a teacher of the class
406
+ const classData = await prisma.class.findFirst({
407
+ where: {
408
+ id: classId,
409
+ teachers: {
410
+ some: {
411
+ id: ctx.user.id,
412
+ },
413
+ },
414
+ },
415
+ });
416
+
417
+ if (!classData) {
418
+ throw new TRPCError({
419
+ code: "NOT_FOUND",
420
+ message: "Class not found or you are not a teacher",
421
+ });
422
+ }
423
+
424
+ // Verify announcement exists and belongs to the class
425
+ const announcement = await prisma.announcement.findFirst({
426
+ where: {
427
+ id: announcementId,
428
+ classId: classId,
429
+ },
430
+ });
431
+
432
+ if (!announcement) {
433
+ throw new TRPCError({
434
+ code: "NOT_FOUND",
435
+ message: "Announcement not found",
436
+ });
437
+ }
438
+
439
+ // Create direct upload files
440
+ const directUploadFiles = await createDirectUploadFiles(
441
+ files,
442
+ ctx.user.id,
443
+ undefined, // No specific directory
444
+ undefined, // No assignment ID
445
+ undefined, // No submission ID
446
+ announcementId
447
+ );
448
+
449
+ return {
450
+ success: true,
451
+ uploadFiles: directUploadFiles,
452
+ };
453
+ }),
454
+
455
+ confirmAnnouncementUpload: protectedTeacherProcedure
456
+ .input(confirmAnnouncementUploadSchema)
457
+ .mutation(async ({ ctx, input }) => {
458
+ const { fileId, uploadSuccess, errorMessage } = input;
459
+
460
+ if (!ctx.user) {
461
+ throw new TRPCError({
462
+ code: "UNAUTHORIZED",
463
+ message: "You must be logged in",
464
+ });
465
+ }
466
+
467
+ // Verify file belongs to user and is an announcement file
468
+ const file = await prisma.file.findFirst({
469
+ where: {
470
+ id: fileId,
471
+ userId: ctx.user.id,
472
+ announcement: {
473
+ isNot: null,
474
+ },
475
+ },
476
+ });
477
+
478
+ if (!file) {
479
+ throw new TRPCError({
480
+ code: "NOT_FOUND",
481
+ message: "File not found or you don't have permission",
482
+ });
483
+ }
484
+
485
+ await confirmDirectUpload(fileId, uploadSuccess, errorMessage);
486
+
487
+ return {
488
+ success: true,
489
+ message: uploadSuccess ? "Upload confirmed successfully" : "Upload failed",
490
+ };
491
+ }),
492
+
493
+ // Comment endpoints
494
+ addComment: protectedClassMemberProcedure
495
+ .input(z.object({
496
+ announcementId: z.string(),
497
+ classId: z.string(),
498
+ content: z.string().min(1, "Comment cannot be empty"),
499
+ parentCommentId: z.string().optional(),
500
+ }))
501
+ .mutation(async ({ ctx, input }) => {
502
+ if (!ctx.user) {
503
+ throw new TRPCError({
504
+ code: "UNAUTHORIZED",
505
+ message: "User must be authenticated",
506
+ });
507
+ }
508
+
509
+ // Verify announcement exists and belongs to the class
510
+ const announcement = await prisma.announcement.findFirst({
511
+ where: {
512
+ id: input.announcementId,
513
+ classId: input.classId,
514
+ },
515
+ });
516
+
517
+ if (!announcement) {
518
+ throw new TRPCError({
519
+ code: "NOT_FOUND",
520
+ message: "Announcement not found",
521
+ });
522
+ }
523
+
524
+ // If replying to a comment, verify parent comment exists and belongs to the same announcement
525
+ if (input.parentCommentId) {
526
+ const parentComment = await prisma.announcementComment.findFirst({
527
+ where: {
528
+ id: input.parentCommentId,
529
+ announcementId: input.announcementId,
530
+ },
531
+ });
532
+
533
+ if (!parentComment) {
534
+ throw new TRPCError({
535
+ code: "NOT_FOUND",
536
+ message: "Parent comment not found",
537
+ });
538
+ }
539
+ }
540
+
541
+ const comment = await prisma.announcementComment.create({
542
+ data: {
543
+ content: input.content,
544
+ author: {
545
+ connect: { id: ctx.user.id },
546
+ },
547
+ announcement: {
548
+ connect: { id: input.announcementId },
549
+ },
550
+ ...(input.parentCommentId && {
551
+ parentComment: {
552
+ connect: { id: input.parentCommentId },
553
+ },
554
+ }),
555
+ },
556
+ include: {
557
+ author: {
558
+ select: {
559
+ id: true,
560
+ username: true,
561
+ profile: {
562
+ select: {
563
+ displayName: true,
564
+ profilePicture: true,
565
+ profilePictureThumbnail: true,
566
+ },
567
+ },
568
+ },
569
+ },
570
+ },
571
+ });
572
+
573
+ return { comment };
574
+ }),
575
+
576
+ updateComment: protectedProcedure
577
+ .input(z.object({
578
+ id: z.string(),
579
+ content: z.string().min(1, "Comment cannot be empty"),
580
+ }))
581
+ .mutation(async ({ ctx, input }) => {
582
+ if (!ctx.user) {
583
+ throw new TRPCError({
584
+ code: "UNAUTHORIZED",
585
+ message: "User must be authenticated",
586
+ });
587
+ }
588
+
589
+ const comment = await prisma.announcementComment.findUnique({
590
+ where: { id: input.id },
591
+ });
592
+
593
+ if (!comment) {
594
+ throw new TRPCError({
595
+ code: "NOT_FOUND",
596
+ message: "Comment not found",
597
+ });
598
+ }
599
+
600
+ // Only the author can update their comment
601
+ if (comment.authorId !== ctx.user.id) {
602
+ throw new TRPCError({
603
+ code: "FORBIDDEN",
604
+ message: "Only the comment author can update this comment",
605
+ });
606
+ }
607
+
608
+ const updatedComment = await prisma.announcementComment.update({
609
+ where: { id: input.id },
610
+ data: {
611
+ content: input.content,
612
+ },
613
+ include: {
614
+ author: {
615
+ select: {
616
+ id: true,
617
+ username: true,
618
+ profile: {
619
+ select: {
620
+ displayName: true,
621
+ profilePicture: true,
622
+ profilePictureThumbnail: true,
623
+ },
624
+ },
625
+ },
626
+ },
627
+ },
628
+ });
629
+
630
+ return { comment: updatedComment };
631
+ }),
632
+
633
+ deleteComment: protectedProcedure
634
+ .input(z.object({
635
+ id: z.string(),
636
+ }))
637
+ .mutation(async ({ ctx, input }) => {
638
+ if (!ctx.user) {
639
+ throw new TRPCError({
640
+ code: "UNAUTHORIZED",
641
+ message: "User must be authenticated",
642
+ });
643
+ }
644
+
645
+ const comment = await prisma.announcementComment.findUnique({
646
+ where: { id: input.id },
647
+ include: {
648
+ announcement: {
649
+ include: {
650
+ class: {
651
+ include: {
652
+ teachers: true,
653
+ },
654
+ },
655
+ },
656
+ },
657
+ },
658
+ });
659
+
660
+ if (!comment) {
661
+ throw new TRPCError({
662
+ code: "NOT_FOUND",
663
+ message: "Comment not found",
664
+ });
665
+ }
666
+
667
+ // Only the author or a class teacher can delete comments
668
+ const userId = ctx.user.id;
669
+ const isAuthor = comment.authorId === userId;
670
+ const isClassTeacher = comment.announcement.class.teachers.some(
671
+ (teacher) => teacher.id === userId
672
+ );
673
+
674
+ if (!isAuthor && !isClassTeacher) {
675
+ throw new TRPCError({
676
+ code: "FORBIDDEN",
677
+ message: "Only the comment author or class teachers can delete comments",
678
+ });
679
+ }
680
+
681
+ await prisma.announcementComment.delete({
682
+ where: { id: input.id },
683
+ });
684
+
685
+ return { success: true };
686
+ }),
687
+
688
+ getComments: protectedClassMemberProcedure
689
+ .input(z.object({
690
+ announcementId: z.string(),
691
+ classId: z.string(),
692
+ }))
693
+ .query(async ({ ctx, input }) => {
694
+ // Verify announcement exists and belongs to the class
695
+ const announcement = await prisma.announcement.findFirst({
696
+ where: {
697
+ id: input.announcementId,
698
+ classId: input.classId,
699
+ },
700
+ });
701
+
702
+ if (!announcement) {
703
+ throw new TRPCError({
704
+ code: "NOT_FOUND",
705
+ message: "Announcement not found",
706
+ });
707
+ }
708
+
709
+ // Get all top-level comments (no parent)
710
+ const comments = await prisma.announcementComment.findMany({
711
+ where: {
712
+ announcementId: input.announcementId,
713
+ parentCommentId: null,
714
+ },
715
+ include: {
716
+ author: {
717
+ select: {
718
+ id: true,
719
+ username: true,
720
+ profile: {
721
+ select: {
722
+ displayName: true,
723
+ profilePicture: true,
724
+ profilePictureThumbnail: true,
725
+ },
726
+ },
727
+ },
728
+ },
729
+ replies: {
730
+ include: {
731
+ author: {
732
+ select: {
733
+ id: true,
734
+ username: true,
735
+ profile: {
736
+ select: {
737
+ displayName: true,
738
+ profilePicture: true,
739
+ profilePictureThumbnail: true,
740
+ },
741
+ },
742
+ },
743
+ },
744
+ },
745
+ orderBy: {
746
+ createdAt: 'asc',
747
+ },
748
+ },
749
+ },
750
+ orderBy: {
751
+ createdAt: 'asc',
752
+ },
753
+ });
754
+
755
+ return { comments };
756
+ }),
757
+
758
+ // Reaction endpoints
759
+ addReaction: protectedClassMemberProcedure
760
+ .input(z.object({
761
+ announcementId: z.string().optional(),
762
+ commentId: z.string().optional(),
763
+ classId: z.string(),
764
+ type: z.enum(['THUMBSUP', 'CELEBRATE', 'CARE', 'HEART', 'IDEA', 'HAPPY']),
765
+ }))
766
+ .mutation(async ({ ctx, input }) => {
767
+ if (!ctx.user) {
768
+ throw new TRPCError({
769
+ code: "UNAUTHORIZED",
770
+ message: "User must be authenticated",
771
+ });
772
+ }
773
+
774
+ // Exactly one of announcementId or commentId must be provided
775
+ if (!input.announcementId && !input.commentId) {
776
+ throw new TRPCError({
777
+ code: "BAD_REQUEST",
778
+ message: "Either announcementId or commentId must be provided",
779
+ });
780
+ }
781
+
782
+ if (input.announcementId && input.commentId) {
783
+ throw new TRPCError({
784
+ code: "BAD_REQUEST",
785
+ message: "Cannot react to both announcement and comment at the same time",
786
+ });
787
+ }
788
+
789
+ const userId = ctx.user.id;
790
+
791
+ // Verify the announcement or comment exists and belongs to the class
792
+ if (input.announcementId) {
793
+ const announcement = await prisma.announcement.findFirst({
794
+ where: {
795
+ id: input.announcementId,
796
+ classId: input.classId,
797
+ },
798
+ });
799
+
800
+ if (!announcement) {
801
+ throw new TRPCError({
802
+ code: "NOT_FOUND",
803
+ message: "Announcement not found",
804
+ });
805
+ }
806
+
807
+ // Upsert reaction: update if exists, create if not
808
+ const reaction = await prisma.reaction.upsert({
809
+ where: {
810
+ userId_announcementId: {
811
+ userId,
812
+ announcementId: input.announcementId,
813
+ },
814
+ },
815
+ update: {
816
+ type: input.type,
817
+ },
818
+ create: {
819
+ type: input.type,
820
+ userId,
821
+ announcementId: input.announcementId,
822
+ },
823
+ include: {
824
+ user: {
825
+ select: {
826
+ id: true,
827
+ username: true,
828
+ profile: {
829
+ select: {
830
+ displayName: true,
831
+ profilePicture: true,
832
+ profilePictureThumbnail: true,
833
+ },
834
+ },
835
+ },
836
+ },
837
+ },
838
+ });
839
+
840
+ return { reaction };
841
+ } else if (input.commentId) {
842
+ // Verify comment exists and get its announcement to check class
843
+ const comment = await prisma.announcementComment.findUnique({
844
+ where: { id: input.commentId },
845
+ include: {
846
+ announcement: {
847
+ select: {
848
+ classId: true,
849
+ },
850
+ },
851
+ },
852
+ });
853
+
854
+ if (!comment) {
855
+ throw new TRPCError({
856
+ code: "NOT_FOUND",
857
+ message: "Comment not found",
858
+ });
859
+ }
860
+
861
+ if (comment.announcement.classId !== input.classId) {
862
+ throw new TRPCError({
863
+ code: "FORBIDDEN",
864
+ message: "Comment does not belong to this class",
865
+ });
866
+ }
867
+
868
+ // Upsert reaction: update if exists, create if not
869
+ const reaction = await prisma.reaction.upsert({
870
+ where: {
871
+ userId_commentId: {
872
+ userId,
873
+ commentId: input.commentId,
874
+ },
875
+ },
876
+ update: {
877
+ type: input.type,
878
+ },
879
+ create: {
880
+ type: input.type,
881
+ userId,
882
+ commentId: input.commentId,
883
+ },
884
+ include: {
885
+ user: {
886
+ select: {
887
+ id: true,
888
+ username: true,
889
+ profile: {
890
+ select: {
891
+ displayName: true,
892
+ profilePicture: true,
893
+ profilePictureThumbnail: true,
894
+ },
895
+ },
896
+ },
897
+ },
898
+ },
899
+ });
900
+
901
+ return { reaction };
902
+ }
903
+
904
+ throw new TRPCError({
905
+ code: "INTERNAL_SERVER_ERROR",
906
+ message: "Unexpected error",
907
+ });
908
+ }),
909
+
910
+ removeReaction: protectedProcedure
911
+ .input(z.object({
912
+ announcementId: z.string().optional(),
913
+ commentId: z.string().optional(),
914
+ }))
915
+ .mutation(async ({ ctx, input }) => {
916
+ if (!ctx.user) {
917
+ throw new TRPCError({
918
+ code: "UNAUTHORIZED",
919
+ message: "User must be authenticated",
920
+ });
921
+ }
922
+
923
+ // Exactly one of announcementId or commentId must be provided
924
+ if (!input.announcementId && !input.commentId) {
925
+ throw new TRPCError({
926
+ code: "BAD_REQUEST",
927
+ message: "Either announcementId or commentId must be provided",
928
+ });
929
+ }
930
+
931
+ const userId = ctx.user.id;
932
+
933
+ if (input.announcementId) {
934
+ const reaction = await prisma.reaction.findUnique({
935
+ where: {
936
+ userId_announcementId: {
937
+ userId,
938
+ announcementId: input.announcementId,
939
+ },
940
+ },
941
+ });
942
+
943
+ if (!reaction) {
944
+ throw new TRPCError({
945
+ code: "NOT_FOUND",
946
+ message: "Reaction not found",
947
+ });
948
+ }
949
+
950
+ await prisma.reaction.delete({
951
+ where: { id: reaction.id },
952
+ });
953
+
954
+ return { success: true };
955
+ } else if (input.commentId) {
956
+ const reaction = await prisma.reaction.findUnique({
957
+ where: {
958
+ userId_commentId: {
959
+ userId,
960
+ commentId: input.commentId,
961
+ },
962
+ },
963
+ });
964
+
965
+ if (!reaction) {
966
+ throw new TRPCError({
967
+ code: "NOT_FOUND",
968
+ message: "Reaction not found",
969
+ });
970
+ }
971
+
972
+ await prisma.reaction.delete({
973
+ where: { id: reaction.id },
974
+ });
975
+
976
+ return { success: true };
977
+ }
978
+
979
+ throw new TRPCError({
980
+ code: "INTERNAL_SERVER_ERROR",
981
+ message: "Unexpected error",
982
+ });
983
+ }),
984
+
985
+ getReactions: protectedClassMemberProcedure
986
+ .input(z.object({
987
+ announcementId: z.string().optional(),
988
+ commentId: z.string().optional(),
989
+ classId: z.string(),
990
+ }))
991
+ .query(async ({ ctx, input }) => {
992
+ if (!ctx.user) {
993
+ throw new TRPCError({
994
+ code: "UNAUTHORIZED",
995
+ message: "User must be authenticated",
996
+ });
997
+ }
998
+
999
+ // Exactly one of announcementId or commentId must be provided
1000
+ if (!input.announcementId && !input.commentId) {
1001
+ throw new TRPCError({
1002
+ code: "BAD_REQUEST",
1003
+ message: "Either announcementId or commentId must be provided",
1004
+ });
1005
+ }
1006
+
1007
+ const userId = ctx.user.id;
1008
+
1009
+ if (input.announcementId) {
1010
+ // Verify announcement exists
1011
+ const announcement = await prisma.announcement.findFirst({
1012
+ where: {
1013
+ id: input.announcementId,
1014
+ classId: input.classId,
1015
+ },
1016
+ });
1017
+
1018
+ if (!announcement) {
1019
+ throw new TRPCError({
1020
+ code: "NOT_FOUND",
1021
+ message: "Announcement not found",
1022
+ });
1023
+ }
1024
+
1025
+ // Get reaction counts by type
1026
+ const reactionCounts = await prisma.reaction.groupBy({
1027
+ by: ['type'],
1028
+ where: { announcementId: input.announcementId },
1029
+ _count: { type: true },
1030
+ });
1031
+
1032
+ // Get current user's reaction
1033
+ const userReaction = await prisma.reaction.findUnique({
1034
+ where: {
1035
+ userId_announcementId: {
1036
+ userId,
1037
+ announcementId: input.announcementId,
1038
+ },
1039
+ },
1040
+ });
1041
+
1042
+ // Format counts
1043
+ const counts = {
1044
+ THUMBSUP: 0,
1045
+ CELEBRATE: 0,
1046
+ CARE: 0,
1047
+ HEART: 0,
1048
+ IDEA: 0,
1049
+ HAPPY: 0,
1050
+ };
1051
+
1052
+ reactionCounts.forEach((item) => {
1053
+ counts[item.type as keyof typeof counts] = item._count.type;
1054
+ });
1055
+
1056
+ return {
1057
+ counts,
1058
+ userReaction: userReaction?.type || null,
1059
+ total: reactionCounts.reduce((sum, item) => sum + item._count.type, 0),
1060
+ };
1061
+ } else if (input.commentId) {
1062
+ // Verify comment exists
1063
+ const comment = await prisma.announcementComment.findUnique({
1064
+ where: { id: input.commentId },
1065
+ include: {
1066
+ announcement: {
1067
+ select: {
1068
+ classId: true,
1069
+ },
1070
+ },
1071
+ },
1072
+ });
1073
+
1074
+ if (!comment) {
1075
+ throw new TRPCError({
1076
+ code: "NOT_FOUND",
1077
+ message: "Comment not found",
1078
+ });
1079
+ }
1080
+
1081
+ if (comment.announcement.classId !== input.classId) {
1082
+ throw new TRPCError({
1083
+ code: "FORBIDDEN",
1084
+ message: "Comment does not belong to this class",
1085
+ });
1086
+ }
1087
+
1088
+ // Get reaction counts by type
1089
+ const reactionCounts = await prisma.reaction.groupBy({
1090
+ by: ['type'],
1091
+ where: { commentId: input.commentId },
1092
+ _count: { type: true },
1093
+ });
1094
+
1095
+ // Get current user's reaction
1096
+ const userReaction = await prisma.reaction.findUnique({
1097
+ where: {
1098
+ userId_commentId: {
1099
+ userId,
1100
+ commentId: input.commentId,
1101
+ },
1102
+ },
1103
+ });
1104
+
1105
+ // Format counts
1106
+ const counts = {
1107
+ THUMBSUP: 0,
1108
+ CELEBRATE: 0,
1109
+ CARE: 0,
1110
+ HEART: 0,
1111
+ IDEA: 0,
1112
+ HAPPY: 0,
1113
+ };
1114
+
1115
+ reactionCounts.forEach((item) => {
1116
+ counts[item.type as keyof typeof counts] = item._count.type;
1117
+ });
1118
+
1119
+ return {
1120
+ counts,
1121
+ userReaction: userReaction?.type || null,
1122
+ total: reactionCounts.reduce((sum, item) => sum + item._count.type, 0),
1123
+ };
1124
+ }
1125
+
1126
+ throw new TRPCError({
1127
+ code: "INTERNAL_SERVER_ERROR",
1128
+ message: "Unexpected error",
1129
+ });
1130
+ }),
134
1131
  });