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

term.rs133 lines, 5.1 KiBHistoryRawDownload
1//! Term workflows: the archived-refusal rule every write against past
2//! structure pays, the id resolver a request-supplied term passes through,
3//! the row-level write gate, the delete's linked-refusal mapping, and the
4//! archive that freezes every report card inside the term. The queries live in
5//! [`crate::db::term`].
6
7use crate::database::Database;
8use crate::db::term;
9use crate::domain::academic_year::AcademicYearId;
10use crate::domain::term::{Term, TermId, TermName, archived_error};
11use crate::domain::timestamp::Timestamp;
12use crate::error::{AppError, ValidationError};
13
14/// Mint a term inside an academic year. The year is claimed in the same
15/// statement as the row (see [`term::create`]): past years take no new
16/// structure, which is why the web layer resolves the id through
17/// [`crate::service::academic_year::resolve`] first.
18pub async fn create(
19 db: &Database,
20 name: TermName,
21 year: AcademicYearId,
22 starts_at: Timestamp,
23 ends_at: Timestamp,
24) -> Result<Term, AppError> {
25 term::create(db, name, year, starts_at, ends_at).await
26}
27
28/// The term row, for callers that only inspect it — the web layer's gates
29/// read through here.
30pub async fn read(db: &Database, id: &TermId) -> Result<Option<Term>, AppError> {
31 term::read(db, id).await
32}
33
34pub async fn list_all(
35 db: &Database,
36 limit: Option<i64>,
37 offset: i64,
38) -> Result<(Vec<Term>, i64), AppError> {
39 term::list_all(db, limit, offset).await
40}
41
42/// Turn an optional request-supplied term id into a validated reference —
43/// `None` stays `None`, an unknown id is a `400` naming the field, and an
44/// *archived* one is a `409 term_archived`. That last refusal lives here
45/// rather than in each handler because this is the single spot every
46/// request-supplied term passes through (an exam names one): past years take
47/// no new structure.
48pub async fn resolve(db: &Database, id: Option<&str>) -> Result<Option<TermId>, AppError> {
49 let Some(id) = id else {
50 return Ok(None);
51 };
52 let resolved = term::read(db, &TermId::from_key(id))
53 .await?
54 .ok_or(AppError::Validation(ValidationError::Invalid {
55 field: "term_id",
56 reason: "term does not exist",
57 }))?;
58 if resolved.is_archived() {
59 return Err(archived_error());
60 }
61 Ok(Some(*resolved.get_id()))
62}
63
64/// Refuse when the named term is archived. A missing row is `Ok(())`: a
65/// dangling link is not this guard's error, and the caller that cares
66/// already answers it (see [`crate::domain::term::gone_error`]).
67pub async fn require_open(db: &Database, id: &TermId) -> Result<(), AppError> {
68 match term::read(db, id).await? {
69 Some(term) if term.is_archived() => Err(archived_error()),
70 _ => Ok(()),
71 }
72}
73
74/// Refuse when this term row is archived — the read-only rule for past
75/// structure, judged against a row the caller already read through [`read`];
76/// a missing row is the caller's 404, not this guard's answer.
77pub fn require_writable(t: &Term) -> Result<(), AppError> {
78 if t.is_archived() {
79 return Err(archived_error());
80 }
81 Ok(())
82}
83
84/// Update an open term; an archived one is refused — past years are
85/// read-only.
86pub async fn update(
87 db: &Database,
88 target: Term,
89 name: Option<TermName>,
90 starts_at: Option<Timestamp>,
91 ends_at: Option<Timestamp>,
92) -> Result<Term, AppError> {
93 term::update(db, target, name, starts_at, ends_at).await
94}
95
96/// Delete the term while nothing links it. The linked-refusal is the rule's
97/// own answer, so the message the web layer used to pick is coded here; a
98/// row that is already gone is the store's `404` ([`term::delete`]).
99pub async fn delete(db: &Database, target: Term) -> Result<(), AppError> {
100 if term::delete(db, target).await? {
101 Ok(())
102 } else {
103 Err(AppError::Conflict(
104 "exams are still linked to this dönem, or a karne was frozen for it — \
105 nothing unlinks them for you",
106 ))
107 }
108}
109
110/// Archive a term; idempotent — an already-archived term answers with the
111/// stamp it already had.
112///
113/// The archive freezes every student's report card for the term first
114/// ([`crate::service::karne::freeze`]): from the moment the term is closed
115/// the report is the record the school issued, and a later correction to a
116/// mark must not silently rewrite what a family holds. The freeze runs *before*
117/// the stamp so a failure leaves nothing archived-but-unfrozen — an archived
118/// term whose snapshot is missing would go on serving live computations, and
119/// the store's delete guard (which refuses a term a report card was frozen
120/// for) would not see it either. A term that is already archived is left
121/// alone: a second archive must not re-freeze over a record that was already
122/// issued.
123pub async fn archive(db: &Database, target: Term) -> Result<Term, AppError> {
124 if !target.is_archived() {
125 crate::service::karne::freeze(db, target.get_id()).await?;
126 }
127 term::archive(db, target).await
128}
129
130/// Re-open an archived term; idempotent the same way.
131pub async fn unarchive(db: &Database, target: Term) -> Result<Term, AppError> {
132 term::unarchive(db, target).await
133}