Code-review findings on the holiday and section surfaces
Browse files at this commitecd544bf75825506a8f853f6b5def1b2d99b3262Parent: aa7132a- fix
- code-review findings on the holiday and section surfaces
10 changed files +455 -37
- .sqlx/query-b6c48120a6799eec2c06f2c35332af7f0abaaba4dd55a284c89ab1351113220e.json +191 -0
- src/db/class_course.rs +35 -0
- src/db/course_session.rs +14 -2
- src/domain/calendar.rs +6 -0
- src/service/instance.rs +15 -16
- src/web/users.rs +28 -9
- src/web/weekly_plan.rs +19 -3
- tests/limits_completeness.rs +6 -0
- tests/regress_profile.rs +66 -5
- tests/regress_schedule.rs +75 -2
.sqlx/query-b6c48120a6799eec2c06f2c35332af7f0abaaba4dd55a284c89ab1351113220e.jsonadded+191 -0
| @@ -0,0 +1,191 @@ | ||
| 1 | +{ | |
| 2 | + "db_name": "PostgreSQL", | |
| 3 | + "query": "SELECT cc.id AS \"id: ClassCourseId\", cc.class AS \"class: ClassGroupId\",\n cc.course AS \"course: CourseId\", cc.offering AS \"offering: CourseOfferingId\",\n cc.attached_by AS \"attached_by: UserId\",\n b.grade_level AS \"source?: ClassBlueprintId\",\n cc.title AS \"title?: CourseTitle\",\n cc.description AS \"description?: CourseDescription\",\n cc.ders_saati::bigint AS \"ders_saati?: DersSaati\",\n cc.counts_toward_karne,\n cc.subjects_inherited, cc.exam_weights_inherited, cc.weekly_plan_inherited,\n cc.enrollment_count,\n cc.attached_at AS \"attached_at: Timestamp\"\n FROM class_course cc\n JOIN class_course_teacher t ON t.class_course = cc.id\n LEFT JOIN class_blueprint b ON b.id = cc.source\n WHERE t.teacher = $1\n ORDER BY cc.attached_at DESC, cc.id DESC", | |
| 4 | + "describe": { | |
| 5 | + "columns": [ | |
| 6 | + { | |
| 7 | + "ordinal": 0, | |
| 8 | + "name": "id: ClassCourseId", | |
| 9 | + "type_info": "Uuid", | |
| 10 | + "origin": { | |
| 11 | + "Table": { | |
| 12 | + "table": "class_course", | |
| 13 | + "name": "id" | |
| 14 | + } | |
| 15 | + } | |
| 16 | + }, | |
| 17 | + { | |
| 18 | + "ordinal": 1, | |
| 19 | + "name": "class: ClassGroupId", | |
| 20 | + "type_info": "Uuid", | |
| 21 | + "origin": { | |
| 22 | + "Table": { | |
| 23 | + "table": "class_course", | |
| 24 | + "name": "class" | |
| 25 | + } | |
| 26 | + } | |
| 27 | + }, | |
| 28 | + { | |
| 29 | + "ordinal": 2, | |
| 30 | + "name": "course: CourseId", | |
| 31 | + "type_info": "Uuid", | |
| 32 | + "origin": { | |
| 33 | + "Table": { | |
| 34 | + "table": "class_course", | |
| 35 | + "name": "course" | |
| 36 | + } | |
| 37 | + } | |
| 38 | + }, | |
| 39 | + { | |
| 40 | + "ordinal": 3, | |
| 41 | + "name": "offering: CourseOfferingId", | |
| 42 | + "type_info": "Uuid", | |
| 43 | + "origin": { | |
| 44 | + "Table": { | |
| 45 | + "table": "class_course", | |
| 46 | + "name": "offering" | |
| 47 | + } | |
| 48 | + } | |
| 49 | + }, | |
| 50 | + { | |
| 51 | + "ordinal": 4, | |
| 52 | + "name": "attached_by: UserId", | |
| 53 | + "type_info": "Uuid", | |
| 54 | + "origin": { | |
| 55 | + "Table": { | |
| 56 | + "table": "class_course", | |
| 57 | + "name": "attached_by" | |
| 58 | + } | |
| 59 | + } | |
| 60 | + }, | |
| 61 | + { | |
| 62 | + "ordinal": 5, | |
| 63 | + "name": "source?: ClassBlueprintId", | |
| 64 | + "type_info": "Int2", | |
| 65 | + "origin": { | |
| 66 | + "Table": { | |
| 67 | + "table": "class_blueprint", | |
| 68 | + "name": "grade_level" | |
| 69 | + } | |
| 70 | + } | |
| 71 | + }, | |
| 72 | + { | |
| 73 | + "ordinal": 6, | |
| 74 | + "name": "title?: CourseTitle", | |
| 75 | + "type_info": "Text", | |
| 76 | + "origin": { | |
| 77 | + "Table": { | |
| 78 | + "table": "class_course", | |
| 79 | + "name": "title" | |
| 80 | + } | |
| 81 | + } | |
| 82 | + }, | |
| 83 | + { | |
| 84 | + "ordinal": 7, | |
| 85 | + "name": "description?: CourseDescription", | |
| 86 | + "type_info": "Text", | |
| 87 | + "origin": { | |
| 88 | + "Table": { | |
| 89 | + "table": "class_course", | |
| 90 | + "name": "description" | |
| 91 | + } | |
| 92 | + } | |
| 93 | + }, | |
| 94 | + { | |
| 95 | + "ordinal": 8, | |
| 96 | + "name": "ders_saati?: DersSaati", | |
| 97 | + "type_info": "Int8", | |
| 98 | + "origin": "Expression" | |
| 99 | + }, | |
| 100 | + { | |
| 101 | + "ordinal": 9, | |
| 102 | + "name": "counts_toward_karne", | |
| 103 | + "type_info": "Bool", | |
| 104 | + "origin": { | |
| 105 | + "Table": { | |
| 106 | + "table": "class_course", | |
| 107 | + "name": "counts_toward_karne" | |
| 108 | + } | |
| 109 | + } | |
| 110 | + }, | |
| 111 | + { | |
| 112 | + "ordinal": 10, | |
| 113 | + "name": "subjects_inherited", | |
| 114 | + "type_info": "Bool", | |
| 115 | + "origin": { | |
| 116 | + "Table": { | |
| 117 | + "table": "class_course", | |
| 118 | + "name": "subjects_inherited" | |
| 119 | + } | |
| 120 | + } | |
| 121 | + }, | |
| 122 | + { | |
| 123 | + "ordinal": 11, | |
| 124 | + "name": "exam_weights_inherited", | |
| 125 | + "type_info": "Bool", | |
| 126 | + "origin": { | |
| 127 | + "Table": { | |
| 128 | + "table": "class_course", | |
| 129 | + "name": "exam_weights_inherited" | |
| 130 | + } | |
| 131 | + } | |
| 132 | + }, | |
| 133 | + { | |
| 134 | + "ordinal": 12, | |
| 135 | + "name": "weekly_plan_inherited", | |
| 136 | + "type_info": "Bool", | |
| 137 | + "origin": { | |
| 138 | + "Table": { | |
| 139 | + "table": "class_course", | |
| 140 | + "name": "weekly_plan_inherited" | |
| 141 | + } | |
| 142 | + } | |
| 143 | + }, | |
| 144 | + { | |
| 145 | + "ordinal": 13, | |
| 146 | + "name": "enrollment_count", | |
| 147 | + "type_info": "Int8", | |
| 148 | + "origin": { | |
| 149 | + "Table": { | |
| 150 | + "table": "class_course", | |
| 151 | + "name": "enrollment_count" | |
| 152 | + } | |
| 153 | + } | |
| 154 | + }, | |
| 155 | + { | |
| 156 | + "ordinal": 14, | |
| 157 | + "name": "attached_at: Timestamp", | |
| 158 | + "type_info": "Int8", | |
| 159 | + "origin": { | |
| 160 | + "Table": { | |
| 161 | + "table": "class_course", | |
| 162 | + "name": "attached_at" | |
| 163 | + } | |
| 164 | + } | |
| 165 | + } | |
| 166 | + ], | |
| 167 | + "parameters": { | |
| 168 | + "Left": [ | |
| 169 | + "Uuid" | |
| 170 | + ] | |
| 171 | + }, | |
| 172 | + "nullable": [ | |
| 173 | + false, | |
| 174 | + false, | |
| 175 | + false, | |
| 176 | + false, | |
| 177 | + false, | |
| 178 | + false, | |
| 179 | + true, | |
| 180 | + true, | |
| 181 | + null, | |
| 182 | + true, | |
| 183 | + false, | |
| 184 | + false, | |
| 185 | + false, | |
| 186 | + false, | |
| 187 | + false | |
| 188 | + ] | |
| 189 | + }, | |
| 190 | + "hash": "b6c48120a6799eec2c06f2c35332af7f0abaaba4dd55a284c89ab1351113220e" | |
| 191 | +} |
src/db/class_course.rsmodified+35 -0
| @@ -181,6 +181,41 @@ pub async fn list_for_course_ids( | ||
| 181 | 181 | Ok(rows) |
| 182 | 182 | } |
| 183 | 183 | |
| 184 | +/// Every instance `teacher` is assigned to (one query, unpaged) — the batched | |
| 185 | +/// replacement for [`list_for_teacher`]-then-[`list_for_course`]-per-course: | |
| 186 | +/// a reach read walks the assignment junction directly, so a teacher's | |
| 187 | +/// catalogue page costs one query, not one per taught course. A teacher | |
| 188 | +/// assigned to two sections of one course gets both rows; an id that names no | |
| 189 | +/// assignment is simply absent. | |
| 190 | +pub async fn list_for_teacher_ids( | |
| 191 | + db: &Database, | |
| 192 | + teacher: &UserId, | |
| 193 | +) -> Result<Vec<ClassCourse>, AppError> { | |
| 194 | + let rows = sqlx::query_as!( | |
| 195 | + ClassCourse, | |
| 196 | + r#"SELECT cc.id AS "id: ClassCourseId", cc.class AS "class: ClassGroupId", | |
| 197 | + cc.course AS "course: CourseId", cc.offering AS "offering: CourseOfferingId", | |
| 198 | + cc.attached_by AS "attached_by: UserId", | |
| 199 | + b.grade_level AS "source?: ClassBlueprintId", | |
| 200 | + cc.title AS "title?: CourseTitle", | |
| 201 | + cc.description AS "description?: CourseDescription", | |
| 202 | + cc.ders_saati::bigint AS "ders_saati?: DersSaati", | |
| 203 | + cc.counts_toward_karne, | |
| 204 | + cc.subjects_inherited, cc.exam_weights_inherited, cc.weekly_plan_inherited, | |
| 205 | + cc.enrollment_count, | |
| 206 | + cc.attached_at AS "attached_at: Timestamp" | |
| 207 | + FROM class_course cc | |
| 208 | + JOIN class_course_teacher t ON t.class_course = cc.id | |
| 209 | + LEFT JOIN class_blueprint b ON b.id = cc.source | |
| 210 | + WHERE t.teacher = $1 | |
| 211 | + ORDER BY cc.attached_at DESC, cc.id DESC"#, | |
| 212 | + teacher.uuid(), | |
| 213 | + ) | |
| 214 | + .fetch_all(db) | |
| 215 | + .await?; | |
| 216 | + Ok(rows) | |
| 217 | +} | |
| 218 | + | |
| 184 | 219 | /// Load every instance behind `ids` (one query) — the join half of the reads |
| 185 | 220 | /// that already hold instance ids (a marks report's per-instance blocks). |
| 186 | 221 | pub async fn list_by_ids( |
src/db/course_session.rsmodified+14 -2
| @@ -203,7 +203,7 @@ pub async fn update( | ||
| 203 | 203 | starts_at: Option<Timestamp>, |
| 204 | 204 | ends_at: Option<Option<Timestamp>>, |
| 205 | 205 | ) -> Result<CourseSession, AppError> { |
| 206 | - FieldUpdate::new(COURSE_SESSION_TABLE, session.id.uuid()) | |
| 206 | + let updated = FieldUpdate::new(COURSE_SESSION_TABLE, session.id.uuid()) | |
| 207 | 207 | .set( |
| 208 | 208 | "teacher", |
| 209 | 209 | teacher.map(|teacher| crate::db::page::Param::Uuid(teacher.uuid())), |
| @@ -216,7 +216,19 @@ pub async fn update( | ||
| 216 | 216 | ) |
| 217 | 217 | .ordered("starts_at", "ends_at", range_error()) |
| 218 | 218 | .run::<CourseSession>(db) |
| 219 | - .await | |
| 219 | + .await; | |
| 220 | + match updated { | |
| 221 | + Ok(updated) => Ok(updated), | |
| 222 | + // `23505` — the section's own `UNIQUE (class_course, starts_at)`: the | |
| 223 | + // PATCH moved this lesson onto an instant the section already holds | |
| 224 | + // (a hand-created one, or one a concurrent materialize just minted). | |
| 225 | + // The same coded refusal `create` answers with, never a raw 500. | |
| 226 | + Err(AppError::Db(err)) if unique_violation(&err).is_some() => Err(AppError::ConflictCoded { | |
| 227 | + code: "session_time_taken", | |
| 228 | + message: "this section already has a lesson starting then".into(), | |
| 229 | + }), | |
| 230 | + Err(err) => Err(err), | |
| 231 | + } | |
| 220 | 232 | } |
| 221 | 233 | |
| 222 | 234 | /// Delete the session and cascade-remove its roll-call rows, in one |
src/domain/calendar.rsmodified+6 -0
| @@ -24,6 +24,12 @@ pub fn zone_offset_minutes(timezone: &str) -> i32 { | ||
| 24 | 24 | } |
| 25 | 25 | } |
| 26 | 26 | |
| 27 | +/// The largest shift [`zoned_day`] can ever apply: the biggest offset any | |
| 28 | +/// entry of [`crate::constant::TIMEZONES`] produces. A caller that must not | |
| 29 | +/// panic (the materialize route's raw `i64` instants) uses it for a checked | |
| 30 | +/// pre-flight that mirrors the unchecked arithmetic below. | |
| 31 | +pub const MAX_ZONE_OFFSET_MINUTES: i32 = 3 * 60; | |
| 32 | + | |
| 27 | 33 | /// The calendar day an instant falls on, in the school's zone. |
| 28 | 34 | pub fn zoned_day(millis: i64, offset_minutes: i32) -> chrono::NaiveDate { |
| 29 | 35 | let shifted = millis + i64::from(offset_minutes) * 60_000; |
src/service/instance.rsmodified+15 -16
| @@ -91,24 +91,23 @@ pub async fn visible_instances( | ||
| 91 | 91 | let manages = homeroom_keys.contains(&instance.get_class().key()); |
| 92 | 92 | rows.push((instance, manages)); |
| 93 | 93 | } |
| 94 | - // The instances the caller teaches: read through the catalog courses they | |
| 95 | - // are assigned in (the junction has no teacher-keyed read of its own) and | |
| 96 | - // kept only where the assignment actually names them. | |
| 94 | + // The instances the caller teaches: one statement over the assignment | |
| 95 | + // junction (`class_course` join `class_course_teacher` filtered by the | |
| 96 | + // teacher), not a catalog read plus one query per taught course plus one | |
| 97 | + // gate call per instance. `manages` keeps today's meaning — an assigned | |
| 98 | + // teacher may run the section (D10's teacher arm; the manager+ arm is | |
| 99 | + // unreachable here, a manager's `visible` is served by the class arm | |
| 100 | + // above or read manager-side) — and a row the class arm already carried | |
| 101 | + // (homeroom ∩ assigned) is dropped, the union is a set. | |
| 97 | 102 | if user.get_role().at_least(Role::Teacher) { |
| 98 | - for course in super::course::list_for_teacher(db, user.get_id()).await? { | |
| 99 | - let (taught, _) = | |
| 100 | - crate::db::class_course::list_for_course(db, course.get_id(), None, 0).await?; | |
| 101 | - for instance in taught { | |
| 102 | - if rows | |
| 103 | - .iter() | |
| 104 | - .any(|(row, _)| row.get_id() == instance.get_id()) | |
| 105 | - { | |
| 106 | - continue; | |
| 107 | - } | |
| 108 | - if can_manage_instance(db, instance.get_id(), user).await? { | |
| 109 | - rows.push((instance, true)); | |
| 110 | - } | |
| 103 | + for instance in crate::db::class_course::list_for_teacher_ids(db, user.get_id()).await? { | |
| 104 | + if rows | |
| 105 | + .iter() | |
| 106 | + .any(|(row, _)| row.get_id() == instance.get_id()) | |
| 107 | + { | |
| 108 | + continue; | |
| 111 | 109 | } |
| 110 | + rows.push((instance, true)); | |
| 112 | 111 | } |
| 113 | 112 | } |
| 114 | 113 | Ok(rows) |
src/web/users.rsmodified+28 -9
| @@ -1020,6 +1020,16 @@ struct ProfileClassRef { | ||
| 1020 | 1020 | /// carry their own resolved titles and hours, and the profile lists both. `id` |
| 1021 | 1021 | /// is the instance id (`GET /instances/{id}`); `kind` is the catalog row's, |
| 1022 | 1022 | /// which is the same for every section of one course. |
| 1023 | +/// | |
| 1024 | +/// The class identity rides the profile's **class gate**, not the course one: | |
| 1025 | +/// `class`, `class_name` and `grade_level` name the section's şube, and a | |
| 1026 | +/// class name and grade are exactly what `GET /classes/user/{id}` withholds | |
| 1027 | +/// from a viewer below the [`crate::service::parent_link::ensure_can_observe`] | |
| 1028 | +/// bar — so this block withholds them from exactly the same viewers (they | |
| 1029 | +/// read `null`), the same all-or-nothing rule the `classes` block answers to. | |
| 1030 | +/// A viewer who passes — self, teacher+, a linked parent — reads all three. | |
| 1031 | +/// `id`, `course`, `title` and `kind` stay populated for everyone the course | |
| 1032 | +/// gate lets through: the block's purpose is the section, not the class. | |
| 1023 | 1033 | #[derive(Serialize, ToSchema)] |
| 1024 | 1034 | struct ProfileCourseRef { |
| 1025 | 1035 | /// The instance id (`GET /instances/{id}`). |
| @@ -1028,19 +1038,22 @@ struct ProfileCourseRef { | ||
| 1028 | 1038 | /// The catalog course this section teaches (`GET /courses/{id}`). |
| 1029 | 1039 | #[schema(example = "019732e3-7b00-7000-8000-00000000beef")] |
| 1030 | 1040 | course: String, |
| 1031 | - /// The class this section belongs to (`GET /classes/{id}`). | |
| 1041 | + /// The class this section belongs to (`GET /classes/{id}`) — `null` for a | |
| 1042 | + /// viewer below the class-observation bar. | |
| 1032 | 1043 | #[schema(example = "019732e3-7b00-7000-8000-00000000cafe")] |
| 1033 | - class: String, | |
| 1034 | - /// The class's own name, e.g. `9-A`. | |
| 1044 | + class: Option<String>, | |
| 1045 | + /// The class's own name, e.g. `9-A` — `null` for a viewer below the | |
| 1046 | + /// class-observation bar. | |
| 1035 | 1047 | #[schema(example = "9-A")] |
| 1036 | - class_name: String, | |
| 1048 | + class_name: Option<String>, | |
| 1037 | 1049 | /// The **resolved** display title of this section: its own override, else |
| 1038 | 1050 | /// its offering's, else the catalog course's. |
| 1039 | 1051 | #[schema(example = "Matematik")] |
| 1040 | 1052 | title: String, |
| 1041 | - /// The class's rung on the grade ladder (`0` = anaokulu). | |
| 1053 | + /// The class's rung on the grade ladder (`0` = anaokulu) — `null` for a | |
| 1054 | + /// viewer below the class-observation bar. | |
| 1042 | 1055 | #[schema(example = 9)] |
| 1043 | - grade_level: i16, | |
| 1056 | + grade_level: Option<i16>, | |
| 1044 | 1057 | /// `course`, `study` (supervised study), or `club`. |
| 1045 | 1058 | #[schema(example = "course")] |
| 1046 | 1059 | kind: String, |
| @@ -1215,6 +1228,12 @@ async fn profile_of( | ||
| 1215 | 1228 | if viewer.get_id() != id && ensure_can_observe(viewer, id, &st.db).await.is_err() { |
| 1216 | 1229 | members.clear(); |
| 1217 | 1230 | } |
| 1231 | + // The section rows name their class (`class`/`class_name`/`grade_level`), | |
| 1232 | + // so the same all-or-nothing gate rules what that triple may say here: | |
| 1233 | + // one predicate, computed once, feeding both blocks — never a second | |
| 1234 | + // rule the two blocks could disagree about. | |
| 1235 | + let may_see_class = viewer.get_id() == id | |
| 1236 | + || ensure_can_observe(viewer, id, &st.db).await.is_ok(); | |
| 1218 | 1237 | let class_ids: Vec<ClassGroupId> = members.iter().map(|row| row.get_class().clone()).collect(); |
| 1219 | 1238 | let classes = crate::service::class_group::list_by_ids(&st.db, &class_ids).await?; |
| 1220 | 1239 | // Both totals are the full counts, not the windowed ones — the blocks are a |
| @@ -1249,10 +1268,10 @@ async fn profile_of( | ||
| 1249 | 1268 | .map(|section| ProfileCourseRef { |
| 1250 | 1269 | id: section.id.clone(), |
| 1251 | 1270 | course: section.course.clone(), |
| 1252 | - class: section.class.clone(), | |
| 1253 | - class_name: section.class_name.clone(), | |
| 1271 | + class: may_see_class.then(|| section.class.clone()), | |
| 1272 | + class_name: may_see_class.then(|| section.class_name.clone()), | |
| 1254 | 1273 | title: section.title.clone(), |
| 1255 | - grade_level: section.grade_level, | |
| 1274 | + grade_level: may_see_class.then_some(section.grade_level), | |
| 1256 | 1275 | kind: kinds.get(§ion.course).cloned().unwrap_or_default(), |
| 1257 | 1276 | }) |
| 1258 | 1277 | .collect(), |
src/web/weekly_plan.rsmodified+19 -3
| @@ -509,6 +509,25 @@ async fn materialize_weekly_plan( | ||
| 509 | 509 | ) -> Result<Json<MaterializeReport>, AppError> { |
| 510 | 510 | let instance = instance_or_404(&id, &st.db).await?; |
| 511 | 511 | service::class_course::ensure_instance_teacher(&st.db, &user, instance.get_id()).await?; |
| 512 | + // The day math shifts each instant by the zone offset before it reads the | |
| 513 | + // date off it, and `zoned_day` panics when that shifted value leaves | |
| 514 | + // chrono's representable range — so an absurd magnitude must be refused | |
| 515 | + // *here*, with checked arithmetic, naming the field. Checked before the | |
| 516 | + // offset is even loaded: the refusal cannot depend on the school's zone. | |
| 517 | + for (field, millis) in [("from", req.from), ("to", req.to)] { | |
| 518 | + if millis | |
| 519 | + .checked_add(i64::from( | |
| 520 | + crate::domain::calendar::MAX_ZONE_OFFSET_MINUTES, | |
| 521 | + ) * 60_000) | |
| 522 | + .and_then(chrono::DateTime::from_timestamp_millis) | |
| 523 | + .is_none() | |
| 524 | + { | |
| 525 | + return Err(AppError::Validation(crate::error::ValidationError::Invalid { | |
| 526 | + field, | |
| 527 | + reason: "is outside the representable calendar range", | |
| 528 | + })); | |
| 529 | + } | |
| 530 | + } | |
| 512 | 531 | let from = Timestamp::from_millis(req.from); |
| 513 | 532 | let to = Timestamp::from_millis(req.to); |
| 514 | 533 | check_time_range(Some(from), Some(to))?; |
| @@ -517,9 +536,6 @@ async fn materialize_weekly_plan( | ||
| 517 | 536 | // range may legitimately end in the past when `from` does not, which is |
| 518 | 537 | // how a school backfills a plan it only now entered.) |
| 519 | 538 | check_not_past("from", Some(from))?; |
| 520 | - // The day-bucketing read `attendance` makes: a range's instants name | |
| 521 | - // *school* days, and each lesson lands at its slot's minute of that local | |
| 522 | - // day. | |
| 523 | 539 | let school = service::settings::load(&st.db).await?; |
| 524 | 540 | let offset = zone_offset_minutes(school.get_timezone()); |
| 525 | 541 |
tests/limits_completeness.rsmodified+6 -0
| @@ -565,6 +565,12 @@ const NOT_A_CLIENT_BOUND: &[(&str, &str)] = &[ | ||
| 565 | 565 | the result door returns the stored text; a browser never sizes a \ |
| 566 | 566 | transcript against this", |
| 567 | 567 | ), |
| 568 | + ( | |
| 569 | + "MAX_ZONE_OFFSET_MINUTES", | |
| 570 | + "the largest shift `calendar::zoned_day` applies — internal arithmetic \ | |
| 571 | + for a checked pre-flight in the materialize route, not a value a client \ | |
| 572 | + sizes a request against", | |
| 573 | + ), | |
| 568 | 574 | ]; |
| 569 | 575 | |
| 570 | 576 | #[test] |
tests/regress_profile.rsmodified+66 -5
| @@ -13,8 +13,9 @@ mod common; | ||
| 13 | 13 | use axum::Router; |
| 14 | 14 | use axum::http::StatusCode; |
| 15 | 15 | use common::{ |
| 16 | - app_and_db, create_exam_with, create_homework, create_subject, enroll, id_of, items, login, | |
| 17 | - login_as, me_id, send, set_role, taught_under, ABSENT_ID, | |
| 16 | + app_and_db, attach_instance, create_course, create_exam_with, create_homework, create_subject, | |
| 17 | + create_year, enroll, id_of, items, login, login_as, me_id, send, set_role, taught_under, | |
| 18 | + ABSENT_ID, | |
| 18 | 19 | }; |
| 19 | 20 | use hezarfen_backend::constant::{MAX_BIO_LEN, MAX_DISPLAY_NAME_LEN}; |
| 20 | 21 | use hezarfen_backend::database::Database; |
| @@ -919,6 +920,10 @@ struct Section { | ||
| 919 | 920 | peer: String, |
| 920 | 921 | student: String, |
| 921 | 922 | student_id: String, |
| 923 | + /// The şube the student sits in (`9-A`). | |
| 924 | + class: String, | |
| 925 | + /// The academic year that şube sits in. | |
| 926 | + year: String, | |
| 922 | 927 | } |
| 923 | 928 | |
| 924 | 929 | async fn section() -> Section { |
| @@ -932,7 +937,8 @@ async fn section() -> Section { | ||
| 932 | 937 | let student = login(&app, "kid").await; |
| 933 | 938 | let student_id = me_id(&app, &student).await; |
| 934 | 939 | |
| 935 | - let class = create_class(&app, &manager, "9-A", 9).await; | |
| 940 | + let year = create_year(&app, &manager, "2026-2027").await; | |
| 941 | + let class = create_class(&app, &manager, "9-A", 9, &year).await; | |
| 936 | 942 | add_member(&app, &manager, &class, &student_id).await; |
| 937 | 943 | link_student(&app, &admin, &parent_id, &student_id).await; |
| 938 | 944 | |
| @@ -944,16 +950,18 @@ async fn section() -> Section { | ||
| 944 | 950 | peer, |
| 945 | 951 | student, |
| 946 | 952 | student_id, |
| 953 | + class, | |
| 954 | + year, | |
| 947 | 955 | } |
| 948 | 956 | } |
| 949 | 957 | |
| 950 | -async fn create_class(app: &Router, manager: &str, name: &str, grade_level: i16) -> String { | |
| 958 | +async fn create_class(app: &Router, manager: &str, name: &str, grade_level: i16, year: &str) -> String { | |
| 951 | 959 | let res = send( |
| 952 | 960 | app, |
| 953 | 961 | "POST", |
| 954 | 962 | "/classes", |
| 955 | 963 | Some(manager), |
| 956 | - Some(json!({ "name": name, "grade_level": grade_level })), | |
| 964 | + Some(json!({ "name": name, "grade_level": grade_level, "year": year })), | |
| 957 | 965 | ) |
| 958 | 966 | .await; |
| 959 | 967 | assert_eq!( |
| @@ -1075,6 +1083,59 @@ async fn teacher_parent_manager_and_the_owner_still_read_the_class_block() { | ||
| 1075 | 1083 | assert_eq!(mine.body["classes"][0]["grade_level"], 9); |
| 1076 | 1084 | } |
| 1077 | 1085 | |
| 1086 | +/// The section rows' class triple answers the same bar the `classes` block | |
| 1087 | +/// does. The leak this closes: the section split gave `ProfileCourseRef` a | |
| 1088 | +/// `class`/`class_name`/`grade_level` of its own, filtered only by catalog | |
| 1089 | +/// readability — so a fellow student reading a classmate's profile was served | |
| 1090 | +/// the classmate's şube, name and grade and all, while the `classes` block in | |
| 1091 | +/// the very same response stayed empty for exactly that viewer. Both sides in | |
| 1092 | +/// one test, so the two blocks cannot drift apart unnoticed. | |
| 1093 | +#[tokio::test] | |
| 1094 | +async fn a_section_rows_class_fields_obey_the_class_block_gate() { | |
| 1095 | + let s = section().await; | |
| 1096 | + // Attach a course to the student's own class so the profile's course | |
| 1097 | + // block carries the section row the gate is about. The fixture's şube | |
| 1098 | + // already sits in `s.year`, so the attach lands. | |
| 1099 | + let course = create_course(&s.app, &s.manager, "Tarih").await; | |
| 1100 | + let _instance = attach_instance(&s.app, &s.manager, &s.class, &course).await; | |
| 1101 | + // The peer shares the *catalog course* — their own şube teaches it too — | |
| 1102 | + // which is the only way a fellow student passes the course gate and | |
| 1103 | + // reaches the section row at all. The class gate then withholds the şube | |
| 1104 | + // identity the row names: same course, different class. | |
| 1105 | + let class_b = create_class(&s.app, &s.manager, "9-B", 9, &s.year).await; | |
| 1106 | + let peer_id = me_id(&s.app, &s.peer).await; | |
| 1107 | + add_member(&s.app, &s.manager, &class_b, &peer_id).await; | |
| 1108 | + attach_instance(&s.app, &s.manager, &class_b, &course).await; | |
| 1109 | + | |
| 1110 | + let peer_seen = profile(&s.app, &s.peer, &s.student_id).await; | |
| 1111 | + assert_eq!(peer_seen.status, StatusCode::OK, "{}", peer_seen.body); | |
| 1112 | + let rows = peer_seen.body["courses"].as_array().expect("courses"); | |
| 1113 | + assert_eq!(rows.len(), 1, "the section row itself is served: {}", peer_seen.body); | |
| 1114 | + let row = &rows[0]; | |
| 1115 | + // The block's purpose stays: the section, resolved title, kind. | |
| 1116 | + assert_eq!(row["course"], json!(course), "{}", row); | |
| 1117 | + assert_eq!(row["title"], json!("Tarih"), "{}", row); | |
| 1118 | + assert_eq!(row["kind"], json!("course"), "{}", row); | |
| 1119 | + // The class identity is withheld — null, not dropped. | |
| 1120 | + assert_eq!(row["class"], json!(null), "{}", row); | |
| 1121 | + assert_eq!(row["class_name"], json!(null), "{}", row); | |
| 1122 | + assert_eq!(row["grade_level"], json!(null), "{}", row); | |
| 1123 | + | |
| 1124 | + // A manager passes the bar and reads the triple. | |
| 1125 | + let boss_seen = profile(&s.app, &s.manager, &s.student_id).await; | |
| 1126 | + assert_eq!(boss_seen.status, StatusCode::OK, "{}", boss_seen.body); | |
| 1127 | + let boss_row = &boss_seen.body["courses"][0]; | |
| 1128 | + assert_eq!(boss_row["class_name"], json!("9-A"), "{}", boss_row); | |
| 1129 | + assert_eq!(boss_row["grade_level"], json!(9), "{}", boss_row); | |
| 1130 | + assert!(boss_row["class"].as_str().is_some(), "{}", boss_row); | |
| 1131 | + | |
| 1132 | + // The owner passes it too, reading their own profile. | |
| 1133 | + let mine = send(&s.app, "GET", "/users/me/profile", Some(&s.student), None).await; | |
| 1134 | + assert_eq!(mine.status, StatusCode::OK, "{}", mine.body); | |
| 1135 | + assert_eq!(mine.body["courses"][0]["class_name"], json!("9-A"), "{}", mine.body); | |
| 1136 | + assert_eq!(mine.body["courses"][0]["grade_level"], json!(9), "{}", mine.body); | |
| 1137 | +} | |
| 1138 | + | |
| 1078 | 1139 | /// `stats.classes` is the owner's true total, deliberately *not* the length of |
| 1079 | 1140 | /// the gated block — the same call already made for `stats.courses`: it is the |
| 1080 | 1141 | /// motivational counter, and a magnitude names no class. |
tests/regress_schedule.rsmodified+75 -2
| @@ -15,8 +15,8 @@ mod common; | ||
| 15 | 15 | use axum::http::StatusCode; |
| 16 | 16 | use chrono::{DateTime, Datelike, NaiveDate, Weekday}; |
| 17 | 17 | use common::{ |
| 18 | - app_and_db, attach_instance, create_class, create_course, ensure_year, enroll, id_of, items, | |
| 19 | - login, login_as, me_id, send, taught, | |
| 18 | + app_and_db, attach_instance, create_class, create_course, create_session, ensure_year, enroll, | |
| 19 | + id_of, items, login, login_as, me_id, send, taught, | |
| 20 | 20 | }; |
| 21 | 21 | use serde_json::{json, Value}; |
| 22 | 22 | |
| @@ -674,6 +674,30 @@ async fn materialize_rejects_a_bad_range_before_anything_runs() { | ||
| 674 | 674 | .await; |
| 675 | 675 | assert_eq!(res.status, StatusCode::BAD_REQUEST, "{}", res.body); |
| 676 | 676 | |
| 677 | + // An instant the day math cannot represent: shifting by the zone offset | |
| 678 | + // overflows chrono's range and `zoned_day` panics there. The route must | |
| 679 | + // refuse with a 400 that names the field — for `i64::MAX` and `i64::MIN` | |
| 680 | + // alike — before anything runs. | |
| 681 | + for (field, at) in [("from", i64::MAX), ("to", i64::MAX), ("from", i64::MIN), ("to", i64::MIN)] { | |
| 682 | + let (from, to) = match field { | |
| 683 | + "from" => (at, day_start(later_monday())), | |
| 684 | + _ => (day_start(lesson_monday()), at), | |
| 685 | + }; | |
| 686 | + let res = materialize(&app, &manager, &t.instance, from, to, None).await; | |
| 687 | + assert_eq!(res.status, StatusCode::BAD_REQUEST, "{field}={at}: {}", res.body); | |
| 688 | + let body = res.body.to_string(); | |
| 689 | + assert!( | |
| 690 | + body.contains(field), | |
| 691 | + "the 400 must name `{field}`: {body}" | |
| 692 | + ); | |
| 693 | + } | |
| 694 | + | |
| 695 | + // A sane range through the same gate still works — the guard refuses | |
| 696 | + // magnitudes, not the ordinary calendar. | |
| 697 | + let (from, to) = day_range(lesson_monday()); | |
| 698 | + let res = materialize(&app, &manager, &t.instance, from, to, None).await; | |
| 699 | + assert_eq!(res.status, StatusCode::OK, "{}", res.body); | |
| 700 | + | |
| 677 | 701 | let page = sessions_page(&app, &manager, &t.instance).await; |
| 678 | 702 | assert_eq!(page["total"], json!(0), "nothing was written: {}", page); |
| 679 | 703 | } |
| @@ -744,6 +768,55 @@ async fn a_hand_created_session_on_a_generated_slot_is_a_coded_conflict() { | ||
| 744 | 768 | assert_eq!(page["total"], json!(2), "{}", page); |
| 745 | 769 | } |
| 746 | 770 | |
| 771 | +/// The PATCH door answers the same coded 409, not a 500: moving one lesson's | |
| 772 | +/// `starts_at` onto an instant the section already holds trips the same | |
| 773 | +/// `UNIQUE (class_course, starts_at)` the create door maps. A PATCH to a free | |
| 774 | +/// instant still succeeds. | |
| 775 | +#[tokio::test] | |
| 776 | +async fn a_session_moved_onto_an_occupied_instant_is_a_coded_conflict() { | |
| 777 | + let (app, db) = app_and_db().await; | |
| 778 | + let manager = login_as(&app, &db, "mgr", "manager").await; | |
| 779 | + let (t, _) = planned_monday(&app, &db, &manager, true).await; | |
| 780 | + | |
| 781 | + // Two lessons one minute apart. | |
| 782 | + create_session(&app, &manager, &t.instance, day_start(lesson_monday()) + 540 * 60_000).await; | |
| 783 | + let second = create_session(&app, &manager, &t.instance, day_start(lesson_monday()) + 601 * 60_000).await; | |
| 784 | + | |
| 785 | + // PATCH the second onto the first's instant: the unique wall, through the | |
| 786 | + // update path, must be the coded 409 — never a raw 500. | |
| 787 | + let moved = send( | |
| 788 | + &app, | |
| 789 | + "PATCH", | |
| 790 | + &format!("/sessions/{second}"), | |
| 791 | + Some(&manager), | |
| 792 | + Some(json!({ "starts_at": day_start(lesson_monday()) + 540 * 60_000 })), | |
| 793 | + ) | |
| 794 | + .await; | |
| 795 | + assert_eq!(moved.status, StatusCode::CONFLICT, "{}", moved.body); | |
| 796 | + assert_eq!(moved.body["code"], "session_time_taken", "{}", moved.body); | |
| 797 | + | |
| 798 | + // The refused PATCH wrote nothing: both lessons still sit where they did. | |
| 799 | + let page = sessions_page(&app, &manager, &t.instance).await; | |
| 800 | + assert_eq!(page["total"], json!(2), "{}", page); | |
| 801 | + | |
| 802 | + // A PATCH onto a free instant is not caught by the same wall. | |
| 803 | + let ok = send( | |
| 804 | + &app, | |
| 805 | + "PATCH", | |
| 806 | + &format!("/sessions/{second}"), | |
| 807 | + Some(&manager), | |
| 808 | + Some(json!({ "starts_at": day_start(lesson_monday()) + 661 * 60_000 })), | |
| 809 | + ) | |
| 810 | + .await; | |
| 811 | + assert_eq!(ok.status, StatusCode::OK, "{}", ok.body); | |
| 812 | + assert_eq!( | |
| 813 | + ok.body["starts_at"], | |
| 814 | + json!(day_start(lesson_monday()) + 661 * 60_000), | |
| 815 | + "{}", | |
| 816 | + ok.body | |
| 817 | + ); | |
| 818 | +} | |
| 819 | + | |
| 747 | 820 | // ---- the materializer: shape of the generated set -------------------------------- |
| 748 | 821 | |
| 749 | 822 | #[tokio::test] |