refactor: Remove course_id from lessons table insertion and enhance logging for LLM and database interactions.

This commit is contained in:
2026-01-17 11:13:05 -03:00
parent 3424384ff3
commit 0772a88fbe
+46 -19
View File
@@ -2808,13 +2808,12 @@ pub async fn import_course(
sqlx::query( sqlx::query(
"INSERT INTO lessons ( "INSERT INTO lessons (
module_id, course_id, organization_id, title, content_type, module_id, organization_id, title, content_type,
content_url, position, is_graded, metadata, summary, content_url, position, is_graded, metadata, summary,
transcription, grading_category_id, max_attempts transcription, grading_category_id, max_attempts
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13)", ) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12)",
) )
.bind(new_module.id) .bind(new_module.id)
.bind(new_course.id)
.bind(org_ctx.id) .bind(org_ctx.id)
.bind(lesson.title) .bind(lesson.title)
.bind(lesson.content_type) .bind(lesson.content_type)
@@ -2942,16 +2941,21 @@ RULES:
return Err(StatusCode::INTERNAL_SERVER_ERROR); return Err(StatusCode::INTERNAL_SERVER_ERROR);
} }
let llm_data: serde_json::Value = response let llm_data: serde_json::Value = response.json().await.map_err(|e| {
.json() tracing::error!("Failed to parse LLM JSON response: {}", e);
.await StatusCode::INTERNAL_SERVER_ERROR
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?; })?;
tracing::info!("LLM Response received successfully");
let mut content_str = llm_data["choices"][0]["message"]["content"] let mut content_str = llm_data["choices"][0]["message"]["content"]
.as_str() .as_str()
.unwrap_or("{}") .unwrap_or("{}")
.trim() .trim()
.to_string(); .to_string();
tracing::info!("Extracted content string (length: {})", content_str.len());
// Clean markdown code blocks if present // Clean markdown code blocks if present
if content_str.starts_with("```") { if content_str.starts_with("```") {
content_str = content_str content_str = content_str
@@ -2966,11 +2970,19 @@ RULES:
StatusCode::INTERNAL_SERVER_ERROR StatusCode::INTERNAL_SERVER_ERROR
})?; })?;
tracing::info!(
"JSON parsed successfully. Title: {:?}",
result_json["title"]
);
// 3. Database Transaction // 3. Database Transaction
let mut tx = pool tracing::info!("Starting database transaction...");
.begin() let mut tx = pool.begin().await.map_err(|e| {
.await tracing::error!("Failed to begin transaction: {}", e);
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?; StatusCode::INTERNAL_SERVER_ERROR
})?;
tracing::info!("Transaction started");
// Create Course // Create Course
let course_title = result_json["title"].as_str().unwrap_or("Untitled Course"); let course_title = result_json["title"].as_str().unwrap_or("Untitled Course");
@@ -2992,6 +3004,8 @@ RULES:
StatusCode::INTERNAL_SERVER_ERROR StatusCode::INTERNAL_SERVER_ERROR
})?; })?;
tracing::info!("Course created with ID: {}", course.id);
// Create Modules and Lessons // Create Modules and Lessons
if let Some(modules) = result_json["modules"].as_array() { if let Some(modules) = result_json["modules"].as_array() {
for (m_idx, m_val) in modules.iter().enumerate() { for (m_idx, m_val) in modules.iter().enumerate() {
@@ -3008,7 +3022,12 @@ RULES:
.bind((m_idx + 1) as i32) .bind((m_idx + 1) as i32)
.fetch_one(&mut *tx) .fetch_one(&mut *tx)
.await .await
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?; .map_err(|e| {
tracing::error!("DB Module creation failed: {}", e);
StatusCode::INTERNAL_SERVER_ERROR
})?;
tracing::info!("Module created: {}", m_title);
if let Some(lessons) = m_val["lessons"].as_array() { if let Some(lessons) = m_val["lessons"].as_array() {
for (l_idx, l_val) in lessons.iter().enumerate() { for (l_idx, l_val) in lessons.iter().enumerate() {
@@ -3016,26 +3035,34 @@ RULES:
let l_type = l_val["content_type"].as_str().unwrap_or("text"); let l_type = l_val["content_type"].as_str().unwrap_or("text");
sqlx::query( sqlx::query(
"INSERT INTO lessons (module_id, course_id, organization_id, title, content_type, position) "INSERT INTO lessons (module_id, organization_id, title, content_type, position)
VALUES ($1, $2, $3, $4, $5, $6)" VALUES ($1, $2, $3, $4, $5)"
) )
.bind(module.id) .bind(module.id)
.bind(course.id)
.bind(target_org_id) .bind(target_org_id)
.bind(l_title) .bind(l_title)
.bind(l_type) .bind(l_type)
.bind((l_idx + 1) as i32) .bind((l_idx + 1) as i32)
.execute(&mut *tx) .execute(&mut *tx)
.await .await
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?; .map_err(|e| {
tracing::error!("DB Lesson creation failed: {}", e);
StatusCode::INTERNAL_SERVER_ERROR
})?;
tracing::info!("Lesson created: {}", l_title);
} }
} }
} }
} }
tx.commit() tracing::info!("Committing transaction...");
.await tx.commit().await.map_err(|e| {
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?; tracing::error!("Transaction commit failed: {}", e);
StatusCode::INTERNAL_SERVER_ERROR
})?;
tracing::info!("Transaction committed successfully");
log_action( log_action(
&pool, &pool,