| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | |
| 31 | |
| 32 | |
| 33 | |
| 34 | use std::collections::{HashMap, HashSet}; |
| 35 | |
| 36 | use crate::ai::rag_chat::RagScopePair; |
| 37 | use crate::constant::MAX_RAG_SCOPE_PAIRS; |
| 38 | use crate::database::Database; |
| 39 | use crate::domain::class_course::ClassCourse; |
| 40 | use crate::domain::class_group::ClassGroupId; |
| 41 | use crate::domain::course::CourseId; |
| 42 | use crate::domain::role::Role; |
| 43 | use crate::domain::user::User; |
| 44 | use crate::error::{AppError, ValidationError}; |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | pub async fn for_user( |
| 63 | db: &Database, |
| 64 | user: &User, |
| 65 | role: Role, |
| 66 | ) -> Result<Vec<RagScopePair>, AppError> { |
| 67 | let mut instances: Vec<ClassCourse> = Vec::new(); |
| 68 | if role.at_least(Role::Manager) { |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | let (classes, _) = crate::db::class_group::list_all(db, None, None, 0).await?; |
| 74 | let class_ids: Vec<ClassGroupId> = |
| 75 | classes.iter().map(|class| class.get_id().clone()).collect(); |
| 76 | instances = crate::db::class_course::list_for_class_ids(db, &class_ids).await?; |
| 77 | } else if role == Role::Parent { |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | for link in crate::db::parent_link::list_for_parent(db, user.get_id()).await? { |
| 85 | let Some(child) = super::user::read(db, link.get_student()).await? else { |
| 86 | continue; |
| 87 | }; |
| 88 | instances.extend( |
| 89 | super::instance::visible_instances(&child, db) |
| 90 | .await? |
| 91 | .into_iter() |
| 92 | .map(|(instance, _)| instance), |
| 93 | ); |
| 94 | } |
| 95 | } else { |
| 96 | instances.extend( |
| 97 | super::instance::visible_instances(user, db) |
| 98 | .await? |
| 99 | .into_iter() |
| 100 | .map(|(instance, _)| instance), |
| 101 | ); |
| 102 | } |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | let memberships = if role.at_least(Role::Manager) { |
| 108 | Vec::new() |
| 109 | } else { |
| 110 | crate::db::course_membership::list_for_user(db, user.get_id(), None, 0) |
| 111 | .await? |
| 112 | .0 |
| 113 | }; |
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | let class_ids: Vec<ClassGroupId> = instances |
| 120 | .iter() |
| 121 | .map(|instance| instance.get_class().clone()) |
| 122 | .collect(); |
| 123 | let mut course_ids: Vec<CourseId> = instances |
| 124 | .iter() |
| 125 | .map(|instance| instance.get_course().clone()) |
| 126 | .collect(); |
| 127 | course_ids.extend( |
| 128 | memberships |
| 129 | .iter() |
| 130 | .map(|membership| membership.get_course().clone()), |
| 131 | ); |
| 132 | let classes = crate::db::class_group::list_by_ids(db, &class_ids).await?; |
| 133 | let courses = crate::db::course::list_by_ids(db, &course_ids).await?; |
| 134 | |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | let grades: HashMap<String, String> = classes |
| 140 | .iter() |
| 141 | .map(|class| (class.get_id().key(), class.get_grade_level().label().to_owned())) |
| 142 | .collect(); |
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | |
| 148 | let instance_refs: Vec<&ClassCourse> = instances.iter().collect(); |
| 149 | let resolved = crate::service::instance_resolve::resolved_content(db, &instance_refs).await?; |
| 150 | let ders_by_instance: HashMap<String, String> = resolved |
| 151 | .into_iter() |
| 152 | .map(|(key, content)| (key, content.title)) |
| 153 | .collect(); |
| 154 | let titles: HashMap<String, String> = courses |
| 155 | .iter() |
| 156 | .map(|course| (course.get_id().key(), course.get_title().as_str().to_owned())) |
| 157 | .collect(); |
| 158 | |
| 159 | let mut pairs: Vec<RagScopePair> = Vec::new(); |
| 160 | let mut seen: HashSet<(Option<String>, String)> = HashSet::new(); |
| 161 | for instance in &instances { |
| 162 | |
| 163 | |
| 164 | |
| 165 | let Some(ders) = ders_by_instance.get(instance.get_id().key().as_str()) else { |
| 166 | continue; |
| 167 | }; |
| 168 | push_pair( |
| 169 | &mut pairs, |
| 170 | &mut seen, |
| 171 | grades.get(&instance.get_class().key()).cloned(), |
| 172 | ders.clone(), |
| 173 | ); |
| 174 | } |
| 175 | for membership in &memberships { |
| 176 | let Some(ders) = titles.get(&membership.get_course().key()) else { |
| 177 | continue; |
| 178 | }; |
| 179 | push_pair(&mut pairs, &mut seen, None, ders.clone()); |
| 180 | } |
| 181 | |
| 182 | |
| 183 | |
| 184 | if pairs.len() > MAX_RAG_SCOPE_PAIRS { |
| 185 | return Err(AppError::Validation(ValidationError::TooLong { |
| 186 | field: "scope", |
| 187 | max: MAX_RAG_SCOPE_PAIRS, |
| 188 | got: pairs.len(), |
| 189 | })); |
| 190 | } |
| 191 | Ok(pairs) |
| 192 | } |
| 193 | |
| 194 | |
| 195 | |
| 196 | |
| 197 | |
| 198 | |
| 199 | fn push_pair( |
| 200 | pairs: &mut Vec<RagScopePair>, |
| 201 | seen: &mut HashSet<(Option<String>, String)>, |
| 202 | sinif: Option<String>, |
| 203 | ders: String, |
| 204 | ) { |
| 205 | if seen.insert((sinif.clone(), ders.clone())) { |
| 206 | pairs.push(RagScopePair { sinif, ders }); |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | #[cfg(test)] |
| 211 | mod tests { |
| 212 | use super::*; |
| 213 | use crate::domain::class_group::{ClassGroupId, ClassName}; |
| 214 | use crate::domain::grade::GradeLevel; |
| 215 | use crate::domain::course::{CourseDescription, CourseKind, CourseTitle}; |
| 216 | use crate::domain::user::Username; |
| 217 | |
| 218 | |
| 219 | fn pair(sinif: Option<&str>, ders: &str) -> RagScopePair { |
| 220 | RagScopePair { |
| 221 | sinif: sinif.map(str::to_owned), |
| 222 | ders: ders.to_owned(), |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | |
| 227 | |
| 228 | |
| 229 | async fn person(db: &Database, username: &str, role: Role) -> User { |
| 230 | crate::db::user::create_with_role( |
| 231 | db, |
| 232 | Username::try_new(username).unwrap(), |
| 233 | None, |
| 234 | role, |
| 235 | None, |
| 236 | ) |
| 237 | .await |
| 238 | .unwrap() |
| 239 | } |
| 240 | |
| 241 | |
| 242 | |
| 243 | async fn graded_class(db: &Database, name: &str, grade: &str) -> ClassGroupId { |
| 244 | let office = crate::db::class_member::tests::fixture_user(db, "ragscope-office").await; |
| 245 | crate::service::class_group::create( |
| 246 | db, |
| 247 | &office, |
| 248 | ClassName::try_new(name).unwrap(), |
| 249 | GradeLevel::new(grade.parse().unwrap()).unwrap(), |
| 250 | None, |
| 251 | None, |
| 252 | ) |
| 253 | .await |
| 254 | .unwrap() |
| 255 | .get_id() |
| 256 | .clone() |
| 257 | } |
| 258 | |
| 259 | |
| 260 | async fn attach( |
| 261 | db: &Database, |
| 262 | class: &ClassGroupId, |
| 263 | title: &str, |
| 264 | ) -> crate::domain::class_course::ClassCourse { |
| 265 | let office = crate::db::class_member::tests::fixture_user(db, "ragscope-office").await; |
| 266 | let course = crate::db::class_member::tests::a_course(title, db).await; |
| 267 | crate::service::class_course::attach(db, class, &course, &office) |
| 268 | .await |
| 269 | .unwrap() |
| 270 | } |
| 271 | |
| 272 | |
| 273 | |
| 274 | async fn enrolment(db: &Database, class: &ClassGroupId, student: &User) { |
| 275 | let office = crate::db::class_member::tests::fixture_user(db, "ragscope-office").await; |
| 276 | crate::service::class_member::add(db, class, student.get_id(), &office) |
| 277 | .await |
| 278 | .unwrap(); |
| 279 | } |
| 280 | |
| 281 | |
| 282 | |
| 283 | #[tokio::test] |
| 284 | async fn a_students_sections_scope_their_subjects() { |
| 285 | let (db, _leases) = crate::database::init_test_db().await; |
| 286 | let student = person(&db, "ragscope-student", Role::Student).await; |
| 287 | let class = graded_class(&db, "5-A", "5").await; |
| 288 | attach(&db, &class, "Matematik").await; |
| 289 | enrolment(&db, &class, &student).await; |
| 290 | |
| 291 | let scope = for_user(&db, &student, Role::Student).await.unwrap(); |
| 292 | assert_eq!(scope, vec![pair(Some("5"), "Matematik")]); |
| 293 | } |
| 294 | |
| 295 | |
| 296 | |
| 297 | |
| 298 | #[tokio::test] |
| 299 | async fn an_assigned_teachers_subject_is_in_scope() { |
| 300 | let (db, _leases) = crate::database::init_test_db().await; |
| 301 | let teacher = person(&db, "ragscope-teacher", Role::Teacher).await; |
| 302 | let class = graded_class(&db, "9-A", "9").await; |
| 303 | let instance = attach(&db, &class, "Fizik").await; |
| 304 | |
| 305 | |
| 306 | crate::db::class_course_teacher::assign(&db, instance.get_id(), teacher.get_id()) |
| 307 | .await |
| 308 | .unwrap(); |
| 309 | |
| 310 | let scope = for_user(&db, &teacher, Role::Teacher).await.unwrap(); |
| 311 | assert_eq!(scope, vec![pair(Some("9"), "Fizik")]); |
| 312 | } |
| 313 | |
| 314 | |
| 315 | |
| 316 | |
| 317 | #[tokio::test] |
| 318 | async fn a_parents_scope_is_their_linked_childs() { |
| 319 | let (db, _leases) = crate::database::init_test_db().await; |
| 320 | let parent = person(&db, "ragscope-parent", Role::Parent).await; |
| 321 | let child = person(&db, "ragscope-child", Role::Student).await; |
| 322 | let stranger = person(&db, "ragscope-stranger", Role::Student).await; |
| 323 | let child_class = graded_class(&db, "7-A", "7").await; |
| 324 | attach(&db, &child_class, "Türkçe").await; |
| 325 | enrolment(&db, &child_class, &child).await; |
| 326 | let stranger_class = graded_class(&db, "7-B", "7").await; |
| 327 | attach(&db, &stranger_class, "Türkçe").await; |
| 328 | enrolment(&db, &stranger_class, &stranger).await; |
| 329 | let office = crate::db::class_member::tests::fixture_user(&db, "ragscope-office").await; |
| 330 | crate::service::parent_link::link(&db, parent.get_id(), child.get_id(), &office) |
| 331 | .await |
| 332 | .unwrap(); |
| 333 | |
| 334 | let scope = for_user(&db, &parent, Role::Parent).await.unwrap(); |
| 335 | assert_eq!( |
| 336 | scope, |
| 337 | vec![pair(Some("7"), "Türkçe")], |
| 338 | "the linked child's section, and only that one" |
| 339 | ); |
| 340 | } |
| 341 | |
| 342 | |
| 343 | |
| 344 | #[tokio::test] |
| 345 | async fn a_club_membership_scopes_a_subject_with_no_grade() { |
| 346 | let (db, _leases) = crate::database::init_test_db().await; |
| 347 | let student = person(&db, "ragscope-club", Role::Student).await; |
| 348 | let office = crate::db::class_member::tests::fixture_user(&db, "ragscope-office").await; |
| 349 | let club = crate::db::course::create( |
| 350 | &db, |
| 351 | &office, |
| 352 | CourseTitle::try_new("Satranç").unwrap(), |
| 353 | CourseDescription::try_new("").unwrap(), |
| 354 | CourseKind::try_new("club").unwrap(), |
| 355 | ) |
| 356 | .await |
| 357 | .unwrap(); |
| 358 | crate::db::course_membership::add(&db, club.get_id(), student.get_id(), &office) |
| 359 | .await |
| 360 | .unwrap(); |
| 361 | |
| 362 | let scope = for_user(&db, &student, Role::Student).await.unwrap(); |
| 363 | assert_eq!(scope, vec![pair(None, "Satranç")]); |
| 364 | } |
| 365 | |
| 366 | |
| 367 | |
| 368 | |
| 369 | #[tokio::test] |
| 370 | async fn a_pair_reached_twice_rides_once() { |
| 371 | let (db, _leases) = crate::database::init_test_db().await; |
| 372 | let student = person(&db, "ragscope-twice", Role::Student).await; |
| 373 | let a = graded_class(&db, "6-A", "6").await; |
| 374 | let b = graded_class(&db, "6-B", "6").await; |
| 375 | attach(&db, &a, "Matematik").await; |
| 376 | attach(&db, &b, "Matematik").await; |
| 377 | enrolment(&db, &a, &student).await; |
| 378 | enrolment(&db, &b, &student).await; |
| 379 | |
| 380 | |
| 381 | assert_eq!( |
| 382 | crate::service::instance::visible_instances(&student, &db) |
| 383 | .await |
| 384 | .unwrap() |
| 385 | .len(), |
| 386 | 2 |
| 387 | ); |
| 388 | let scope = for_user(&db, &student, Role::Student).await.unwrap(); |
| 389 | assert_eq!(scope, vec![pair(Some("6"), "Matematik")]); |
| 390 | } |
| 391 | |
| 392 | |
| 393 | |
| 394 | |
| 395 | |
| 396 | |
| 397 | #[tokio::test] |
| 398 | async fn a_manager_with_no_membership_scopes_the_whole_school() { |
| 399 | let (db, _leases) = crate::database::init_test_db().await; |
| 400 | let manager = person(&db, "ragscope-manager", Role::Manager).await; |
| 401 | let admin = person(&db, "ragscope-admin", Role::Admin).await; |
| 402 | let student = person(&db, "ragscope-empty-student", Role::Student).await; |
| 403 | let teacher = person(&db, "ragscope-empty-teacher", Role::Teacher).await; |
| 404 | let grade_eight_a = graded_class(&db, "8-A", "8").await; |
| 405 | let grade_eight_b = graded_class(&db, "8-B", "8").await; |
| 406 | let grade_nine = graded_class(&db, "9-A", "9").await; |
| 407 | attach(&db, &grade_eight_a, "Biology").await; |
| 408 | attach(&db, &grade_eight_b, "Biology").await; |
| 409 | attach(&db, &grade_nine, "Chemistry").await; |
| 410 | |
| 411 | let expected = vec![ |
| 412 | (Some("8".to_owned()), "Biology".to_owned()), |
| 413 | (Some("9".to_owned()), "Chemistry".to_owned()), |
| 414 | ]; |
| 415 | let manager_scope = for_user(&db, &manager, Role::Manager).await.unwrap(); |
| 416 | let admin_scope = for_user(&db, &admin, Role::Admin).await.unwrap(); |
| 417 | assert_eq!(pair_keys(&manager_scope), expected); |
| 418 | assert_eq!(pair_keys(&admin_scope), expected); |
| 419 | assert_eq!( |
| 420 | manager_scope.len(), |
| 421 | 2, |
| 422 | "the repeated grade-8 subject is one pair" |
| 423 | ); |
| 424 | assert!( |
| 425 | for_user(&db, &student, Role::Student) |
| 426 | .await |
| 427 | .unwrap() |
| 428 | .is_empty(), |
| 429 | "a student with no membership still has an empty scope" |
| 430 | ); |
| 431 | assert!( |
| 432 | for_user(&db, &teacher, Role::Teacher) |
| 433 | .await |
| 434 | .unwrap() |
| 435 | .is_empty(), |
| 436 | "a teacher with no assignment still has an empty scope" |
| 437 | ); |
| 438 | } |
| 439 | |
| 440 | |
| 441 | fn pair_keys(pairs: &[RagScopePair]) -> Vec<(Option<String>, String)> { |
| 442 | let mut keys: Vec<_> = pairs |
| 443 | .iter() |
| 444 | .map(|pair| (pair.sinif.clone(), pair.ders.clone())) |
| 445 | .collect(); |
| 446 | keys.sort(); |
| 447 | keys |
| 448 | } |
| 449 | } |