forge
Explore
Log in

Don't you dare go Hollow

hezarfen_backend

Clone
★ 0

0 watching0 forksRust8.3 MiB6d68754Ahmet Kaan Gümüş2 months ago566 commits

school.rs66 lines, 2.3 KiBHistoryRawDownload
1//! The caller's own school, on the school side of the line.
2//!
3//! `GET /schools` is the vendor registry and answers only a builder cookie.
4//! This route is the inverse: the school the session cookie is already bound
5//! to, readable by every authenticated role. The uuid is the identity; nothing but the name is on the wire besides the uuid.
6
7use axum::Json;
8use serde::Serialize;
9use utoipa::ToSchema;
10use utoipa_axum::router::OpenApiRouter;
11use utoipa_axum::routes;
12
13use crate::error::{AppError, ErrorResponse};
14use crate::state::AppState;
15use crate::tenant::{ResolvedTenant, School};
16use crate::web::CurrentUser;
17use crate::web::tenant_state::State;
18
19pub fn routes() -> OpenApiRouter<AppState> {
20 OpenApiRouter::new().routes(routes!(current_school))
21}
22
23/// The school behind the session cookie. `id` is the registry uuid — the
24/// identity the database name and every structural reference mint from — and
25/// `name` is the display name. Nothing but the uuid and the display name is published.
26#[derive(Serialize, ToSchema)]
27struct SchoolIdentity {
28 #[schema(example = "019732e3-7b00-7000-8000-00000000dead")]
29 id: String,
30 #[schema(example = "Ata Koleji")]
31 name: String,
32}
33
34/// The school the session cookie is bound to: its uuid and display name.
35///
36/// Any authenticated school user may read it, including a student or a parent.
37/// A builder cookie names no school and is `401`, the same as on every other
38/// school surface.
39#[utoipa::path(
40 get,
41 path = "/school",
42 tag = "school",
43 security(("session_cookie" = [])),
44 responses(
45 (status = 200, description = "The school this session is bound to", body = SchoolIdentity),
46 (status = 401, description = "Not authenticated", body = ErrorResponse),
47 ),
48)]
49async fn current_school(
50 State(st): State<AppState>,
51 tenant: ResolvedTenant,
52 _user: CurrentUser,
53) -> Result<Json<SchoolIdentity>, AppError> {
54 let school = School::read(&tenant.id, st.tenants.control())
55 .await?
56 .ok_or_else(|| {
57 AppError::Internal(format!(
58 "the resolved school `{}` has no control row",
59 tenant.id
60 ))
61 })?;
62 Ok(Json(SchoolIdentity {
63 id: school.get_id().uuid().to_string(),
64 name: school.name().to_string(),
65 }))
66}