Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

flow: add inst api: modify vars & complete testing #478

Merged
merged 9 commits into from
Oct 8, 2023
Merged
13 changes: 13 additions & 0 deletions middleware/flow/src/api/cc/flow_cc_inst_api.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
use std::collections::HashMap;

use serde_json::Value;
use tardis::web::context_extractor::TardisContextExtractor;
use tardis::web::poem_openapi;
use tardis::web::poem_openapi::param::{Path, Query};
Expand Down Expand Up @@ -106,4 +109,14 @@ impl FlowCcInstApi {
funs.commit().await?;
TardisResp::ok(Void {})
}

/// Modify list of variables / 同步当前变量列表
#[oai(path = "/:flow_inst_id/modify_current_vars", method = "patch")]
async fn modify_current_vars(&self, flow_inst_id: Path<String>, modify_req: Json<HashMap<String, Value>>, ctx: TardisContextExtractor) -> TardisApiResult<Void> {
let mut funs = flow_constants::get_tardis_inst();
funs.begin().await?;
FlowInstServ::modify_current_vars(&flow_inst_id.0, &modify_req.0, &funs, &ctx.0).await?;
funs.commit().await?;
TardisResp::ok(Void {})
}
}
9 changes: 7 additions & 2 deletions middleware/flow/src/dto/flow_inst_dto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ use tardis::{
web::poem_openapi,
};

use super::{flow_state_dto::FlowSysStateKind, flow_transition_dto::FlowTransitionDoubleCheckInfo, flow_var_dto::FlowVarInfo};
use super::{
flow_state_dto::{FlowStateRelModelExt, FlowSysStateKind},
flow_transition_dto::FlowTransitionDoubleCheckInfo,
flow_var_dto::FlowVarInfo,
};

#[derive(Serialize, Deserialize, Debug, poem_openapi::Object)]
pub struct FlowInstStartReq {
Expand Down Expand Up @@ -143,7 +147,6 @@ pub struct FlowInstFindNextTransitionResp {
pub next_flow_state_color: String,

pub vars_collect: Option<Vec<FlowVarInfo>>,

pub double_check: Option<FlowTransitionDoubleCheckInfo>,
}

Expand All @@ -159,6 +162,7 @@ pub struct FlowInstFindStateAndTransitionsResp {
pub current_flow_state_name: String,
pub current_flow_state_kind: FlowSysStateKind,
pub current_flow_state_color: String,
pub current_flow_state_ext: FlowStateRelModelExt,
pub finish_time: Option<DateTime<Utc>>,
pub next_flow_transitions: Vec<FlowInstFindNextTransitionResp>,
}
Expand All @@ -178,6 +182,7 @@ pub struct FlowInstTransferResp {
pub new_flow_state_id: String,
pub new_flow_state_name: String,
pub new_flow_state_color: String,
pub new_flow_state_ext: FlowStateRelModelExt,
pub finish_time: Option<DateTime<Utc>>,

pub vars: Option<HashMap<String, Value>>,
Expand Down
5 changes: 3 additions & 2 deletions middleware/flow/src/dto/flow_transition_dto.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use bios_basic::{dto::BasicQueryCondInfo, basic_enumeration::BasicQueryOpKind};
use bios_basic::{basic_enumeration::BasicQueryOpKind, dto::BasicQueryCondInfo};
use serde::{Deserialize, Serialize};
use tardis::{basic::field::TrimString, db::sea_orm, serde_json::Value, web::poem_openapi, TardisFuns};

Expand Down Expand Up @@ -328,8 +328,9 @@ pub struct FlowTransitionFrontActionInfo {
pub left_label: String,
pub right_value: FlowTransitionFrontActionRightValue,
pub select_field: Option<String>,
pub select_field_label: Option<String>,
pub change_content: Option<String>,
pub real_time: Option<bool>,
pub change_content_label: Option<String>,
}

#[derive(Serialize, Deserialize, Clone, PartialEq, Debug, poem_openapi::Enum)]
Expand Down
47 changes: 42 additions & 5 deletions middleware/flow/src/serv/flow_inst_serv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ use crate::{
FlowInstTransitionInfo, FlowOperationContext,
},
flow_model_dto::{FlowModelDetailResp, FlowModelFilterReq},
flow_state_dto::{FlowStateFilterReq, FlowSysStateKind},
flow_state_dto::{FlowStateFilterReq, FlowStateRelModelExt, FlowSysStateKind},
flow_transition_dto::{
FlowTransitionActionByStateChangeInfo, FlowTransitionActionChangeAgg, FlowTransitionActionChangeInfo, FlowTransitionActionChangeKind, FlowTransitionDetailResp,
StateChangeConditionOp,
Expand All @@ -48,7 +48,10 @@ use crate::{
serv::{flow_model_serv::FlowModelServ, flow_state_serv::FlowStateServ},
};

use super::flow_external_serv::FlowExternalServ;
use super::{
flow_external_serv::FlowExternalServ,
flow_rel_serv::{FlowRelKind, FlowRelServ},
};

pub struct FlowInstServ;

Expand Down Expand Up @@ -758,6 +761,14 @@ impl FlowInstServ {
prev_flow_state_id: prev_flow_state.id,
prev_flow_state_name: prev_flow_state.name,
prev_flow_state_color: prev_flow_state.color,
new_flow_state_ext: TardisFuns::json.str_to_obj::<FlowStateRelModelExt>(
&FlowRelServ::find_from_simple_rels(&FlowRelKind::FlowModelState, &flow_model.id, None, None, funs, ctx)
.await?
.into_iter()
.find(|rel| next_flow_state.id == rel.rel_id)
.ok_or_else(|| funs.err().not_found("flow_inst", "do_find_next_transitions", "flow state is not found", "404-flow-state-not-found"))?
.ext,
)?,
new_flow_state_id: next_flow_state.id,
new_flow_state_name: next_flow_state.name,
new_flow_state_color: next_flow_state.color,
Expand Down Expand Up @@ -1116,14 +1127,23 @@ impl FlowInstServ {
funs,
ctx,
)
.await?;
.await?
.ok_or_else(|| funs.err().not_found("flow_inst", "do_find_next_transitions", "flow state is not found", "404-flow-state-not-found"))?;

let state_and_next_transitions = FlowInstFindStateAndTransitionsResp {
flow_inst_id: flow_inst.id.to_string(),
finish_time: flow_inst.finish_time,
current_flow_state_name: flow_inst.current_state_name.as_ref().unwrap_or(&"".to_string()).to_string(),
current_flow_state_kind: current_flow_state.as_ref().map(|state| state.sys_state.clone()).unwrap_or(FlowSysStateKind::Finish),
current_flow_state_color: current_flow_state.as_ref().map(|state| state.color.clone()).unwrap_or_default(),
current_flow_state_kind: current_flow_state.sys_state.clone(),
current_flow_state_color: current_flow_state.color.clone(),
current_flow_state_ext: TardisFuns::json.str_to_obj::<FlowStateRelModelExt>(
&FlowRelServ::find_from_simple_rels(&FlowRelKind::FlowModelState, &flow_inst.rel_flow_model_id, None, None, funs, ctx)
.await?
.into_iter()
.find(|rel| current_flow_state.id == rel.rel_id)
.ok_or_else(|| funs.err().not_found("flow_inst", "do_find_next_transitions", "flow state is not found", "404-flow-state-not-found"))?
.ext,
)?,
next_flow_transitions: next_transitions,
};
Ok(state_and_next_transitions)
Expand All @@ -1148,4 +1168,21 @@ impl FlowInstServ {
Ok(false)
}
}

pub async fn modify_current_vars(flow_inst_id: &str, current_vars: &HashMap<String, Value>, funs: &TardisFunsInst, ctx: &TardisContext) -> TardisResult<()> {
let flow_inst_detail = Self::get(flow_inst_id, funs, ctx).await?;
let mut new_vars: HashMap<String, Value> = HashMap::new();
if let Some(old_current_vars) = &flow_inst_detail.current_vars {
new_vars.extend(old_current_vars.clone());
}
new_vars.extend(current_vars.clone());
let flow_inst = flow_inst::ActiveModel {
id: Set(flow_inst_id.to_string()),
current_vars: Set(Some(TardisFuns::json.obj_to_json(&new_vars)?)),
..Default::default()
};
funs.db().update_one(flow_inst, ctx).await?;

Ok(())
}
}
57 changes: 52 additions & 5 deletions middleware/flow/tests/test_flow_scenes_fsm.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::collections::HashMap;

use bios_basic::basic_enumeration::BasicQueryOpKind;
use bios_basic::dto::BasicQueryCondInfo;
use bios_basic::rbum::rbum_enumeration::RbumScopeLevelKind;
use bios_basic::test::test_http_client::TestHttpClient;

Expand All @@ -14,8 +16,9 @@ use bios_mw_flow::dto::flow_model_dto::{
};
use bios_mw_flow::dto::flow_state_dto::{FlowStateAddReq, FlowStateSummaryResp, FlowSysStateKind};
use bios_mw_flow::dto::flow_transition_dto::{
FlowTransitionActionChangeInfo, FlowTransitionActionChangeKind, FlowTransitionAddReq, FlowTransitionDoubleCheckInfo, FlowTransitionModifyReq, StateChangeCondition,
StateChangeConditionItem, StateChangeConditionOp,
FlowTransitionActionChangeInfo, FlowTransitionActionChangeKind, FlowTransitionAddReq, FlowTransitionDoubleCheckInfo, FlowTransitionFrontActionInfo,
FlowTransitionFrontActionRightValue, FlowTransitionModifyReq, FlowTransitionSortStateInfoReq, FlowTransitionSortStatesReq, StateChangeCondition, StateChangeConditionItem,
StateChangeConditionOp,
};

use bios_mw_flow::dto::flow_var_dto::{FlowVarInfo, RbumDataTypeKind, RbumWidgetTypeKind};
Expand Down Expand Up @@ -179,6 +182,31 @@ pub async fn test(flow_client: &mut TestHttpClient, _kv_client: &mut TestHttpCli
let trans_complate = req_model_agg.states.iter().find(|state| state.name == "进行中").unwrap().transitions.iter().find(|trans| trans.name == "完成").unwrap();
let trans_close = req_model_agg.states.iter().find(|state| state.name == "进行中").unwrap().transitions.iter().find(|trans| trans.name == "关闭").unwrap();

let _: Void = flow_client
.patch(
&format!("/cc/model/{}", req_model_id),
&json!({
"modify_transitions": [
{
"id": trans_start.id.clone(),
"action_by_front_changes": [
{
"relevance_relation": "in",
"relevance_label": "包含",
"left_value": "status",
"left_label": "状态",
"right_value": "select_field",
"select_field": "status",
"change_content": null,
"select_field_label": "status",
"change_content_label": null
}
]
}
]
}),
)
.await;
let _: Void = flow_client
.patch(
&format!("/cc/model/{}", req_model_id),
Expand Down Expand Up @@ -218,7 +246,6 @@ pub async fn test(flow_client: &mut TestHttpClient, _kv_client: &mut TestHttpCli
]),
action_by_pre_callback: None,
action_by_post_callback: None,
action_by_front_changes: None,
action_by_post_changes: Some(vec![FlowTransitionActionChangeInfo {
kind: FlowTransitionActionChangeKind::State,
describe: "".to_string(),
Expand All @@ -242,6 +269,7 @@ pub async fn test(flow_client: &mut TestHttpClient, _kv_client: &mut TestHttpCli
is_open: true,
content: Some("再次确认该操作生效".to_string()),
}),
action_by_front_changes: None,
sort: None,
},
FlowTransitionModifyReq {
Expand Down Expand Up @@ -304,6 +332,23 @@ pub async fn test(flow_client: &mut TestHttpClient, _kv_client: &mut TestHttpCli
},
)
.await;
let _: Void = flow_client
.post(
&format!("/cc/model/{}/resort_transition", &req_model_id),
&FlowTransitionSortStatesReq {
sort_states: vec![
FlowTransitionSortStateInfoReq {
id: req_model_agg.states.iter().find(|state| state.name == "待开始").unwrap().transitions.iter().find(|trans| trans.name == "开始").unwrap().id.clone(),
sort: 2,
},
FlowTransitionSortStateInfoReq {
id: req_model_agg.states.iter().find(|state| state.name == "待开始").unwrap().transitions.iter().find(|trans| trans.name == "关闭").unwrap().id.clone(),
sort: 1,
},
],
},
)
.await;
// 3-3. check post action endless loop
let proj_trans = proj_model_agg.states.iter().find(|state| state.name == "进行中").unwrap().transitions.iter().find(|trans| trans.name == "有风险").unwrap();
let _: Void = flow_client
Expand Down Expand Up @@ -619,8 +664,10 @@ pub async fn test(flow_client: &mut TestHttpClient, _kv_client: &mut TestHttpCli
let next_transitions: Vec<FlowInstFindNextTransitionResp> =
flow_client.put(&format!("/cc/inst/{}/transition/next", req_inst_id1), &FlowInstFindNextTransitionsReq { vars: None }).await;
assert_eq!(next_transitions.len(), 2);
assert!(next_transitions.iter().any(|trans| trans.next_flow_transition_name.contains("开始")));
assert!(next_transitions.iter().any(|trans| trans.next_flow_transition_name.contains("关闭")));
assert_eq!(next_transitions[0].next_flow_transition_name, "关闭");
assert_eq!(next_transitions[1].next_flow_transition_name, "开始-modify");
// assert!(next_transitions.iter().any(|trans| trans.next_flow_transition_name.contains("开始")));
// assert!(next_transitions.iter().any(|trans| trans.next_flow_transition_name.contains("关闭")));
// Find the state and transfer information of the specified instances in batch
let state_and_next_transitions: Vec<FlowInstFindStateAndTransitionsResp> = flow_client
.put(
Expand Down
2 changes: 1 addition & 1 deletion spi/spi-log/src/serv/pg/log_pg_initializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,5 @@ pub async fn init_table_and_conn(bs_inst: TypedSpiBsInst<'_, TardisRelDBClient>,
None,
None,
)
.await
.await
}
10 changes: 5 additions & 5 deletions spi/spi-log/src/serv/pg/log_pg_item_serv.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use tardis::{
basic::{dto::TardisContext, result::TardisResult},
db::{reldb_client::TardisRelDBClient, sea_orm::Value},
TardisFuns,
TardisFunsInst, web::web_resp::TardisPage,
web::web_resp::TardisPage,
TardisFuns, TardisFunsInst,
};

use bios_basic::{basic_enumeration::BasicQueryOpKind, dto::BasicQueryCondInfo, helper::db_helper, spi::spi_funs::SpiBsInst};
Expand Down Expand Up @@ -47,7 +47,7 @@ VALUES
),
params,
)
.await?;
.await?;
conn.commit().await?;
Ok(id)
}
Expand Down Expand Up @@ -127,7 +127,7 @@ pub async fn find(find_req: &mut LogItemFindReq, funs: &TardisFunsInst, ctx: &Ta
Err(funs.err().not_found(
"item",
"log",
&format!("The ext field=[{}] value=[{}] operation=[{}] is not legal.", &ext.field, ext.value, &ext.op, ),
&format!("The ext field=[{}] value=[{}] operation=[{}] is not legal.", &ext.field, ext.value, &ext.op,),
"404-spi-log-op-not-legal",
))
};
Expand Down Expand Up @@ -274,7 +274,7 @@ ORDER BY ts DESC
where_fragments.join(" AND "),
page_fragments
)
.as_str(),
.as_str(),
sql_vals,
)
.await?;
Expand Down