From b77e957e784c8036f14314669051927c38cee8a9 Mon Sep 17 00:00:00 2001 From: EdwinBetanc0urt Date: Thu, 23 May 2024 14:22:40 -0400 Subject: [PATCH] fix: Get documents with base values. --- src/models/browser.rs | 46 +++++++++++------ src/models/form.rs | 35 +++++++++---- src/models/menu.rs | 114 +++++++++++++++++++++++------------------- src/models/mod.rs | 50 ++++++++++++------ src/models/process.rs | 44 +++++++++++----- src/models/window.rs | 42 +++++++++++----- 6 files changed, 215 insertions(+), 116 deletions(-) diff --git a/src/models/browser.rs b/src/models/browser.rs index 44f6c68..1988d1f 100644 --- a/src/models/browser.rs +++ b/src/models/browser.rs @@ -301,28 +301,44 @@ async fn get_index_name(_language: Option<&String>, _client_id: Option<&String>, if _role_id.is_none() { return Err(Error::new(ErrorKind::InvalidData.into(), "Role is Mandatory")); } - let _index = "browser".to_string(); - let _user_index = match _user_id { - Some(_) => user_index(_index.to_owned(), _language, _client_id, _role_id, _user_id), - None => role_index(_index.to_owned(), _language, _client_id, _role_id) - }; + + let _index: String = "browser".to_string(); + + let _user_index = user_index(_index.to_owned(), _language, _client_id, _role_id, _user_id); let _role_index = role_index(_index.to_owned(), _language, _client_id, _role_id); - let _client_index = client_index(_index.to_owned(), _language, _client_id, _role_id); - let _language_index = language_index(_index.to_owned(), _language, _client_id, _role_id); - let _default_index = default_index(_index.to_owned(), _language, _client_id, _role_id); - // Find index + let _client_index = client_index(_index.to_owned(), _language, _client_id); + let _language_index = language_index(_index.to_owned(), _language); + let _default_index = default_index(_index.to_owned()); + + // Find index match exists_index(_user_index.to_owned()).await { - Ok(_) => Ok(_user_index), + Ok(_) => { + log::info!("Find with user index `{:}`", _user_index); + Ok(_user_index) + }, Err(_) => { + log::info!("No user index `{:}`", _user_index); match exists_index(_role_index.to_owned()).await { - Ok(_) => Ok(_role_index), + Ok(_) => { + log::info!("Find with role index `{:}`", _role_index); + Ok(_role_index) + }, Err(_) => { + log::info!("No role index `{:}`", _role_index); match exists_index(_client_index.to_owned()).await { - Ok(_) => Ok(_client_index), + Ok(_) => { + log::info!("Find with client index `{:}`", _client_index); + Ok(_client_index) + }, Err(_) => { - match exists_index(_language_index.to_owned()).await { - Ok(_) => Ok(_language_index), + log::info!("No client index `{:}`", _client_index); + match exists_index(_language_index.to_owned()).await { + Ok(_) => { + log::info!("Find with language index `{:}`", _language_index); + Ok(_language_index) + }, Err(_) => { + log::info!("No language index `{:}`. Find with default index `{:}`.", _language_index, _default_index); Ok(_default_index) } } @@ -339,8 +355,10 @@ pub async fn browsers(_language: Option<&String>, _client_id: Option<&String>, _ Some(value) => value.clone(), None => "".to_owned() }; + let _index_name = get_index_name(_language, _client_id, _role_id, _user_id).await.expect("Error getting index"); log::info!("Index to search {:}", _index_name); + let mut _document = Browser::default(); _document.index_value = Some(_index_name); let _browser_document: &dyn IndexDocument = &_document; diff --git a/src/models/form.rs b/src/models/form.rs index d97ab02..e6adb9f 100644 --- a/src/models/form.rs +++ b/src/models/form.rs @@ -3,7 +3,7 @@ use salvo::prelude::*; use serde_json::json; use std::{io::ErrorKind, io::Error}; -use crate::{controller::opensearch::{IndexDocument, get_by_id, find, exists_index}, models::{default_index, language_index}}; +use crate::{controller::opensearch::{IndexDocument, get_by_id, find, exists_index}, models::{client_index, default_index, language_index}}; #[derive(Deserialize, Extractible, Debug, Clone)] #[salvo(extract(default_source(from = "body")))] @@ -152,18 +152,31 @@ async fn get_index_name(_language: Option<&String>, _client_id: Option<&String>, if _client_id.is_none() { return Err(Error::new(ErrorKind::InvalidData.into(), "Client is Mandatory")); } - // if _role_id.is_none() { - // return Err(Error::new(ErrorKind::InvalidData.into(), "Role is Mandatory")); - // } - let _index = "form".to_string(); - let _language_index = language_index(_index.to_owned(), _language, _client_id, _role_id); - let _default_index = default_index(_index.to_owned(), _language, _client_id, _role_id); + let _index: String = "form".to_string(); + + let _client_index = client_index(_index.to_owned(), _language, _client_id); + let _language_index = language_index(_index.to_owned(), _language); + let _default_index = default_index(_index.to_owned()); + // Find index - match exists_index(_language_index.to_owned()).await { - Ok(_) => Ok(_language_index), + match exists_index(_client_index.to_owned()).await { + Ok(_) => { + log::info!("Find with client index `{:}`", _client_index); + Ok(_client_index) + }, Err(_) => { - Ok(_default_index) + log::info!("No client index `{:}`", _client_index); + match exists_index(_language_index.to_owned()).await { + Ok(_) => { + log::info!("Find with language index `{:}`", _language_index); + Ok(_language_index) + }, + Err(_) => { + log::info!("No language index `{:}`. Find with default index `{:}`.", _language_index, _default_index); + Ok(_default_index) + } + } } } } @@ -173,8 +186,10 @@ pub async fn forms(_language: Option<&String>, _client_id: Option<&String>, _rol Some(value) => value.clone(), None => "".to_owned() }; + let _index_name = get_index_name(_language, _client_id, _role_id, _user_id).await.expect("Error getting index"); log::info!("Index to search {:}", _index_name); + let mut _document = Form::default(); _document.index_value = Some(_index_name); let _forms_document: &dyn IndexDocument = &_document; diff --git a/src/models/menu.rs b/src/models/menu.rs index a0a8f33..8b1898e 100644 --- a/src/models/menu.rs +++ b/src/models/menu.rs @@ -212,68 +212,78 @@ pub async fn menu_from_id(_id: Option) -> Result { } } -pub async fn menus( - _language: Option<&String>, _client_id: Option<&String>, _role_id: Option<&String>, _user_id: Option<&String>, - _search_value: Option<&String>, _page_number: Option<&String>, _page_size: Option<&String> -) -> Result { - // Validate - if _language.is_none() { - return Err(Error::new(ErrorKind::InvalidData.into(), "Language is Mandatory")); - } - if _client_id.is_none() { - return Err(Error::new(ErrorKind::InvalidData.into(), "Client is Mandatory")); - } - if _role_id.is_none() { - return Err(Error::new(ErrorKind::InvalidData.into(), "Role is Mandatory")); - } - let _search_value = match _search_value { - Some(value) => value.clone(), - None => "".to_owned() - }; - let _index = "menu".to_string(); - let _user_index = match _user_id { - Some(_) => user_index(_index.to_owned(), _language, _client_id, _role_id, _user_id), - None => role_index(_index.to_owned(), _language, _client_id, _role_id) - }; +async fn get_index_name(_language: Option<&String>, _client_id: Option<&String>, _role_id: Option<&String>, _user_id: Option<&String>) -> Result { + // Validate + if _language.is_none() { + return Err(Error::new(ErrorKind::InvalidData.into(), "Language is Mandatory")); + } + if _client_id.is_none() { + return Err(Error::new(ErrorKind::InvalidData.into(), "Client is Mandatory")); + } + if _role_id.is_none() { + return Err(Error::new(ErrorKind::InvalidData.into(), "Role is Mandatory")); + } + + let _index: String = "menu".to_string(); + + let _user_index = user_index(_index.to_owned(), _language, _client_id, _role_id, _user_id); let _role_index = role_index(_index.to_owned(), _language, _client_id, _role_id); - let _client_index = client_index(_index.to_owned(), _language, _client_id, _role_id); - let _language_index = language_index(_index.to_owned(), _language, _client_id, _role_id); - let _default_index = default_index(_index.to_owned(), _language, _client_id, _role_id); - // Find index - let _index_name = match exists_index(_user_index.to_owned()).await { + let _client_index = client_index(_index.to_owned(), _language, _client_id); + let _language_index = language_index(_index.to_owned(), _language); + let _default_index = default_index(_index.to_owned()); + + // Find index + match exists_index(_user_index.to_owned()).await { Ok(_) => { - log::info!("Find with user index {:}", _user_index); - _user_index + log::info!("Find with user index `{:}`", _user_index); + Ok(_user_index) }, - Err(_) => { - match exists_index(_role_index.to_owned()).await { + Err(_) => { + log::info!("No user index `{:}`", _user_index); + match exists_index(_role_index.to_owned()).await { Ok(_) => { - log::info!("Find with role index {:}", _role_index); - _role_index - }, - Err(_) => { - match exists_index(_client_index.to_owned()).await { + log::info!("Find with role index `{:}`", _role_index); + Ok(_role_index) + }, + Err(_) => { + log::info!("No role index `{:}`", _role_index); + match exists_index(_client_index.to_owned()).await { Ok(_) => { - log::info!("Find with client index {:}", _client_index); - _client_index + log::info!("Find with client index `{:}`", _client_index); + Ok(_client_index) }, - Err(_) => { - match exists_index(_language_index.to_owned()).await { + Err(_) => { + log::info!("No client index `{:}`", _client_index); + match exists_index(_language_index.to_owned()).await { Ok(_) => { - log::info!("Find with language index {:}", _language_index); - _language_index + log::info!("Find with language index `{:}`", _language_index); + Ok(_language_index) }, - Err(_) => { - _default_index - } - } - } - } - } + Err(_) => { + log::info!("No language index `{:}`. Find with default index `{:}`.", _language_index, _default_index); + Ok(_default_index) + } + } + } + } + } } - } - }; + } + } +} + +pub async fn menus( + _language: Option<&String>, _client_id: Option<&String>, _role_id: Option<&String>, _user_id: Option<&String>, + _search_value: Option<&String>, _page_number: Option<&String>, _page_size: Option<&String> +) -> Result { + let _search_value = match _search_value { + Some(value) => value.clone(), + None => "".to_owned() + }; + + let _index_name = get_index_name(_language, _client_id, _role_id, _user_id).await.expect("Error getting index"); log::info!("Index to search {:}", _index_name); + let mut _document = Menu::default(); _document.index_value = Some(_index_name); let _menu_document: &dyn IndexDocument = &_document; diff --git a/src/models/mod.rs b/src/models/mod.rs index 9737cee..8e472ea 100644 --- a/src/models/mod.rs +++ b/src/models/mod.rs @@ -17,35 +17,57 @@ pub struct Metadata { pub user_id: Option, } -fn default_index(_index_name: String, _language: Option<&String>, _client_id: Option<&String>, _role_id: Option<&String>) -> String { +fn default_index(_index_name: String) -> String { let mut _index_to_find: String = _index_name.to_owned(); _index_to_find.to_lowercase() } -fn language_index(_index_name: String, _language: Option<&String>, _client_id: Option<&String>, _role_id: Option<&String>) -> String { - let mut _index_to_find: String = default_index(_index_name, _language, _client_id, _role_id); - _index_to_find.push_str("_"); - _index_to_find.push_str(_language.unwrap()); +fn language_index(_index_name: String, _language: Option<&String>) -> String { + let mut _index_to_find: String = default_index(_index_name); + if let Some(language) = _language { + if language != "en_US" { + _index_to_find.push_str("_"); + _index_to_find.push_str(language); + } + } _index_to_find.to_lowercase() } -fn client_index(_index_name: String, _language: Option<&String>, _client_id: Option<&String>, _role_id: Option<&String>) -> String { - let mut _index_to_find: String = language_index(_index_name, _language, _client_id, _role_id); - _index_to_find.push_str("_"); - _index_to_find.push_str(_client_id.unwrap()); +fn client_index(_index_name: String, _language: Option<&String>, _client_id: Option<&String>) -> String { + let mut _index_to_find: String = language_index(_index_name, _language); + if let Some(client_id) = _client_id { + if let Ok(id) = client_id.parse::() { + if id > 0 { + _index_to_find.push_str("_"); + _index_to_find.push_str(client_id); + } + } + } _index_to_find.to_lowercase() } fn role_index(_index_name: String, _language: Option<&String>, _client_id: Option<&String>, _role_id: Option<&String>) -> String { - let mut _index_to_find: String = client_index(_index_name, _language, _client_id, _role_id); - _index_to_find.push_str("_"); - _index_to_find.push_str(_role_id.unwrap()); + let mut _index_to_find: String = client_index(_index_name, _language, _client_id); + if let Some(role_id) = _role_id { + if let Ok(id) = role_id.parse::() { + if id > 0 { + _index_to_find.push_str("_"); + _index_to_find.push_str(role_id); + } + } + } _index_to_find.to_lowercase() } fn user_index(_index_name: String, _language: Option<&String>, _client_id: Option<&String>, _role_id: Option<&String>, _user_id: Option<&String>) -> String { let mut _index_to_find: String = role_index(_index_name, _language, _client_id, _role_id); - _index_to_find.push_str("_"); - _index_to_find.push_str(_user_id.unwrap()); + if let Some(user_id) = _user_id { + if let Ok(id) = user_id.parse::() { + if id > 0 { + _index_to_find.push_str("_"); + _index_to_find.push_str(user_id); + } + } + } _index_to_find.to_lowercase() } diff --git a/src/models/process.rs b/src/models/process.rs index e306c81..1817419 100644 --- a/src/models/process.rs +++ b/src/models/process.rs @@ -269,28 +269,44 @@ async fn get_index_name(_language: Option<&String>, _client_id: Option<&String>, if _role_id.is_none() { return Err(Error::new(ErrorKind::InvalidData.into(), "Role is Mandatory")); } - let _index = "process".to_string(); - let _user_index = match _user_id { - Some(_) => user_index(_index.to_owned(), _language, _client_id, _role_id, _user_id), - None => role_index(_index.to_owned(), _language, _client_id, _role_id) - }; + + let _index: String = "process".to_string(); + + let _user_index = user_index(_index.to_owned(), _language, _client_id, _role_id, _user_id); let _role_index = role_index(_index.to_owned(), _language, _client_id, _role_id); - let _client_index = client_index(_index.to_owned(), _language, _client_id, _role_id); - let _language_index = language_index(_index.to_owned(), _language, _client_id, _role_id); - let _default_index = default_index(_index.to_owned(), _language, _client_id, _role_id); + let _client_index = client_index(_index.to_owned(), _language, _client_id); + let _language_index = language_index(_index.to_owned(), _language); + let _default_index = default_index(_index.to_owned()); + // Find index match exists_index(_user_index.to_owned()).await { - Ok(_) => Ok(_user_index), + Ok(_) => { + log::info!("Find with user index `{:}`", _user_index); + Ok(_user_index) + }, Err(_) => { + log::info!("No user index `{:}`", _user_index); match exists_index(_role_index.to_owned()).await { - Ok(_) => Ok(_role_index), + Ok(_) => { + log::info!("Find with role index `{:}`", _role_index); + Ok(_role_index) + }, Err(_) => { + log::info!("No role index `{:}`", _role_index); match exists_index(_client_index.to_owned()).await { - Ok(_) => Ok(_client_index), + Ok(_) => { + log::info!("Find with client index `{:}`", _client_index); + Ok(_client_index) + }, Err(_) => { - match exists_index(_language_index.to_owned()).await { - Ok(_) => Ok(_language_index), + log::info!("No client index `{:}`", _client_index); + match exists_index(_language_index.to_owned()).await { + Ok(_) => { + log::info!("Find with language index `{:}`", _language_index); + Ok(_language_index) + }, Err(_) => { + log::info!("No language index `{:}`. Find with default index `{:}`.", _language_index, _default_index); Ok(_default_index) } } @@ -307,8 +323,10 @@ pub async fn processes(_language: Option<&String>, _client_id: Option<&String>, Some(value) => value.clone(), None => "".to_owned() }; + let _index_name = get_index_name(_language, _client_id, _role_id, _user_id).await.expect("Error getting index"); log::info!("Index to search {:}", _index_name); + let mut _document = Process::default(); _document.index_value = Some(_index_name); let _process_document: &dyn IndexDocument = &_document; diff --git a/src/models/window.rs b/src/models/window.rs index 9026513..26c5727 100644 --- a/src/models/window.rs +++ b/src/models/window.rs @@ -309,28 +309,44 @@ async fn get_index_name(_language: Option<&String>, _client_id: Option<&String>, if _role_id.is_none() { return Err(Error::new(ErrorKind::InvalidData.into(), "Role is Mandatory")); } - let _index = "window".to_string(); - let _user_index = match _user_id { - Some(_) => user_index(_index.to_owned(), _language, _client_id, _role_id, _user_id), - None => role_index(_index.to_owned(), _language, _client_id, _role_id) - }; + + let _index: String = "window".to_string(); + + let _user_index = user_index(_index.to_owned(), _language, _client_id, _role_id, _user_id); let _role_index = role_index(_index.to_owned(), _language, _client_id, _role_id); - let _client_index = client_index(_index.to_owned(), _language, _client_id, _role_id); - let _language_index = language_index(_index.to_owned(), _language, _client_id, _role_id); - let _default_index = default_index(_index.to_owned(), _language, _client_id, _role_id); + let _client_index = client_index(_index.to_owned(), _language, _client_id); + let _language_index = language_index(_index.to_owned(), _language); + let _default_index = default_index(_index.to_owned()); + // Find index match exists_index(_user_index.to_owned()).await { - Ok(_) => Ok(_user_index), + Ok(_) => { + log::info!("Find with user index `{:}`", _user_index); + Ok(_user_index) + }, Err(_) => { + log::info!("No user index `{:}`", _user_index); match exists_index(_role_index.to_owned()).await { - Ok(_) => Ok(_role_index), + Ok(_) => { + log::info!("Find with role index `{:}`", _role_index); + Ok(_role_index) + }, Err(_) => { + log::info!("No role index `{:}`", _role_index); match exists_index(_client_index.to_owned()).await { - Ok(_) => Ok(_client_index), + Ok(_) => { + log::info!("Find with client index `{:}`", _client_index); + Ok(_client_index) + }, Err(_) => { - match exists_index(_language_index.to_owned()).await { - Ok(_) => Ok(_language_index), + log::info!("No client index `{:}`", _client_index); + match exists_index(_language_index.to_owned()).await { + Ok(_) => { + log::info!("Find with language index `{:}`", _language_index); + Ok(_language_index) + }, Err(_) => { + log::info!("No language index `{:}`. Find with default index `{:}`.", _language_index, _default_index); Ok(_default_index) } }