Skip to content

Commit

Permalink
Update Routes and Create task function
Browse files Browse the repository at this point in the history
  • Loading branch information
arturofigueroabim committed Feb 21, 2024
1 parent 93cb922 commit 90c03eb
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 32 deletions.
36 changes: 15 additions & 21 deletions src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::schema::tokens::dsl::*;
use crate::schema::tokens;
use crate::enums::{OpalTokenStatus, OpalProjectStatus};
use crate::handlers::{check_project_status_request, fetch_project_tables_names_request, check_token_status_request};
use crate::models::{NewToken, TokenManager, TokenParams, TokenStatus};
use crate::models::{NewToken, TokenManager, TokenParams, TokenStatus, TokensQueryParams, ProjectQueryParams};

const MIGRATIONS: EmbeddedMigrations = embed_migrations!();

Expand Down Expand Up @@ -167,45 +167,39 @@ impl Db {
.optional()
}

pub async fn check_token_status(
&mut self,
user: String,
bridgehead: String,
project: String,
) -> Result<Json<serde_json::Value>, (StatusCode, String)> {

pub async fn check_token_status(&mut self, params: TokensQueryParams) -> Result<Json<serde_json::Value>, (StatusCode, String)> {

let mut token_status_json = json!({
"project_id": project.clone(),
"bk": bridgehead.clone(),
"user_id": user.clone(),
"project_id": params.project_id.clone(),
"bk": params.bk.clone(),
"user_id": params.user_id.clone(),
"token_created_at": "",
"project_status": OpalTokenStatus::NOTFOUND,
"token_status": OpalProjectStatus::NOTFOUND,
});

if let Ok(json_response) = check_project_status_request(project.clone(), bridgehead.clone()).await {
if let Ok(json_response) = check_project_status_request(ProjectQueryParams { bk: params.bk.clone(), project_id: params.project_id.clone()}).await {
token_status_json["project_status"] = json_response.0["project_status"].clone();
} else {
error!("Error retrieving project status");
}

let token_name_response = match self.get_token_name(user.clone(), project.clone()) {
let token_name_response = match self.get_token_name(params.user_id.clone(), params.project_id.clone()) {
Ok(Some(name)) => name,
Ok(None) => return Ok(Json(token_status_json)),
Err(e) => return Err((StatusCode::INTERNAL_SERVER_ERROR, e.to_string())),
};

let records = match tokens
.filter(user_id.eq(&user))
.filter(bk.eq(&bridgehead))
.filter(project_id.eq(&project))
.filter(user_id.eq(&params.user_id))
.filter(bk.eq(&params.bk))
.filter(project_id.eq(&params.project_id))
.select(TokenManager::as_select())
.load::<TokenManager>(&mut self.0)
{
Ok(records) if !records.is_empty() => records,
Ok(_) => {
info!("Token not found with user_id: {}", &user);
info!("Token not found with user_id: {}", &params.user_id);
return Ok(Json(token_status_json));
}
Err(err) => {
Expand All @@ -218,14 +212,14 @@ impl Db {
token_status_json["token_created_at"] = json!(record.token_created_at);
let token_value = json!(record.token).as_str().unwrap_or_default().to_string();

if let Ok(json_response) = check_token_status_request(user.clone(), bridgehead.clone(), project.clone(), token_name_response.clone(), token_value.clone()).await {
if let Ok(json_response) = check_token_status_request(params.user_id.clone(), params.bk.clone(), params.project_id.clone(), token_name_response.clone(), token_value.clone()).await {
token_status_json["token_status"] = json_response.0["token_status"].clone();

let new_token_status = TokenStatus {
project_id: &project.clone(),
bk: &bridgehead.clone(),
project_id: &params.project_id.clone(),
bk: &params.bk.clone(),
token_status: OpalTokenStatus::CREATED.as_str(),
user_id: &user.clone(),
user_id: &params.user_id.clone(),
};
self.update_token_status_db(new_token_status);

Expand Down
10 changes: 5 additions & 5 deletions src/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,14 +157,14 @@ pub async fn fetch_project_tables_names_request(token_params: TokenParams) -> Re
result
}

pub async fn check_project_status_request(project_id: String, bridgehead: String) -> Result<Json<serde_json::Value>, (StatusCode, String)> {
pub async fn check_project_status_request(query_params: ProjectQueryParams) -> Result<Json<serde_json::Value>, (StatusCode, String)> {
let mut response_json = json!({
"project_id": project_id.clone(),
"bk": bridgehead.clone(),
"project_id": query_params.project_id.clone(),
"bk": query_params.bk.clone(),
"project_status": OpalTokenStatus::NOTFOUND,
});

let task = match create_and_send_task_request(OpalRequestType::STATUS, None, Some(project_id.clone().to_string()), Some(vec![bridgehead.clone().to_string()]), None).await {
let task = match create_and_send_task_request(OpalRequestType::STATUS, None, Some(query_params.project_id.clone().to_string()), Some(vec![query_params.bk.clone().to_string()]), None).await {
Ok(result) => result,
Err(e) => return Err((StatusCode::INTERNAL_SERVER_ERROR, format!("Error creating task: {}", e))),
};
Expand Down Expand Up @@ -540,7 +540,7 @@ async fn create_and_send_task_request(
let broker = CONFIG.beam_id.as_ref().splitn(3, '.').nth(2)
.ok_or_else(|| anyhow::Error::msg("Invalid app id"))?;

let bks: Vec<_> = bridgeheads.unwrap_or_else(|| Vec::new()).iter().map(|bk| AppId::new_unchecked(format!("{}.{}.{}", CONFIG.opal_beam_name, bk, broker))).collect();
let bks: Vec<_> = bridgeheads.unwrap_or_else(Vec::new).iter().map(|bridgehead_id| {AppId::new_unchecked(format!("{}.{}", bridgehead_id, broker))}).collect();

let request = OpalRequest {
request_type: request_type.to_string(),
Expand Down
10 changes: 4 additions & 6 deletions src/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,19 @@ async fn create_token(


async fn check_project_status(
query: Query<ProjectQueryParams>,
status_query: Query<ProjectQueryParams>,
) -> impl IntoResponse {
let params = query.0;
match check_project_status_request(params.project_id, params.bk).await {
match check_project_status_request(status_query.0).await {
Ok(json) => (StatusCode::OK, json).into_response(),
Err((status, message)) => (status, Json(json!({"message": message}))).into_response(),
}
}

async fn check_token_status(
mut db: Db,
query: Query<TokensQueryParams>,
status_query: Query<TokensQueryParams>,
) -> impl IntoResponse {
let params = query.0;
match db.check_token_status(params.user_id, params.bk, params.project_id).await {
match db.check_token_status(status_query.0).await {
Ok(json) => (StatusCode::OK, json).into_response(),
Err((status, message)) => (status, Json(json!({"message": message}))).into_response(),
}
Expand Down

0 comments on commit 90c03eb

Please sign in to comment.