This commit is contained in:
2026-04-13 10:43:17 +05:00
parent c54f688406
commit 7c6b3ee1a4
4 changed files with 3197 additions and 19 deletions

View File

@@ -237,12 +237,25 @@ app.get('/api/filter-options/subjects', async (req, res) => {
}
});
// --------------------- Импорт уроков из JSON (без даты/времени) ---------------------
// --------------------- Универсальный парсер для импорта (JSON / XLSX) ---------------------
function parseLessonFromJsonRecord(record) {
const fields = {};
for (const [key, value] of record) {
fields[key.trim()] = (value || '').trim();
let fields = {};
if (Array.isArray(record)) {
// Массив пар [["key","value"], ...]
for (const [key, value] of record) {
fields[key.trim()] = (value || '').toString().trim();
}
} else if (typeof record === 'object' && record !== null) {
// Обычный объект
fields = { ...record };
Object.keys(fields).forEach(k => {
fields[k.trim()] = (fields[k] || '').toString().trim();
});
} else {
throw new Error('Неверный формат записи');
}
const lastName = fields["Фамилия Учителя"] || "";
const firstName = fields["Имя Учителя"] || "";
const patronymic = fields["Отчество Учителя"] || "";
@@ -253,6 +266,7 @@ function parseLessonFromJsonRecord(record) {
const classLetter = fields["Класс (буква)"] || "";
const parallel = parseInt(parallelRaw, 10);
const className = `${parallelRaw}${classLetter}`.trim();
return { teacher, subject, topic, parallel, className };
}
@@ -264,8 +278,12 @@ app.post('/api/admin/import/preview', isAuthenticated, isAdmin, async (req, res)
const preview = [];
for (const record of records) {
if (!Array.isArray(record)) continue;
const lesson = parseLessonFromJsonRecord(record);
let lesson;
try {
lesson = parseLessonFromJsonRecord(record);
} catch(e) {
continue; // пропускаем битые записи
}
if (lesson.teacher && lesson.subject && !isNaN(lesson.parallel)) {
preview.push(lesson);
}
@@ -273,7 +291,7 @@ app.post('/api/admin/import/preview', isAuthenticated, isAdmin, async (req, res)
res.json({ success: true, preview });
} catch (err) {
console.error(err);
res.status(500).json({ error: 'Ошибка разбора JSON' });
res.status(500).json({ error: 'Ошибка разбора JSON/Excel' });
}
});