headless_lms_models/library/credit_registration/
pending_reason.rs1use utoipa::ToSchema;
9
10use crate::prelude::*;
11
12#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone, Copy, Hash, ToSchema)]
14#[serde(rename_all = "snake_case")]
15pub enum CreditRegistrationPendingReason {
16 Completion,
18 StudentNumber,
19 CourseCode,
21}
22
23#[derive(Debug, Clone, Copy, PartialEq, Eq)]
25pub struct PendingPreconditions {
26 pub completion_eligible: bool,
27 pub has_verified_student_number: bool,
28 pub course_code_allowed: bool,
29}
30
31impl PendingPreconditions {
32 pub const ALL_MET: Self = Self {
34 completion_eligible: true,
35 has_verified_student_number: true,
36 course_code_allowed: true,
37 };
38
39 pub fn reason(self) -> Option<CreditRegistrationPendingReason> {
46 if !self.completion_eligible {
47 Some(CreditRegistrationPendingReason::Completion)
48 } else if !self.has_verified_student_number {
49 Some(CreditRegistrationPendingReason::StudentNumber)
50 } else if !self.course_code_allowed {
51 Some(CreditRegistrationPendingReason::CourseCode)
52 } else {
53 None
54 }
55 }
56}
57
58#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone, Copy, Default, ToSchema)]
60pub struct PendingReasonCounts {
61 pub completion_count: i64,
62 pub student_number_count: i64,
63}
64
65#[cfg(test)]
66mod tests {
67 use super::*;
68 use CreditRegistrationPendingReason as Reason;
69
70 #[test]
72 fn the_first_unmet_precondition_is_the_one_reported() {
73 assert_eq!(PendingPreconditions::ALL_MET.reason(), None);
74 assert_eq!(
75 PendingPreconditions {
76 completion_eligible: false,
77 has_verified_student_number: false,
78 course_code_allowed: false,
79 }
80 .reason(),
81 Some(Reason::Completion)
82 );
83 assert_eq!(
84 PendingPreconditions {
85 has_verified_student_number: false,
86 ..PendingPreconditions::ALL_MET
87 }
88 .reason(),
89 Some(Reason::StudentNumber)
90 );
91 assert_eq!(
92 PendingPreconditions {
93 course_code_allowed: false,
94 ..PendingPreconditions::ALL_MET
95 }
96 .reason(),
97 Some(Reason::CourseCode)
98 );
99 }
100}