@studious-lms/server 1.2.32 → 1.2.34

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.
@@ -154,6 +154,22 @@ export const classRouter = createTRPCRouter({
154
154
  },
155
155
  },
156
156
  assignments: {
157
+ ...(!isTeacher && {
158
+ where: {OR: [
159
+ {
160
+ assignedTo: {
161
+ some: {
162
+ id: ctx.user?.id,
163
+ },
164
+ },
165
+ },
166
+ {
167
+ assignedTo: {
168
+ none: {},
169
+ },
170
+ },
171
+ ],}
172
+ }),
157
173
  select: {
158
174
  type: true,
159
175
  id: true,
@@ -217,8 +233,8 @@ export const classRouter = createTRPCRouter({
217
233
  assignments: classData.assignments.map(assignment => ({
218
234
  ...assignment,
219
235
  late: assignment.dueDate < new Date(),
220
- submitted: assignment.submissions.some(submission => submission.studentId === ctx.user?.id),
221
- returned: assignment.submissions.some(submission => submission.studentId === ctx.user?.id && submission.returned),
236
+ submitted: assignment.submissions.find(submission => submission.studentId === ctx.user?.id)?.submitted,
237
+ returned: assignment.submissions.find(submission => submission.studentId === ctx.user?.id)?.returned,
222
238
  })),
223
239
  }
224
240
 
@@ -1,7 +1,7 @@
1
1
  import { TRPCError } from "@trpc/server";
2
- import { createTRPCRouter, protectedProcedure } from "src/trpc";
2
+ import { createTRPCRouter, protectedProcedure } from "../trpc.js";
3
3
  import { z } from "zod";
4
- import { prisma } from "src/lib/prisma";
4
+ import { prisma } from "../lib/prisma.js";
5
5
  import { WorksheetQuestionType } from "@prisma/client";
6
6
 
7
7
  export const worksheetRouter = createTRPCRouter({
@@ -112,12 +112,13 @@ export const worksheetRouter = createTRPCRouter({
112
112
  worksheetId: z.string(),
113
113
  question: z.string(),
114
114
  answer: z.string(),
115
+ points: z.number().optional(),
115
116
  options: z.any().optional(), // JSON field
116
117
  markScheme: z.any().optional(), // JSON field
117
118
  type: z.enum(['MULTIPLE_CHOICE', 'TRUE_FALSE', 'SHORT_ANSWER', 'LONG_ANSWER', 'MATH_EXPRESSION', 'ESSAY']),
118
119
  }))
119
120
  .mutation(async ({ ctx, input }) => {
120
- const { worksheetId, question, answer, options, markScheme, type } = input;
121
+ const { worksheetId, question, points, answer, options, markScheme, type } = input;
121
122
 
122
123
  const worksheet = await prisma.worksheet.findUnique({
123
124
  where: { id: worksheetId },
@@ -131,6 +132,7 @@ export const worksheetRouter = createTRPCRouter({
131
132
  data: {
132
133
  worksheetId,
133
134
  type,
135
+ points,
134
136
  question,
135
137
  answer,
136
138
  options,
@@ -201,12 +203,13 @@ export const worksheetRouter = createTRPCRouter({
201
203
  questionId: z.string(),
202
204
  question: z.string().optional(),
203
205
  answer: z.string().optional(),
206
+ points: z.number().optional(),
204
207
  options: z.any().optional(), // JSON field
205
208
  markScheme: z.any().optional(), // JSON field
206
209
  type: z.enum(['MULTIPLE_CHOICE', 'TRUE_FALSE', 'SHORT_ANSWER', 'LONG_ANSWER', 'MATH_EXPRESSION', 'ESSAY']).optional(),
207
210
  }))
208
211
  .mutation(async ({ ctx, input }) => {
209
- const { worksheetId, questionId, question, answer, options, markScheme, type } = input;
212
+ const { worksheetId, questionId, points, question, answer, options, markScheme, type } = input;
210
213
 
211
214
  const worksheet = await prisma.worksheet.findUnique({
212
215
  where: { id: worksheetId },
@@ -223,6 +226,8 @@ export const worksheetRouter = createTRPCRouter({
223
226
  ...(answer !== undefined && { answer }),
224
227
  ...(markScheme !== undefined && { markScheme }),
225
228
  ...(type !== undefined && { type }),
229
+ ...(options !== undefined && { options }),
230
+ ...(points !== undefined && { points }),
226
231
  },
227
232
  });
228
233
 
@@ -267,14 +272,41 @@ export const worksheetRouter = createTRPCRouter({
267
272
  throw new TRPCError({ code: 'NOT_FOUND', message: 'Submission not found' });
268
273
  }
269
274
 
270
- const worksheetResponses = await prisma.studentWorksheetResponse.findMany({
271
- where: { submissionId },
272
- include: {
273
- responses: true,
274
- },
275
+ // Find or create worksheet response for this submission
276
+ const worksheetResponse = await prisma.$transaction(async (tx) => {
277
+ // First check if a response exists
278
+ const existing = await tx.studentWorksheetResponse.findFirst({
279
+ where: {
280
+ submissionId,
281
+ worksheetId
282
+ },
283
+ include: {
284
+ responses: true,
285
+ },
286
+ });
287
+
288
+ if (existing) {
289
+ return existing;
290
+ }
291
+
292
+ // Create new response if it doesn't exist
293
+ const created = await tx.studentWorksheetResponse.create({
294
+ data: {
295
+ worksheetId,
296
+ submissionId,
297
+ studentId: submission.studentId,
298
+ },
299
+ include: {
300
+ responses: true,
301
+ },
302
+ });
303
+
304
+ return created;
275
305
  });
276
306
 
277
- return worksheetResponses;
307
+
308
+ console.log(worksheetResponse);
309
+ return worksheetResponse;
278
310
  }),
279
311
  answerQuestion: protectedProcedure
280
312
  .input(z.object({
@@ -421,14 +453,14 @@ export const worksheetRouter = createTRPCRouter({
421
453
  }),
422
454
 
423
455
  // Get all student responses for a worksheet (teacher view)
424
- getWorksheetResponses: protectedProcedure
456
+ getWorksheetResponse: protectedProcedure
425
457
  .input(z.object({
426
458
  worksheetId: z.string(),
427
459
  }))
428
460
  .query(async ({ ctx, input }) => {
429
461
  const { worksheetId } = input;
430
462
 
431
- const responses = await prisma.studentWorksheetResponse.findMany({
463
+ const responses = await prisma.studentWorksheetResponse.findFirst({
432
464
  where: { worksheetId },
433
465
  include: {
434
466
  student: {