fplus_lib/core/application/
allocation.rs

1use chrono::Utc;
2
3use super::file::{
4    Allocation, AllocationRequest, AllocationRequestType, Allocations, Notaries, Notary,
5};
6
7impl Default for Notaries {
8    fn default() -> Self {
9        Self(vec![])
10    }
11}
12
13impl Notaries {
14    pub fn add(&self, signer: Notary) -> Self {
15        let mut res = self.0.clone();
16        res.push(signer);
17        Self(res)
18    }
19}
20
21impl AllocationRequest {
22    pub fn new(
23        actor: String,
24        id: String,
25        kind: AllocationRequestType,
26        allocation_amount: String,
27    ) -> Self {
28        Self {
29            actor,
30            id,
31            kind,
32            allocation_amount,
33            is_active: true,
34        }
35    }
36}
37
38impl Allocation {
39    pub fn new(request_information: AllocationRequest) -> Self {
40        Self {
41            id: request_information.id,
42            request_type: request_information.kind.to_string(),
43            created_at: Utc::now().to_string(),
44            updated_at: Utc::now().to_string(),
45            is_active: true,
46            amount: request_information.allocation_amount,
47            signers: Notaries::default(),
48        }
49    }
50}
51
52impl Default for Allocations {
53    fn default() -> Self {
54        Self(vec![])
55    }
56}
57
58impl Allocations {
59    pub fn init(request_information: AllocationRequest) -> Self {
60        let allocation = Allocation::new(request_information);
61        Self(vec![allocation])
62    }
63
64    // should be changed to option
65    pub fn active(&self) -> Option<Allocation> {
66        let curr: Vec<Allocation> = self.0.clone();
67        let mut allocation: Option<Allocation> = None;
68        for alloc in curr.iter() {
69            if alloc.is_active {
70                allocation = Some(alloc.clone());
71                break;
72            }
73        }
74        allocation
75    }
76
77    // should be changed to option
78    pub fn find_one(&self, request_id: String) -> Option<Allocation> {
79        let curr: Vec<Allocation> = self.0.clone();
80        let mut allocation: Option<Allocation> = None;
81        for alloc in curr.iter() {
82            if alloc.id == request_id {
83                allocation = Some(alloc.clone());
84                break;
85            }
86        }
87        allocation
88    }
89
90    // should be changed to option
91    pub fn is_active(&self, request_id: String) -> bool {
92        let curr: Vec<Allocation> = self.0.clone();
93        let mut is_active = false;
94        for alloc in curr.iter() {
95            if alloc.id == request_id {
96                is_active = alloc.is_active;
97                break;
98            }
99        }
100        is_active
101    }
102
103    pub fn add_signer(&self, request_id: String, signer: Notary) -> Self {
104        let mut res: Vec<Allocation> = self.0.clone();
105        for allocation in res.iter_mut() {
106            if allocation.id == request_id && allocation.is_active {
107                allocation.signers = allocation.signers.add(signer);
108                break;
109            }
110        }
111        Self(res)
112    }
113
114    pub fn add_signer_and_complete(&self, request_id: String, signer: Notary) -> Self {
115        let mut res: Vec<Allocation> = self.0.clone();
116        for allocation in res.iter_mut() {
117            if allocation.id == request_id && allocation.is_active {
118                allocation.signers = allocation.signers.add(signer);
119                allocation.is_active = false;
120                break;
121            }
122        }
123        Self(res)
124    }
125
126    pub fn complete_allocation(&self, request_id: String) -> Self {
127        let mut res: Vec<Allocation> = self.0.clone();
128        for allocation in res.iter_mut() {
129            if allocation.id == request_id && allocation.is_active {
130                allocation.is_active = false;
131            }
132        }
133        Self(res)
134    }
135
136    pub fn push(&mut self, request: AllocationRequest) -> Self {
137        let allocation = Allocation::new(request);
138        self.0.push(allocation);
139        self.clone()
140    }
141}