Skip to content

Commit

Permalink
react: Reach modify template add req (#497)
Browse files Browse the repository at this point in the history
* reach-modify-template-add-req

* implement try_get_by RbumScopeLevelKind

* reach: add find paged message log api
  • Loading branch information
4t145 authored Oct 23, 2023
1 parent 219a1f4 commit 249765f
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 27 deletions.
22 changes: 19 additions & 3 deletions basic/src/rbum/rbum_enumeration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -21,6 +21,22 @@ pub enum RbumScopeLevelKind {
L3,
}

impl<'de> Deserialize<'de> for RbumScopeLevelKind {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
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<RbumScopeLevelKind> {
match s {
Expand Down Expand Up @@ -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<I: sea_orm::ColIdx>(_res: &QueryResult, _index: I) -> Result<Self, TryGetError> {
panic!("not implemented")
fn try_get_by<I: sea_orm::ColIdx>(res: &QueryResult, index: I) -> Result<Self, TryGetError> {
i16::try_get_by(res, index).map(RbumScopeLevelKind::from_int)?.map_err(|e| TryGetError::DbErr(DbErr::Custom(format!("invalid scope level: {e}"))))
}
}

Expand Down
28 changes: 24 additions & 4 deletions support/reach/src/api/reach_api_ct/reach_api_ct_msg_log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ 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;
#[cfg_attr(feature = "simple-client", simple_invoke_client(Client<'_>))]
#[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<String>, TardisContextExtractor(ctx): TardisContextExtractor) -> TardisApiResult<Vec<ReachMsgLogSummaryResp>> {
#[oai(method = "get", path = "/all")]
pub async fn find_msg_log(&self, reach_message_id: Query<String>, TardisContextExtractor(ctx): TardisContextExtractor) -> TardisApiResult<Vec<ReachMsgLogSummaryResp>> {
let funs = get_tardis_inst();
// filter
let mut filter = ReachMsgLogFilterReq::default();
Expand All @@ -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<Option<u32>>,
page_size: Query<Option<u32>>,
reach_message_id: Query<Option<String>>,
TardisContextExtractor(ctx): TardisContextExtractor,
) -> TardisApiResult<TardisPage<ReachMsgLogSummaryResp>> {
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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "/")]
Expand Down
6 changes: 3 additions & 3 deletions support/reach/src/domain/message_log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
7 changes: 3 additions & 4 deletions support/reach/src/domain/message_template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub struct Model {
pub update_time: DateTime<Utc>,
/// 资源作用级别
#[sea_orm(column_name = "scope_level")]
pub scope_level: i16,
pub scope_level: Option<i16>,
/// 编码
#[sea_orm(column_type = "String(Some(255))")]
pub code: String,
Expand Down Expand Up @@ -98,7 +98,6 @@ impl From<&ReachMessageTemplateAddReq> for ActiveModel {
icon,
code,
name,
scope_level,
sort,
disabled,
variables,
Expand All @@ -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
}
}
Expand All @@ -125,7 +125,6 @@ impl From<&ReachMessageTemplateModifyReq> for ActiveModel {
..Default::default()
};
fill_by_mod_req!(value => {
scope_level,
code,
name,
note,
Expand Down Expand Up @@ -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())
Expand Down
14 changes: 7 additions & 7 deletions support/reach/src/dto/message/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Utc>,
// pub update_time: DateTime<Utc>,
/// 用户触达等级类型
#[oai(default)]
pub scope_level: i16,
pub scope_level: Option<RbumScopeLevelKind>,
/// 编码
#[oai(validator(max_length = "255"), default)]
pub code: String,
Expand Down Expand Up @@ -78,7 +78,7 @@ pub struct ReachMessageTemplateAddReq {
#[derive(Debug, poem_openapi::Object, Serialize, Deserialize, Default)]
pub struct ReachMessageTemplateModifyReq {
/// 用户触达等级类型
pub scope_level: Option<i16>,
pub scope_level: Option<RbumScopeLevelKind>,
/// 编码
#[oai(validator(max_length = "255"))]
pub code: Option<String>,
Expand Down Expand Up @@ -151,7 +151,7 @@ pub struct ReachMessageTemplateSummaryResp {
pub owner: String,
pub create_time: DateTime<Utc>,
pub update_time: DateTime<Utc>,
pub scope_level: Option<i16>,
pub scope_level: Option<RbumScopeLevelKind>,
/// 编码
#[oai(validator(max_length = "255"))]
pub code: Option<String>,
Expand Down Expand Up @@ -210,7 +210,7 @@ pub struct ReachMessageTemplateDetailResp {
pub owner_name: String,
pub create_time: DateTime<Utc>,
pub update_time: DateTime<Utc>,
pub scope_level: Option<i16>,
pub scope_level: Option<RbumScopeLevelKind>,
/// 编码
#[oai(validator(max_length = "255"))]
pub code: Option<String>,
Expand Down
3 changes: 2 additions & 1 deletion support/reach/src/serv/message_log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -38,6 +38,7 @@ impl RbumCrudOperation<message_log::ActiveModel, ReachMsgLogAddReq, ReachMsgLogM

async fn package_query(is_detail: bool, filter: &ReachMsgLogFilterReq, _: &TardisFunsInst, ctx: &TardisContext) -> TardisResult<SelectStatement> {
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));
Expand Down
9 changes: 5 additions & 4 deletions support/reach/tests/test_message_send.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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}"]"#)
Expand All @@ -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(),
Expand All @@ -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(),
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 249765f

Please sign in to comment.