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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
pub use super::*;

impl<T: Config> Pallet<T> {
	// Helper function for approving sellers.
	pub fn approve_seller(sender: T::AccountId, who: T::AccountId) -> bool {
		let sellers = Self::get_pending_house_sellers();
		let mut exist = false;

		for (index, sell) in sellers.iter().enumerate() {
			if sell.account_id == who.clone() {
				let mut seller = sell.clone();
				seller.activated = true;
				seller.verifier = sender;
				HouseSellerLog::<T>::insert(&who, seller);
				SellerApprovalList::<T>::mutate(|list| {
					list.remove(index);
				});
				AccountsRolesLog::<T>::insert(&who, Accounts::SELLER);
				let now = <frame_system::Pallet<T>>::block_number();
				Self::deposit_event(Event::SellerCreated(now, who));
				exist = true;
				break;
			}
		}
		exist
	}

	// Helper function for approving servicers
	pub fn approve_servicer(sender: T::AccountId, who: T::AccountId) -> bool {
		let servicers = Self::get_pending_servicers();
		let mut exist = false;

		for (index, serv) in servicers.iter().enumerate() {
			if serv.account_id == who.clone() {
				let mut servicer = serv.clone();
				servicer.activated = true;
				servicer.verifier = sender;
				ServicerLog::<T>::insert(&who, servicer);
				ServicerApprovalList::<T>::mutate(|list| {
					list.remove(index);
				});
				AccountsRolesLog::<T>::insert(&who, Accounts::SERVICER);
				let now = <frame_system::Pallet<T>>::block_number();
				Self::deposit_event(Event::ServicerCreated(now, who));
				exist = true;
				break;
			}
		}
		exist
	}

	// Helper function for approving notaries
	pub fn approve_notary(sender: T::AccountId, who: T::AccountId) -> bool {
		let notaries = Self::get_pending_notaries();
		let mut exist = false;

		for (index, notary) in notaries.iter().enumerate() {
			if notary.account_id == who.clone() {
				let mut notary_ = notary.clone();
				notary_.activated = true;
				notary_.verifier = sender;
				NotaryLog::<T>::insert(&who, notary_);
				NotaryApprovalList::<T>::mutate(|list| {
					list.remove(index);
				});
				AccountsRolesLog::<T>::insert(&who, Accounts::NOTARY);
				let now = <frame_system::Pallet<T>>::block_number();
				Self::deposit_event(Event::NotaryCreated(now, who));
				exist = true;
				break;
			}
		}
		exist
	}

	//Helper function for account creation approval by admin only
	pub fn approve_account(sender: T::AccountId, who: T::AccountId) -> DispatchResult {
		let role = Self::get_requested_role(who.clone());
		ensure!(role.is_some(), Error::<T>::NotInWaitingList);
		let role = role.unwrap();
		let success = match role {
			Accounts::SELLER => Self::approve_seller(sender, who),
			Accounts::SERVICER => Self::approve_servicer(sender, who),
			Accounts::NOTARY => Self::approve_notary(sender, who),
			_ => false,
		};
		ensure!(success, Error::<T>::NotInWaitingList);
		Self::increase_total_members()
	}

	// TODO: This function can be updated
	pub fn check_account_role(caller: T::AccountId) -> DispatchResult {
		ensure!(!HouseSellerLog::<T>::contains_key(&caller), Error::<T>::OneRoleAllowed);
		ensure!(!InvestorLog::<T>::contains_key(&caller), Error::<T>::OneRoleAllowed);
		ensure!(!ServicerLog::<T>::contains_key(&caller), Error::<T>::OneRoleAllowed);
		ensure!(!TenantLog::<T>::contains_key(&caller), Error::<T>::OneRoleAllowed);
		ensure!(!RepresentativeLog::<T>::contains_key(&caller), Error::<T>::OneRoleAllowed);
		ensure!(Self::total_members() < T::MaxMembers::get(), Error::<T>::TotalMembersExceeded);
		Ok(())
	}

	pub fn reject_seller(who: T::AccountId) -> bool {
		let sellers = Self::get_pending_house_sellers();
		let mut exist = false;
		for (index, sell) in sellers.iter().enumerate() {
			if sell.account_id == who.clone() {
				SellerApprovalList::<T>::mutate(|list| {
					list.remove(index);
				});
				let now = <frame_system::Pallet<T>>::block_number();
				Self::deposit_event(Event::SellerAccountCreationRejected(now, who));
				exist = true;
				break;
			}
		}
		exist
	}

	pub fn reject_servicer(who: T::AccountId) -> bool {
		let servicers = Self::get_pending_servicers();
		let mut exist = false;

		for (index, serv) in servicers.iter().enumerate() {
			if serv.account_id == who.clone() {
				ServicerApprovalList::<T>::mutate(|list| {
					list.remove(index);
				});
				let now = <frame_system::Pallet<T>>::block_number();
				Self::deposit_event(Event::ServicerAccountCreationRejected(now, who));
				exist = true;
				break;
			}
		}
		exist
	}

	pub fn reject_notary(who: T::AccountId) -> bool {
		let notaries = Self::get_pending_notaries();
		let mut exist = false;

		for (index, notary) in notaries.iter().enumerate() {
			if notary.account_id == who.clone() {
				NotaryApprovalList::<T>::mutate(|list| {
					list.remove(index);
				});
				let now = <frame_system::Pallet<T>>::block_number();
				Self::deposit_event(Event::NotaryAccountCreationRejected(now, who));
				exist = true;
				break;
			}
		}

		exist
	}

	// Helper function for account creation rejection by admin only
	pub fn reject_account(who: T::AccountId) -> DispatchResult {
		let role = Self::get_requested_role(who.clone());
		ensure!(role.is_some(), Error::<T>::NotInWaitingList);
		let role = role.unwrap();
		let success = match role {
			Accounts::SELLER => Self::reject_seller(who),
			Accounts::SERVICER => Self::reject_servicer(who),
			Accounts::NOTARY => Self::reject_notary(who),
			_ => false,
		};
		ensure!(success, Error::<T>::NotInWaitingList);
		Ok(())
	}

	pub fn tenant_list() -> Box<dyn Iterator<Item = T::AccountId>> {
		Box::new(TenantLog::<T>::iter_keys())
	}

	pub fn init_representatives(representatives: Vec<AccountIdOf<T>>) {
		let now = <frame_system::Pallet<T>>::block_number();
		let rep_count = representatives.len() as u32;
		for (index, account) in representatives.iter().enumerate() {
			AccountsRolesLog::<T>::insert(account, Accounts::REPRESENTATIVE);
			RepresentativeLog::<T>::insert(
				account,
				Representative::<T> {
					account_id: account.clone(),
					age: now,
					activated: false,
					assets_accounts: vec![],
					index: index as u32,
				},
			);
		}
		let members = Self::total_members();
		TotalMembers::<T>::put(members.saturating_add(rep_count));

		let reps = Self::rep_num();
		RepNumber::<T>::put(reps.saturating_add(rep_count));
	}

	pub fn increase_total_members() -> DispatchResult {
		let members: u32 = Self::total_members();
		ensure!(members < T::MaxMembers::get(), Error::<T>::TotalMembersExceeded);
		TotalMembers::<T>::put(members.saturating_add(1));

		Ok(())
	}
}