Skip to content

Commit

Permalink
fix(scoring): add weighted professor scoring (#132)
Browse files Browse the repository at this point in the history
  • Loading branch information
Suraj-Ram authored Dec 7, 2023
1 parent 6f99b5b commit dafde45
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 6 deletions.
29 changes: 29 additions & 0 deletions src/client/professorScore.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,32 @@ export const updateComputedProfessorScoreForUser = async (
return ResponseStatus.UnknownError;
}
};

export const getWeightedProfessorScoreForUser = async (
userId: number,
): Promise<
| number
| ResponseStatus.Unauthorized
| ResponseStatus.BadRequest
| ResponseStatus.UnknownError
> => {
try {
const response = await fetch(apiRoot + '/weighted-score/' + userId, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
},
});

if (response.ok || response.status === 200) {
const data = await response.json();
return data;
} else if (response.status === 400) return ResponseStatus.BadRequest;
else if (response.status === 401) return ResponseStatus.Unauthorized;
else return ResponseStatus.UnknownError;
} catch (error) {
console.log(error);
return ResponseStatus.UnknownError;
}
};
23 changes: 17 additions & 6 deletions src/components/ProfessorScoring/ProfessorScoreCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React, { useEffect, useState } from 'react';
import {
getProfessorScoreForUser,
updateProfessorScoreForUser,
getWeightedProfessorScoreForUser,
} from '@/client/professorScore.client';
import {
CreateProfessorScoreDto,
Expand All @@ -28,6 +29,7 @@ const ProfessorScoreCard: React.FC<ProfessorScoreCardProps> = ({
const scores = useSelector(selectProfessorScores);
const [finalScore, setFinalScore] = useState('');
const [error, setError] = useState<string | null>(null);
const [weightedScore, setWeightedScore] = useState<number | null>(null);

const dispatch = useDispatch();

Expand Down Expand Up @@ -55,6 +57,20 @@ const ProfessorScoreCard: React.FC<ProfessorScoreCardProps> = ({
}
}, [finalScore, professorId]);

useEffect(() => {
getWeightedProfessorScoreForUser(professorId).then((data) => {
if (data === ResponseStatus.UnknownError) {
setError('Unknown Error');
} else if (data === ResponseStatus.Unauthorized) {
setError('Unauthorized');
} else if (data === ResponseStatus.BadRequest) {
setError('Bad Request');
} else {
setWeightedScore(data);
}
});
}, []);

if (!scores) {
return null;
}
Expand All @@ -72,11 +88,6 @@ const ProfessorScoreCard: React.FC<ProfessorScoreCardProps> = ({
{ category: 'Service', score: scores.serviceScore },
];

const averageScore = (
(scores.teachingScore + scores.researchScore + scores.serviceScore) /
3
).toFixed(1);

const tooltipMessage =
'A professor may be recently hired or have special circumstances, so you can adjust their score accordingly below.';

Expand All @@ -97,7 +108,7 @@ const ProfessorScoreCard: React.FC<ProfessorScoreCardProps> = ({
<div className="flex flex-col justify-between px-4 xl:flex-row">
<ProfessorScoreItem
category={'Average'}
score={parseFloat(averageScore)}
score={parseFloat(weightedScore?.toFixed(1) ?? '0')}
className="space-y-2"
/>
<div className="flex items-center justify-center xl:justify-start">
Expand Down
35 changes: 35 additions & 0 deletions src/pages/api/professor-scores/weighted-score/[professorId].ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { NextApiRequest, NextApiResponse } from 'next';

import { calculateWeightedProfScore } from '@/services/professorScore';

export default async function handler(
req: NextApiRequest,
res: NextApiResponse,
) {
const { professorId } = req.query;

if (!professorId) {
res.status(400).json({ error: 'Professor not found' });
return;
}

const parsedProfessorId = parseInt(
Array.isArray(professorId) ? professorId[0] : professorId,
10,
);

if (req.method === 'GET') {
try {
const professorScore =
await calculateWeightedProfScore(parsedProfessorId);

if (professorScore) {
res.status(200).json(professorScore);
} else {
res.status(404).json({ error: 'Professor score not found' });
}
} catch (e) {
res.status(500).json({ error: (e as Error)?.message || 'Unknown error' });
}
}
}
18 changes: 18 additions & 0 deletions src/services/professorScore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,24 @@ export const getAllProfessorScores = async (): Promise<ProfessorScore[]> => {
return scores;
};

export const calculateWeightedProfScore = async (userId: number) => {
const score = await getProfessorScore(userId);

const weights = await prisma.professorInfo.findUnique({
where: { userId: userId },
});

if (score == null || weights == null) {
return 0;
}

const teachingScore = score.teachingScore * weights.teachingPercent;
const researchScore = score.researchScore * weights.researchPercent;
const serviceScore = score.serviceScore * weights.servicePercent;

return teachingScore + researchScore + serviceScore;
};

export const upsertProfessorScore = async (
score: UpdateProfessorScoreDto,
): Promise<CreateProfessorScoreDto> => {
Expand Down

1 comment on commit dafde45

@vercel
Copy link

@vercel vercel bot commented on dafde45 Dec 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.