diff --git a/src/pages/admin/utils_page.rs b/src/pages/admin/utils_page.rs index a923f74..7b8e7d1 100644 --- a/src/pages/admin/utils_page.rs +++ b/src/pages/admin/utils_page.rs @@ -52,10 +52,116 @@ fn get_important_dates() -> Vec { ] } +#[server] +async fn create_scholarship_export() -> Result, ServerFnError> { + // Exports olarship information - specifically the name of the scholarship, the name of the + // provider, and the scholarship's requirements. + // This means we'll need to get all the relations, all the scholarships, and all the contact info. + use crate::utils::server::create_dynamo_client; + use aws_sdk_dynamodb::client::Client; + use crate::pages::api::{get_comparison_info, SCHOLARSHIPS_TABLE, PROVIDER_CONTACT_TABLE}; + use std::collections::HashMap; + use crate::common::ValueType; + + let scan_table_items = async |client: &Client, table_name: &str| { + client.scan() + .table_name(table_name) + .send() + .await + .ok().and_then(|res| res.items) + .unwrap_or_default() + }; + + #[derive(serde::Serialize, serde::Deserialize, Debug)] + struct ScholarshipInfo { + name: String, + provider_name: String, + relations: Vec, + } + + let client = create_dynamo_client().await; + + let scholarships = dbg!(scan_table_items(&client, SCHOLARSHIPS_TABLE) + .await + .iter().map(|item| { + item.into_iter() + .map(|(k, v)| (k.clone(), ValueType::from(v))) + .collect() + }).collect::>>() + ); + let contacts = dbg!( + scan_table_items(&client, PROVIDER_CONTACT_TABLE).await + .iter().map(|item| { + item.into_iter() + .map(|(k, v)| (k.clone(), ValueType::from(v))) + .collect() + }).collect::>>() + ); + let db_relations = dbg!( + get_comparison_info().await.unwrap_or_default() + ); + + // Build a new structure containing some of the scholarship's information. + let resolved = scholarships.into_iter() + .map(|scholarship| { + // We just want the scholarship's name, the relations, and the provider's name. + // In here, we're going to resolve all the scholarship's information to provide this in + // a readable format. + let name = scholarship.get("name") + .unwrap_or(&ValueType::String(None)) + .to_string(); + + // The list of relations - we're going to search the master list for each comparison's + // name. It's structured as a map of lists, or a ValueType::Map(Option>)>>) + let relations = scholarship.get("requirements") + .unwrap_or(&ValueType::Map(None)) + .as_map().ok().flatten() + .unwrap_or_default() // We have our map of categories, now we need all the values inside each category. + .iter() + .map(|(_, list)| { + list.as_list().ok().flatten() + .unwrap_or_default() + }) + .flatten() + .collect::>() // This is a Vec + .iter().map(|val| val.clone().as_string().ok().flatten().unwrap_or_default()) + .collect::>(); + + // The provider ID, which we'll use to search for the provider's name. + let provider_id = scholarship.get("provider_id") + .unwrap_or(&ValueType::String(None)) + .to_string(); + + // This is a mess, we'll clean it up later. + let parsed_relations = relations + .iter() + .filter_map(|relation| { + db_relations.iter().find(|search| { + search.id == *relation + }) + .and_then(|search| Some(search.display_text.clone())) + }) + .collect::>(); + + ScholarshipInfo { + name, + provider_name: provider_id, + relations: parsed_relations, + } + }) + .collect::>(); + + // TODO Convert into CSV and send to client. For now I think I'm just going to serialize to json and print it. + leptos::logging::log!("Scholarships list: {:?}", resolved); + + Ok(Vec::new()) +} + #[component] pub fn AdminUtilsPage() -> impl IntoView { let create_comparisons = ServerAction::::new(); let create_dates = ServerAction::::new(); + let create_export = ServerAction::::new(); let on_click_comparisons = move |_| { create_comparisons.dispatch(CreateTestComparisons{}); @@ -65,6 +171,10 @@ pub fn AdminUtilsPage() -> impl IntoView { create_dates.dispatch(CreateDates{dates: get_important_dates()}); }; + let on_click_export = move |_| { + create_export.dispatch(CreateScholarshipExport{}); + }; + view! {
@@ -73,6 +183,7 @@ pub fn AdminUtilsPage() -> impl IntoView {
"Create Comparisons" "Create Dates" + "Scholarship Export"
}.into_any()