feat: implement modern Sınav Rapor dashboard with dual-metric delta tracking and inline mistake review
This commit is contained in:
parent
455baa6aa4
commit
f138403918
10 changed files with 2838 additions and 3 deletions
254
src/server.ts
254
src/server.ts
|
|
@ -7,6 +7,7 @@ import { ScraperService } from './services/scraper';
|
|||
import { LocalDatabase } from './services/db';
|
||||
import { InsightService } from './services/insight';
|
||||
import { SyllabusService, DAY_NAMES } from './services/syllabus';
|
||||
import { ExamService } from './services/exam';
|
||||
import { Assignment } from './types';
|
||||
|
||||
dotenv.config();
|
||||
|
|
@ -772,6 +773,259 @@ app.post('/api/private-lessons/sync', requireAuth, async (req: Request, res: Res
|
|||
}
|
||||
});
|
||||
|
||||
// ----------------------------------------------------
|
||||
// Exam Reports (Sınav Rapor) Routes
|
||||
// ----------------------------------------------------
|
||||
|
||||
/**
|
||||
* Helper to ensure active portal cookies
|
||||
*/
|
||||
async function getOrRefreshPortalCookies(req: Request, studentId: string): Promise<string | undefined> {
|
||||
if (req.session.portalCookies) {
|
||||
return req.session.portalCookies;
|
||||
}
|
||||
const creds = db.getRememberedCredentials(studentId);
|
||||
if (creds) {
|
||||
const loginRes = await ScraperService.login(creds.studentId, creds.password);
|
||||
if (loginRes.success) {
|
||||
req.session.portalCookies = loginRes.cookies;
|
||||
return loginRes.cookies;
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* GET /rapor/sinav
|
||||
* Official portal alias redirect
|
||||
*/
|
||||
app.get('/rapor/sinav', (req: Request, res: Response) => {
|
||||
res.redirect('/exams');
|
||||
});
|
||||
|
||||
/**
|
||||
* GET /exams
|
||||
* Main Exam Reports Dashboard
|
||||
*/
|
||||
app.get('/exams', requireAuth, async (req: Request, res: Response) => {
|
||||
const studentId = req.session.studentId!;
|
||||
let examStore = db.getExamReports(studentId);
|
||||
|
||||
// If no cached exams or empty, sync now
|
||||
if (!examStore || !examStore.reports || Object.keys(examStore.reports).length === 0) {
|
||||
const cookies = await getOrRefreshPortalCookies(req, studentId);
|
||||
if (cookies) {
|
||||
try {
|
||||
const examList = await ScraperService.scrapeExamList(cookies);
|
||||
if (examList.length > 0) {
|
||||
const detailPromises = examList.map(item => ScraperService.scrapeExamDetail(cookies, item.id));
|
||||
const details = (await Promise.all(detailPromises)).filter(
|
||||
(d): d is NonNullable<typeof d> => d !== null
|
||||
);
|
||||
db.saveExamReports(studentId, details);
|
||||
examStore = db.getExamReports(studentId);
|
||||
}
|
||||
} catch (err: any) {
|
||||
console.error('Initial exam scrape error:', err.message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const rawDetails = examStore?.reports ? Object.values(examStore.reports) : [];
|
||||
const processedExams = ExamService.computeDeltasAndRatings(rawDetails);
|
||||
|
||||
const tytExams = processedExams.filter(e => e.summary.examType === 'TYT');
|
||||
const aytExams = processedExams.filter(e => e.summary.examType === 'AYT');
|
||||
|
||||
const requestedId = (req.query.id as string) || '';
|
||||
const selectedExam =
|
||||
(requestedId ? processedExams.find(e => e.summary.id === requestedId) : null) ||
|
||||
processedExams[0] ||
|
||||
null;
|
||||
|
||||
const kpis = ExamService.calculateKPIs(processedExams);
|
||||
|
||||
res.render('exams', {
|
||||
title: 'Sınav Raporları - BeKultur',
|
||||
studentId,
|
||||
studentName: req.session.studentName || '',
|
||||
allExams: processedExams,
|
||||
tytExams,
|
||||
aytExams,
|
||||
selectedExam,
|
||||
kpis,
|
||||
lastSynced: examStore?.lastSynced || null
|
||||
});
|
||||
});
|
||||
|
||||
/**
|
||||
* GET /api/exams/:id
|
||||
* Retrieve specific exam scorecard and subject breakdown
|
||||
*/
|
||||
app.get('/api/exams/:id', requireAuth, async (req: Request, res: Response) => {
|
||||
const studentId = req.session.studentId!;
|
||||
const examId = req.params.id;
|
||||
|
||||
let examStore = db.getExamReports(studentId);
|
||||
let exam = examStore?.reports?.[examId];
|
||||
|
||||
if (!exam) {
|
||||
const cookies = await getOrRefreshPortalCookies(req, studentId);
|
||||
if (cookies) {
|
||||
const fetched = await ScraperService.scrapeExamDetail(cookies, examId);
|
||||
if (fetched) {
|
||||
db.saveExamReports(studentId, [fetched]);
|
||||
examStore = db.getExamReports(studentId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const rawDetails = examStore?.reports ? Object.values(examStore.reports) : [];
|
||||
const processed = ExamService.computeDeltasAndRatings(rawDetails);
|
||||
const matched = processed.find(e => e.summary.id === examId);
|
||||
|
||||
if (!matched) {
|
||||
return res.status(404).json({ success: false, error: 'Exam not found' });
|
||||
}
|
||||
|
||||
res.json({ success: true, exam: matched });
|
||||
});
|
||||
|
||||
/**
|
||||
* GET /api/exams/:id/questions
|
||||
* Retrieve question review items (mistakes / empty questions)
|
||||
*/
|
||||
app.get('/api/exams/:id/questions', requireAuth, async (req: Request, res: Response) => {
|
||||
const studentId = req.session.studentId!;
|
||||
const examId = req.params.id;
|
||||
const dersId = (req.query.dersId as string) || '';
|
||||
const ogrenciDonemId = (req.query.ogrenciDonemId as string) || '';
|
||||
const tip = ((req.query.tip as string) || 'Y').toUpperCase() as 'D' | 'Y' | 'B';
|
||||
|
||||
const cookies = await getOrRefreshPortalCookies(req, studentId);
|
||||
if (!cookies) {
|
||||
return res.status(401).json({ success: false, error: 'Session expired' });
|
||||
}
|
||||
|
||||
// If specific subject requested, fetch directly
|
||||
if (dersId && ogrenciDonemId) {
|
||||
const questions = await ScraperService.scrapeExamQuestions(cookies, examId, ogrenciDonemId, dersId, tip);
|
||||
return res.json({ success: true, questions });
|
||||
}
|
||||
|
||||
// Otherwise, inspect exam details and fetch questions for all subjects with mistakes
|
||||
const examStore = db.getExamReports(studentId);
|
||||
const exam = examStore?.reports?.[examId];
|
||||
|
||||
if (!exam) {
|
||||
return res.status(404).json({ success: false, error: 'Exam detail not cached' });
|
||||
}
|
||||
|
||||
const subjectQuestionsList: Array<{
|
||||
subjectName: string;
|
||||
subjectId: string;
|
||||
questions: any[];
|
||||
}> = [];
|
||||
|
||||
for (const subject of exam.subjects) {
|
||||
const shouldFetch = tip === 'Y' ? subject.wrongCount > 0 : tip === 'B' ? subject.emptyCount > 0 : true;
|
||||
if (shouldFetch && subject.subjectId && subject.ogrenciDonemId) {
|
||||
const q = await ScraperService.scrapeExamQuestions(
|
||||
cookies,
|
||||
examId,
|
||||
subject.ogrenciDonemId,
|
||||
subject.subjectId,
|
||||
tip
|
||||
);
|
||||
if (q.length > 0) {
|
||||
subjectQuestionsList.push({
|
||||
subjectName: subject.subjectName,
|
||||
subjectId: subject.subjectId,
|
||||
questions: q
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
totalMistakes: subjectQuestionsList.reduce((acc, s) => acc + s.questions.length, 0),
|
||||
subjects: subjectQuestionsList
|
||||
});
|
||||
});
|
||||
|
||||
/**
|
||||
* GET /api/exams/:id/weaknesses
|
||||
* Retrieve topic weakness breakdown
|
||||
*/
|
||||
app.get('/api/exams/:id/weaknesses', requireAuth, async (req: Request, res: Response) => {
|
||||
const studentId = req.session.studentId!;
|
||||
const examId = req.params.id;
|
||||
|
||||
const cached = db.getTopicWeaknesses(studentId, examId);
|
||||
if (cached && cached.length > 0) {
|
||||
return res.json({ success: true, weaknesses: cached });
|
||||
}
|
||||
|
||||
const cookies = await getOrRefreshPortalCookies(req, studentId);
|
||||
if (!cookies) {
|
||||
return res.status(401).json({ success: false, error: 'Session expired' });
|
||||
}
|
||||
|
||||
const weaknesses = await ScraperService.scrapeTopicWeaknesses(cookies, examId);
|
||||
db.saveTopicWeaknesses(studentId, examId, weaknesses);
|
||||
|
||||
res.json({ success: true, weaknesses });
|
||||
});
|
||||
|
||||
/**
|
||||
* POST /api/exams/sync
|
||||
* Manually refresh all exam data from portal
|
||||
*/
|
||||
app.post('/api/exams/sync', requireAuth, async (req: Request, res: Response) => {
|
||||
const studentId = req.session.studentId!;
|
||||
const cookies = await getOrRefreshPortalCookies(req, studentId);
|
||||
|
||||
if (!cookies) {
|
||||
return res.status(401).json({
|
||||
success: false,
|
||||
error: 'Session expired. Please log in again to sync exams.'
|
||||
});
|
||||
}
|
||||
|
||||
try {
|
||||
const examList = await ScraperService.scrapeExamList(cookies);
|
||||
if (!examList || examList.length === 0) {
|
||||
return res.json({ success: true, count: 0, message: 'No exams found on portal.' });
|
||||
}
|
||||
|
||||
const detailPromises = examList.map(item => ScraperService.scrapeExamDetail(cookies, item.id));
|
||||
const details = (await Promise.all(detailPromises)).filter(
|
||||
(d): d is NonNullable<typeof d> => d !== null
|
||||
);
|
||||
|
||||
db.saveExamReports(studentId, details);
|
||||
const updatedStore = db.getExamReports(studentId);
|
||||
const rawDetails = updatedStore?.reports ? Object.values(updatedStore.reports) : [];
|
||||
const processed = ExamService.computeDeltasAndRatings(rawDetails);
|
||||
const kpis = ExamService.calculateKPIs(processed);
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
count: processed.length,
|
||||
lastSynced: updatedStore?.lastSynced,
|
||||
kpis,
|
||||
exams: processed
|
||||
});
|
||||
} catch (err: any) {
|
||||
console.error('API exams sync error:', err.message);
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
error: `Exams sync failed: ${err.message}`
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
// Start Server
|
||||
if (process.env.NODE_ENV !== 'test') {
|
||||
app.listen(PORT, () => {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue