Refactor updateFamilyConversationExercises for improved database queries and clarity
- Replaced direct model queries with raw SQL queries for fetching the Bisaya language and associated courses, enhancing performance and readability. - Simplified the retrieval of native language information for courses by using a single SQL query with a LEFT JOIN, reducing the number of database calls. - Updated variable names for better clarity and consistency in the codebase.
This commit is contained in:
@@ -12,8 +12,6 @@ import { sequelize } from '../utils/sequelize.js';
|
||||
import VocabCourseLesson from '../models/community/vocab_course_lesson.js';
|
||||
import VocabGrammarExercise from '../models/community/vocab_grammar_exercise.js';
|
||||
import VocabCourse from '../models/community/vocab_course.js';
|
||||
import VocabLanguage from '../models/community/vocab_language.js';
|
||||
import VocabGrammarExerciseType from '../models/community/vocab_grammar_exercise_type.js';
|
||||
import User from '../models/community/user.js';
|
||||
import crypto from 'crypto';
|
||||
import bcrypt from 'bcryptjs';
|
||||
@@ -425,31 +423,34 @@ async function updateFamilyConversationExercises() {
|
||||
console.log('✅ Datenbankverbindung erfolgreich hergestellt.\n');
|
||||
|
||||
const systemUser = await findOrCreateSystemUser();
|
||||
const bisayaLanguage = await VocabLanguage.findOne({ where: { name: 'Bisaya' } });
|
||||
|
||||
if (!bisayaLanguage) {
|
||||
// Finde Bisaya-Sprache mit SQL
|
||||
const [bisayaLangResult] = await sequelize.query(
|
||||
`SELECT id FROM community.vocab_language WHERE name = 'Bisaya' LIMIT 1`,
|
||||
{ type: sequelize.QueryTypes.SELECT }
|
||||
);
|
||||
|
||||
if (!bisayaLangResult) {
|
||||
console.error('❌ Bisaya-Sprache nicht gefunden.');
|
||||
return;
|
||||
}
|
||||
|
||||
// Hole alle Bisaya-Kurse
|
||||
const courses = await VocabCourse.findAll({
|
||||
where: { languageId: bisayaLanguage.id },
|
||||
attributes: ['id', 'title', 'nativeLanguageId']
|
||||
});
|
||||
const bisayaLanguageId = bisayaLangResult.id;
|
||||
|
||||
// Hole native language info für jeden Kurs
|
||||
const coursesWithNativeLang = await Promise.all(
|
||||
courses.map(async (course) => {
|
||||
let nativeLanguage = null;
|
||||
if (course.nativeLanguageId) {
|
||||
nativeLanguage = await VocabLanguage.findByPk(course.nativeLanguageId);
|
||||
}
|
||||
return {
|
||||
...course.get({ plain: true }),
|
||||
nativeLanguage: nativeLanguage ? nativeLanguage.get({ plain: true }) : null
|
||||
};
|
||||
})
|
||||
// Hole alle Bisaya-Kurse mit native language info
|
||||
const courses = await sequelize.query(
|
||||
`SELECT
|
||||
c.id,
|
||||
c.title,
|
||||
c.native_language_id,
|
||||
nl.name as native_language_name
|
||||
FROM community.vocab_course c
|
||||
LEFT JOIN community.vocab_language nl ON c.native_language_id = nl.id
|
||||
WHERE c.language_id = :bisayaLanguageId`,
|
||||
{
|
||||
replacements: { bisayaLanguageId },
|
||||
type: sequelize.QueryTypes.SELECT
|
||||
}
|
||||
);
|
||||
|
||||
console.log(`📚 Gefunden: ${courses.length} Bisaya-Kurse\n`);
|
||||
@@ -457,11 +458,11 @@ async function updateFamilyConversationExercises() {
|
||||
let totalExercisesCreated = 0;
|
||||
let totalLessonsProcessed = 0;
|
||||
|
||||
for (const course of coursesWithNativeLang) {
|
||||
for (const course of courses) {
|
||||
console.log(`📖 Kurs: ${course.title} (ID: ${course.id})`);
|
||||
|
||||
// Finde native language name
|
||||
const nativeLanguageName = course.nativeLanguage?.name || 'Deutsch';
|
||||
const nativeLanguageName = course.native_language_name || 'Deutsch';
|
||||
console.log(` Muttersprache: ${nativeLanguageName}`);
|
||||
|
||||
// Finde "Familien-Gespräche" Lektion
|
||||
|
||||
Reference in New Issue
Block a user