1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
use chrono::prelude::*;

use super::file::{AppState, LifeCycle};

impl AppState {
    pub fn as_str(&self) -> &str {
        match *self {
            AppState::Submitted => "Submitted",
            AppState::ReadyToSign => "Ready to Sign Datacap",
            AppState::StartSignDatacap => "Start Sign Datacap",
            AppState::Granted => "Granted",
            AppState::TotalDatacapReached => "Total Datacap Reached",
            AppState::Error => "Error",
        }
    }
}

impl LifeCycle {
    pub fn submitted(client_on_chain_address: String, multisig_address: String) -> Self {
        let empty = "".to_string();
        LifeCycle {
            state: AppState::Submitted,
            validated_at: empty.clone(),
            validated_by: empty.clone(),
            is_active: true,
            updated_at: Utc::now().to_string(),
            active_request: Some(empty),
            client_on_chain_address,
            multisig_address,
        }
    }

    /// Change Application state to Proposal from Governance Review
    /// Actor input is the actor who is changing the state
    pub fn finish_governance_review(&self, actor: String, current_allocation_id: String) -> Self {
        LifeCycle {
            state: AppState::ReadyToSign,
            validated_by: actor,
            validated_at: Utc::now().to_string(),
            updated_at: Utc::now().to_string(),
            active_request: Some(current_allocation_id),
            ..self.clone()
        }
    }

    pub fn finish_proposal(&self) -> Self {
        LifeCycle {
            state: AppState::StartSignDatacap,
            updated_at: Utc::now().to_string(),
            ..self.clone()
        }
    }

    pub fn finish_approval(&self) -> Self {
        LifeCycle {
            state: AppState::Granted,
            updated_at: Utc::now().to_string(),
            ..self.clone()
        }
    }

    pub fn get_state(&self) -> AppState {
        let res = self.state.clone();
        res
    }

    pub fn start_refill_request(&self, request_id: String) -> Self {
        LifeCycle {
            state: AppState::ReadyToSign,
            updated_at: Utc::now().to_string(),
            active_request: Some(request_id),
            ..self.clone()
        }
    }

    pub fn get_active_allocation_id(self) -> Option<String> {
        self.active_request
    }

    pub fn reached_total_datacap(self) -> Self {
        let empty = "".to_string();

        LifeCycle {
            is_active: false,
            updated_at: Utc::now().to_string(),
            active_request: Some(empty),
            ..self
        }
    }

    pub fn move_back_to_governance_review(self) -> Self {
        let empty = "".to_string();

        LifeCycle {
            state: AppState::Submitted,
            validated_at: empty.clone(),
            validated_by: empty.clone(),
            updated_at: Utc::now().to_string(),
            active_request: Some(empty),
            ..self
        }
    }
}