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 pub course_code_count: i64,
64}
65
66#[cfg(test)]
67mod tests {
68 use super::*;
69 use CreditRegistrationPendingReason as Reason;
70
71 #[test]
73 fn the_first_unmet_precondition_is_the_one_reported() {
74 assert_eq!(PendingPreconditions::ALL_MET.reason(), None);
75 assert_eq!(
76 PendingPreconditions {
77 completion_eligible: false,
78 has_verified_student_number: false,
79 course_code_allowed: false,
80 }
81 .reason(),
82 Some(Reason::Completion)
83 );
84 assert_eq!(
85 PendingPreconditions {
86 has_verified_student_number: false,
87 ..PendingPreconditions::ALL_MET
88 }
89 .reason(),
90 Some(Reason::StudentNumber)
91 );
92 assert_eq!(
93 PendingPreconditions {
94 course_code_allowed: false,
95 ..PendingPreconditions::ALL_MET
96 }
97 .reason(),
98 Some(Reason::CourseCode)
99 );
100 }
101}