Skip to content

Commit

Permalink
flow: fix bug(checkout model template) (#826)
Browse files Browse the repository at this point in the history
* flow: fix bug

* flow: fix bug(update inst states bug)

---------

Co-authored-by: ZzIsGod1019 <1498852723@11.com>
  • Loading branch information
ZzIsGod1019 and ZzIsGod1019 authored Aug 19, 2024
1 parent d5944ff commit 6ca9383
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 37 deletions.
48 changes: 47 additions & 1 deletion backend/middlewares/flow/src/api/cc/flow_cc_model_api.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use std::collections::HashMap;

use bios_basic::rbum::dto::rbum_filer_dto::RbumBasicFilterReq;
use bios_basic::rbum::dto::rbum_filer_dto::{RbumBasicFilterReq, RbumRelFilterReq};
use bios_basic::rbum::serv::rbum_item_serv::RbumItemCrudOperation;
use bios_basic::rbum::serv::rbum_rel_serv::RbumRelServ;
use itertools::Itertools;
use tardis::basic::dto::TardisContext;
use tardis::web::context_extractor::TardisContextExtractor;
use tardis::web::poem::Request;
use tardis::web::poem_openapi;
Expand All @@ -18,6 +20,7 @@ use crate::dto::flow_state_dto::FlowStateRelModelModifyReq;
use crate::dto::flow_transition_dto::{FlowTransitionModifyReq, FlowTransitionSortStatesReq};
use crate::flow_constants;
use crate::serv::flow_model_serv::FlowModelServ;
use crate::serv::flow_rel_serv::{FlowRelKind, FlowRelServ};
#[derive(Clone)]
pub struct FlowCcModelApi;

Expand Down Expand Up @@ -361,4 +364,47 @@ impl FlowCcModelApi {
ctx.0.execute_task().await?;
TardisResp::ok(Void {})
}

/// batch add rels with template and app
///
/// 批量添加模板和应用的关联关系
#[oai(path = "/batch_add_template_app_rels", method = "get")]
async fn batch_add_template_app_rels(&self, _request: &Request) -> TardisApiResult<Void> {
let mut funs = flow_constants::get_tardis_inst();
let global_ctx = TardisContext::default();
funs.begin().await?;
let rels = RbumRelServ::find_rels(&RbumRelFilterReq {
basic: RbumBasicFilterReq {
with_sub_own_paths: true,
..Default::default()
},
tag: Some("FlowModelPath".to_string()),
..Default::default()
}, None, None, &funs, &global_ctx).await?;
for rel in rels {
let ctx = TardisContext {
own_paths: rel.rel.own_paths,
owner: rel.rel.owner,
..Default::default()
};
let rel_model_id = rel.rel.from_rbum_id;
if let Some(template_id) = FlowRelServ::find_from_simple_rels(&FlowRelKind::FlowModelTemplate, &rel_model_id, None, None, &funs, &ctx).await?.pop().map(|rel| rel.rel_id) {
FlowRelServ::add_simple_rel(
&FlowRelKind::FlowAppTemplate,
&rel.rel.to_rbum_item_id.split('/').collect::<Vec<&str>>().last().map(|s| s.to_string()).unwrap_or_default(),
&template_id,
None,
None,
true,
true,
None,
&funs,
&ctx,
)
.await?;
}
}
funs.commit().await?;
TardisResp::ok(Void)
}
}
46 changes: 45 additions & 1 deletion backend/middlewares/flow/src/api/ci/flow_ci_model_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ use crate::flow_constants;
use crate::serv::flow_inst_serv::FlowInstServ;
use crate::serv::flow_model_serv::FlowModelServ;
use crate::serv::flow_rel_serv::{FlowRelKind, FlowRelServ};
use bios_basic::rbum::dto::rbum_filer_dto::RbumBasicFilterReq;
use bios_basic::rbum::dto::rbum_filer_dto::{RbumBasicFilterReq, RbumRelFilterReq};
use bios_basic::rbum::helper::rbum_scope_helper::{self, check_without_owner_and_unsafe_fill_ctx};
use bios_basic::rbum::rbum_enumeration::RbumScopeLevelKind;
use bios_basic::rbum::serv::rbum_item_serv::RbumItemCrudOperation;
use bios_basic::rbum::serv::rbum_rel_serv::RbumRelServ;
use itertools::Itertools;
use std::iter::Iterator;
use tardis::basic::dto::TardisContext;
Expand Down Expand Up @@ -289,4 +290,47 @@ impl FlowCiModelApi {

TardisResp::ok(result)
}

/// batch add rels with template and app
///
/// 批量添加模板和应用的关联关系
#[oai(path = "/batch_add_template_app_rels", method = "get")]
async fn batch_add_template_app_rels(&self, _request: &Request) -> TardisApiResult<Void> {
let mut funs = flow_constants::get_tardis_inst();
let global_ctx = TardisContext::default();
funs.begin().await?;
let rels = RbumRelServ::find_rels(&RbumRelFilterReq {
basic: RbumBasicFilterReq {
with_sub_own_paths: true,
..Default::default()
},
tag: Some("FlowModelPath".to_string()),
..Default::default()
}, None, None, &funs, &global_ctx).await?;
for rel in rels {
let ctx = TardisContext {
own_paths: rel.rel.own_paths,
owner: rel.rel.owner,
..Default::default()
};
let rel_model_id = rel.rel.from_rbum_id;
if let Some(template_id) = FlowRelServ::find_from_simple_rels(&FlowRelKind::FlowModelTemplate, &rel_model_id, None, None, &funs, &ctx).await?.pop().map(|rel| rel.rel_id) {
FlowRelServ::add_simple_rel(
&FlowRelKind::FlowAppTemplate,
&rel.rel.to_rbum_item_id.split('/').collect::<Vec<&str>>().last().map(|s| s.to_string()).unwrap_or_default(),
&template_id,
None,
None,
true,
true,
None,
&funs,
&ctx,
)
.await?;
}
}
funs.commit().await?;
TardisResp::ok(Void)
}
}
25 changes: 16 additions & 9 deletions backend/middlewares/flow/src/serv/flow_inst_serv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -615,9 +615,9 @@ impl FlowInstServ {
}
let model_transition = flow_model.transitions();
let next_transition_detail = model_transition.iter().find(|trans| trans.id == transfer_req.flow_transition_id).unwrap().to_owned();
if FlowModelServ::check_post_action_ring(&flow_model, funs, ctx).await? {
return Err(funs.err().not_found("flow_inst", "transfer", "this post action exist endless loop", "500-flow-transition-endless-loop"));
}
// if FlowModelServ::check_post_action_ring(&flow_model, funs, ctx).await? {
// return Err(funs.err().not_found("flow_inst", "transfer", "this post action exist endless loop", "500-flow-transition-endless-loop"));
// }

let next_flow_transition = next_flow_transition.unwrap();
let prev_flow_state = FlowStateServ::get_item(
Expand Down Expand Up @@ -1192,6 +1192,8 @@ impl FlowInstServ {
funs: &TardisFunsInst,
ctx: &TardisContext,
) -> TardisResult<()> {
FlowInstServ::unsafe_update_state_by_tag(original_model_id.clone(), tag, modify_model_id, modify_model_states, state_id, funs, ctx).await?;

let mut update_statement = Query::update();
update_statement.table(flow_inst::Entity);
update_statement.value(flow_inst::Column::RelFlowModelId, modify_model_id);
Expand All @@ -1203,7 +1205,6 @@ impl FlowInstServ {
}
funs.db().execute(&update_statement).await?;

FlowInstServ::unsafe_update_state_by_tag(original_model_id, tag, modify_model_id, modify_model_states, state_id, funs, ctx).await?;
Ok(())
}

Expand All @@ -1216,14 +1217,16 @@ impl FlowInstServ {
funs: &TardisFunsInst,
ctx: &TardisContext,
) -> TardisResult<()> {
let gloabl_ctx = TardisContext::default();
let insts = Self::find_details(
&FlowInstFilterReq {
flow_model_id: original_model_id,
flow_model_id: original_model_id.clone(),
tag: Some(tag.to_string()),
with_sub: Some(true),
..Default::default()
},
funs,
ctx,
if original_model_id.is_some() { &gloabl_ctx } else { ctx },
)
.await?
.into_iter()
Expand All @@ -1233,6 +1236,10 @@ impl FlowInstServ {
insts
.iter()
.map(|inst| async {
let mock_ctx = TardisContext{
own_paths: inst.own_paths.clone(),
..ctx.clone()
};
let global_ctx = TardisContext::default();

let flow_inst = flow_inst::ActiveModel {
Expand All @@ -1241,8 +1248,8 @@ impl FlowInstServ {
transitions: Set(Some(vec![])),
..Default::default()
};
funs.db().update_one(flow_inst, ctx).await.unwrap();
let modify_model_detail = FlowModelServ::get_item(modify_model_id, &FlowModelFilterReq::default(), funs, ctx).await.unwrap();
funs.db().update_one(flow_inst, &mock_ctx).await.unwrap();
let model_tag = FlowModelServ::get_item(modify_model_id, &FlowModelFilterReq::default(), funs, ctx).await.map(|detail| detail.tag);
let next_flow_state = FlowStateServ::get_item(
state_id,
&FlowStateFilterReq {
Expand All @@ -1259,7 +1266,7 @@ impl FlowInstServ {
.unwrap();

FlowExternalServ::do_notify_changes(
&modify_model_detail.tag,
model_tag.unwrap_or_default().as_str(),
&inst.id,
&inst.rel_business_obj_id,
"".to_string(),
Expand Down
55 changes: 32 additions & 23 deletions backend/middlewares/flow/src/serv/flow_model_serv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,9 +396,6 @@ impl RbumItemCrudOperation<flow_model::ActiveModel, FlowModelAddReq, FlowModelMo
{
return Err(funs.err().not_found(&Self::get_obj_name(), "delete_item", "the model prohibit delete", "500-flow_model-prohibit-delete"));
}
if !FlowRelServ::find_from_simple_rels(&FlowRelKind::FlowModelPath, flow_model_id, None, None, funs, ctx).await?.is_empty() {
return Err(funs.err().not_found(&Self::get_obj_name(), "delete_item", "the model prohibit delete", "500-flow_model-prohibit-delete"));
}
let detail = Self::get_item(flow_model_id, &FlowModelFilterReq::default(), funs, ctx).await?;

join_all(
Expand Down Expand Up @@ -1031,7 +1028,11 @@ impl FlowModelServ {
let mut result = HashMap::new();

let filter_ids = if template_id.is_none() {
Some(FlowRelServ::find_to_simple_rels(&FlowRelKind::FlowModelPath, &ctx.own_paths, None, None, funs, ctx).await?.into_iter().map(|rel| rel.rel_id).collect_vec())
if let Some(app_id) = Self::get_app_id_by_ctx(ctx) {
Some(FlowRelServ::find_model_ids_by_app_id(&app_id, funs, ctx).await.unwrap_or_default())
} else {
Some(vec![])
}
} else {
None
};
Expand Down Expand Up @@ -1108,19 +1109,23 @@ impl FlowModelServ {
)
.await?
} else {
FlowRelServ::add_simple_rel(
&FlowRelKind::FlowModelPath,
rel_model_id,
&rel_own_paths.unwrap_or_default(),
None,
None,
false,
true,
None,
funs,
ctx,
)
.await?;
if let Some(template_id) = FlowRelServ::find_from_simple_rels(&FlowRelKind::FlowModelTemplate, rel_model_id, None, None, funs, ctx).await?.pop().map(|rel| rel.rel_id) {
if !FlowRelServ::exist_rels(&FlowRelKind::FlowAppTemplate, &template_id, Self::get_app_id_by_ctx(ctx).unwrap_or_default().as_str(), funs, ctx).await? {
FlowRelServ::add_simple_rel(
&FlowRelKind::FlowAppTemplate,
Self::get_app_id_by_ctx(&mock_ctx).unwrap_or_default().as_str(),
&template_id,
None,
None,
true,
true,
None,
funs,
ctx,
)
.await?;
}
}
rel_model_id.to_string()
}
}
Expand Down Expand Up @@ -1484,9 +1489,7 @@ impl FlowModelServ {
if let Some(rel_model) = Self::find_items(
&FlowModelFilterReq {
basic: RbumBasicFilterReq {
ids: Some(
FlowRelServ::find_to_simple_rels(&FlowRelKind::FlowModelPath, &ctx.own_paths, None, None, funs, ctx).await?.into_iter().map(|rel| rel.rel_id).collect_vec(),
),
ids: Some(FlowRelServ::find_model_ids_by_app_id(Self::get_app_id_by_ctx(ctx).unwrap_or_default().as_str(), funs, ctx).await.unwrap_or_default()),
ignore_scope: true,
own_paths: Some("".to_string()),
with_sub_own_paths: true,
Expand Down Expand Up @@ -1662,18 +1665,24 @@ impl FlowModelServ {
continue;
}
}
for rel in FlowRelServ::find_from_simple_rels(&FlowRelKind::FlowModelPath, &model.id, None, None, funs, &global_ctx).await? {
FlowRelServ::delete_simple_rel(&FlowRelKind::FlowModelPath, &model.id, &rel.rel_id, funs, &global_ctx).await?;
}
if rel_template_id.clone().is_some() {
for rel in FlowRelServ::find_from_simple_rels(&FlowRelKind::FlowModelTemplate, &model.id, None, None, funs, &global_ctx).await? {
FlowRelServ::delete_simple_rel(&FlowRelKind::FlowModelTemplate, &model.id, &rel.rel_id, funs, &global_ctx).await?;
}
} else {
// clean reference template rel
for rel in FlowRelServ::find_from_simple_rels(&FlowRelKind::FlowAppTemplate, Self::get_app_id_by_ctx(&ctx).unwrap_or_default().as_str(), None, None, funs, &global_ctx).await?.into_iter() {
FlowRelServ::delete_simple_rel(&FlowRelKind::FlowAppTemplate, Self::get_app_id_by_ctx(&ctx).unwrap_or_default().as_str(), &rel.rel_id, funs, &global_ctx).await?;
}
}
if ctx.own_paths == model.own_paths {
Self::delete_item(&model.id, funs, ctx).await?;
}
}
Ok(models)
}

fn get_app_id_by_ctx(ctx: &TardisContext) -> Option<String> {
rbum_scope_helper::get_max_level_id_by_context(ctx)
}
}
22 changes: 21 additions & 1 deletion backend/middlewares/flow/src/serv/flow_rel_serv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use bios_basic::rbum::{
rbum_enumeration::{RbumRelEnvKind, RbumRelFromKind},
serv::rbum_rel_serv::RbumRelServ,
};
use itertools::Itertools;
use serde::{Deserialize, Serialize};

use strum::Display;
Expand All @@ -27,6 +28,7 @@ pub enum FlowRelKind {
FlowModelState,
FlowModelTemplate,
FlowModelPath,
FlowAppTemplate,
}

impl FlowRelServ {
Expand Down Expand Up @@ -106,7 +108,7 @@ impl FlowRelServ {
Ok(())
}

async fn exist_rels(flow_rel_kind: &FlowRelKind, from_rbum_id: &str, to_rbum_item_id: &str, funs: &TardisFunsInst, ctx: &TardisContext) -> TardisResult<bool> {
pub async fn exist_rels(flow_rel_kind: &FlowRelKind, from_rbum_id: &str, to_rbum_item_id: &str, funs: &TardisFunsInst, ctx: &TardisContext) -> TardisResult<bool> {
// TODO In-depth inspection
RbumRelServ::check_simple_rel(
&RbumRelSimpleFindReq {
Expand Down Expand Up @@ -240,4 +242,22 @@ impl FlowRelServ {
None
}
}

pub async fn find_model_ids_by_app_id(app_id: &str, funs: &TardisFunsInst, ctx: &TardisContext) -> TardisResult<Vec<String>> {
let template_id = Self::find_from_simple_rels(&FlowRelKind::FlowAppTemplate, app_id, None, None, funs, ctx).await?.pop().map(|rel| rel.rel_id);
if let Some(template_id) = template_id {
Ok(Self::find_to_simple_rels(&FlowRelKind::FlowModelTemplate, &template_id, None, None, funs, ctx).await?.into_iter().map(|rel| rel.rel_id).collect_vec())
} else {
Err(funs.err().conflict(&FlowRelKind::FlowAppTemplate.to_string(), "find_models_by_app_id", "rel not found", "404-rel-not-found"))
}
}

pub async fn find_app_ids_by_model_id(model_id: &str, funs: &TardisFunsInst, ctx: &TardisContext) -> TardisResult<Vec<String>> {
let template_id = Self::find_from_simple_rels(&FlowRelKind::FlowModelTemplate, model_id, None, None, funs, ctx).await?.pop().map(|rel| rel.rel_id);
if let Some(template_id) = template_id {
Ok(Self::find_to_simple_rels(&FlowRelKind::FlowAppTemplate, &template_id, None, None, funs, ctx).await?.into_iter().map(|rel| rel.rel_id).collect_vec())
} else {
Err(funs.err().conflict(&FlowRelKind::FlowModelTemplate.to_string(), "find_models_by_app_id", "rel not found", "404-rel-not-found"))
}
}
}
5 changes: 3 additions & 2 deletions backend/middlewares/flow/src/serv/flow_state_serv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,9 @@ impl RbumItemCrudOperation<flow_state::ActiveModel, FlowStateAddReq, FlowStateMo
cond = cond.add(Expr::col(flow_state::Column::Tags).eq(""));
for tag in tag.split(',') {
cond = cond.add(Expr::col(flow_state::Column::Tags).eq(tag));
cond = cond.add(Expr::col(flow_state::Column::Tags).like(format!("%{},%", tag)));
cond = cond.add(Expr::col(flow_state::Column::Tags).like(format!("%,{}%", tag)));
cond = cond.add(Expr::col(flow_state::Column::Tags).like(format!("{},%", tag)));
cond = cond.add(Expr::col(flow_state::Column::Tags).like(format!("%,{}", tag)));
cond = cond.add(Expr::col(flow_state::Column::Tags).like(format!("%,{},%", tag)));
}
query.cond_where(cond);
}
Expand Down

0 comments on commit 6ca9383

Please sign in to comment.