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

fee_plan_assignment.rs117 lines, 4.2 KiBHistoryRawDownload
1//! One fee plan placed on one student — and the moment the plan becomes money
2//! owed: assigning appends *every* installment of the plan as a charge line at
3//! once, each carrying its own due date.
4//!
5//! The row is keyed (plan, student), and every charge it raises is keyed from
6//! that same pair plus the installment number, so the whole operation is
7//! idempotent **by identity**: assigning twice writes nothing the second time,
8//! however the two requests interleave, and an assign cut short after three of
9//! twelve charges landed completes itself when it is repeated. No scan decides
10//! "has this been billed yet?" — a scan can be slipped past by a concurrent
11//! writer, and the cost of that mistake here is a double-billed family.
12//!
13//! Nothing is ever edited or deleted: unassigning is not a thing, because the
14//! charges are already history. A plan raised in error is undone by reversing
15//! its charges. That is also why the plan's assignment refcount is only ever
16//! claimed and never released — a plan, once assigned, stays frozen for good.
17
18use crate::domain::fee_plan::FeePlanId;
19use crate::domain::timestamp::Timestamp;
20use crate::domain::user::UserId;
21
22/// The identity of one (plan, student) pair. Not a row column: the table's
23/// primary key *is* the pair — one assignment per pair by construction — and
24/// this struct's job is the underscore-joined wire form at the HTTP edge.
25#[derive(Debug, Clone, PartialEq, Eq)]
26pub struct FeePlanAssignmentId {
27 pub(crate) plan: FeePlanId,
28 pub(crate) student: UserId,
29}
30
31impl FeePlanAssignmentId {
32 pub fn composite(plan: &FeePlanId, student: &UserId) -> Self {
33 Self {
34 plan: plan.clone(),
35 student: *student,
36 }
37 }
38
39 /// Splits a wire key back into its pair. A malformed key (no `_` joiner,
40 /// unparseable halves) yields nil components, which match no row — the
41 /// same 404 a well-formed but absent key gets.
42 pub fn from_key(key: &str) -> Self {
43 let (plan, student) = match key.split_once('_') {
44 Some((plan, student)) => (plan, student),
45 None => ("", ""),
46 };
47 Self {
48 plan: FeePlanId::from_key(plan),
49 student: UserId::from_key(student),
50 }
51 }
52
53 /// The underscore-joined wire form (`{plan}_{student}`).
54 pub fn key(&self) -> String {
55 format!("{}_{}", self.plan.key(), self.student.key())
56 }
57}
58
59#[derive(Debug, Clone, sqlx::FromRow)]
60pub struct FeePlanAssignment {
61 pub(crate) plan: FeePlanId,
62 pub(crate) student: UserId,
63 pub(crate) assigned_by: UserId,
64 pub(crate) created_at: Timestamp,
65}
66
67impl FeePlanAssignment {
68 /// The row's identity, built back from its primary-key columns.
69 pub fn get_id(&self) -> FeePlanAssignmentId {
70 FeePlanAssignmentId::composite(&self.plan, &self.student)
71 }
72
73 pub fn get_plan(&self) -> &FeePlanId {
74 &self.plan
75 }
76
77 pub fn get_student(&self) -> &UserId {
78 &self.student
79 }
80
81 pub fn get_assigned_by(&self) -> &UserId {
82 &self.assigned_by
83 }
84
85 pub fn get_created_at(&self) -> Timestamp {
86 self.created_at
87 }
88}
89
90#[cfg(test)]
91mod tests {
92 use super::*;
93
94 const PLAN: &str = "0198f1a2-0000-7000-8000-000000000001";
95 const STUDENT: &str = "0198f1a2-1111-7000-8000-000000000002";
96
97 /// The wire key is the pair, and the pair survives the round trip — this
98 /// is what lets a charge line's `assignment` reference travel as one path
99 /// segment.
100 #[test]
101 fn the_wire_key_round_trips_through_the_pair() {
102 let plan = FeePlanId::from_key(PLAN);
103 let student = UserId::from_key(STUDENT);
104 let id = FeePlanAssignmentId::composite(&plan, &student);
105 assert_eq!(id.key(), format!("{PLAN}_{STUDENT}"));
106 assert_eq!(FeePlanAssignmentId::from_key(&id.key()), id);
107 }
108
109 /// A key that is not a pair cannot name an assignment: it decodes to nil
110 /// components, which match no row (a 404, never a panic).
111 #[test]
112 fn a_malformed_key_decodes_to_something_that_matches_nothing() {
113 let junk = FeePlanAssignmentId::from_key("not-a-pair");
114 assert_eq!(junk.plan.key(), uuid::Uuid::nil().to_string());
115 assert_eq!(junk.student.key(), uuid::Uuid::nil().to_string());
116 }
117}