hezarfen_backend
Clone
https://git.tahinli.com/archive/hezarfen_backend.git
0 watching0 forksRust8.3 MiB6d68754Ahmet Kaan Gümüş2 months ago566 commits
| 1 | use crate; |
| 2 | use Json; |
| 3 | use ; |
| 4 | use StatusCode; |
| 5 | use Response; |
| 6 | use ; |
| 7 | use json; |
| 8 | use ; |
| 9 | use OpenApiRouter; |
| 10 | use routes; |
| 11 | |
| 12 | use crate |
| 13 | |
| 14 | |
| 15 | ; |
| 16 | use crateDatabase; |
| 17 | use crate; |
| 18 | use crateClassGroupId; |
| 19 | |
| 20 | use crate; |
| 21 | use crate |
| 22 | |
| 23 | ; |
| 24 | use crateRole; |
| 25 | use crate; |
| 26 | use crate; |
| 27 | use crateensure_can_observe; |
| 28 | use crateAppState; |
| 29 | |
| 30 | use visible_courses; |
| 31 | use AssignableRole; |
| 32 | use Role as RoleSchema; |
| 33 | use |
| 34 | |
| 35 | |
| 36 | ; |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | /// Partial personal-info update. Per field: omitted (or `null`) keeps the |
| 77 | /// current value, an empty string clears it, anything else is validated and set. |
| 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 | /// Partial UI-preference update. Same field semantics as [`UpdateProfile`]: |
| 127 | /// omitted (or `null`) keeps the current value, an empty string clears it back |
| 128 | /// to "never chose" (the client then follows the device preference), anything |
| 129 | /// else is validated and set. |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | /// Resolve one patched field into what the save should write: absent (or |
| 145 | /// `null`) is `None` — the column is not written at all, so a concurrent PATCH |
| 146 | /// of it survives; `""` is `Some(None)`, an explicit clear; anything else must |
| 147 | /// parse into the domain newtype, exactly as a create would. |
| 148 | |
| 149 | |
| 150 | |
| 151 | |
| 152 | |
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | |
| 158 | |
| 159 | /// Resolve the `branch` a request names. Same shape as [`merge_field`] — absent |
| 160 | /// is "keep", `""` is an explicit clear — with the one check a free string |
| 161 | /// cannot make: the value must be one the *school* lists (`GET /settings`). |
| 162 | /// The vocabulary is the school's, so nothing else can decide it, and a school |
| 163 | /// with no list can only ever have its `branch` cleared. |
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | |
| 169 | |
| 170 | |
| 171 | |
| 172 | |
| 173 | |
| 174 | |
| 175 | |
| 176 | |
| 177 | |
| 178 | |
| 179 | |
| 180 | |
| 181 | |
| 182 | |
| 183 | /// Resolve the `student_number` a request names. Same merge shape as |
| 184 | /// [`merge_field`] — absent (or `null`) is "keep", blank is an explicit |
| 185 | /// clear — plus the two checks a plain field does not carry: |
| 186 | /// |
| 187 | /// * **The role gate.** A number names a student of this school, so only a |
| 188 | /// `student` row may receive one; naming one for any other role is a 400, |
| 189 | /// not a silent write the next role change would clear. |
| 190 | /// * **The shape**, through [`StudentNumber`]: non-blank after trimming and |
| 191 | /// within the published bound. Blank is a *clear* rather than a parse |
| 192 | /// failure, so whitespace can never become a stored number — the same |
| 193 | /// reading every other patchable field has. |
| 194 | /// |
| 195 | /// `""` on a non-student stays a clear (a no-op: such a row never holds one), |
| 196 | /// and a value that collides with another student's is the unique index's |
| 197 | /// refusal — a 409 from the writer, never a pre-check that could race. |
| 198 | |
| 199 | |
| 200 | |
| 201 | |
| 202 | |
| 203 | |
| 204 | |
| 205 | |
| 206 | |
| 207 | |
| 208 | |
| 209 | |
| 210 | |
| 211 | |
| 212 | |
| 213 | |
| 214 | /// Validate and persist exactly the info fields `req` carried — nothing is |
| 215 | /// merged from `user`'s snapshot, so a concurrent PATCH of another field is not |
| 216 | /// reverted. Shared by the self-service and admin profile endpoints — they |
| 217 | /// differ only in whose row they load and who may call them. |
| 218 | /// |
| 219 | /// `student_number` is resolved against the target's *live* role, read with the |
| 220 | /// row we hold — so the role gate cannot be raced by a role change landing in |
| 221 | /// between (a promotion that lands first makes this a 400; this write landing |
| 222 | /// first leaves nothing behind, because the role write clears the number in the |
| 223 | /// same statement). |
| 224 | async |
| 225 | |
| 226 | |
| 227 | |
| 228 | |
| 229 | |
| 230 | |
| 231 | |
| 232 | |
| 233 | |
| 234 | |
| 235 | |
| 236 | |
| 237 | |
| 238 | |
| 239 | |
| 240 | |
| 241 | |
| 242 | |
| 243 | |
| 244 | |
| 245 | |
| 246 | |
| 247 | |
| 248 | |
| 249 | |
| 250 | |
| 251 | |
| 252 | |
| 253 | |
| 254 | |
| 255 | |
| 256 | |
| 257 | |
| 258 | |
| 259 | |
| 260 | |
| 261 | |
| 262 | |
| 263 | |
| 264 | |
| 265 | |
| 266 | |
| 267 | |
| 268 | |
| 269 | |
| 270 | |
| 271 | |
| 272 | |
| 273 | |
| 274 | |
| 275 | /// Validate and persist exactly the preference fields `req` carried — same |
| 276 | /// no-merge reasoning as [`apply_profile`]. Shared by the |
| 277 | /// self-service and admin preference endpoints — they differ only in whose row |
| 278 | /// they load and who may call them. |
| 279 | async |
| 280 | |
| 281 | |
| 282 | |
| 283 | |
| 284 | |
| 285 | |
| 286 | |
| 287 | |
| 288 | |
| 289 | |
| 290 | |
| 291 | |
| 292 | |
| 293 | |
| 294 | |
| 295 | |
| 296 | |
| 297 | |
| 298 | |
| 299 | |
| 300 | |
| 301 | |
| 302 | |
| 303 | |
| 304 | |
| 305 | |
| 306 | |
| 307 | |
| 308 | |
| 309 | |
| 310 | /// Find users by username or name — backs the pickers (enroll, grade, mark |
| 311 | /// attendance) and, for a student or parent, the one way to find the staff |
| 312 | /// member they are allowed to message. Any authenticated user may ask; a |
| 313 | /// caller below teacher only ever sees the roles they may message (teacher, |
| 314 | /// manager, admin), in the items *and* in `total`. A blank or omitted `q` |
| 315 | /// lists everyone the caller may see — what the pickers open with; `role` |
| 316 | /// narrows to one role (e.g. `role=student` for an enroll picker), and a |
| 317 | /// student or parent naming a role they may not message is refused. Paged |
| 318 | /// via `?limit=&offset=` like the other lists (omit `limit` for every |
| 319 | /// match); returns a `{items, total, limit, offset}` envelope carrying only |
| 320 | /// id/username/display name and the student number — no contact details. |
| 321 | |
| 322 | |
| 323 | |
| 324 | |
| 325 | |
| 326 | |
| 327 | |
| 328 | |
| 329 | |
| 330 | |
| 331 | |
| 332 | |
| 333 | |
| 334 | async |
| 335 | |
| 336 | |
| 337 | |
| 338 | |
| 339 | |
| 340 | |
| 341 | |
| 342 | |
| 343 | |
| 344 | |
| 345 | |
| 346 | |
| 347 | |
| 348 | |
| 349 | |
| 350 | |
| 351 | |
| 352 | |
| 353 | |
| 354 | |
| 355 | |
| 356 | |
| 357 | |
| 358 | |
| 359 | |
| 360 | |
| 361 | |
| 362 | |
| 363 | |
| 364 | |
| 365 | |
| 366 | |
| 367 | |
| 368 | /// The admin directory's optional `roles` narrowing: a comma-separated list |
| 369 | /// of role names, e.g. `roles=manager,admin` for the two staff tabs in one |
| 370 | /// read. Absent (`None`) is every user — today's response, unchanged. |
| 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 | /// List every user with their role, newest first. Admin only. Paged: pass |
| 409 | /// `?limit=&offset=` to take a window (omit `limit` for the whole list); the |
| 410 | /// response is a `{items, total, limit, offset}` envelope where `total` counts |
| 411 | /// every user. |
| 412 | /// |
| 413 | /// The optional `roles` parameter narrows the directory to a set of roles — |
| 414 | /// a comma-separated list (`roles=manager,admin` is the two staff tabs in |
| 415 | /// one read, an OR). Omitted, the response is the whole roster. The window |
| 416 | /// and `total` are taken after the narrowing, so `total` counts the filtered |
| 417 | /// set and paging stays inside it. Each name is the vocabulary |
| 418 | /// `GET /users/search` parses; an unknown name, a blank value, or a blank |
| 419 | /// token is a `400` naming `roles`. |
| 420 | |
| 421 | |
| 422 | |
| 423 | |
| 424 | |
| 425 | |
| 426 | |
| 427 | |
| 428 | |
| 429 | |
| 430 | |
| 431 | |
| 432 | |
| 433 | async |
| 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 | /// Create a school account directly — the school-office path for adding a |
| 467 | /// student or a staff member with no self-registration and no invite. Admin |
| 468 | /// only. `{username, password}` are required and `role` is optional (omitted → |
| 469 | /// `student`); the row is born with its role rather than promoted into it, so |
| 470 | /// a new teacher is never briefly a student. `student_number` numbers a new |
| 471 | /// student at mint time — a `409` when another student in this school already |
| 472 | /// holds it, a `400` on a non-student role. The username is a global |
| 473 | /// **person** credential exactly as at `POST /auth/register`: a name new |
| 474 | /// everywhere creates the person and this school's `app_user`, while a person |
| 475 | /// who already exists is attached to this school only when the password |
| 476 | /// matches the stored credential — a mismatch is a `409`. A username already |
| 477 | /// taken in this school is a `409`, and the reserved staff-reading names |
| 478 | /// (`admin`, `root`, …) are a `400`, the same policy registration holds. |
| 479 | |
| 480 | |
| 481 | |
| 482 | |
| 483 | |
| 484 | |
| 485 | |
| 486 | |
| 487 | |
| 488 | |
| 489 | |
| 490 | |
| 491 | |
| 492 | |
| 493 | |
| 494 | async |
| 495 | |
| 496 | |
| 497 | |
| 498 | |
| 499 | |
| 500 | |
| 501 | |
| 502 | |
| 503 | |
| 504 | |
| 505 | |
| 506 | |
| 507 | |
| 508 | |
| 509 | |
| 510 | |
| 511 | |
| 512 | |
| 513 | |
| 514 | |
| 515 | |
| 516 | |
| 517 | |
| 518 | |
| 519 | |
| 520 | |
| 521 | |
| 522 | |
| 523 | |
| 524 | |
| 525 | |
| 526 | |
| 527 | |
| 528 | |
| 529 | |
| 530 | |
| 531 | |
| 532 | |
| 533 | |
| 534 | |
| 535 | |
| 536 | |
| 537 | |
| 538 | |
| 539 | |
| 540 | |
| 541 | |
| 542 | |
| 543 | |
| 544 | |
| 545 | |
| 546 | |
| 547 | |
| 548 | |
| 549 | |
| 550 | |
| 551 | /// Update the caller's own personal info: name, surname, email, phone, birth |
| 552 | /// date, plus the public-profile pair `display_name` and `bio` (both readable |
| 553 | /// school-wide at `GET /users/{id}/profile`, unlike the contact fields). Any |
| 554 | /// authenticated role. Omitted fields stay as they are; an empty string clears |
| 555 | /// a field. `student_number` follows the same merge semantics, with two |
| 556 | /// refusals of its own: naming one while the account is not a `student` is a |
| 557 | /// `400`, and a number another student in this school already holds is a `409` |
| 558 | /// (`code: student_number_taken`). |
| 559 | |
| 560 | |
| 561 | |
| 562 | |
| 563 | |
| 564 | |
| 565 | |
| 566 | |
| 567 | |
| 568 | |
| 569 | |
| 570 | |
| 571 | |
| 572 | |
| 573 | async |
| 574 | |
| 575 | |
| 576 | |
| 577 | |
| 578 | |
| 579 | |
| 580 | |
| 581 | /// Update the caller's own UI preferences: `theme` (`light`/`dark`), |
| 582 | /// `language` (`tr`/`en`), and `palette_color` (accent color as `#rrggbb`). |
| 583 | /// Any authenticated role. Omitted fields stay as they |
| 584 | /// are; an empty string clears one back to "never chose" (the client then |
| 585 | /// follows the device preference). Read them back on any user response, e.g. |
| 586 | /// `GET /auth/me`. |
| 587 | |
| 588 | |
| 589 | |
| 590 | |
| 591 | |
| 592 | |
| 593 | |
| 594 | |
| 595 | |
| 596 | |
| 597 | |
| 598 | |
| 599 | |
| 600 | async |
| 601 | |
| 602 | |
| 603 | |
| 604 | |
| 605 | |
| 606 | |
| 607 | |
| 608 | /// Fetch one user with their role and personal info. Admin only. |
| 609 | |
| 610 | |
| 611 | |
| 612 | |
| 613 | |
| 614 | |
| 615 | |
| 616 | |
| 617 | |
| 618 | |
| 619 | |
| 620 | |
| 621 | |
| 622 | async |
| 623 | |
| 624 | |
| 625 | |
| 626 | |
| 627 | |
| 628 | |
| 629 | |
| 630 | |
| 631 | |
| 632 | |
| 633 | /// Update any user's personal info. Admin only — the school-office path for |
| 634 | /// maintaining records on behalf of students and staff. Same field semantics |
| 635 | /// as `PATCH /users/me`, `student_number` included: the number belongs to the |
| 636 | /// student identity the office maintains, so this is the door that issues it — |
| 637 | /// a `400` when the target is not a `student`, a `409` |
| 638 | /// (`code: student_number_taken`) when another student in this school already |
| 639 | /// holds it. A role change away from `student` clears it on its own |
| 640 | /// (`PATCH /users/{id}/role`), which is also the answer to "this account is no |
| 641 | /// longer a student". |
| 642 | |
| 643 | |
| 644 | |
| 645 | |
| 646 | |
| 647 | |
| 648 | |
| 649 | |
| 650 | |
| 651 | |
| 652 | |
| 653 | |
| 654 | |
| 655 | |
| 656 | |
| 657 | |
| 658 | |
| 659 | async |
| 660 | |
| 661 | |
| 662 | |
| 663 | |
| 664 | |
| 665 | |
| 666 | |
| 667 | |
| 668 | |
| 669 | |
| 670 | |
| 671 | /// Update any user's UI preferences. Admin only — everyone else manages their |
| 672 | /// own through `PATCH /users/me/preferences`, which this mirrors field for |
| 673 | /// field. |
| 674 | |
| 675 | |
| 676 | |
| 677 | |
| 678 | |
| 679 | |
| 680 | |
| 681 | |
| 682 | |
| 683 | |
| 684 | |
| 685 | |
| 686 | |
| 687 | |
| 688 | |
| 689 | |
| 690 | async |
| 691 | |
| 692 | |
| 693 | |
| 694 | |
| 695 | |
| 696 | |
| 697 | |
| 698 | |
| 699 | |
| 700 | |
| 701 | |
| 702 | /// Set a user's role. Admin only. An admin cannot change their own role, and |
| 703 | /// the school's **last** admin cannot be demoted by anyone (`409`) — together |
| 704 | /// those keep role management from locking everyone out, including when two |
| 705 | /// admins demote each other at the same instant (the floor is a predicate on |
| 706 | /// the role write itself, so the racing demotions serialize on row locks). |
| 707 | /// A school that has already lost its admins is recovered by hand against the |
| 708 | /// database, since the seed never promotes. |
| 709 | /// Setting any non-`student` role also drops the user's course enrollments — |
| 710 | /// only students enroll, so a promoted user leaves every roster — and clears |
| 711 | /// the account's student number in the same transaction: the number names a |
| 712 | /// student of this school, so no window exists where a promoted account still |
| 713 | /// holds one. Demoting below |
| 714 | /// `teacher` drops their course teaching assignments for the mirror reason, and |
| 715 | /// withdraws their published appointment slots, cancelling the live bookings on |
| 716 | /// them: nothing could reach either afterwards — a slot is listed only on its |
| 717 | /// own teacher's calendar and deleted only by a teacher+, and a booking on one |
| 718 | /// is decided only by a teacher+ and cancelled only by its requester, who is |
| 719 | /// refused once the window opens. The requester keeps the booking as |
| 720 | /// `cancelled`, naming the ex-teacher and the reason. |
| 721 | /// Demoting to `parent` additionally gives back the seats they hold on |
| 722 | /// still-open event signup lists: a parent can no longer free them, and nobody |
| 723 | /// else may. Seats on lists that have already closed stay as they are — that |
| 724 | /// roster is history. It also takes the account off every whiteboard roster and |
| 725 | /// **closes every board it created** — permanently read-only, nothing deleted. |
| 726 | /// A demoted creator is a `404` on their own board, the four commands that could |
| 727 | /// end it are the creator's alone, and no route lists a board the caller is not |
| 728 | /// on, so the room would otherwise be commandable by nobody while its |
| 729 | /// participants kept drawing on it. Closed, they keep reading the board, its |
| 730 | /// history and its epochs; only writes are refused. |
| 731 | |
| 732 | |
| 733 | |
| 734 | |
| 735 | |
| 736 | |
| 737 | |
| 738 | |
| 739 | |
| 740 | |
| 741 | |
| 742 | |
| 743 | |
| 744 | |
| 745 | |
| 746 | |
| 747 | |
| 748 | async |
| 749 | |
| 750 | |
| 751 | |
| 752 | |
| 753 | |
| 754 | |
| 755 | |
| 756 | |
| 757 | |
| 758 | |
| 759 | |
| 760 | |
| 761 | |
| 762 | |
| 763 | |
| 764 | |
| 765 | |
| 766 | |
| 767 | |
| 768 | |
| 769 | |
| 770 | |
| 771 | |
| 772 | |
| 773 | |
| 774 | |
| 775 | |
| 776 | |
| 777 | |
| 778 | |
| 779 | |
| 780 | |
| 781 | |
| 782 | |
| 783 | |
| 784 | |
| 785 | |
| 786 | |
| 787 | |
| 788 | |
| 789 | |
| 790 | |
| 791 | |
| 792 | |
| 793 | |
| 794 | |
| 795 | |
| 796 | |
| 797 | |
| 798 | |
| 799 | |
| 800 | |
| 801 | |
| 802 | |
| 803 | |
| 804 | |
| 805 | |
| 806 | |
| 807 | |
| 808 | |
| 809 | |
| 810 | |
| 811 | |
| 812 | |
| 813 | /// One page of a parent's observed students, sorted by username. Shared by the |
| 814 | /// admin listing and the parent's own `/me/students` — they differ only in |
| 815 | /// whose links they read and who may call them. |
| 816 | async |
| 817 | |
| 818 | |
| 819 | |
| 820 | |
| 821 | |
| 822 | |
| 823 | |
| 824 | |
| 825 | |
| 826 | |
| 827 | |
| 828 | |
| 829 | |
| 830 | |
| 831 | |
| 832 | |
| 833 | |
| 834 | |
| 835 | |
| 836 | |
| 837 | |
| 838 | |
| 839 | |
| 840 | |
| 841 | |
| 842 | /// Tie a student to a parent account. Admin only — family ties are school-office |
| 843 | /// records, like roles. `{id}` must hold the `parent` role and `user_id` the |
| 844 | /// `student` role; a parent may observe any number of students. Idempotent: |
| 845 | /// linking the same pair again keeps the one tie (the `linked_by` stamp moves |
| 846 | /// to the latest linker, like re-enrolling). The tie grants the |
| 847 | /// parent read access to the student's marks, attendance, and pomodoro reports |
| 848 | /// — nothing else, and never any write. |
| 849 | |
| 850 | |
| 851 | |
| 852 | |
| 853 | |
| 854 | |
| 855 | |
| 856 | |
| 857 | |
| 858 | |
| 859 | |
| 860 | |
| 861 | |
| 862 | |
| 863 | |
| 864 | |
| 865 | async |
| 866 | |
| 867 | |
| 868 | |
| 869 | |
| 870 | |
| 871 | |
| 872 | |
| 873 | |
| 874 | |
| 875 | |
| 876 | |
| 877 | |
| 878 | |
| 879 | |
| 880 | |
| 881 | |
| 882 | |
| 883 | |
| 884 | |
| 885 | |
| 886 | /// List the students tied to a parent account, sorted by username. Admin only |
| 887 | /// — parents read their own list at `GET /users/me/students`. Paged via |
| 888 | /// `?limit=&offset=` like the other lists. |
| 889 | |
| 890 | |
| 891 | |
| 892 | |
| 893 | |
| 894 | |
| 895 | |
| 896 | |
| 897 | |
| 898 | |
| 899 | |
| 900 | |
| 901 | |
| 902 | |
| 903 | async |
| 904 | |
| 905 | |
| 906 | |
| 907 | |
| 908 | |
| 909 | |
| 910 | |
| 911 | |
| 912 | |
| 913 | |
| 914 | |
| 915 | |
| 916 | /// Untie a student from a parent account. Admin only. The student's data is |
| 917 | /// untouched — only the parent's read grant goes away. |
| 918 | |
| 919 | |
| 920 | |
| 921 | |
| 922 | |
| 923 | |
| 924 | |
| 925 | |
| 926 | |
| 927 | |
| 928 | |
| 929 | |
| 930 | |
| 931 | |
| 932 | |
| 933 | |
| 934 | async |
| 935 | |
| 936 | |
| 937 | : , |
| 938 | ) |
| 939 | |
| 940 | |
| 941 | |
| 942 | |
| 943 | |
| 944 | |
| 945 | |
| 946 | |
| 947 | |
| 948 | |
| 949 | |
| 950 | |
| 951 | |
| 952 | /// The students the calling parent observes, sorted by username. Requires the |
| 953 | /// `parent` role. Each entry's reports live at `GET /marks/{user}`, |
| 954 | /// `GET /attendance/{user}`, and `GET /pomodoro/{user}`. Paged via |
| 955 | /// `?limit=&offset=`. |
| 956 | |
| 957 | |
| 958 | |
| 959 | |
| 960 | |
| 961 | |
| 962 | |
| 963 | |
| 964 | |
| 965 | |
| 966 | |
| 967 | |
| 968 | |
| 969 | async |
| 970 | |
| 971 | |
| 972 | |
| 973 | |
| 974 | |
| 975 | |
| 976 | |
| 977 | |
| 978 | |
| 979 | |
| 980 | // ---- the public profile ----------------------------------------------------- |
| 981 | // The school-wide read half of a user row: who they are, what they belong to, |
| 982 | // and the counters that motivate. Deliberately *not* `UserResponse` — that one |
| 983 | // carries email, phone, and birth date, which keep exactly the gate they have |
| 984 | // today (self, teacher+, admin). Nothing here widens where they are reachable. |
| 985 | // |
| 986 | // The student number is deliberately on `UserResponse` (and `PersonRef`) and |
| 987 | // *not* here: it is the school office's identifier for a student, readable |
| 988 | // wherever the reader is already looking at student identity — the admin user |
| 989 | // surfaces, the pickers, a parent's own children's roster — while this profile |
| 990 | // is readable by every authenticated account school-wide. A number is not a |
| 991 | // secret, but broadcasting one to the whole school is a widening nobody asked |
| 992 | // for. |
| 993 | |
| 994 | /// A user's public profile. Contact details are not part of it, at any role. |
| 995 | |
| 996 | |
| 997 | |
| 998 | |
| 999 | |
| 1000 | |
| 1001 | |
| 1002 | |
| 1003 | |
| 1004 | |
| 1005 | |
| 1006 | |
| 1007 | |
| 1008 | |
| 1009 | |
| 1010 | |
| 1011 | |
| 1012 | |
| 1013 | |
| 1014 | |
| 1015 | |
| 1016 | |
| 1017 | |
| 1018 | |
| 1019 | |
| 1020 | |
| 1021 | |
| 1022 | |
| 1023 | |
| 1024 | |
| 1025 | |
| 1026 | |
| 1027 | |
| 1028 | |
| 1029 | |
| 1030 | |
| 1031 | |
| 1032 | /// One earned badge. Permanent: once it appears here it never leaves, even if |
| 1033 | /// the counter behind it falls back below the threshold that earned it. |
| 1034 | |
| 1035 | |
| 1036 | |
| 1037 | |
| 1038 | |
| 1039 | |
| 1040 | |
| 1041 | |
| 1042 | |
| 1043 | |
| 1044 | |
| 1045 | /// What a stored avatar is, without its bytes. |
| 1046 | |
| 1047 | |
| 1048 | |
| 1049 | |
| 1050 | |
| 1051 | |
| 1052 | |
| 1053 | |
| 1054 | |
| 1055 | /// A class section as a profile shows it: the label, and nothing about who runs |
| 1056 | /// it. The office-side view (creator, homeroom teacher) stays behind |
| 1057 | /// `GET /classes/{id}`, which is teacher+. |
| 1058 | |
| 1059 | |
| 1060 | |
| 1061 | |
| 1062 | |
| 1063 | |
| 1064 | |
| 1065 | |
| 1066 | |
| 1067 | |
| 1068 | |
| 1069 | /// A course as a profile shows it — one row per **class section** the person |
| 1070 | /// is reached by, never the catalog row: the 9-A and 12-A sections of one ders |
| 1071 | /// carry their own resolved titles and hours, and the profile lists both. `id` |
| 1072 | /// is the instance id (`GET /instances/{id}`); `kind` is the catalog row's, |
| 1073 | /// which is the same for every section of one course. |
| 1074 | /// |
| 1075 | /// The class identity rides the profile's **class gate**, not the course one: |
| 1076 | /// `class`, `class_name` and `grade_level` name the section's şube, and a |
| 1077 | /// class name and grade are exactly what `GET /classes/user/{id}` withholds |
| 1078 | /// from a viewer below the [`crate::service::parent_link::ensure_can_observe`] |
| 1079 | /// bar — so this block withholds them from exactly the same viewers (they |
| 1080 | /// read `null`), the same all-or-nothing rule the `classes` block answers to. |
| 1081 | /// A viewer who passes — self, teacher+, a linked parent — reads all three. |
| 1082 | /// `id`, `course`, `title` and `kind` stay populated for everyone the course |
| 1083 | /// gate lets through: the block's purpose is the section, not the class. |
| 1084 | |
| 1085 | |
| 1086 | |
| 1087 | |
| 1088 | |
| 1089 | |
| 1090 | |
| 1091 | |
| 1092 | |
| 1093 | |
| 1094 | |
| 1095 | |
| 1096 | |
| 1097 | |
| 1098 | |
| 1099 | |
| 1100 | |
| 1101 | |
| 1102 | |
| 1103 | |
| 1104 | |
| 1105 | |
| 1106 | |
| 1107 | |
| 1108 | |
| 1109 | |
| 1110 | |
| 1111 | |
| 1112 | |
| 1113 | /// The motivational counters. Derived, never settable, and never `null`: an |
| 1114 | /// account with no rows behind a counter reads a true `0`. |
| 1115 | /// |
| 1116 | /// Two kinds live here. The first four are **computed at read time** from the |
| 1117 | /// rows that exist right now, so deleting the rows moves them down. Every |
| 1118 | /// `*_total` one is a **stored lifetime counter**, incremented as the work |
| 1119 | /// happens and never recomputed — they can therefore outlive the rows behind |
| 1120 | /// them (an exam deleted by its teacher still counts as sat), which is exactly |
| 1121 | /// why a badge earned off them stays earned. Each is named for a `stat` of the |
| 1122 | /// badge catalog at `GET /limits`, plus `_total`, and that spelling is the join |
| 1123 | /// a client makes between the two surfaces — so the suffix is on the key even |
| 1124 | /// where the counter is not a running total (`study_streak_total` is a longest, |
| 1125 | /// not a sum). |
| 1126 | /// |
| 1127 | /// Some are staff counters and some student ones; a role that never does the |
| 1128 | /// work simply reads zero, which is why no key is conditional. |
| 1129 | /// |
| 1130 | /// Every number here is the **owner's** true total for every reader, never |
| 1131 | /// narrowed per viewer — deliberately so even where the rows behind it sit |
| 1132 | /// behind a gate this reader would fail (`GET /pomodoro/{id}`, |
| 1133 | /// `GET /marks/user/{id}`, `GET /attendance/user/{id}`). A magnitude names no |
| 1134 | /// course, class, lesson or exam, and a per-reader counter would make one |
| 1135 | /// profile read differently to different people. |
| 1136 | |
| 1137 | |
| 1138 | |
| 1139 | |
| 1140 | |
| 1141 | |
| 1142 | |
| 1143 | |
| 1144 | |
| 1145 | |
| 1146 | |
| 1147 | |
| 1148 | |
| 1149 | |
| 1150 | |
| 1151 | |
| 1152 | |
| 1153 | |
| 1154 | |
| 1155 | |
| 1156 | |
| 1157 | |
| 1158 | |
| 1159 | |
| 1160 | |
| 1161 | |
| 1162 | |
| 1163 | |
| 1164 | |
| 1165 | |
| 1166 | |
| 1167 | |
| 1168 | |
| 1169 | |
| 1170 | |
| 1171 | |
| 1172 | |
| 1173 | |
| 1174 | |
| 1175 | |
| 1176 | |
| 1177 | |
| 1178 | |
| 1179 | |
| 1180 | |
| 1181 | |
| 1182 | |
| 1183 | |
| 1184 | |
| 1185 | |
| 1186 | /// Build one profile as `viewer` may see it. The course block is chosen off the |
| 1187 | /// owner's **live** role, not off the `creator`/`teachers` columns: those are |
| 1188 | /// historical and no demotion sweeps them, so a demoted ex-teacher lists the |
| 1189 | /// sections they are reached by, like any other student. It lists one row per |
| 1190 | /// *section* and rides [`crate::web::courses::reached_instances`] — the class |
| 1191 | /// roster / homeroom / taught set `GET /instances/me` serves, unioned with the |
| 1192 | /// owner's own enrollment rows — so a section that is theirs to sit or run is |
| 1193 | /// the only thing that appears. |
| 1194 | /// |
| 1195 | /// A section carries a resolved title, and `GET /courses/{id}` hands that title |
| 1196 | /// only to the enrolled, the staff who run it, and manager+ — so the block is |
| 1197 | /// intersected with [`visible_courses`], the same catalog `GET /courses` |
| 1198 | /// serves. Reading your own profile, or reading as manager+, needs no |
| 1199 | /// intersection: both already see the whole list. |
| 1200 | /// |
| 1201 | /// The class block answers to [`ensure_can_observe`], the gate |
| 1202 | /// `GET /classes/user/{id}` holds — teacher+, or a parent linked to the target |
| 1203 | /// — because every other `/classes` read is teacher+ and a class name and grade |
| 1204 | /// are exactly what those routes withhold. That gate is all-or-nothing rather |
| 1205 | /// than per class, so a viewer who fails it gets an empty block, not a 403: the |
| 1206 | /// rest of the profile is still public to them. |
| 1207 | /// |
| 1208 | /// So no *named* thing here crosses a gate the viewer would fail directly. The |
| 1209 | /// magnitudes deliberately do — **every** one of them, not just the two that |
| 1210 | /// count the gated blocks. `stats.courses` and `stats.classes` are the obvious |
| 1211 | /// pair; `pomodoro_focus_ms` and `pomodoro_focus_ms_total` are byte-identical |
| 1212 | /// to the `total_focus_ms` that `GET /pomodoro/{id}` serves behind |
| 1213 | /// [`ensure_can_observe`]; and `lessons_attended_total`, |
| 1214 | /// `homework_submitted_total`, `homework_on_time_total`, `exam_sat_total` and |
| 1215 | /// `high_mark_total` are magnitudes of the same report data that gate holds — |
| 1216 | /// for a reader who is exactly [`Role::Teacher`] they are in fact *wider* than |
| 1217 | /// the reports themselves, which [`crate::web::marks`] and |
| 1218 | /// [`crate::web::attendance`] narrow to the courses that teacher manages. |
| 1219 | /// |
| 1220 | /// That is the decision, not an oversight: every magnitude in `stats` is the |
| 1221 | /// owner's true total for every reader. They are the motivational counters, a |
| 1222 | /// per-reader number would make one profile read differently to different |
| 1223 | /// people, and a magnitude names no course, no class, no lesson and no exam — |
| 1224 | /// the *named* things stay behind their own gates, above. |
| 1225 | async |
| 1226 | |
| 1227 | |
| 1228 | |
| 1229 | |
| 1230 | |
| 1231 | |
| 1232 | |
| 1233 | |
| 1234 | |
| 1235 | |
| 1236 | |
| 1237 | |
| 1238 | |
| 1239 | |
| 1240 | |
| 1241 | |
| 1242 | |
| 1243 | |
| 1244 | |
| 1245 | |
| 1246 | |
| 1247 | |
| 1248 | |
| 1249 | |
| 1250 | |
| 1251 | |
| 1252 | |
| 1253 | |
| 1254 | |
| 1255 | |
| 1256 | |
| 1257 | |
| 1258 | |
| 1259 | |
| 1260 | |
| 1261 | |
| 1262 | |
| 1263 | |
| 1264 | |
| 1265 | |
| 1266 | |
| 1267 | |
| 1268 | |
| 1269 | |
| 1270 | |
| 1271 | |
| 1272 | |
| 1273 | |
| 1274 | |
| 1275 | |
| 1276 | |
| 1277 | |
| 1278 | |
| 1279 | |
| 1280 | |
| 1281 | |
| 1282 | |
| 1283 | |
| 1284 | |
| 1285 | |
| 1286 | |
| 1287 | |
| 1288 | |
| 1289 | |
| 1290 | |
| 1291 | |
| 1292 | |
| 1293 | |
| 1294 | |
| 1295 | |
| 1296 | |
| 1297 | |
| 1298 | |
| 1299 | |
| 1300 | |
| 1301 | |
| 1302 | |
| 1303 | |
| 1304 | |
| 1305 | |
| 1306 | |
| 1307 | |
| 1308 | |
| 1309 | |
| 1310 | |
| 1311 | |
| 1312 | |
| 1313 | |
| 1314 | |
| 1315 | |
| 1316 | |
| 1317 | |
| 1318 | |
| 1319 | |
| 1320 | |
| 1321 | |
| 1322 | |
| 1323 | |
| 1324 | |
| 1325 | |
| 1326 | |
| 1327 | |
| 1328 | |
| 1329 | |
| 1330 | |
| 1331 | |
| 1332 | |
| 1333 | |
| 1334 | |
| 1335 | |
| 1336 | |
| 1337 | |
| 1338 | |
| 1339 | |
| 1340 | |
| 1341 | |
| 1342 | |
| 1343 | |
| 1344 | |
| 1345 | |
| 1346 | |
| 1347 | |
| 1348 | |
| 1349 | |
| 1350 | |
| 1351 | |
| 1352 | |
| 1353 | |
| 1354 | |
| 1355 | |
| 1356 | |
| 1357 | /// The owner's earned badges, and the backstop that keeps them honest. |
| 1358 | /// |
| 1359 | /// Every write that moves a counter syncs the badges behind it, so in the |
| 1360 | /// steady state the earned set holds nothing the award rows do not already |
| 1361 | /// carry and this is one read and **no write at all**. Only a threshold crossed |
| 1362 | /// by a path whose sync was lost — a transient database error, which those |
| 1363 | /// callers log and swallow on purpose — leaves a gap, and that heals here on |
| 1364 | /// the next profile read of that account. |
| 1365 | /// |
| 1366 | /// Nothing here revokes: [`badge::sync`] only ever adds, so a counter that has |
| 1367 | /// since fallen back below its threshold leaves the badge standing. The sync's |
| 1368 | /// own error is logged and dropped, and the awards already read are served — |
| 1369 | /// a decoration may not fail the profile it decorates. |
| 1370 | async |
| 1371 | |
| 1372 | |
| 1373 | |
| 1374 | |
| 1375 | |
| 1376 | |
| 1377 | |
| 1378 | |
| 1379 | |
| 1380 | |
| 1381 | |
| 1382 | |
| 1383 | |
| 1384 | |
| 1385 | |
| 1386 | |
| 1387 | |
| 1388 | |
| 1389 | |
| 1390 | |
| 1391 | /// The profile gate: any authenticated account reads any profile — except a |
| 1392 | /// parent, who is an observer of their own children and of nobody else. The |
| 1393 | /// link alone is not the grant either; [`ensure_can_observe`] re-reads the |
| 1394 | /// target's live role, so a student who was promoted out stops being readable. |
| 1395 | async |
| 1396 | |
| 1397 | |
| 1398 | |
| 1399 | |
| 1400 | |
| 1401 | |
| 1402 | |
| 1403 | |
| 1404 | |
| 1405 | |
| 1406 | /// The target's row, 404 if it is gone, once the caller is allowed to see it. |
| 1407 | async |
| 1408 | |
| 1409 | |
| 1410 | |
| 1411 | |
| 1412 | |
| 1413 | |
| 1414 | |
| 1415 | /// The caller's own public profile — what everyone else sees of them. |
| 1416 | |
| 1417 | |
| 1418 | |
| 1419 | |
| 1420 | |
| 1421 | |
| 1422 | |
| 1423 | |
| 1424 | |
| 1425 | |
| 1426 | async |
| 1427 | |
| 1428 | |
| 1429 | |
| 1430 | |
| 1431 | |
| 1432 | |
| 1433 | /// One user's public profile: display name, bio, avatar meta, their class and |
| 1434 | /// course blocks, the badges they have earned, and the motivational counters. |
| 1435 | /// Badges carry an id and the instant they were first earned; their labels and |
| 1436 | /// icons come from the `badges` catalog at `GET /limits`. Readable by every |
| 1437 | /// authenticated account — except a parent, who reads only their own and their |
| 1438 | /// linked students'. Never carries email, phone, or birth date; those stay on |
| 1439 | /// `GET /users/{id}` (admin) and `GET /auth/me`. The embedded blocks are capped |
| 1440 | /// at `max_profile_classes` / `max_profile_courses` (see `GET /limits`) — the |
| 1441 | /// full paged lists are `GET /classes/me` and `GET /courses/me`. The course |
| 1442 | /// block is also cut to what the *reader* may already see: only courses they |
| 1443 | /// would pass `GET /courses/{id}` on. The class block holds the same bar as |
| 1444 | /// `GET /classes/user/{id}` — teacher+, a parent linked to this student, or the |
| 1445 | /// owner themselves; every other reader gets an empty `classes` array rather |
| 1446 | /// than a 403. Every number in `stats` stays the owner's true total either way, |
| 1447 | /// including the ones whose underlying reports are gated (`/pomodoro/{id}`, |
| 1448 | /// `/marks/user/{id}`, `/attendance/user/{id}`): they are motivational |
| 1449 | /// counters, and a magnitude names no course, class, lesson or exam. |
| 1450 | |
| 1451 | |
| 1452 | |
| 1453 | |
| 1454 | |
| 1455 | |
| 1456 | |
| 1457 | |
| 1458 | |
| 1459 | |
| 1460 | |
| 1461 | |
| 1462 | |
| 1463 | async |
| 1464 | |
| 1465 | |
| 1466 | |
| 1467 | |
| 1468 | |
| 1469 | |
| 1470 | |
| 1471 | |
| 1472 | // ---- the avatar ------------------------------------------------------------- |
| 1473 | // One optional picture per user, the same one-image-on-the-owning-row shape the |
| 1474 | // question pool uses: bytes on disk under a server-generated ULID, metadata on |
| 1475 | // the user row. Every write here must take the replaced blob off disk — no |
| 1476 | // route deletes a user, so nothing else would ever collect it. |
| 1477 | |
| 1478 | /// Upload (or replace) the caller's own avatar. `multipart/form-data` with the |
| 1479 | /// image under a `file` field; the declared content type must be `image/png`, |
| 1480 | /// `image/jpeg`, `image/webp`, or `image/gif` (rasters only — no SVG), the |
| 1481 | /// bytes at most the school's `max_file_bytes` (settings). Replacing one drops |
| 1482 | /// the previous picture. |
| 1483 | |
| 1484 | |
| 1485 | |
| 1486 | |
| 1487 | |
| 1488 | |
| 1489 | |
| 1490 | |
| 1491 | |
| 1492 | |
| 1493 | |
| 1494 | |
| 1495 | |
| 1496 | |
| 1497 | async |
| 1498 | |
| 1499 | |
| 1500 | |
| 1501 | |
| 1502 | |
| 1503 | |
| 1504 | |
| 1505 | |
| 1506 | |
| 1507 | |
| 1508 | |
| 1509 | |
| 1510 | |
| 1511 | |
| 1512 | |
| 1513 | |
| 1514 | |
| 1515 | |
| 1516 | |
| 1517 | |
| 1518 | |
| 1519 | |
| 1520 | |
| 1521 | |
| 1522 | |
| 1523 | |
| 1524 | |
| 1525 | |
| 1526 | |
| 1527 | |
| 1528 | |
| 1529 | |
| 1530 | |
| 1531 | |
| 1532 | /// The caller's own avatar bytes. The self alias of `GET /{id}/avatar` — the |
| 1533 | /// static `/me/avatar` segment wins over `/{id}/avatar` in the router, so |
| 1534 | /// without this a client that never learned its own id gets a bodyless `405` |
| 1535 | /// on the obvious route. It serves the caller's own row and nothing else, so |
| 1536 | /// it asks no gate: the session already proves the reach. |
| 1537 | |
| 1538 | |
| 1539 | |
| 1540 | |
| 1541 | |
| 1542 | |
| 1543 | |
| 1544 | |
| 1545 | |
| 1546 | |
| 1547 | |
| 1548 | async |
| 1549 | |
| 1550 | |
| 1551 | |
| 1552 | |
| 1553 | |
| 1554 | |
| 1555 | /// The avatar bytes. Same reach as the profile itself: every authenticated |
| 1556 | /// account, except a parent, who is limited to their own and their linked |
| 1557 | /// students'. |
| 1558 | |
| 1559 | |
| 1560 | |
| 1561 | |
| 1562 | |
| 1563 | |
| 1564 | |
| 1565 | |
| 1566 | |
| 1567 | |
| 1568 | |
| 1569 | |
| 1570 | |
| 1571 | async |
| 1572 | |
| 1573 | |
| 1574 | |
| 1575 | |
| 1576 | |
| 1577 | |
| 1578 | |
| 1579 | |
| 1580 | /// The shared tail of both avatar reads: the bytes, or a `404` when the row |
| 1581 | /// carries none. The caller owns the gate — this one is reached already. |
| 1582 | async |
| 1583 | |
| 1584 | |
| 1585 | |
| 1586 | |
| 1587 | |
| 1588 | |
| 1589 | |
| 1590 | |
| 1591 | /// Remove the caller's own avatar. |
| 1592 | |
| 1593 | |
| 1594 | |
| 1595 | |
| 1596 | |
| 1597 | |
| 1598 | |
| 1599 | |
| 1600 | |
| 1601 | |
| 1602 | |
| 1603 | async |
| 1604 | |
| 1605 | |
| 1606 | |
| 1607 | |
| 1608 | |
| 1609 | |
| 1610 | /// Remove any user's avatar. Admin only — the moderation path: an offensive |
| 1611 | /// picture is a school problem, and no route deletes the account it hangs on. |
| 1612 | |
| 1613 | |
| 1614 | |
| 1615 | |
| 1616 | |
| 1617 | |
| 1618 | |
| 1619 | |
| 1620 | |
| 1621 | |
| 1622 | |
| 1623 | |
| 1624 | |
| 1625 | async |
| 1626 | |
| 1627 | |
| 1628 | |
| 1629 | |
| 1630 | |
| 1631 | |
| 1632 | |
| 1633 | /// Clear the row's avatar and take its blob off disk — the shared tail of the |
| 1634 | /// self and moderation deletes. A row without one (or no row at all) is a 404. |
| 1635 | async |
| 1636 | |
| 1637 | |
| 1638 | |
| 1639 | |
| 1640 | |
| 1641 | |
| 1642 | |
| 1643 | |
| 1644 | |
| 1645 | |
| 1646 | |
| 1647 | |
| 1648 | |
| 1649 | |
| 1650 | |
| 1651 | |
| 1652 | |
| 1653 | |
| 1654 | |
| 1655 | |
| 1656 | |
| 1657 | |
| 1658 | |
| 1659 | |
| 1660 | |
| 1661 | |
| 1662 | |
| 1663 | |
| 1664 | |
| 1665 | |
| 1666 | |
| 1667 | |
| 1668 | |
| 1669 | |
| 1670 | |
| 1671 | |
| 1672 | |
| 1673 | |
| 1674 | |
| 1675 | |
| 1676 | |
| 1677 | |
| 1678 | |
| 1679 | |
| 1680 | |
| 1681 | |
| 1682 | |
| 1683 | |
| 1684 | |
| 1685 | |
| 1686 | |
| 1687 | |
| 1688 | |
| 1689 | |
| 1690 | |
| 1691 |