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

registration.rs81 lines, 3.7 KiBHistoryRawDownload
1//! The registration workflow: taking a seat on a registration-audience
2//! event. The reads and the seat-returning delete are
3//! [`crate::db::registration`]; the seat itself is [`cap`]'s
4//! claim-on-the-event-row.
5
6use crate::database::Database;
7use crate::db::cap::Claimed;
8use crate::db::registration;
9use crate::domain::event::EventId;
10use crate::domain::registration::Registration;
11use crate::domain::role::Role;
12use crate::domain::user::UserId;
13use crate::error::AppError;
14
15/// Register (idempotently) `user` onto `event`, refusing when a capacity
16/// cap is set and every seat is taken. Someone already listed gets their
17/// existing row back untouched — a true no-op that never counts against
18/// the cap and never rewrites who placed them. The seat is taken by
19/// [`cap::claim_live_and_create`] on the event row — an atomic single-record
20/// conditional write that reads `audience.capacity` off that same row as it
21/// decides, so neither a racing registration nor a concurrent PATCH
22/// *lowering* the capacity can over-admit. Bound as a number instead, the
23/// snapshot below would admit every request already in flight when the
24/// lower cap landed.
25///
26/// The seat is claimed on the event row, so the same statement says nothing
27/// about the *user* it is for — and a fall to `parent` is the one demotion
28/// whose sweep ([`crate::service::user::set_role`]) can miss a seat and
29/// leave it unfreeable: `unregister` refuses a non-student target and a
30/// parent cannot reach the route at all. So the transaction also claims the
31/// holder's own record ([`cap::role_claim`]), which is the key that
32/// demotion writes: either the sweep sees this seat, or this write sees the
33/// parent and takes it back.
34pub async fn register(
35 db: &Database,
36 event: &EventId,
37 user: &UserId,
38 registered_by: &UserId,
39) -> Result<Registration, AppError> {
40 if let Some(existing) = registration::read_for_user(db, event, user).await? {
41 return Ok(existing);
42 }
43 // Read for its refusals only — the audience kind and the closing time.
44 // The seat count itself is re-read by the claim.
45 crate::db::event::read(db, event)
46 .await?
47 .ok_or(AppError::NotFound)?
48 .registration_capacity()?;
49 match registration::claim_seat(db, event, user, registered_by).await? {
50 Claimed::Made(created) => Ok(created),
51 // A concurrent placement of the same pair got there first: hand its
52 // row over, the same no-op the early return above would have made,
53 // and with no seat spent either way.
54 Claimed::Duplicate => registration::read_for_user(db, event, user)
55 .await?
56 .ok_or_else(|| AppError::Internal("failed to register user".into())),
57 // Full, the event was deleted between the read and the claim, or
58 // the holder fell to parent while this ran — the conditional writes
59 // match nothing (or throw) either way, and only this path pays for
60 // the reads that tell them apart.
61 Claimed::Full => match crate::db::event::read(db, event).await? {
62 None => Err(AppError::NotFound),
63 Some(_) => match crate::db::user::read(db, user).await? {
64 Some(held) if held.get_role() == Role::Parent => Err(AppError::Forbidden(
65 "that account was demoted to parent while this request ran — \
66 only students can be registered for",
67 )),
68 _ => Err(AppError::Conflict("the event is full")),
69 },
70 },
71 }
72}
73
74/// Take a user off the list; `Some` iff they held a seat.
75pub async fn remove(
76 db: &Database,
77 event: &EventId,
78 user: &UserId,
79) -> Result<Option<Registration>, AppError> {
80 registration::remove(db, event, user).await
81}