hezarfen_backend
Clone
https://git.tahinli.com/archive/hezarfen_backend.git
0 watching0 forksRust8.3 MiB6d68754Ahmet Kaan Gümüş2 months ago566 commits
| 1 | //! The `enrollment` table: the roster read and listing, the counter-returning |
| 2 | //! delete, and the enroll claim — one transaction that checks the pair free |
| 3 | //! and the member still a student. The target-user gates the HTTP handlers pay |
| 4 | //! live in [`crate::service::enrollment`]. |
| 5 | //! |
| 6 | //! Enrollment keys on the *instance* now (D4): a student is enrolled in the |
| 7 | //! Matematik that 5-A teaches, not in a school-wide Matematik row. The seat |
| 8 | //! gate is gone with the catalog's `capacity` (D5) — the roster counter is a |
| 9 | //! count, not a ceiling — so the only refusals left are "already enrolled" |
| 10 | //! (which is an idempotent hit, not an error) and "that user is not a |
| 11 | //! student". |
| 12 | |
| 13 | use crate; |
| 14 | use cratePagedList; |
| 15 | use crateClassCourseId; |
| 16 | use crateClassGroupId; |
| 17 | use crateCourseId; |
| 18 | use crateEnrollment; |
| 19 | use crateExamId; |
| 20 | use crateRole; |
| 21 | use crateTimestamp; |
| 22 | use crateUserId; |
| 23 | use crate; |
| 24 | |
| 25 | /// What [`enroll`]'s transaction settled on. |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | /// Enroll (idempotently) `user` into one instance. The table's primary key |
| 36 | /// *is* the (instance, user) pair, so concurrent enrolls of the same pair |
| 37 | /// converge on one row instead of racing into a 500: the loser reads the |
| 38 | /// winner's row back and returns it. An already enrolled user is returned |
| 39 | /// as-is — the counter moves once, and never twice for one row — except that a |
| 40 | /// row a class pumped is *disowned* on the way out (see |
| 41 | /// [`disown_if_pumped`]): the operator's hand enroll is what the row now |
| 42 | /// records. |
| 43 | /// |
| 44 | /// `source` is the class the roster pump wrote this row on behalf of, or |
| 45 | /// `None` for a hand-placed (elective) enrollment. The two halves of that |
| 46 | /// rule: only a row carrying the key is a class's to take back |
| 47 | /// ([`crate::db::class_pump`]), and a hand unenroll wins permanently. |
| 48 | /// |
| 49 | /// The `enrollment_count` counter moves in the same statement as the row (a |
| 50 | /// refused insert takes its own bump back), so the instance's roster size |
| 51 | /// cannot drift from the rows behind it. |
| 52 | /// |
| 53 | /// Gate order is the old THROW order: an existing pair outranks the role |
| 54 | /// check. The role check takes the user row `FOR NO KEY UPDATE`, the same row |
| 55 | /// a demotion cascade updates before its sweep, so grant and demotion |
| 56 | /// serialize on the row lock in both directions — a row landing after the |
| 57 | /// sweep is impossible, and a demotion landing before this check refuses it. |
| 58 | pub async |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | |
| 152 | |
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | |
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | |
| 169 | |
| 170 | |
| 171 | |
| 172 | |
| 173 | /// A hand enroll landing on a row a class pumped takes the row *off* the |
| 174 | /// class: `source` goes, so no later class sweep can unenroll a student an |
| 175 | /// operator placed on purpose. The mirror of the rule the sweep already holds |
| 176 | /// in the other direction — a manual unenroll wins, permanently — and without |
| 177 | /// it "hand-placed" was a state only a first enroll could reach. |
| 178 | /// |
| 179 | /// `NULL`, not absence: the column is a nullable uuid whose NULL *is* the |
| 180 | /// meaning a missing key used to carry, and it is what a hand enroll writes on |
| 181 | /// the create path too. |
| 182 | async |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | |
| 190 | |
| 191 | |
| 192 | |
| 193 | |
| 194 | |
| 195 | |
| 196 | |
| 197 | |
| 198 | |
| 199 | |
| 200 | |
| 201 | |
| 202 | |
| 203 | |
| 204 | |
| 205 | |
| 206 | |
| 207 | /// Some(_) iff `user` is enrolled in that instance — the grading gate. |
| 208 | pub async |
| 209 | |
| 210 | |
| 211 | |
| 212 | |
| 213 | |
| 214 | |
| 215 | |
| 216 | |
| 217 | |
| 218 | |
| 219 | |
| 220 | |
| 221 | |
| 222 | |
| 223 | |
| 224 | |
| 225 | |
| 226 | |
| 227 | /// Every instance `user` holds an enrollment row on, in one statement for the |
| 228 | /// whole set — the read behind the surfaces that must list a section a **hand** |
| 229 | /// enrollment reaches. A hand-placed row (and a class row an operator later |
| 230 | /// disowned, `source` NULL) is on no class roster, so |
| 231 | /// [`crate::service::instance::visible_instances`] — keyed on the class — never |
| 232 | /// sees it; `/courses/me` and the profile course block listed it before the |
| 233 | /// section split, so they union this in. Ordered by instance id for a stable |
| 234 | /// answer; a user with no enrollment reads an empty list, and the caller |
| 235 | /// resolves the ids through [`crate::db::class_course`] (one batch read). |
| 236 | pub async |
| 237 | |
| 238 | |
| 239 | |
| 240 | |
| 241 | |
| 242 | |
| 243 | |
| 244 | |
| 245 | |
| 246 | |
| 247 | |
| 248 | |
| 249 | |
| 250 | /// The roster an exam is sat by: every enrollment of every instance the exam |
| 251 | /// is addressed to (`exam_audience`, D2), deduped by student — the live |
| 252 | /// monitor's roster read, and the one that makes an announced-to section's |
| 253 | /// sitters visible to it. For an exam nobody was announced to this is the |
| 254 | /// owner instance's roster, row for row. |
| 255 | /// |
| 256 | /// `DISTINCT ON (app_user)` because a student enrolled in two addressed |
| 257 | /// instances sits once: the roster is a set of people, and the monitor joins |
| 258 | /// it with one attempt/mark map per person. |
| 259 | pub async |
| 260 | |
| 261 | |
| 262 | |
| 263 | |
| 264 | |
| 265 | |
| 266 | |
| 267 | |
| 268 | |
| 269 | |
| 270 | |
| 271 | |
| 272 | |
| 273 | |
| 274 | |
| 275 | |
| 276 | |
| 277 | |
| 278 | |
| 279 | |
| 280 | |
| 281 | |
| 282 | /// Whether `user` is one of `course`'s students right now — the point probe |
| 283 | /// behind an event addressed at a course. |
| 284 | /// |
| 285 | /// The two membership tiers again: an enrollment in any of the course's |
| 286 | /// instances, or an individual club/supervised-study membership. Both are existence |
| 287 | /// probes on their own indexes; one entity's audience must not fold a whole |
| 288 | /// roster to answer yes. |
| 289 | pub async |
| 290 | |
| 291 | |
| 292 | |
| 293 | |
| 294 | |
| 295 | |
| 296 | |
| 297 | |
| 298 | |
| 299 | |
| 300 | |
| 301 | |
| 302 | |
| 303 | |
| 304 | |
| 305 | |
| 306 | |
| 307 | |
| 308 | |
| 309 | |
| 310 | |
| 311 | /// Every student the catalog course reaches right now — the union of its |
| 312 | /// instances' rosters and its own individual memberships. |
| 313 | /// |
| 314 | /// One course's "students" is two populations since D9 split the two tiers: a |
| 315 | /// course reaches them through the instance a section teaches (enrollment), and |
| 316 | /// a club or supervised study through the school-scoped membership. A reader |
| 317 | /// that wants the course's audience — an event addressed at the course, a |
| 318 | /// who-missed report — means both, and deduping in SQL keeps a student who is |
| 319 | /// in two of its instances on the list once. |
| 320 | pub async |
| 321 | |
| 322 | |
| 323 | |
| 324 | |
| 325 | |
| 326 | |
| 327 | |
| 328 | |
| 329 | |
| 330 | |
| 331 | |
| 332 | |
| 333 | |
| 334 | |
| 335 | |
| 336 | |
| 337 | |
| 338 | |
| 339 | /// The instance's roster, newest first — the natural-PK columns sort the way |
| 340 | /// the old composite key did, with its instance prefix constant. |
| 341 | pub async |
| 342 | |
| 343 | |
| 344 | |
| 345 | |
| 346 | |
| 347 | |
| 348 | |
| 349 | |
| 350 | |
| 351 | |
| 352 | |
| 353 | |
| 354 | |
| 355 | |
| 356 | /// Take the row and give the instance's roster counter back in one statement, |
| 357 | /// so nothing but a lost transaction can drift the counter. `None` when the |
| 358 | /// pair held no row. |
| 359 | pub async |
| 360 | |
| 361 | |
| 362 | |
| 363 | |
| 364 | |
| 365 | |
| 366 | |
| 367 | |
| 368 | |
| 369 | |
| 370 | |
| 371 | |
| 372 | |
| 373 | |
| 374 | |
| 375 | |
| 376 | |
| 377 | |
| 378 | |
| 379 | |
| 380 | |
| 381 | |
| 382 | |
| 383 | |
| 384 | |
| 385 | |
| 386 | |
| 387 | |
| 388 | |
| 389 | |
| 390 | |
| 391 | |
| 392 | |
| 393 | |
| 394 | |
| 395 | |
| 396 | |
| 397 | |
| 398 | |
| 399 | |
| 400 | |
| 401 | |
| 402 | |
| 403 | |
| 404 | |
| 405 | |
| 406 | |
| 407 | |
| 408 | |
| 409 | |
| 410 | |
| 411 | |
| 412 | |
| 413 | |
| 414 | |
| 415 | |
| 416 | |
| 417 | |
| 418 | |
| 419 | |
| 420 | |
| 421 | |
| 422 | |
| 423 | |
| 424 | |
| 425 | |
| 426 | |
| 427 | |
| 428 | |
| 429 | |
| 430 | |
| 431 | |
| 432 | |
| 433 | |
| 434 | |
| 435 | |
| 436 | |
| 437 | |
| 438 | |
| 439 | |
| 440 | |
| 441 | |
| 442 | |
| 443 | |
| 444 | |
| 445 | |
| 446 | |
| 447 | |
| 448 | |
| 449 | |
| 450 | |
| 451 | |
| 452 | |
| 453 | |
| 454 | |
| 455 | |
| 456 | |
| 457 | |
| 458 | |
| 459 | |
| 460 | |
| 461 | |
| 462 | |
| 463 | |
| 464 | |
| 465 | |
| 466 | |
| 467 | |
| 468 | |
| 469 | |
| 470 | |
| 471 | |
| 472 | |
| 473 | |
| 474 | |
| 475 | |
| 476 | |
| 477 | |
| 478 |