Skip to content

Commit

Permalink
Merge pull request #733 from UTDNebula/fix-develop
Browse files Browse the repository at this point in the history
Revert "Merge pull request #716 from UTDNebula/refactor-validatets"
  • Loading branch information
cubetastic33 authored Nov 10, 2023
2 parents 64bca23 + c3502a0 commit 7c8e606
Show file tree
Hide file tree
Showing 4 changed files with 269 additions and 118 deletions.
2 changes: 1 addition & 1 deletion src/components/planner/Tiles/SemesterCourseItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ const DraggableSemesterCourseItem: FC<DraggableSemesterCourseItemProps> = ({
const hoverList: [Array<string>, Array<string>, Array<string>] = [[], [], []];
const prereqData = requirementsData.data?.prereq?.get(course.code);
const coreqData = requirementsData.data?.coreq?.get(course.code);
const coorpreData = requirementsData.data?.coorpre?.get(course.code);
const coorpreData = requirementsData.data?.coorepre?.get(course.code);
if (coreqData) {
isValid[1] = coreqData.length > 0 ? false : true;
coreqData.map((data) => {
Expand Down
5 changes: 4 additions & 1 deletion src/server/trpc/router/courseCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class CourseCache {
private mutex = new Mutex();

public async getCourses(year: number) {
const formattedYear = year.toString().slice(-2);
// Acquire lock before success check so if another request is fetching, we don't fetch again.
const release = await this.mutex.acquire();
if (this.coursesByYear.has(year)) {
Expand All @@ -23,7 +24,9 @@ class CourseCache {
console.info(`Fetching courses for year ${year}...`);
return await platformPrisma.courses
.findMany({
distinct: ['title', 'course_number', 'subject_prefix'],
where: {
catalog_year: formattedYear,
},
})
.then((courses) => {
this.coursesByYear.set(year, courses);
Expand Down
69 changes: 31 additions & 38 deletions src/server/trpc/router/plan.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Prisma, PrismaClient, Semester } from '@prisma/client';
import { Prisma, Semester } from '@prisma/client';
import { TRPCError } from '@trpc/server';
import { v4 as uuidv4 } from 'uuid';
import { z } from 'zod';
Expand All @@ -13,42 +13,6 @@ import { SemesterCode, computeSemesterCode } from 'prisma/utils';

import { protectedProcedure, router } from '../trpc';

// extracted for reusability for other trpc routes
export const getPlanFromUserId = async (ctx: { prisma: PrismaClient }, id: string) => {
// Fetch current plan
const planData = await ctx.prisma.plan.findUnique({
where: {
id,
},
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',
});
}
return planData;
};

export const planRouter = router({
// Protected route: route uses session user id to find user plans
getUserPlans: protectedProcedure.query(async ({ ctx }) => {
Expand All @@ -72,7 +36,36 @@ export const planRouter = router({
// 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 getPlanFromUserId(ctx, input);
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' });
Expand Down
Loading

0 comments on commit 7c8e606

Please sign in to comment.