feat: move creation entry config to database
This commit is contained in:
@@ -11,6 +11,7 @@ use module_auth::{
|
||||
RefreshSessionService, WechatAuthService, WechatAuthStateService,
|
||||
};
|
||||
use module_runtime::RuntimeSnapshotRecord;
|
||||
use shared_contracts::creation_entry_config::CreationEntryConfigResponse;
|
||||
#[cfg(test)]
|
||||
use module_runtime::{SAVE_SNAPSHOT_VERSION, format_utc_micros};
|
||||
use platform_agent::MockLangChainRustAgentExecutor;
|
||||
@@ -41,6 +42,8 @@ pub struct AppState {
|
||||
auth_jwt_config: JwtConfig,
|
||||
admin_runtime: Option<AdminRuntime>,
|
||||
refresh_cookie_config: RefreshCookieConfig,
|
||||
#[cfg(test)]
|
||||
test_creation_entry_config: Arc<Mutex<Option<CreationEntryConfigResponse>>>,
|
||||
oss_client: Option<OssClient>,
|
||||
#[cfg_attr(test, allow(dead_code))]
|
||||
auth_store: InMemoryAuthStore,
|
||||
@@ -189,6 +192,10 @@ impl AppState {
|
||||
auth_jwt_config,
|
||||
admin_runtime,
|
||||
refresh_cookie_config,
|
||||
#[cfg(test)]
|
||||
test_creation_entry_config: Arc::new(Mutex::new(Some(
|
||||
crate::creation_entry_config::test_creation_entry_config_response(),
|
||||
))),
|
||||
oss_client,
|
||||
auth_store,
|
||||
password_entry_service,
|
||||
@@ -221,6 +228,68 @@ impl AppState {
|
||||
&self.refresh_cookie_config
|
||||
}
|
||||
|
||||
pub async fn get_creation_entry_config(
|
||||
&self,
|
||||
) -> Result<CreationEntryConfigResponse, SpacetimeClientError> {
|
||||
match self.spacetime_client.get_creation_entry_config().await {
|
||||
Ok(config) => {
|
||||
#[cfg(test)]
|
||||
self.cache_test_creation_entry_config(config.clone());
|
||||
Ok(config)
|
||||
}
|
||||
#[cfg(test)]
|
||||
Err(_) => Ok(self.read_test_creation_entry_config()),
|
||||
#[cfg(not(test))]
|
||||
Err(error) => Err(error),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn is_creation_entry_route_enabled(
|
||||
&self,
|
||||
creation_type_id: &str,
|
||||
) -> Result<bool, SpacetimeClientError> {
|
||||
let config = self.get_creation_entry_config().await?;
|
||||
Ok(config
|
||||
.creation_types
|
||||
.iter()
|
||||
.find(|item| item.id == creation_type_id)
|
||||
.map(|item| item.visible && item.open)
|
||||
.unwrap_or(true))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub(crate) fn set_test_creation_entry_route_enabled(
|
||||
&self,
|
||||
creation_type_id: impl AsRef<str>,
|
||||
enabled: bool,
|
||||
) {
|
||||
let creation_type_id = creation_type_id.as_ref();
|
||||
let mut config = self.read_test_creation_entry_config();
|
||||
if let Some(item) = config
|
||||
.creation_types
|
||||
.iter_mut()
|
||||
.find(|item| item.id == creation_type_id)
|
||||
{
|
||||
item.visible = enabled;
|
||||
item.open = enabled;
|
||||
} else {
|
||||
config.creation_types.push(
|
||||
shared_contracts::creation_entry_config::CreationEntryTypeResponse {
|
||||
id: creation_type_id.to_string(),
|
||||
title: creation_type_id.to_string(),
|
||||
subtitle: String::new(),
|
||||
badge: String::new(),
|
||||
image_src: format!("/creation-type-references/{creation_type_id}.webp"),
|
||||
visible: enabled,
|
||||
open: enabled,
|
||||
sort_order: i32::try_from(config.creation_types.len()).unwrap_or(i32::MAX),
|
||||
updated_at_micros: 0,
|
||||
},
|
||||
);
|
||||
}
|
||||
self.cache_test_creation_entry_config(config);
|
||||
}
|
||||
|
||||
pub fn oss_client(&self) -> Option<&OssClient> {
|
||||
self.oss_client.as_ref()
|
||||
}
|
||||
@@ -488,6 +557,21 @@ impl AppState {
|
||||
|
||||
#[cfg(test)]
|
||||
impl AppState {
|
||||
fn cache_test_creation_entry_config(&self, config: CreationEntryConfigResponse) {
|
||||
*self
|
||||
.test_creation_entry_config
|
||||
.lock()
|
||||
.expect("test creation entry config should lock") = Some(config);
|
||||
}
|
||||
|
||||
fn read_test_creation_entry_config(&self) -> CreationEntryConfigResponse {
|
||||
self.test_creation_entry_config
|
||||
.lock()
|
||||
.expect("test creation entry config should lock")
|
||||
.clone()
|
||||
.unwrap_or_else(crate::creation_entry_config::test_creation_entry_config_response)
|
||||
}
|
||||
|
||||
pub(crate) async fn seed_test_phone_user_with_password(
|
||||
&self,
|
||||
phone_number: &str,
|
||||
|
||||
Reference in New Issue
Block a user