@studious-lms/server 1.1.11 → 1.1.12
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.
- package/LAB_CHAT_API_SPEC.md +518 -0
- package/dist/routers/_app.d.ts +410 -0
- package/dist/routers/_app.d.ts.map +1 -1
- package/dist/routers/_app.js +2 -0
- package/dist/routers/labChat.d.ts +207 -0
- package/dist/routers/labChat.d.ts.map +1 -0
- package/dist/routers/labChat.js +500 -0
- package/package.json +1 -1
- package/prisma/migrations/20250925072732_add_lab_chat_system/migration.sql +25 -0
- package/prisma/schema.prisma +18 -0
- package/src/routers/_app.ts +2 -0
- package/src/routers/labChat.ts +541 -0
|
@@ -0,0 +1,541 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { createTRPCRouter, protectedProcedure } from '../trpc.js';
|
|
3
|
+
import { prisma } from '../lib/prisma.js';
|
|
4
|
+
import { pusher } from '../lib/pusher.js';
|
|
5
|
+
import { TRPCError } from '@trpc/server';
|
|
6
|
+
|
|
7
|
+
export const labChatRouter = createTRPCRouter({
|
|
8
|
+
create: protectedProcedure
|
|
9
|
+
.input(
|
|
10
|
+
z.object({
|
|
11
|
+
classId: z.string(),
|
|
12
|
+
title: z.string().min(1).max(200),
|
|
13
|
+
context: z.string(), // JSON string for LLM context
|
|
14
|
+
})
|
|
15
|
+
)
|
|
16
|
+
.mutation(async ({ input, ctx }) => {
|
|
17
|
+
const userId = ctx.user!.id;
|
|
18
|
+
const { classId, title, context } = input;
|
|
19
|
+
|
|
20
|
+
// Verify user is a teacher in the class
|
|
21
|
+
const classWithTeachers = await prisma.class.findFirst({
|
|
22
|
+
where: {
|
|
23
|
+
id: classId,
|
|
24
|
+
teachers: {
|
|
25
|
+
some: {
|
|
26
|
+
id: userId,
|
|
27
|
+
},
|
|
28
|
+
},
|
|
29
|
+
},
|
|
30
|
+
include: {
|
|
31
|
+
students: {
|
|
32
|
+
select: {
|
|
33
|
+
id: true,
|
|
34
|
+
username: true,
|
|
35
|
+
profile: {
|
|
36
|
+
select: {
|
|
37
|
+
displayName: true,
|
|
38
|
+
profilePicture: true,
|
|
39
|
+
},
|
|
40
|
+
},
|
|
41
|
+
},
|
|
42
|
+
},
|
|
43
|
+
teachers: {
|
|
44
|
+
select: {
|
|
45
|
+
id: true,
|
|
46
|
+
username: true,
|
|
47
|
+
profile: {
|
|
48
|
+
select: {
|
|
49
|
+
displayName: true,
|
|
50
|
+
profilePicture: true,
|
|
51
|
+
},
|
|
52
|
+
},
|
|
53
|
+
},
|
|
54
|
+
},
|
|
55
|
+
},
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
if (!classWithTeachers) {
|
|
59
|
+
throw new TRPCError({
|
|
60
|
+
code: 'FORBIDDEN',
|
|
61
|
+
message: 'Not a teacher in this class',
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// Validate context is valid JSON
|
|
66
|
+
try {
|
|
67
|
+
JSON.parse(context);
|
|
68
|
+
} catch (error) {
|
|
69
|
+
throw new TRPCError({
|
|
70
|
+
code: 'BAD_REQUEST',
|
|
71
|
+
message: 'Context must be valid JSON',
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// Create lab chat with associated conversation
|
|
76
|
+
const result = await prisma.$transaction(async (tx) => {
|
|
77
|
+
// Create conversation for the lab chat
|
|
78
|
+
const conversation = await tx.conversation.create({
|
|
79
|
+
data: {
|
|
80
|
+
type: 'GROUP',
|
|
81
|
+
name: `Lab: ${title}`,
|
|
82
|
+
displayInChat: false, // Lab chats don't show in regular chat list
|
|
83
|
+
},
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
// Add all class members to the conversation
|
|
87
|
+
const allMembers = [
|
|
88
|
+
...classWithTeachers.teachers.map(t => ({ userId: t.id, role: 'ADMIN' as const })),
|
|
89
|
+
...classWithTeachers.students.map(s => ({ userId: s.id, role: 'MEMBER' as const })),
|
|
90
|
+
];
|
|
91
|
+
|
|
92
|
+
await tx.conversationMember.createMany({
|
|
93
|
+
data: allMembers.map(member => ({
|
|
94
|
+
userId: member.userId,
|
|
95
|
+
conversationId: conversation.id,
|
|
96
|
+
role: member.role,
|
|
97
|
+
})),
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
// Create the lab chat
|
|
101
|
+
const labChat = await tx.labChat.create({
|
|
102
|
+
data: {
|
|
103
|
+
title,
|
|
104
|
+
context,
|
|
105
|
+
classId,
|
|
106
|
+
conversationId: conversation.id,
|
|
107
|
+
createdById: userId,
|
|
108
|
+
},
|
|
109
|
+
include: {
|
|
110
|
+
conversation: {
|
|
111
|
+
include: {
|
|
112
|
+
members: {
|
|
113
|
+
include: {
|
|
114
|
+
user: {
|
|
115
|
+
select: {
|
|
116
|
+
id: true,
|
|
117
|
+
username: true,
|
|
118
|
+
profile: {
|
|
119
|
+
select: {
|
|
120
|
+
displayName: true,
|
|
121
|
+
profilePicture: true,
|
|
122
|
+
},
|
|
123
|
+
},
|
|
124
|
+
},
|
|
125
|
+
},
|
|
126
|
+
},
|
|
127
|
+
},
|
|
128
|
+
},
|
|
129
|
+
},
|
|
130
|
+
createdBy: {
|
|
131
|
+
select: {
|
|
132
|
+
id: true,
|
|
133
|
+
username: true,
|
|
134
|
+
profile: {
|
|
135
|
+
select: {
|
|
136
|
+
displayName: true,
|
|
137
|
+
},
|
|
138
|
+
},
|
|
139
|
+
},
|
|
140
|
+
},
|
|
141
|
+
class: {
|
|
142
|
+
select: {
|
|
143
|
+
id: true,
|
|
144
|
+
name: true,
|
|
145
|
+
subject: true,
|
|
146
|
+
section: true,
|
|
147
|
+
},
|
|
148
|
+
},
|
|
149
|
+
},
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
return labChat;
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
// Broadcast lab chat creation to class members
|
|
156
|
+
try {
|
|
157
|
+
await pusher.trigger(`class-${classId}`, 'lab-chat-created', {
|
|
158
|
+
id: result.id,
|
|
159
|
+
title: result.title,
|
|
160
|
+
classId: result.classId,
|
|
161
|
+
conversationId: result.conversationId,
|
|
162
|
+
createdBy: result.createdBy,
|
|
163
|
+
createdAt: result.createdAt,
|
|
164
|
+
});
|
|
165
|
+
} catch (error) {
|
|
166
|
+
console.error('Failed to broadcast lab chat creation:', error);
|
|
167
|
+
// Don't fail the request if Pusher fails
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
return result;
|
|
171
|
+
}),
|
|
172
|
+
|
|
173
|
+
get: protectedProcedure
|
|
174
|
+
.input(z.object({ labChatId: z.string() }))
|
|
175
|
+
.query(async ({ input, ctx }) => {
|
|
176
|
+
const userId = ctx.user!.id;
|
|
177
|
+
const { labChatId } = input;
|
|
178
|
+
|
|
179
|
+
const labChat = await prisma.labChat.findFirst({
|
|
180
|
+
where: {
|
|
181
|
+
id: labChatId,
|
|
182
|
+
conversation: {
|
|
183
|
+
members: {
|
|
184
|
+
some: {
|
|
185
|
+
userId,
|
|
186
|
+
},
|
|
187
|
+
},
|
|
188
|
+
},
|
|
189
|
+
},
|
|
190
|
+
include: {
|
|
191
|
+
conversation: {
|
|
192
|
+
include: {
|
|
193
|
+
members: {
|
|
194
|
+
include: {
|
|
195
|
+
user: {
|
|
196
|
+
select: {
|
|
197
|
+
id: true,
|
|
198
|
+
username: true,
|
|
199
|
+
profile: {
|
|
200
|
+
select: {
|
|
201
|
+
displayName: true,
|
|
202
|
+
profilePicture: true,
|
|
203
|
+
},
|
|
204
|
+
},
|
|
205
|
+
},
|
|
206
|
+
},
|
|
207
|
+
},
|
|
208
|
+
},
|
|
209
|
+
},
|
|
210
|
+
},
|
|
211
|
+
createdBy: {
|
|
212
|
+
select: {
|
|
213
|
+
id: true,
|
|
214
|
+
username: true,
|
|
215
|
+
profile: {
|
|
216
|
+
select: {
|
|
217
|
+
displayName: true,
|
|
218
|
+
},
|
|
219
|
+
},
|
|
220
|
+
},
|
|
221
|
+
},
|
|
222
|
+
class: {
|
|
223
|
+
select: {
|
|
224
|
+
id: true,
|
|
225
|
+
name: true,
|
|
226
|
+
subject: true,
|
|
227
|
+
section: true,
|
|
228
|
+
},
|
|
229
|
+
},
|
|
230
|
+
},
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
if (!labChat) {
|
|
234
|
+
throw new TRPCError({
|
|
235
|
+
code: 'NOT_FOUND',
|
|
236
|
+
message: 'Lab chat not found or access denied',
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
return labChat;
|
|
241
|
+
}),
|
|
242
|
+
|
|
243
|
+
list: protectedProcedure
|
|
244
|
+
.input(z.object({ classId: z.string() }))
|
|
245
|
+
.query(async ({ input, ctx }) => {
|
|
246
|
+
const userId = ctx.user!.id;
|
|
247
|
+
const { classId } = input;
|
|
248
|
+
|
|
249
|
+
// Verify user is a member of the class
|
|
250
|
+
const classMembership = await prisma.class.findFirst({
|
|
251
|
+
where: {
|
|
252
|
+
id: classId,
|
|
253
|
+
OR: [
|
|
254
|
+
{
|
|
255
|
+
students: {
|
|
256
|
+
some: {
|
|
257
|
+
id: userId,
|
|
258
|
+
},
|
|
259
|
+
},
|
|
260
|
+
},
|
|
261
|
+
{
|
|
262
|
+
teachers: {
|
|
263
|
+
some: {
|
|
264
|
+
id: userId,
|
|
265
|
+
},
|
|
266
|
+
},
|
|
267
|
+
},
|
|
268
|
+
],
|
|
269
|
+
},
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
if (!classMembership) {
|
|
273
|
+
throw new TRPCError({
|
|
274
|
+
code: 'FORBIDDEN',
|
|
275
|
+
message: 'Not a member of this class',
|
|
276
|
+
});
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
const labChats = await prisma.labChat.findMany({
|
|
280
|
+
where: {
|
|
281
|
+
classId,
|
|
282
|
+
},
|
|
283
|
+
include: {
|
|
284
|
+
createdBy: {
|
|
285
|
+
select: {
|
|
286
|
+
id: true,
|
|
287
|
+
username: true,
|
|
288
|
+
profile: {
|
|
289
|
+
select: {
|
|
290
|
+
displayName: true,
|
|
291
|
+
},
|
|
292
|
+
},
|
|
293
|
+
},
|
|
294
|
+
},
|
|
295
|
+
conversation: {
|
|
296
|
+
include: {
|
|
297
|
+
messages: {
|
|
298
|
+
orderBy: {
|
|
299
|
+
createdAt: 'desc',
|
|
300
|
+
},
|
|
301
|
+
take: 1,
|
|
302
|
+
include: {
|
|
303
|
+
sender: {
|
|
304
|
+
select: {
|
|
305
|
+
id: true,
|
|
306
|
+
username: true,
|
|
307
|
+
profile: {
|
|
308
|
+
select: {
|
|
309
|
+
displayName: true,
|
|
310
|
+
},
|
|
311
|
+
},
|
|
312
|
+
},
|
|
313
|
+
},
|
|
314
|
+
},
|
|
315
|
+
},
|
|
316
|
+
_count: {
|
|
317
|
+
select: {
|
|
318
|
+
messages: true,
|
|
319
|
+
},
|
|
320
|
+
},
|
|
321
|
+
},
|
|
322
|
+
},
|
|
323
|
+
},
|
|
324
|
+
orderBy: {
|
|
325
|
+
createdAt: 'desc',
|
|
326
|
+
},
|
|
327
|
+
});
|
|
328
|
+
|
|
329
|
+
return labChats.map((labChat) => ({
|
|
330
|
+
id: labChat.id,
|
|
331
|
+
title: labChat.title,
|
|
332
|
+
classId: labChat.classId,
|
|
333
|
+
conversationId: labChat.conversationId,
|
|
334
|
+
createdBy: labChat.createdBy,
|
|
335
|
+
createdAt: labChat.createdAt,
|
|
336
|
+
updatedAt: labChat.updatedAt,
|
|
337
|
+
lastMessage: labChat.conversation.messages[0] || null,
|
|
338
|
+
messageCount: labChat.conversation._count.messages,
|
|
339
|
+
}));
|
|
340
|
+
}),
|
|
341
|
+
|
|
342
|
+
postToLabChat: protectedProcedure
|
|
343
|
+
.input(
|
|
344
|
+
z.object({
|
|
345
|
+
labChatId: z.string(),
|
|
346
|
+
content: z.string().min(1).max(4000),
|
|
347
|
+
mentionedUserIds: z.array(z.string()).optional(),
|
|
348
|
+
})
|
|
349
|
+
)
|
|
350
|
+
.mutation(async ({ input, ctx }) => {
|
|
351
|
+
const userId = ctx.user!.id;
|
|
352
|
+
const { labChatId, content, mentionedUserIds = [] } = input;
|
|
353
|
+
|
|
354
|
+
// Get lab chat and verify user is a member
|
|
355
|
+
const labChat = await prisma.labChat.findFirst({
|
|
356
|
+
where: {
|
|
357
|
+
id: labChatId,
|
|
358
|
+
conversation: {
|
|
359
|
+
members: {
|
|
360
|
+
some: {
|
|
361
|
+
userId,
|
|
362
|
+
},
|
|
363
|
+
},
|
|
364
|
+
},
|
|
365
|
+
},
|
|
366
|
+
include: {
|
|
367
|
+
conversation: {
|
|
368
|
+
select: {
|
|
369
|
+
id: true,
|
|
370
|
+
},
|
|
371
|
+
},
|
|
372
|
+
},
|
|
373
|
+
});
|
|
374
|
+
|
|
375
|
+
if (!labChat) {
|
|
376
|
+
throw new TRPCError({
|
|
377
|
+
code: 'FORBIDDEN',
|
|
378
|
+
message: 'Lab chat not found or access denied',
|
|
379
|
+
});
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
// Verify mentioned users are members of the conversation
|
|
383
|
+
if (mentionedUserIds.length > 0) {
|
|
384
|
+
const mentionedMemberships = await prisma.conversationMember.findMany({
|
|
385
|
+
where: {
|
|
386
|
+
conversationId: labChat.conversationId,
|
|
387
|
+
userId: { in: mentionedUserIds },
|
|
388
|
+
},
|
|
389
|
+
});
|
|
390
|
+
|
|
391
|
+
if (mentionedMemberships.length !== mentionedUserIds.length) {
|
|
392
|
+
throw new TRPCError({
|
|
393
|
+
code: 'BAD_REQUEST',
|
|
394
|
+
message: 'Some mentioned users are not members of this lab chat',
|
|
395
|
+
});
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
// Create message and mentions
|
|
400
|
+
const result = await prisma.$transaction(async (tx) => {
|
|
401
|
+
const message = await tx.message.create({
|
|
402
|
+
data: {
|
|
403
|
+
content,
|
|
404
|
+
senderId: userId,
|
|
405
|
+
conversationId: labChat.conversationId,
|
|
406
|
+
},
|
|
407
|
+
include: {
|
|
408
|
+
sender: {
|
|
409
|
+
select: {
|
|
410
|
+
id: true,
|
|
411
|
+
username: true,
|
|
412
|
+
profile: {
|
|
413
|
+
select: {
|
|
414
|
+
displayName: true,
|
|
415
|
+
profilePicture: true,
|
|
416
|
+
},
|
|
417
|
+
},
|
|
418
|
+
},
|
|
419
|
+
},
|
|
420
|
+
},
|
|
421
|
+
});
|
|
422
|
+
|
|
423
|
+
// Create mentions
|
|
424
|
+
if (mentionedUserIds.length > 0) {
|
|
425
|
+
await tx.mention.createMany({
|
|
426
|
+
data: mentionedUserIds.map((mentionedUserId) => ({
|
|
427
|
+
messageId: message.id,
|
|
428
|
+
userId: mentionedUserId,
|
|
429
|
+
})),
|
|
430
|
+
});
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
// Update lab chat timestamp
|
|
434
|
+
await tx.labChat.update({
|
|
435
|
+
where: { id: labChatId },
|
|
436
|
+
data: { updatedAt: new Date() },
|
|
437
|
+
});
|
|
438
|
+
|
|
439
|
+
return message;
|
|
440
|
+
});
|
|
441
|
+
|
|
442
|
+
// Broadcast to Pusher channel (using conversation ID)
|
|
443
|
+
try {
|
|
444
|
+
await pusher.trigger(`conversation-${labChat.conversationId}`, 'new-message', {
|
|
445
|
+
id: result.id,
|
|
446
|
+
content: result.content,
|
|
447
|
+
senderId: result.senderId,
|
|
448
|
+
conversationId: result.conversationId,
|
|
449
|
+
createdAt: result.createdAt,
|
|
450
|
+
sender: result.sender,
|
|
451
|
+
mentionedUserIds,
|
|
452
|
+
labChatId, // Include lab chat ID for frontend context
|
|
453
|
+
});
|
|
454
|
+
} catch (error) {
|
|
455
|
+
console.error('Failed to broadcast lab chat message:', error);
|
|
456
|
+
// Don't fail the request if Pusher fails
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
return {
|
|
460
|
+
id: result.id,
|
|
461
|
+
content: result.content,
|
|
462
|
+
senderId: result.senderId,
|
|
463
|
+
conversationId: result.conversationId,
|
|
464
|
+
createdAt: result.createdAt,
|
|
465
|
+
sender: result.sender,
|
|
466
|
+
mentionedUserIds,
|
|
467
|
+
labChatId,
|
|
468
|
+
};
|
|
469
|
+
}),
|
|
470
|
+
|
|
471
|
+
delete: protectedProcedure
|
|
472
|
+
.input(z.object({ labChatId: z.string() }))
|
|
473
|
+
.mutation(async ({ input, ctx }) => {
|
|
474
|
+
const userId = ctx.user!.id;
|
|
475
|
+
const { labChatId } = input;
|
|
476
|
+
|
|
477
|
+
// Verify user is the creator of the lab chat
|
|
478
|
+
const labChat = await prisma.labChat.findFirst({
|
|
479
|
+
where: {
|
|
480
|
+
id: labChatId,
|
|
481
|
+
createdById: userId,
|
|
482
|
+
},
|
|
483
|
+
});
|
|
484
|
+
|
|
485
|
+
if (!labChat) {
|
|
486
|
+
throw new TRPCError({
|
|
487
|
+
code: 'FORBIDDEN',
|
|
488
|
+
message: 'Lab chat not found or not the creator',
|
|
489
|
+
});
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
// Delete lab chat and associated conversation
|
|
493
|
+
await prisma.$transaction(async (tx) => {
|
|
494
|
+
// Delete mentions first
|
|
495
|
+
await tx.mention.deleteMany({
|
|
496
|
+
where: {
|
|
497
|
+
message: {
|
|
498
|
+
conversationId: labChat.conversationId,
|
|
499
|
+
},
|
|
500
|
+
},
|
|
501
|
+
});
|
|
502
|
+
|
|
503
|
+
// Delete messages
|
|
504
|
+
await tx.message.deleteMany({
|
|
505
|
+
where: {
|
|
506
|
+
conversationId: labChat.conversationId,
|
|
507
|
+
},
|
|
508
|
+
});
|
|
509
|
+
|
|
510
|
+
// Delete conversation members
|
|
511
|
+
await tx.conversationMember.deleteMany({
|
|
512
|
+
where: {
|
|
513
|
+
conversationId: labChat.conversationId,
|
|
514
|
+
},
|
|
515
|
+
});
|
|
516
|
+
|
|
517
|
+
// Delete lab chat
|
|
518
|
+
await tx.labChat.delete({
|
|
519
|
+
where: { id: labChatId },
|
|
520
|
+
});
|
|
521
|
+
|
|
522
|
+
// Delete conversation
|
|
523
|
+
await tx.conversation.delete({
|
|
524
|
+
where: { id: labChat.conversationId },
|
|
525
|
+
});
|
|
526
|
+
});
|
|
527
|
+
|
|
528
|
+
// Broadcast lab chat deletion
|
|
529
|
+
try {
|
|
530
|
+
await pusher.trigger(`class-${labChat.classId}`, 'lab-chat-deleted', {
|
|
531
|
+
labChatId,
|
|
532
|
+
classId: labChat.classId,
|
|
533
|
+
});
|
|
534
|
+
} catch (error) {
|
|
535
|
+
console.error('Failed to broadcast lab chat deletion:', error);
|
|
536
|
+
// Don't fail the request if Pusher fails
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
return { success: true };
|
|
540
|
+
}),
|
|
541
|
+
});
|