diff --git a/.sqlx/query-a1451a96ea5c3c080d934a4f60d8d4f4f7f3ef907fa211f2952951fa5fac2637.json b/.sqlx/query-a1451a96ea5c3c080d934a4f60d8d4f4f7f3ef907fa211f2952951fa5fac2637.json new file mode 100644 index 0000000..9399be1 --- /dev/null +++ b/.sqlx/query-a1451a96ea5c3c080d934a4f60d8d4f4f7f3ef907fa211f2952951fa5fac2637.json @@ -0,0 +1,38 @@ +{ + "db_name": "PostgreSQL", + "query": "SELECT id, name FROM class_group ORDER BY name, id", + "describe": { + "columns": [ + { + "ordinal": 0, + "name": "id", + "type_info": "Uuid", + "origin": { + "Table": { + "table": "class_group", + "name": "id" + } + } + }, + { + "ordinal": 1, + "name": "name", + "type_info": "Text", + "origin": { + "Table": { + "table": "class_group", + "name": "name" + } + } + } + ], + "parameters": { + "Left": [] + }, + "nullable": [ + false, + false + ] + }, + "hash": "a1451a96ea5c3c080d934a4f60d8d4f4f7f3ef907fa211f2952951fa5fac2637" +} diff --git a/src/ai/insight.rs b/src/ai/insight.rs index cee1d1a..b2e50a0 100644 --- a/src/ai/insight.rs +++ b/src/ai/insight.rs @@ -220,6 +220,14 @@ pub struct ReportRequest { /// The school's own identity, so the document prints a display name /// rather than a slug. pub school: ReportSchool, + /// Every class (şube) of the school, id and display name: the summaries' + /// evidence carries class **ids**, and a document that groups by class + /// must print the name a reader recognizes. A sibling key rather than a + /// member of [`Self::school`]: it is a lookup for the rows, not part of + /// the school's identity. A class a row names but this list does not + /// carry (a deleted one) is absent on purpose — the renderer falls back + /// to an honest label, never a name invented here. + pub classes: Vec, /// Every stored summary (with its attention items) in the school. pub summaries: Vec, pub recommendations: Vec, @@ -358,9 +366,9 @@ fn malformed(err: serde_json::Error) -> AiError { // names exist, what each payload is, and how a refusal reads. pub use crate::db::insight::{ - AttentionRow, PendingList, ProfileRow, ProfileWriteRequest, PurgeRequest, RecommendationRow, - RecommendationWriteRequest, ReportSchool, RunRow, RunWriteRequest, SchoolDirectory, - SegmentConfidences, SegmentLabels, SegmentRow, SegmentWriteRequest, SummaryRow, + AttentionRow, ClassLabel, PendingList, ProfileRow, ProfileWriteRequest, PurgeRequest, + RecommendationRow, RecommendationWriteRequest, ReportSchool, RunRow, RunWriteRequest, + SchoolDirectory, SegmentConfidences, SegmentLabels, SegmentRow, SegmentWriteRequest, SummaryRow, SummaryWriteRequest, TableVerdicts, WriteReceipt, }; @@ -609,6 +617,10 @@ mod tests { slug: "demo".into(), name: "Demo Okulu".into(), }, + classes: vec![ClassLabel { + id: "class-1".into(), + name: "8-A".into(), + }], summaries: Vec::new(), recommendations: Vec::new(), profiles: Vec::new(), @@ -621,6 +633,7 @@ mod tests { "run_day": "2026-09-17", "requested_by": "user-9", "school": { "id": "school-1", "slug": "demo", "name": "Demo Okulu" }, + "classes": [{ "id": "class-1", "name": "8-A" }], "summaries": [], "recommendations": [], "profiles": [], diff --git a/src/db/insight.rs b/src/db/insight.rs index f453a59..2d6abdd 100644 --- a/src/db/insight.rs +++ b/src/db/insight.rs @@ -326,6 +326,16 @@ pub struct ReportSchool { pub name: String, } +/// One class (şube) as a rendered document labels it: the id the summaries' +/// own evidence carries, and the display name a reader should see in its +/// place. A document that prints the id instead is a machine identifier on +/// the most human-facing artifact the product ships. +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, ToSchema)] +pub struct ClassLabel { + pub id: String, + pub name: String, +} + // ---- shared validation ----------------------------------------------------- /// Refuse a batch past [`MAX_INSIGHT_BATCH_ROWS`] rather than taking it @@ -874,6 +884,27 @@ pub async fn school_identity( })) } +/// Every class (şube) of the school, id and display name, in name order. +/// +/// The service's summaries carry class ids inside their own evidence objects, +/// so a document that groups by class has only those ids to print — and an id +/// is not a label. This is the lookup that turns them into names. It is sent +/// whole rather than narrowed to the ids the current rows mention: a school's +/// class list is tens of rows, and a partial list would make the renderer's +/// fallback depend on how complete this read's filters were. +pub async fn all_classes(db: &Database) -> Result, AppError> { + let rows = sqlx::query!("SELECT id, name FROM class_group ORDER BY name, id") + .fetch_all(db) + .await?; + Ok(rows + .into_iter() + .map(|row| ClassLabel { + id: row.id.to_string(), + name: row.name, + }) + .collect()) +} + // ---- the school-wide reads the report payload carries ----------------------- // // Every read above is per-student or per-audience, because that is every diff --git a/src/web/insights.rs b/src/web/insights.rs index cb60c2c..0bed2d4 100644 --- a/src/web/insights.rs +++ b/src/web/insights.rs @@ -413,6 +413,7 @@ async fn generate_report( run_day: run_day.clone(), requested_by: user.get_id().key(), school, + classes: db::insight::all_classes(&st.db).await?, summaries: db::insight::all_summaries(&st.db).await?, recommendations: db::insight::all_recommendations(&st.db).await?, profiles: db::insight::all_profiles(&st.db).await?, diff --git a/tests/insight_report.rs b/tests/insight_report.rs index 0189b53..9b13eda 100644 --- a/tests/insight_report.rs +++ b/tests/insight_report.rs @@ -429,6 +429,7 @@ async fn a_generated_report_is_stored_and_served_to_its_manager() { let manager_id = common::me_id(&app, &manager).await; let student = common::login(&app, "ali").await; let student_id = common::me_id(&app, &student).await; + let class_id = common::create_class(&app, &manager, "8-A", json!({})).await; seed_report_rows(&db, "2026-09-16", &student_id, &manager_id).await; let res = generate(&app, &manager, "2026-09-16").await; @@ -463,6 +464,16 @@ async fn a_generated_report_is_stored_and_served_to_its_manager() { .is_some_and(|name| !name.is_empty()), "the school's display name travels" ); + // The class (şube) lookup: the summaries' evidence carries class ids, and + // the document must print the name a reader recognizes. + let classes = request.payload["classes"] + .as_array() + .expect("classes is a list"); + let seeded = classes + .iter() + .find(|row| row["id"] == class_id.as_str()) + .expect("the school's own class is in the payload"); + assert_eq!(seeded["name"], "8-A"); let summaries = request.payload["summaries"] .as_array() .expect("summaries is a list");