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 VocabCourseLesson from '../models/community/vocab_course_lesson.js';
|
||||||
import VocabGrammarExercise from '../models/community/vocab_grammar_exercise.js';
|
import VocabGrammarExercise from '../models/community/vocab_grammar_exercise.js';
|
||||||
import VocabCourse from '../models/community/vocab_course.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 User from '../models/community/user.js';
|
||||||
import crypto from 'crypto';
|
import crypto from 'crypto';
|
||||||
import bcrypt from 'bcryptjs';
|
import bcrypt from 'bcryptjs';
|
||||||
@@ -425,31 +423,34 @@ async function updateFamilyConversationExercises() {
|
|||||||
console.log('✅ Datenbankverbindung erfolgreich hergestellt.\n');
|
console.log('✅ Datenbankverbindung erfolgreich hergestellt.\n');
|
||||||
|
|
||||||
const systemUser = await findOrCreateSystemUser();
|
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.');
|
console.error('❌ Bisaya-Sprache nicht gefunden.');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Hole alle Bisaya-Kurse
|
const bisayaLanguageId = bisayaLangResult.id;
|
||||||
const courses = await VocabCourse.findAll({
|
|
||||||
where: { languageId: bisayaLanguage.id },
|
|
||||||
attributes: ['id', 'title', 'nativeLanguageId']
|
|
||||||
});
|
|
||||||
|
|
||||||
// Hole native language info für jeden Kurs
|
// Hole alle Bisaya-Kurse mit native language info
|
||||||
const coursesWithNativeLang = await Promise.all(
|
const courses = await sequelize.query(
|
||||||
courses.map(async (course) => {
|
`SELECT
|
||||||
let nativeLanguage = null;
|
c.id,
|
||||||
if (course.nativeLanguageId) {
|
c.title,
|
||||||
nativeLanguage = await VocabLanguage.findByPk(course.nativeLanguageId);
|
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
|
||||||
}
|
}
|
||||||
return {
|
|
||||||
...course.get({ plain: true }),
|
|
||||||
nativeLanguage: nativeLanguage ? nativeLanguage.get({ plain: true }) : null
|
|
||||||
};
|
|
||||||
})
|
|
||||||
);
|
);
|
||||||
|
|
||||||
console.log(`📚 Gefunden: ${courses.length} Bisaya-Kurse\n`);
|
console.log(`📚 Gefunden: ${courses.length} Bisaya-Kurse\n`);
|
||||||
@@ -457,11 +458,11 @@ async function updateFamilyConversationExercises() {
|
|||||||
let totalExercisesCreated = 0;
|
let totalExercisesCreated = 0;
|
||||||
let totalLessonsProcessed = 0;
|
let totalLessonsProcessed = 0;
|
||||||
|
|
||||||
for (const course of coursesWithNativeLang) {
|
for (const course of courses) {
|
||||||
console.log(`📖 Kurs: ${course.title} (ID: ${course.id})`);
|
console.log(`📖 Kurs: ${course.title} (ID: ${course.id})`);
|
||||||
|
|
||||||
// Finde native language name
|
// Finde native language name
|
||||||
const nativeLanguageName = course.nativeLanguage?.name || 'Deutsch';
|
const nativeLanguageName = course.native_language_name || 'Deutsch';
|
||||||
console.log(` Muttersprache: ${nativeLanguageName}`);
|
console.log(` Muttersprache: ${nativeLanguageName}`);
|
||||||
|
|
||||||
// Finde "Familien-Gespräche" Lektion
|
// Finde "Familien-Gespräche" Lektion
|
||||||
|
|||||||
Reference in New Issue
Block a user