Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crash with no planners saved to account #503

Merged
merged 6 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions api/src/controllers/roadmap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ const roadmapsRouter = router({
.where(eq(transferredCourse.userId, ctx.session.userId!)),
db.select({ timestamp: user.lastRoadmapEditAt }).from(user).where(eq(user.id, ctx.session.userId!)),
]);
if (planners.length === 0) {
return undefined;
}
const roadmap: SavedRoadmap = {
planners: planners as SavedPlannerData[],
transfers: transfers as TransferData[],
Expand Down Expand Up @@ -67,9 +70,11 @@ const roadmapsRouter = router({
.delete(transferredCourse)
.where(eq(transferredCourse.userId, userId))
.then(() => {
return db
.insert(transferredCourse)
.values(transfers.map((transfer) => ({ userId, courseName: transfer.name, units: transfer.units })));
if (transfers.length > 0) {
return db
.insert(transferredCourse)
.values(transfers.map((transfer) => ({ userId, courseName: transfer.name, units: transfer.units })));
}
});

const updateLastEditTimestamp = db
Expand Down
61 changes: 54 additions & 7 deletions site/src/helpers/planner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
Prerequisite,
PrerequisiteTree,
QuarterName,
quarters,
SavedPlannerData,
SavedPlannerQuarterData,
SavedPlannerYearData,
Expand Down Expand Up @@ -33,6 +34,25 @@ export const quarterDisplayNames: Record<QuarterName, string> = {
Summer10wk: 'Summer 10 Week',
};

export function normalizeQuarterName(name: string): QuarterName {
if (quarters.includes(name as QuarterName)) return name as QuarterName;
const lookup: { [k: string]: QuarterName } = {
fall: 'Fall',
winter: 'Winter',
spring: 'Spring',
// Old Lowercase Display Names
'summer I': 'Summer1',
'summer II': 'Summer2',
'summer 10 Week': 'Summer10wk',
// Transcript Names
'First Summer': 'Summer1',
'Second Summer': 'Summer2',
'Special / 10-Week Summer': 'Summer10wk',
};
if (!lookup[name]) throw TypeError('Invalid Quarter Name: ' + name);
return lookup[name];
}

// remove all unecessary data to store into the database
export const collapsePlanner = (planner: PlannerData): SavedPlannerYearData[] => {
const savedPlanner: SavedPlannerYearData[] = [];
Expand Down Expand Up @@ -115,24 +135,51 @@ export const loadRoadmap = async (
let isLocalNewer = false;

if (!roadmap && localRoadmap) {
roadmap = localRoadmap;
roadmap = convertLegacyLocalRoadmap(localRoadmap);
} else if (roadmap && localRoadmap && new Date(localRoadmap.timestamp ?? 0) > new Date(roadmap.timestamp ?? 0)) {
isLocalNewer = true;
} else if (!roadmap && !localRoadmap) {
// no saved planner
return;
}

// Support conversions from the old format
const loadedData =
'planners' in roadmap
? roadmap.planners
: [{ name: defaultPlan.name, content: (roadmap as { planner: SavedPlannerYearData[] }).planner }];
// expand planner and set the state
const planners = await expandAllPlanners(loadedData);
const planners = await expandAllPlanners(roadmap.planners);
loadHandler(planners, roadmap, isLocalNewer);
};

interface LegacyRoadmap {
planner: SavedPlannerYearData[];
transfers: TransferData[];
timestamp?: string;
}

export function convertLegacyLocalRoadmap(roadmap: SavedRoadmap | LegacyRoadmap): SavedRoadmap {
if ('planners' in roadmap) {
// if already in multiplanner format, everything is good
return roadmap;
} else {
// if not, convert to multiplanner format, also normalize quarter names
return {
planners: [
{
name: defaultPlan.name,
content: normalizePlannerQuarterNames((roadmap as { planner: SavedPlannerYearData[] }).planner),
},
],
transfers: roadmap.transfers,
timestamp: roadmap.timestamp,
};
}
}

function normalizePlannerQuarterNames(yearPlans: SavedPlannerYearData[]) {
return yearPlans.map((year) => ({
...year,
quarters: year.quarters.map((quarter) => ({ ...quarter, name: normalizeQuarterName(quarter.name) })),
}));
}

type PrerequisiteNode = Prerequisite | PrerequisiteTree;

type plannerCallback = (missing: Set<string>, invalidCourses: InvalidCourseData[]) => void;
Expand Down
4 changes: 3 additions & 1 deletion site/src/pages/RoadmapPage/ImportTranscriptPopup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { BatchCourseData, PlannerQuarterData, PlannerYearData } from '../../type
import { quarters } from '@peterportal/types';
import { searchAPIResults } from '../../helpers/util';
import { QuarterName } from '@peterportal/types';
import { normalizeQuarterName } from '../../helpers/planner';

interface TransferUnitDetails {
date: string;
Expand Down Expand Up @@ -87,7 +88,8 @@ function toPlannerQuarter(
): { startYear: number; quarterData: PlannerQuarterData } {
const year = parseInt(quarter.name.split(' ')[0]);
// Removes the year number and "Session/Quarter" at the end
const name = quarter.name.split(' ').slice(1, -1).join(' ') as QuarterName;
const tName = quarter.name.split(' ').slice(1, -1).join(' ');
const name = normalizeQuarterName(tName);

return {
startYear: name === 'Fall' ? year : year - 1,
Expand Down
4 changes: 2 additions & 2 deletions site/src/pages/RoadmapPage/Planner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
} from '../../store/slices/roadmapSlice';
import { useFirstRender } from '../../hooks/firstRenderer';
import { SavedRoadmap } from '@peterportal/types';
import { defaultYear, expandAllPlanners } from '../../helpers/planner';
import { convertLegacyLocalRoadmap, defaultYear, expandAllPlanners } from '../../helpers/planner';
import ImportTranscriptPopup from './ImportTranscriptPopup';
import { collapseAllPlanners, loadRoadmap, validatePlanner } from '../../helpers/planner';
import { Button, Modal } from 'react-bootstrap';
Expand All @@ -41,7 +41,7 @@ const Planner: FC = () => {
let roadmap: SavedRoadmap = null!;
const roadmapItem = localStorage.getItem('roadmap');
if (roadmapItem) {
roadmap = JSON.parse(roadmapItem);
roadmap = convertLegacyLocalRoadmap(JSON.parse(roadmapItem));
}

const planner = await expandAllPlanners(roadmap.planners);
Expand Down
Loading