|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +// Copyright (c) 2025 SkillCert |
| 3 | + |
| 4 | +use crate::schema::{Course, CourseBackupData, CourseCategory, CourseGoal, CourseId, CourseModule, DataKey}; |
| 5 | +use soroban_sdk::{Address, Env, Map, String, Vec}; |
| 6 | + |
| 7 | +/// Export all course data for backup purposes |
| 8 | +/// |
| 9 | +/// This function creates a complete backup of all course data including courses, |
| 10 | +/// categories, modules, goals, and prerequisites. |
| 11 | +/// |
| 12 | +/// # Arguments |
| 13 | +/// * `env` - Soroban environment |
| 14 | +/// * `caller` - Address requesting the backup (must be admin) |
| 15 | +/// |
| 16 | +/// # Returns |
| 17 | +/// * `CourseBackupData` - Complete backup structure |
| 18 | +/// |
| 19 | +/// # Panics |
| 20 | +/// * If caller is not an admin |
| 21 | +pub fn export_course_data(env: Env, caller: Address) -> CourseBackupData { |
| 22 | + caller.require_auth(); |
| 23 | + |
| 24 | + // Verify caller is admin |
| 25 | + if !is_admin(&env, caller) { |
| 26 | + panic!("Unauthorized: Only admins can export course data"); |
| 27 | + } |
| 28 | + |
| 29 | + // Initialize maps for backup data |
| 30 | + let mut courses = Map::new(&env); |
| 31 | + let mut categories = Map::new(&env); |
| 32 | + let mut modules = Map::new(&env); |
| 33 | + let mut goals = Map::new(&env); |
| 34 | + let mut prerequisites = Map::new(&env); |
| 35 | + |
| 36 | + // Get all courses by iterating through course IDs |
| 37 | + // Courses are stored as (Symbol("course"), course_id) -> Course |
| 38 | + let course_key = soroban_sdk::symbol_short!("course"); |
| 39 | + let mut all_courses = Vec::new(&env); |
| 40 | + |
| 41 | + // Get the current course ID to know how many courses exist |
| 42 | + let course_id_key = soroban_sdk::symbol_short!("course"); |
| 43 | + let max_course_id: u128 = env |
| 44 | + .storage() |
| 45 | + .persistent() |
| 46 | + .get(&course_id_key) |
| 47 | + .unwrap_or(0u128); |
| 48 | + |
| 49 | + // Iterate through all possible course IDs |
| 50 | + for id in 1..=max_course_id { |
| 51 | + let course_id_str = super::utils::u32_to_string(&env, id as u32); |
| 52 | + let storage_key = (course_key.clone(), course_id_str.clone()); |
| 53 | + |
| 54 | + if let Some(course) = env.storage().persistent().get::<_, Course>(&storage_key) { |
| 55 | + all_courses.push_back(course.clone()); |
| 56 | + courses.set(course.id.clone(), course.clone()); |
| 57 | + |
| 58 | + // Export course goals |
| 59 | + if let Some(course_goals) = env |
| 60 | + .storage() |
| 61 | + .persistent() |
| 62 | + .get::<DataKey, Vec<CourseGoal>>(&DataKey::CourseGoalList(course.id.clone())) |
| 63 | + { |
| 64 | + goals.set(course.id.clone(), course_goals); |
| 65 | + } |
| 66 | + |
| 67 | + // Export course prerequisites |
| 68 | + if let Some(course_prereqs) = env |
| 69 | + .storage() |
| 70 | + .persistent() |
| 71 | + .get::<DataKey, Vec<CourseId>>(&DataKey::CoursePrerequisites(course.id.clone())) |
| 72 | + { |
| 73 | + prerequisites.set(course.id.clone(), course_prereqs); |
| 74 | + } |
| 75 | + |
| 76 | + // Export course modules (simplified version) |
| 77 | + let module_id = String::from_str(&env, "default_module"); |
| 78 | + let course_module = CourseModule { |
| 79 | + id: module_id.clone(), |
| 80 | + course_id: course.id.clone(), |
| 81 | + position: 1, |
| 82 | + title: String::from_str(&env, "Default Module"), |
| 83 | + created_at: env.ledger().timestamp(), |
| 84 | + }; |
| 85 | + modules.set(module_id, course_module); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + |
| 90 | + // Export all categories |
| 91 | + let mut category_id = 1u128; |
| 92 | + loop { |
| 93 | + if let Some(category) = env |
| 94 | + .storage() |
| 95 | + .persistent() |
| 96 | + .get::<DataKey, CourseCategory>(&DataKey::CourseCategory(category_id)) |
| 97 | + { |
| 98 | + categories.set(category_id, category); |
| 99 | + category_id += 1; |
| 100 | + } else { |
| 101 | + break; |
| 102 | + } |
| 103 | + |
| 104 | + // Safety check to avoid infinite loops |
| 105 | + if category_id > 10000 { |
| 106 | + break; |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + // Get category sequence counter |
| 111 | + let category_seq: u128 = env |
| 112 | + .storage() |
| 113 | + .persistent() |
| 114 | + .get(&DataKey::CategorySeq) |
| 115 | + .unwrap_or(0); |
| 116 | + |
| 117 | + // Get admin list |
| 118 | + let admins: Vec<Address> = env |
| 119 | + .storage() |
| 120 | + .persistent() |
| 121 | + .get(&DataKey::Admins) |
| 122 | + .unwrap_or(Vec::new(&env)); |
| 123 | + |
| 124 | + // Create backup data structure |
| 125 | + CourseBackupData { |
| 126 | + courses, |
| 127 | + categories, |
| 128 | + modules, |
| 129 | + goals, |
| 130 | + prerequisites, |
| 131 | + category_seq, |
| 132 | + admins, |
| 133 | + backup_timestamp: env.ledger().timestamp(), |
| 134 | + backup_version: String::from_str(&env, "1.0.0"), |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +/// Import course data from backup |
| 139 | +/// |
| 140 | +/// This function restores course data from a backup structure. |
| 141 | +/// This operation will overwrite existing data. |
| 142 | +/// |
| 143 | +/// # Arguments |
| 144 | +/// * `env` - Soroban environment |
| 145 | +/// * `caller` - Address performing the import (must be admin) |
| 146 | +/// * `backup_data` - Backup data to restore |
| 147 | +/// |
| 148 | +/// # Returns |
| 149 | +/// * `u32` - Number of courses imported |
| 150 | +/// |
| 151 | +/// # Panics |
| 152 | +/// * If caller is not an admin |
| 153 | +/// * If backup data is invalid |
| 154 | +pub fn import_course_data(env: Env, caller: Address, backup_data: CourseBackupData) -> u32 { |
| 155 | + caller.require_auth(); |
| 156 | + |
| 157 | + // Verify caller is admin |
| 158 | + if !is_admin(&env, caller) { |
| 159 | + panic!("Unauthorized: Only admins can import course data"); |
| 160 | + } |
| 161 | + |
| 162 | + // Validate backup version compatibility |
| 163 | + let expected_version = String::from_str(&env, "1.0.0"); |
| 164 | + if backup_data.backup_version != expected_version { |
| 165 | + panic!("Incompatible backup version"); |
| 166 | + } |
| 167 | + |
| 168 | + let mut imported_count = 0u32; |
| 169 | + let course_key = soroban_sdk::symbol_short!("course"); |
| 170 | + |
| 171 | + // Import courses - store each course individually |
| 172 | + for (_course_id, course) in backup_data.courses.iter() { |
| 173 | + let storage_key = (course_key.clone(), course.id.clone()); |
| 174 | + env.storage() |
| 175 | + .persistent() |
| 176 | + .set(&storage_key, &course); |
| 177 | + imported_count += 1; |
| 178 | + } |
| 179 | + |
| 180 | + // Import categories |
| 181 | + for (category_id, category) in backup_data.categories.iter() { |
| 182 | + env.storage() |
| 183 | + .persistent() |
| 184 | + .set(&DataKey::CourseCategory(category_id), &category); |
| 185 | + } |
| 186 | + |
| 187 | + // Import modules |
| 188 | + for (module_id, module) in backup_data.modules.iter() { |
| 189 | + env.storage() |
| 190 | + .persistent() |
| 191 | + .set(&DataKey::Module(module_id), &module); |
| 192 | + } |
| 193 | + |
| 194 | + // Import goals |
| 195 | + for (course_id, course_goals) in backup_data.goals.iter() { |
| 196 | + env.storage() |
| 197 | + .persistent() |
| 198 | + .set(&DataKey::CourseGoalList(course_id), &course_goals); |
| 199 | + } |
| 200 | + |
| 201 | + // Import prerequisites |
| 202 | + for (course_id, prereqs) in backup_data.prerequisites.iter() { |
| 203 | + env.storage() |
| 204 | + .persistent() |
| 205 | + .set(&DataKey::CoursePrerequisites(course_id), &prereqs); |
| 206 | + } |
| 207 | + |
| 208 | + // Import category sequence counter |
| 209 | + env.storage() |
| 210 | + .persistent() |
| 211 | + .set(&DataKey::CategorySeq, &backup_data.category_seq); |
| 212 | + |
| 213 | + // Import admin list |
| 214 | + env.storage() |
| 215 | + .persistent() |
| 216 | + .set(&DataKey::Admins, &backup_data.admins); |
| 217 | + |
| 218 | + // Emit import event |
| 219 | + env.events().publish( |
| 220 | + (String::from_str(&env, "course_data_imported"),), |
| 221 | + (imported_count, backup_data.backup_timestamp), |
| 222 | + ); |
| 223 | + |
| 224 | + imported_count |
| 225 | +} |
| 226 | + |
| 227 | +/// Check if an address is an admin |
| 228 | +/// |
| 229 | +/// This is a simplified version for the backup system. |
| 230 | +/// In a real implementation, this would check against the user_management contract. |
| 231 | +fn is_admin(env: &Env, address: Address) -> bool { |
| 232 | + let admins: Vec<Address> = env |
| 233 | + .storage() |
| 234 | + .persistent() |
| 235 | + .get(&DataKey::Admins) |
| 236 | + .unwrap_or(Vec::new(env)); |
| 237 | + |
| 238 | + for admin in admins.iter() { |
| 239 | + if admin == address { |
| 240 | + return true; |
| 241 | + } |
| 242 | + } |
| 243 | + false |
| 244 | +} |
0 commit comments