-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathplan.ts
638 lines (606 loc) · 17.1 KB
/
plan.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
import { Prisma, Semester } from '@prisma/client';
import { TRPCError } from '@trpc/server';
import { v4 as uuidv4 } from 'uuid';
import { z } from 'zod';
import { Semester as PlanSemester } from '@/components/planner/types';
import {
createYearBasedOnFall,
createSemesterCodeRange,
isEarlierSemester,
} from '@/utils/utilFunctions';
import { SemesterCode, computeSemesterCode } from 'prisma/utils';
import { protectedProcedure, router } from '../trpc';
export const planRouter = router({
// Protected route: route uses session user id to find user plans
getUserPlans: protectedProcedure.query(async ({ ctx }) => {
const plans = await ctx.prisma.user.findUnique({
where: {
id: ctx.session.user.id,
},
select: {
plans: {
select: {
name: true,
requirements: true,
id: true,
},
},
},
});
return plans;
}),
// Protected route: checks if session user and plan owner have the same id
getPlanById: protectedProcedure.input(z.string().min(1)).query(async ({ ctx, input }) => {
// Fetch current plan
const planData = await ctx.prisma.plan.findUnique({
where: {
id: input,
},
select: {
name: true,
id: true,
userId: true,
semesters: {
include: {
courses: true,
},
},
transferCredits: true,
},
});
// Make sure semesters are in right orer
if (planData && planData.semesters) {
planData.semesters = planData.semesters.sort((a, b) =>
isEarlierSemester(computeSemesterCode(a), computeSemesterCode(b)) ? -1 : 1,
);
}
if (!planData) {
throw new TRPCError({
code: 'NOT_FOUND',
message: 'Plan not found',
});
}
if (ctx.session.user.id !== planData.userId) {
throw new TRPCError({ code: 'FORBIDDEN' });
}
return { plan: { ...planData, semesters: planData.semesters.map(computeSemesterCode) } };
}),
// Protected route: route uses session user id
deletePlanById: protectedProcedure.input(z.string().min(1)).mutation(async ({ ctx, input }) => {
// check if plans belongs to user with id = ctx.session.user.id
const planData = await ctx.prisma.user.findUnique({
where: {
id: ctx.session.user.id,
},
select: {
plans: {
where: {
id: input,
},
select: {
id: true,
},
},
},
});
const plan = planData?.plans[0];
await ctx.prisma.plan.delete({
where: {
id: plan?.id,
},
});
return true;
}),
// Protected route: checks if session user and plan owner have the same id
modifySemesters: protectedProcedure
.input(
z.object({
planId: z.string(),
newStartSemester: z.object({
semester: z.string(),
year: z.number(),
}),
newEndSemester: z.object({
semester: z.string(),
year: z.number(),
}),
}),
)
.mutation(async ({ ctx, input: { planId, newStartSemester, newEndSemester } }) => {
const plan = await ctx.prisma.plan.findUnique({
where: { id: planId },
select: { semesters: true, userId: true },
});
if (!plan) {
throw new TRPCError({
code: 'NOT_FOUND',
message: 'Plan not found',
});
}
if (ctx.session.user.id !== plan.userId) {
throw new TRPCError({ code: 'FORBIDDEN' });
}
// Since we're deleting things anyway, we can just create new xD
const newSems = createSemesterCodeRange(
newStartSemester as SemesterCode,
newEndSemester as SemesterCode,
true,
true,
).map(({ year, semester }) => ({
color: '',
planId,
id: uuidv4(),
year,
semester,
})) as Semester[];
await ctx.prisma.semester.deleteMany({ where: { planId } });
await ctx.prisma.semester.createMany({ data: newSems });
}),
// Protected route: route uses session user id
deleteYear: protectedProcedure.input(z.string().min(1)).mutation(async ({ ctx, input }) => {
try {
const semesterIds = await ctx.prisma.user.findUnique({
where: {
id: ctx.session.user.id,
},
select: {
plans: {
where: {
id: input,
},
select: {
semesters: {
select: {
id: true,
},
take: -3,
},
},
},
},
});
await ctx.prisma.semester.deleteMany({
where: {
id: { in: semesterIds?.plans[0].semesters.map((val) => val.id) },
},
});
return true;
} catch (error) {
return false;
}
}),
// Protected route: checks if session user and plan owner have the same id
addYear: protectedProcedure
.input(z.object({ planId: z.string(), semesterIds: z.array(z.string()).length(3) }))
.mutation(async ({ ctx, input }) => {
const { planId, semesterIds } = input;
try {
const plan = await ctx.prisma.plan.findUnique({
where: {
id: planId,
},
select: {
userId: true,
semesters: {
take: -1,
},
},
});
if (!plan) {
throw new TRPCError({
code: 'NOT_FOUND',
message: 'Plan not found',
});
}
if (ctx.session.user.id !== plan.userId) {
throw new TRPCError({ code: 'FORBIDDEN' });
}
const newYear: PlanSemester[] = createYearBasedOnFall(
computeSemesterCode(plan.semesters[0] ?? { semester: 'u', year: 2022 }).year,
);
await ctx.prisma.plan.update({
where: {
id: planId,
},
data: {
semesters: {
createMany: {
data: newYear.map((semester, idx) => {
// !TODO change this wtf
return {
courses: undefined,
semester: semester.code.semester,
year: semester.code.year,
id: semesterIds[idx].toString(),
};
}),
},
},
},
});
return true;
} catch (error) {
console.error(error);
return false;
}
}),
// Protected route: route uses session user id
addCourseToSemester: protectedProcedure
.input(z.object({ planId: z.string(), semesterId: z.string(), courseName: z.string() }))
.mutation(async ({ ctx, input }) => {
// Get semester you're adding the course to
try {
const { semesterId, courseName } = input;
// Update courses
await ctx.prisma.semester.update({
where: {
id: semesterId,
plan: { userId: ctx.session.user.id },
},
data: {
courses: {
create: { code: courseName, color: '' },
},
},
});
return true;
} catch (error) {
console.error(error);
return false;
}
}),
// Protected route: route uses session user id
removeCourseFromSemester: protectedProcedure
.input(z.object({ planId: z.string(), semesterId: z.string(), courseName: z.string() }))
.mutation(async ({ ctx, input }) => {
try {
const { semesterId, courseName } = input;
// This works bc semesters are stored in its own table
// Once integrated w/ Nebula API, use Promise.all() to call concurrently
await ctx.prisma.course.delete({
where: {
semester: { plan: { userId: ctx.session.user.id } },
semesterId_code: {
semesterId,
code: courseName,
},
},
});
return true;
} catch (error) {
console.error(error);
return false;
}
}),
// Protected route: route uses session user id
deleteAllCoursesFromSemester: protectedProcedure
.input(z.object({ semesterId: z.string() }))
.mutation(async ({ ctx, input: { semesterId } }) => {
await ctx.prisma.course
.deleteMany({
where: { semesterId, semester: { plan: { userId: ctx.session.user.id } } },
})
.catch((err) => {
if (err instanceof Prisma.PrismaClientKnownRequestError) {
if (err.code === 'P2025') {
throw new TRPCError({
code: 'NOT_FOUND',
message: 'Semester does not exist',
});
}
}
});
}),
// Protected route: route uses session user id
moveCourseFromSemester: protectedProcedure
.input(
z.object({
planId: z.string(),
oldSemesterId: z.string(),
newSemesterId: z.string(),
courseName: z.string(),
}),
)
.mutation(async ({ ctx, input }) => {
try {
const { oldSemesterId, newSemesterId, courseName } = input;
await ctx.prisma.course.update({
where: {
semester: { plan: { userId: ctx.session.user.id } },
semesterId_code: {
semesterId: oldSemesterId,
code: courseName,
},
},
data: {
semesterId: newSemesterId,
},
});
return true;
} catch (error) {
console.error(error);
}
}),
// Unprotected route
validateDegreePlan: protectedProcedure
.input(
z.object({
courses: z.array(
z.object({
name: z.string(),
department: z.string(),
level: z.number(),
hours: z.number(),
}),
),
bypasses: z.array(
z.object({
course: z.string(),
requirement: z.string(),
hours: z.number(),
}),
),
degree: z.string(),
}),
)
.query(async ({ ctx, input }) => {
try {
return await fetch('http://0.0.0.0:5001/test-validate', {
method: 'POST',
body: JSON.stringify(input),
headers: {
'content-type': 'application/json',
},
}).then(async (res) => {
const rawData = await res.json();
return rawData;
// Transform data
});
} catch (error) {
console.log(error);
}
}),
// Protected route: route uses session user id
changeSemesterColor: protectedProcedure
.input(
z.object({
semesterId: z.string(),
color: z.string(),
}),
)
.mutation(async ({ ctx, input }) => {
await ctx.prisma.semester.update({
where: {
id: input.semesterId,
plan: { userId: ctx.session.user.id },
},
data: {
color: input.color,
},
});
return true;
}),
// Protected route: route uses session user id
changeCourseColor: protectedProcedure
.input(
z.object({
planId: z.string(),
semesterId: z.string(),
courseName: z.string(),
color: z.string(),
}),
)
.mutation(async ({ ctx, input }) => {
await ctx.prisma.course.update({
where: {
semester: { plan: { userId: ctx.session.user.id } },
semesterId_code: {
semesterId: input.semesterId,
code: input.courseName,
},
},
data: {
color: input.color,
},
});
return true;
}),
// Protected route: route uses session user id
changeCoursePrereqOverride: protectedProcedure
.input(
z.object({
semesterId: z.string(),
courseName: z.string(),
prereqOverriden: z.boolean(),
}),
)
.mutation(async ({ ctx, input }) => {
await ctx.prisma.course.update({
where: {
semester: { plan: { userId: ctx.session.user.id } },
semesterId_code: {
semesterId: input.semesterId,
code: input.courseName,
},
},
data: {
prereqOverriden: input.prereqOverriden,
},
});
return true;
}),
// Protected route: route uses session user id
changeCourseLock: protectedProcedure
.input(
z.object({
semesterId: z.string(),
courseName: z.string(),
locked: z.boolean(),
}),
)
.mutation(async ({ ctx, input }) => {
await ctx.prisma.course.update({
where: {
semester: { plan: { userId: ctx.session.user.id } },
semesterId_code: {
semesterId: input.semesterId,
code: input.courseName,
},
},
data: {
locked: input.locked,
},
});
return true;
}),
// Protected route: route uses session user id
changeSemesterLock: protectedProcedure
.input(
z.object({
semesterId: z.string(),
locked: z.boolean(),
}),
)
.mutation(async ({ ctx, input }) => {
await ctx.prisma.semester.update({
where: {
plan: { userId: ctx.session.user.id },
id: input.semesterId,
},
data: {
locked: input.locked,
},
});
return true;
}),
// Protected route: route uses session user id
addBypass: protectedProcedure
.input(z.object({ planId: z.string(), requirement: z.string() }))
.mutation(async ({ ctx, input }) => {
try {
const { planId, requirement } = input;
const degreeRequirements = await ctx.prisma.degreeRequirements.findFirstOrThrow({
where: {
plan: { userId: ctx.session.user.id, id: planId },
},
select: {
bypasses: true,
id: true,
},
});
const bypasses = [...degreeRequirements.bypasses, requirement];
const updatedDegreeRequirements = await ctx.prisma.degreeRequirements.update({
where: {
plan: { userId: ctx.session.user.id },
id: degreeRequirements.id,
},
data: {
bypasses,
},
});
return true;
} catch (e) {
console.error(e);
return false;
}
}),
// Protected route: route uses session user id
removeBypass: protectedProcedure
.input(z.object({ planId: z.string(), requirement: z.string() }))
.mutation(async ({ ctx, input }) => {
try {
const { planId, requirement } = input;
const degreeRequirements = await ctx.prisma.degreeRequirements.findFirstOrThrow({
where: {
plan: { userId: ctx.session.user.id, id: planId },
},
select: {
bypasses: true,
id: true,
},
});
// Create NewBypass if it doesn't exist
if (degreeRequirements.bypasses === null) {
throw 'No bypass';
}
// If we know the bypass model exists, we can update it directly
const newBypass = await ctx.prisma.degreeRequirements.update({
where: {
plan: { userId: ctx.session.user.id },
id: degreeRequirements.id, // Null-assertion bc type narrowing is being dumb here
},
data: {
bypasses: [...degreeRequirements.bypasses.filter((id) => id !== requirement)].sort(),
},
});
return newBypass.id;
} catch {}
}),
// Protected route: route uses session user id
getDegreeRequirements: protectedProcedure
.input(z.object({ planId: z.string() }))
.query(async ({ ctx, input }) => {
try {
const { planId } = input;
const degreeRequirements = await ctx.prisma.degreeRequirements.findFirst({
where: {
plan: { userId: ctx.session.user.id, id: planId },
},
select: {
major: true,
id: true,
},
});
if (!degreeRequirements) {
throw 'No degree requirements';
}
return degreeRequirements;
} catch {}
}),
// Protected route: route uses session user id
updatePlanTitle: protectedProcedure
.input(z.object({ planId: z.string(), title: z.string().min(1) }))
.mutation(async ({ ctx, input }) => {
try {
const { planId, title } = input;
await ctx.prisma.plan.update({
where: {
userId: ctx.session.user.id,
id: planId,
},
data: {
name: title,
},
});
return true;
} catch {
return false;
}
}),
// Protected route: route uses session user id
updatePlanMajor: protectedProcedure
.input(
z.object({
degreeRequirementsId: z.string().min(1),
planId: z.string().min(1),
major: z.string().min(1),
}),
)
.mutation(async ({ ctx, input }) => {
try {
const { planId, major, degreeRequirementsId } = input;
await ctx.prisma.degreeRequirements.update({
where: {
id: degreeRequirementsId,
plan: { userId: ctx.session.user.id, id: planId },
},
data: {
major,
},
});
return true;
} catch {
return false;
}
}),
});