Skip to content

Commit

Permalink
Create an allocator with installation ID
Browse files Browse the repository at this point in the history
  • Loading branch information
Filip Lelek authored and kacperzuk-neti committed Jul 18, 2024
1 parent 6c3bd72 commit 608337e
Show file tree
Hide file tree
Showing 5 changed files with 191 additions and 239 deletions.
3 changes: 1 addition & 2 deletions fplus-http-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,7 @@ async fn main() -> std::io::Result<()> {
.service(router::allocator::allocators)
.service(router::allocator::allocator)
.service(router::allocator::delete)
.service(router::allocator::create_from_json)
.service(router::allocator::update_single_installation_id)
.service(router::allocator::create_allocator_from_json)
.service(router::allocator::update_allocator_force)
// .service(router::allocator::get_installation_ids)
})
Expand Down
192 changes: 9 additions & 183 deletions fplus-http-server/src/router/allocator.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
use actix_web::{delete, get, post, web, HttpResponse, Responder};
use fplus_database::database::allocation_amounts as allocation_amounts_db;
use fplus_database::database::allocators as allocators_db;
use fplus_lib::core::allocator::fetch_installation_ids;
use fplus_lib::core::allocator::generate_github_app_jwt;
use fplus_lib::core::{
allocator::{
create_allocator_repo, force_update_allocators, is_allocator_repo_created,
process_allocator_file, update_single_installation_id_logic,
validate_amount_type_and_options,
create_allocator_from_file, fetch_installation_ids, force_update_allocators,
generate_github_app_jwt,
},
AllocatorUpdateForceInfo, ChangedAllocators, InstallationIdUpdateInfo,
AllocatorUpdateForceInfo, ChangedAllocators,
};
use fplus_lib::helpers::process_amount;
use reqwest::Client;

/**
* Get all allocators
*
Expand Down Expand Up @@ -41,157 +35,13 @@ pub async fn allocators() -> impl Responder {
* @return HttpResponse - The result of the operation
*/
#[post("/allocator/create")]
pub async fn create_from_json(
files: web::Json<ChangedAllocators>,
) -> actix_web::Result<impl Responder> {
let mut error_response: Option<HttpResponse> = None;

for file_name in &files.files_changed {
log::info!("Starting allocator creation on: {}", file_name);

match process_allocator_file(file_name).await {
Ok(mut model) => {
let mut quantity_options: Vec<String>;
if let Some(allocation_amount) = model.application.allocation_amount.clone() {
if allocation_amount.amount_type.clone().is_none()
|| allocation_amount.quantity_options.clone().is_none()
{
error_response = Some(
HttpResponse::BadRequest()
.body("Amount type and quantity options are required"),
);
break;
}

let amount_type = allocation_amount
.amount_type
.clone()
.unwrap()
.to_lowercase(); // Assuming you still want to unwrap here
quantity_options = allocation_amount.quantity_options.unwrap(); // Assuming unwrap is desired

for option in quantity_options.iter_mut() {
*option = process_amount(option.clone());
}

match validate_amount_type_and_options(&amount_type, &quantity_options) {
Ok(()) => println!("Options are valid"),
Err(e) => {
error_response = Some(HttpResponse::BadRequest().body(e.to_string()));
break;
}
}

model
.application
.allocation_amount
.as_mut()
.unwrap()
.quantity_options = Some(quantity_options);
}

let verifiers_gh_handles = if model.application.verifiers_gh_handles.is_empty() {
None
} else {
Some(model.application.verifiers_gh_handles.join(", ")) // Join verifiers in a string if exists
};
let tooling = if model.application.tooling.is_empty() {
None
} else {
Some(model.application.tooling.join(", "))
};
let owner = model.owner.clone().unwrap_or_default().to_string();
let repo = model.repo.clone().unwrap_or_default().to_string();

let allocator_creation_result = allocators_db::create_or_update_allocator(
owner.clone(),
repo.clone(),
None,
Some(model.pathway_addresses.msig),
verifiers_gh_handles,
model.multisig_threshold,
model
.application
.allocation_amount
.clone()
.and_then(|a| a.amount_type.clone()),
model.address,
tooling,
Some(model.application.data_types),
Some(model.application.required_sps),
Some(model.application.required_replicas),
Some(file_name.to_owned()),
)
.await;

match allocator_creation_result {
Ok(_) => match is_allocator_repo_created(&owner, &repo).await {
Ok(true) => (),
Ok(false) => match create_allocator_repo(&owner, &repo).await {
Ok(_) => (),
Err(e) => {
error_response =
Some(HttpResponse::BadRequest().body(e.to_string()));
break;
}
},
Err(e) => {
error_response = Some(HttpResponse::BadRequest().body(e.to_string()));
break;
}
},
Err(e) => {
error_response = Some(HttpResponse::BadRequest().body(e.to_string()));
break;
}
}

let allocator_id = allocator_creation_result.unwrap().id;

// Delete all old allocation amounts by allocator id
match allocation_amounts_db::delete_allocation_amounts_by_allocator_id(allocator_id)
.await
{
Ok(_) => (),
Err(err) => {
error_response = Some(HttpResponse::BadRequest().body(err.to_string()));
break;
}
}

if let Some(allocation_amount) = model.application.allocation_amount.clone() {
let allocation_amounts = allocation_amount.quantity_options.unwrap();

for allocation_amount in allocation_amounts {
let parsed_allocation_amount = allocation_amount.replace('%', "");
match allocation_amounts_db::create_allocation_amount(
allocator_id,
parsed_allocation_amount,
)
.await
{
Ok(_) => (),
Err(err) => {
error_response =
Some(HttpResponse::BadRequest().body(err.to_string()));
break;
}
}
}
}
}
Err(e) => {
error_response = Some(HttpResponse::BadRequest().body(e.to_string()));
break;
}
}
pub async fn create_allocator_from_json(files: web::Json<ChangedAllocators>) -> impl Responder {
let ChangedAllocators { files_changed } = files.into_inner();
match create_allocator_from_file(files_changed).await {
Ok(()) => HttpResponse::Ok()
.body(serde_json::to_string_pretty("All files processed successfully").unwrap()),
Err(e) => HttpResponse::BadRequest().body(e.to_string()),
}

if let Some(response) = error_response {
return Ok(response);
}

Ok(HttpResponse::Ok().body("All files processed successfully"))
}

/**
Expand Down Expand Up @@ -238,30 +88,6 @@ pub async fn delete(path: web::Path<(String, String)>) -> impl Responder {
}
}

// #[post("/allocator/update_installation_ids")]
// pub async fn update_installation_ids() -> impl Responder {
// match update_installation_ids_logic().await {
// Ok(results) => HttpResponse::Ok().json(results),
// Err(e) => {
// log::error!("Failed to fetch installation ids: {}", e);
// HttpResponse::InternalServerError().body(format!("{}", e))
// }
// }
// }

#[get("/allocator/update_installation_id")]
pub async fn update_single_installation_id(
query: web::Query<InstallationIdUpdateInfo>,
) -> impl Responder {
match update_single_installation_id_logic(query.installation_id.to_string()).await {
Ok(results) => HttpResponse::Ok().json(results),
Err(e) => {
log::error!("Failed to fetch installation ids: {}", e);
HttpResponse::InternalServerError().body(format!("{}", e))
}
}
}

/**
* Force updating allocator files from template.
* It receives a list of changed files and allocators to update.
Expand Down
Loading

0 comments on commit 608337e

Please sign in to comment.