feat: enforce Europe/Istanbul timezone, fix PWA image proxy, add Monday weekly login sync, and standardize English routes

This commit is contained in:
tyuXX 2026-11-09 21:45:27 +00:00
parent 56274f479b
commit 93f0bcb7a1
16 changed files with 851 additions and 175 deletions

View file

@ -1,3 +1,5 @@
import '../utils/time';
import { toIstanbulIsoString } from '../utils/time';
import fs from 'fs';
import path from 'path';
import crypto from 'crypto';
@ -28,6 +30,7 @@ const SYNTHETIC_TEST_ID_REGEX = /^(test_student_|audit_)/i;
interface StudentRow {
student_id: string;
last_synced: string | null;
last_login_at: string | null;
remembered_password: string | null;
advisory_teacher: string | null;
syllabus_json: string | null;
@ -123,6 +126,7 @@ export class LocalDatabase {
CREATE TABLE IF NOT EXISTS students (
student_id TEXT PRIMARY KEY,
last_synced TEXT,
last_login_at TEXT,
remembered_password TEXT,
advisory_teacher TEXT,
syllabus_json TEXT,
@ -163,6 +167,12 @@ export class LocalDatabase {
CREATE INDEX IF NOT EXISTS idx_sessions_expire ON sessions(expire);
`);
// Ensure last_login_at column exists on pre-existing databases without data loss
const studentCols = this.sqlite.pragma('table_info(students)') as Array<{ name: string }>;
if (!studentCols.some(col => col.name === 'last_login_at')) {
this.sqlite.exec('ALTER TABLE students ADD COLUMN last_login_at TEXT');
}
}
/**
@ -289,7 +299,7 @@ export class LocalDatabase {
INSERT OR REPLACE INTO meta (key, value) VALUES (?, ?)
`);
const nowIso = new Date().toISOString();
const nowIso = toIstanbulIsoString();
const runMigration = this.sqlite.transaction(() => {
for (const [studentId, store] of Object.entries(studentsMap)) {
@ -368,25 +378,27 @@ export class LocalDatabase {
return existing;
}
const now = new Date().toISOString();
const now = toIstanbulIsoString();
this.sqlite
.prepare(`
INSERT INTO students (
student_id,
last_synced,
last_login_at,
remembered_password,
advisory_teacher,
syllabus_json,
private_lessons_json,
exams_json,
updated_at
) VALUES (?, NULL, NULL, NULL, NULL, NULL, NULL, ?)
) VALUES (?, NULL, NULL, NULL, NULL, NULL, NULL, NULL, ?)
`)
.run(studentId, now);
return {
student_id: studentId,
last_synced: null,
last_login_at: null,
remembered_password: null,
advisory_teacher: null,
syllabus_json: null,
@ -395,6 +407,21 @@ export class LocalDatabase {
};
}
public getLastLogin(studentId: string): string | null {
const row = this.sqlite
.prepare('SELECT last_login_at, last_synced FROM students WHERE student_id = ?')
.get(studentId) as { last_login_at: string | null; last_synced: string | null } | undefined;
if (!row) return null;
return row.last_login_at || row.last_synced || null;
}
public updateLastLogin(studentId: string, loginAt: string = toIstanbulIsoString()): void {
this.ensureStudentRow(studentId);
this.sqlite
.prepare('UPDATE students SET last_login_at = ?, updated_at = ? WHERE student_id = ?')
.run(loginAt, loginAt, studentId);
}
private mapAssignmentRow(row: AssignmentRow): Assignment {
return {
id: row.id,
@ -471,18 +498,14 @@ export class LocalDatabase {
return store;
}
public mergeAssignments(studentId: string, newAssignments: Assignment[]): StudentStore {
public mergeAssignments(studentId: string, newAssignments: Assignment[], syncedAt?: string): StudentStore {
this.ensureStudentRow(studentId);
const now = new Date().toISOString();
const now = syncedAt || toIstanbulIsoString();
const updateStudentSync = this.sqlite.prepare(
'UPDATE students SET last_synced = ?, updated_at = ? WHERE student_id = ?'
);
const getExistingStmt = this.sqlite.prepare(
'SELECT completed, completed_at, excluded FROM assignments WHERE student_id = ? AND id = ?'
);
const upsertStmt = this.sqlite.prepare(`
INSERT INTO assignments (
student_id,
@ -532,10 +555,10 @@ export class LocalDatabase {
parsed_end_date = excluded.parsed_end_date,
description = excluded.description,
detected_check_date = excluded.detected_check_date,
status = excluded.status,
completed = excluded.completed,
completed_at = excluded.completed_at,
excluded = excluded.excluded,
status = CASE WHEN assignments.completed = 1 THEN 'completed' ELSE excluded.status END,
completed = CASE WHEN assignments.completed = 1 THEN 1 ELSE excluded.completed END,
completed_at = COALESCE(assignments.completed_at, excluded.completed_at),
excluded = CASE WHEN assignments.excluded = 1 THEN 1 ELSE excluded.excluded END,
page = excluded.page
`);
@ -543,15 +566,7 @@ export class LocalDatabase {
updateStudentSync.run(now, now, studentId);
for (const item of newAssignments) {
const existing = getExistingStmt.get(studentId, item.id) as
| { completed: number; completed_at: string | null; excluded: number }
| undefined;
const isCompleted = existing ? existing.completed === 1 : Boolean(item.completed);
const completedAt = existing ? existing.completed_at : (item.completedAt || null);
const isExcluded = existing ? existing.excluded === 1 : Boolean(item.excluded);
const status = isCompleted ? 'completed' : item.status;
const isCompleted = Boolean(item.completed);
upsertStmt.run({
student_id: studentId,
id: item.id,
@ -565,10 +580,10 @@ export class LocalDatabase {
parsed_end_date: item.parsedEndDate || null,
description: item.description || '',
detected_check_date: item.detectedCheckDate || null,
status,
status: isCompleted ? 'completed' : item.status,
completed: isCompleted ? 1 : 0,
completed_at: completedAt,
excluded: isExcluded ? 1 : 0,
completed_at: item.completedAt || null,
excluded: item.excluded ? 1 : 0,
page: item.page ?? null
});
}
@ -619,7 +634,7 @@ export class LocalDatabase {
}
const newCompleted = completed !== undefined ? completed : existing.completed !== 1;
const completedAt = newCompleted ? new Date().toISOString() : null;
const completedAt = newCompleted ? toIstanbulIsoString() : null;
const newStatus = newCompleted ? 'completed' : existing.status;
this.sqlite
@ -640,14 +655,14 @@ export class LocalDatabase {
const encryptedPassword = encryptCredential(password);
this.sqlite
.prepare('UPDATE students SET remembered_password = ?, updated_at = ? WHERE student_id = ?')
.run(encryptedPassword, new Date().toISOString(), studentId);
.run(encryptedPassword, toIstanbulIsoString(), studentId);
}
public clearCredentials(studentId: string): void {
this.ensureStudentRow(studentId);
this.sqlite
.prepare('UPDATE students SET remembered_password = NULL, updated_at = ? WHERE student_id = ?')
.run(new Date().toISOString(), studentId);
.run(toIstanbulIsoString(), studentId);
}
public getRememberedCredentials(studentId: string): { studentId: string; password: string } | null {
@ -675,24 +690,26 @@ export class LocalDatabase {
studentId: string,
weekId: string,
weekRange: string,
schedule: Record<number, any>
schedule: Record<number, any>,
syncedAt?: string
): void {
const row = this.ensureStudentRow(studentId);
const existingSyllabus = row.syllabus_json ? (JSON.parse(row.syllabus_json) as SyllabusStore) : null;
const existingStation = existingSyllabus?.selectedStation || null;
const now = syncedAt || toIstanbulIsoString();
const syllabus: SyllabusStore = {
weekId,
weekRange,
selectedStation: existingStation,
advisoryTeacher: row.advisory_teacher || existingSyllabus?.advisoryTeacher || null,
lastSynced: new Date().toISOString(),
lastSynced: now,
schedule
};
this.sqlite
.prepare('UPDATE students SET syllabus_json = ?, updated_at = ? WHERE student_id = ?')
.run(JSON.stringify(syllabus), new Date().toISOString(), studentId);
.run(JSON.stringify(syllabus), now, studentId);
}
public setStudentStation(studentId: string, station: string | null): void {
@ -713,7 +730,7 @@ export class LocalDatabase {
this.sqlite
.prepare('UPDATE students SET syllabus_json = ?, updated_at = ? WHERE student_id = ?')
.run(JSON.stringify(syllabus), new Date().toISOString(), studentId);
.run(JSON.stringify(syllabus), toIstanbulIsoString(), studentId);
}
public getSyllabus(studentId: string): SyllabusStore | null {
@ -741,7 +758,7 @@ export class LocalDatabase {
: null;
const privateLessons: PrivateLessonsStore = {
lastSynced: new Date().toISOString(),
lastSynced: toIstanbulIsoString(),
lessons,
formMeta: formMeta || existing?.formMeta,
detectedAdvisor: detectedAdvisor || existing?.detectedAdvisor
@ -771,7 +788,7 @@ export class LocalDatabase {
JSON.stringify(privateLessons),
newAdvisor,
updatedSyllabusJson,
new Date().toISOString(),
toIstanbulIsoString(),
studentId
);
}
@ -804,7 +821,7 @@ export class LocalDatabase {
this.sqlite
.prepare('UPDATE students SET advisory_teacher = ?, syllabus_json = ?, updated_at = ? WHERE student_id = ?')
.run(teacher, updatedSyllabusJson, new Date().toISOString(), studentId);
.run(teacher, updatedSyllabusJson, toIstanbulIsoString(), studentId);
}
public getStudentAdvisoryTeacher(studentId: string): string | null {
@ -829,17 +846,18 @@ export class LocalDatabase {
// Exam Reports (Sınav Rapor) Store
// ----------------------------------------------------
public saveExamReports(studentId: string, reports: ExamReportDetail[]): void {
public saveExamReports(studentId: string, reports: ExamReportDetail[], syncedAt?: string): void {
const row = this.ensureStudentRow(studentId);
const existing = row.exams_json ? (JSON.parse(row.exams_json) as ExamStore) : null;
const reportsMap: Record<string, ExamReportDetail> = existing?.reports ? { ...existing.reports } : {};
const now = syncedAt || toIstanbulIsoString();
reports.forEach(r => {
reportsMap[r.summary.id] = r;
});
const exams: ExamStore = {
lastSynced: new Date().toISOString(),
lastSynced: now,
reports: reportsMap,
topicWeaknesses: existing?.topicWeaknesses || {},
questions: existing?.questions || {}
@ -847,7 +865,7 @@ export class LocalDatabase {
this.sqlite
.prepare('UPDATE students SET exams_json = ?, updated_at = ? WHERE student_id = ?')
.run(JSON.stringify(exams), new Date().toISOString(), studentId);
.run(JSON.stringify(exams), now, studentId);
}
public getExamReports(studentId: string): ExamStore | null {
@ -868,7 +886,7 @@ export class LocalDatabase {
const exams: ExamStore = row.exams_json
? (JSON.parse(row.exams_json) as ExamStore)
: {
lastSynced: new Date().toISOString(),
lastSynced: toIstanbulIsoString(),
reports: {},
topicWeaknesses: {},
questions: {}
@ -881,7 +899,7 @@ export class LocalDatabase {
this.sqlite
.prepare('UPDATE students SET exams_json = ?, updated_at = ? WHERE student_id = ?')
.run(JSON.stringify(exams), new Date().toISOString(), studentId);
.run(JSON.stringify(exams), toIstanbulIsoString(), studentId);
}
public getTopicWeaknesses(studentId: string, examId: string): ExamTopicWeakness[] | null {
@ -894,7 +912,7 @@ export class LocalDatabase {
const exams: ExamStore = row.exams_json
? (JSON.parse(row.exams_json) as ExamStore)
: {
lastSynced: new Date().toISOString(),
lastSynced: toIstanbulIsoString(),
reports: {},
topicWeaknesses: {},
questions: {}
@ -907,7 +925,7 @@ export class LocalDatabase {
this.sqlite
.prepare('UPDATE students SET exams_json = ?, updated_at = ? WHERE student_id = ?')
.run(JSON.stringify(exams), new Date().toISOString(), studentId);
.run(JSON.stringify(exams), toIstanbulIsoString(), studentId);
}
public getExamQuestions(studentId: string, examId: string): ExamQuestionItem[] | null {