From 249765f1ebf9750909b44bb281bb602b6b43c727 Mon Sep 17 00:00:00 2001 From: 4t145 Date: Mon, 23 Oct 2023 11:10:05 +0800 Subject: [PATCH] react: Reach modify template add req (#497) * reach-modify-template-add-req * implement try_get_by RbumScopeLevelKind * reach: add find paged message log api --- basic/src/rbum/rbum_enumeration.rs | 22 +++++++++++++-- .../api/reach_api_ct/reach_api_ct_msg_log.rs | 28 ++++++++++++++++--- .../reach_api_ct_trigger_global.rs | 2 +- support/reach/src/domain/message_log.rs | 6 ++-- support/reach/src/domain/message_template.rs | 7 ++--- support/reach/src/dto/message/template.rs | 14 +++++----- support/reach/src/serv/message_log.rs | 3 +- support/reach/tests/test_message_send.rs | 9 +++--- 8 files changed, 64 insertions(+), 27 deletions(-) diff --git a/basic/src/rbum/rbum_enumeration.rs b/basic/src/rbum/rbum_enumeration.rs index 6519f6349..672ae14c9 100644 --- a/basic/src/rbum/rbum_enumeration.rs +++ b/basic/src/rbum/rbum_enumeration.rs @@ -11,7 +11,7 @@ use tardis::derive_more::Display; #[cfg(feature = "default")] use tardis::web::poem_openapi; -#[derive(Display, Clone, Debug, PartialEq, Eq, Deserialize, Serialize)] +#[derive(Display, Clone, Debug, PartialEq, Eq, Serialize)] #[cfg_attr(feature = "default", derive(poem_openapi::Enum))] pub enum RbumScopeLevelKind { Private, @@ -21,6 +21,22 @@ pub enum RbumScopeLevelKind { L3, } +impl<'de> Deserialize<'de> for RbumScopeLevelKind { + fn deserialize(deserializer: D) -> Result + where + D: serde::Deserializer<'de>, + { + String::deserialize(deserializer).and_then(|s| match s.to_lowercase().as_str() { + "private" => Ok(RbumScopeLevelKind::Private), + "root" => Ok(RbumScopeLevelKind::Root), + "l1" => Ok(RbumScopeLevelKind::L1), + "l2" => Ok(RbumScopeLevelKind::L2), + "l3" => Ok(RbumScopeLevelKind::L3), + _ => Err(serde::de::Error::custom(format!("invalid RbumScopeLevelKind: {s}"))), + }) + } +} + impl RbumScopeLevelKind { pub fn from_int(s: i16) -> TardisResult { match s { @@ -51,8 +67,8 @@ impl TryGetable for RbumScopeLevelKind { RbumScopeLevelKind::from_int(s).map_err(|_| TryGetError::DbErr(DbErr::RecordNotFound(format!("{pre}:{col}")))) } - fn try_get_by(_res: &QueryResult, _index: I) -> Result { - panic!("not implemented") + fn try_get_by(res: &QueryResult, index: I) -> Result { + i16::try_get_by(res, index).map(RbumScopeLevelKind::from_int)?.map_err(|e| TryGetError::DbErr(DbErr::Custom(format!("invalid scope level: {e}")))) } } diff --git a/support/reach/src/api/reach_api_ct/reach_api_ct_msg_log.rs b/support/reach/src/api/reach_api_ct/reach_api_ct_msg_log.rs index 0d234b297..a731694a8 100644 --- a/support/reach/src/api/reach_api_ct/reach_api_ct_msg_log.rs +++ b/support/reach/src/api/reach_api_ct/reach_api_ct_msg_log.rs @@ -6,9 +6,9 @@ use crate::serv::*; use bios_basic::rbum::serv::rbum_crud_serv::RbumCrudOperation; use bios_sdk_invoke::simple_invoke_client; use tardis::web::context_extractor::TardisContextExtractor; -use tardis::web::poem::web::Path; use tardis::web::poem_openapi; -use tardis::web::web_resp::{TardisApiResult, TardisResp}; +use tardis::web::poem_openapi::param::Query; +use tardis::web::web_resp::{TardisApiResult, TardisPage, TardisResp}; #[derive(Clone, Default)] /// 消息记录-租户控制台 pub struct ReachMsgLogCtApi; @@ -16,8 +16,8 @@ pub struct ReachMsgLogCtApi; #[poem_openapi::OpenApi(prefix_path = "/ct/msg/log", tag = "bios_basic::ApiTag::App")] impl ReachMsgLogCtApi { /// 获取所有消息记录数据 - #[oai(method = "get", path = "/:reach_message_id")] - pub async fn find_msg_log(&self, reach_message_id: Path, TardisContextExtractor(ctx): TardisContextExtractor) -> TardisApiResult> { + #[oai(method = "get", path = "/all")] + pub async fn find_msg_log(&self, reach_message_id: Query, TardisContextExtractor(ctx): TardisContextExtractor) -> TardisApiResult> { let funs = get_tardis_inst(); // filter let mut filter = ReachMsgLogFilterReq::default(); @@ -26,4 +26,24 @@ impl ReachMsgLogCtApi { let resp = ReachMessageLogServ::find_rbums(&filter, None, None, &funs, &ctx).await?; TardisResp::ok(resp) } + + /// 获取所有消息记录数据 + #[oai(method = "get", path = "/page")] + pub async fn find_msg_log_paged( + &self, + page_number: Query>, + page_size: Query>, + reach_message_id: Query>, + TardisContextExtractor(ctx): TardisContextExtractor, + ) -> TardisApiResult> { + let funs = get_tardis_inst(); + let page_number = page_number.unwrap_or(1); + let page_size = page_size.unwrap_or(10); + // filter + let mut filter = ReachMsgLogFilterReq::default(); + filter.base_filter.basic.with_sub_own_paths = true; + filter.rel_reach_message_id = reach_message_id.0; + let page_resp = ReachMessageLogServ::paginate_rbums(&filter, page_number, page_size, Some(true), None, &funs, &ctx).await?; + TardisResp::ok(page_resp) + } } diff --git a/support/reach/src/api/reach_api_ct/reach_api_ct_trigger_global.rs b/support/reach/src/api/reach_api_ct/reach_api_ct_trigger_global.rs index 103806caf..48c078125 100644 --- a/support/reach/src/api/reach_api_ct/reach_api_ct_trigger_global.rs +++ b/support/reach/src/api/reach_api_ct/reach_api_ct_trigger_global.rs @@ -17,7 +17,7 @@ use crate::reach_invoke::Client; pub struct ReachTriggerGlobalConfigCtApi; #[cfg_attr(feature = "simple-client", bios_sdk_invoke::simple_invoke_client(Client<'_>))] -#[poem_openapi::OpenApi(prefix_path = "/ct/msg/global/config", tag = "bios_basic::ApiTag::App")] +#[poem_openapi::OpenApi(prefix_path = "/ct/trigger/global/config", tag = "bios_basic::ApiTag::App")] impl ReachTriggerGlobalConfigCtApi { /// 获取所有用户触达触发全局配置数据 #[oai(method = "get", path = "/")] diff --git a/support/reach/src/domain/message_log.rs b/support/reach/src/domain/message_log.rs index 14a0df5f4..918c2c60d 100644 --- a/support/reach/src/domain/message_log.rs +++ b/support/reach/src/domain/message_log.rs @@ -109,9 +109,9 @@ impl TardisActiveModel for ActiveModel { .col(ColumnDef::new(Column::RelAccountId).not_null().string()) .col(ColumnDef::new(Column::DndTime).not_null().string()) .col(ColumnDef::new(Column::DndStrategy).not_null().string()) - .col(ColumnDef::new(Column::StartTime).timestamp()) - .col(ColumnDef::new(Column::EndTime).timestamp()) - .col(ColumnDef::new(Column::FinishTime).extra("DEFAULT CURRENT_TIMESTAMP".to_string()).timestamp()) + .col(ColumnDef::new(Column::StartTime).timestamp_with_time_zone()) + .col(ColumnDef::new(Column::EndTime).timestamp_with_time_zone()) + .col(ColumnDef::new(Column::FinishTime).extra("DEFAULT CURRENT_TIMESTAMP".to_string()).timestamp_with_time_zone()) .col(ColumnDef::new(Column::Failure).not_null().boolean()) .col(ColumnDef::new(Column::FailMessage).not_null().string()) .col(ColumnDef::new(Column::RelReachMessageId).not_null().string()) diff --git a/support/reach/src/domain/message_template.rs b/support/reach/src/domain/message_template.rs index 8239e751d..20c7c09b6 100644 --- a/support/reach/src/domain/message_template.rs +++ b/support/reach/src/domain/message_template.rs @@ -27,7 +27,7 @@ pub struct Model { pub update_time: DateTime, /// 资源作用级别 #[sea_orm(column_name = "scope_level")] - pub scope_level: i16, + pub scope_level: Option, /// 编码 #[sea_orm(column_type = "String(Some(255))")] pub code: String, @@ -98,7 +98,6 @@ impl From<&ReachMessageTemplateAddReq> for ActiveModel { icon, code, name, - scope_level, sort, disabled, variables, @@ -114,6 +113,7 @@ impl From<&ReachMessageTemplateAddReq> for ActiveModel { sms_signature, sms_from, } model); + model.scope_level = Set(add_req.scope_level.clone().map(|level| level.to_int())); model } } @@ -125,7 +125,6 @@ impl From<&ReachMessageTemplateModifyReq> for ActiveModel { ..Default::default() }; fill_by_mod_req!(value => { - scope_level, code, name, note, @@ -164,7 +163,7 @@ impl TardisActiveModel for ActiveModel { .col(ColumnDef::new(Column::Id).not_null().string().primary_key()) .col(ColumnDef::new(Column::OwnPaths).not_null().string()) .col(ColumnDef::new(Column::Owner).not_null().string()) - .col(ColumnDef::new(Column::ScopeLevel).not_null().small_integer()) + .col(ColumnDef::new(Column::ScopeLevel).small_integer()) .col(ColumnDef::new(Column::Code).not_null().string()) .col(ColumnDef::new(Column::Name).not_null().string()) .col(ColumnDef::new(Column::Note).not_null().string()) diff --git a/support/reach/src/dto/message/template.rs b/support/reach/src/dto/message/template.rs index b407a84fb..c6e437485 100644 --- a/support/reach/src/dto/message/template.rs +++ b/support/reach/src/dto/message/template.rs @@ -7,16 +7,16 @@ use tardis::{ }; use crate::dto::*; - +use bios_basic::rbum::rbum_enumeration::RbumScopeLevelKind; #[derive(Debug, poem_openapi::Object, Serialize, Deserialize, Default)] pub struct ReachMessageTemplateAddReq { - pub own_paths: String, - pub owner: String, + // pub own_paths: String, + // pub owner: String, // pub create_time: DateTime, // pub update_time: DateTime, /// 用户触达等级类型 #[oai(default)] - pub scope_level: i16, + pub scope_level: Option, /// 编码 #[oai(validator(max_length = "255"), default)] pub code: String, @@ -78,7 +78,7 @@ pub struct ReachMessageTemplateAddReq { #[derive(Debug, poem_openapi::Object, Serialize, Deserialize, Default)] pub struct ReachMessageTemplateModifyReq { /// 用户触达等级类型 - pub scope_level: Option, + pub scope_level: Option, /// 编码 #[oai(validator(max_length = "255"))] pub code: Option, @@ -151,7 +151,7 @@ pub struct ReachMessageTemplateSummaryResp { pub owner: String, pub create_time: DateTime, pub update_time: DateTime, - pub scope_level: Option, + pub scope_level: Option, /// 编码 #[oai(validator(max_length = "255"))] pub code: Option, @@ -210,7 +210,7 @@ pub struct ReachMessageTemplateDetailResp { pub owner_name: String, pub create_time: DateTime, pub update_time: DateTime, - pub scope_level: Option, + pub scope_level: Option, /// 编码 #[oai(validator(max_length = "255"))] pub code: Option, diff --git a/support/reach/src/serv/message_log.rs b/support/reach/src/serv/message_log.rs index e61b8c8ec..426db9e1d 100644 --- a/support/reach/src/serv/message_log.rs +++ b/support/reach/src/serv/message_log.rs @@ -7,8 +7,8 @@ use tardis::basic::result::TardisResult; use tardis::db::reldb_client::TardisActiveModel; use tardis::db::sea_orm::sea_query::{Query, SelectStatement}; -use tardis::db::sea_orm::EntityName; use tardis::db::sea_orm::{ColumnTrait, Set}; +use tardis::db::sea_orm::{EntityName, Iterable}; use tardis::{TardisFuns, TardisFunsInst}; pub struct ReachMessageLogServ; @@ -38,6 +38,7 @@ impl RbumCrudOperation TardisResult { let mut query = Query::select(); + query.columns(message_log::Column::iter().map(|c| (message_log::Entity, c))); query.from(message_log::Entity); if let Some(id) = &filter.rel_reach_message_id { query.and_where(message_log::Column::RelReachMessageId.eq(id)); diff --git a/support/reach/tests/test_message_send.rs b/support/reach/tests/test_message_send.rs index 95f66fddb..0e4b44add 100644 --- a/support/reach/tests/test_message_send.rs +++ b/support/reach/tests/test_message_send.rs @@ -15,7 +15,7 @@ pub async fn test_ct_api() -> TardisResult<()> { let ctx = get_test_ctx(); let funs = get_tardis_inst(); let client = reach_invoke::Client::new("http://localhost:8080/reach", ctx, &funs); - const CONTENT_TEMPLATE: &str = "hello {name}, your code is {code}"; + const CONTENT_TEMPLATE: &str = "[\"hello {name}, your code is {code}\"]"; let template_name = random_string(16); fn expected_content(name: &str, code: &str) -> String { format!(r#"["hello {name}, your code is {code}"]"#) @@ -25,8 +25,6 @@ pub async fn test_ct_api() -> TardisResult<()> { let message_add_req = ReachMessageTemplateAddReq { rel_reach_channel: ReachChannelKind::Sms, content: CONTENT_TEMPLATE.into(), - own_paths: ctx.own_paths.clone(), - owner: ctx.owner.clone(), variables: "name,code".into(), level_kind: ReachLevelKind::Normal, topic: "hellow".to_string(), @@ -37,7 +35,6 @@ pub async fn test_ct_api() -> TardisResult<()> { sms_template_id: "sms-tempalte-id".into(), sms_signature: "sms-signature".into(), sms_from: "reach@bios.dev".into(), - scope_level: 0, code: "test-code".into(), name: template_name.clone(), note: "test-note".into(), @@ -254,6 +251,10 @@ pub async fn test_ct_api() -> TardisResult<()> { log::info!("latest message for {name}: {:?}", msg); let msg = msg.expect("message is empty"); assert_eq!(msg, expected_content(name, &code)); + + // should have log now + let logs = client.find_msg_log_paged(Some(1), Some(10), None).await?; + dbg!(logs); } drop(holder);