backbone_integrations/application/service/
integration_event_service.rs1use backbone_core::GenericCrudService;
9
10use crate::presentation::dto::{CreateIntegrationEventDto, UpdateIntegrationEventDto};
11use crate::domain::entity::IntegrationEvent;
12use crate::infrastructure::persistence::IntegrationEventRepository;
13
14pub type IntegrationEventService = GenericCrudService<
29 IntegrationEvent,
30 CreateIntegrationEventDto,
31 UpdateIntegrationEventDto,
32 IntegrationEventRepository,
33>;
34
35pub struct IntegrationEventRowRules;
40
41#[async_trait::async_trait]
42impl backbone_core::write_guard::WriteGuard<IntegrationEvent> for IntegrationEventRowRules {
43 async fn check(
44 &self,
45 ctx: &backbone_core::write_guard::WriteCtx<'_, IntegrationEvent>,
46 ) -> backbone_core::write_guard::GuardOutcome {
47 use backbone_core::row_rule as r;
48 use backbone_core::violation::Violation;
49 use backbone_core::write_guard::{GuardOutcome, WriteKind};
50 let (create, update) = (ctx.kind == WriteKind::Create, ctx.kind == WriteKind::Update);
51 let Some(after) = ctx.after.filter(|_| create || update) else {
52 return GuardOutcome::allow();
53 };
54 let Ok(row) = serde_json::to_value(after) else {
55 return GuardOutcome::allow();
56 };
57 let row = &row;
58 let old = ctx.before.and_then(|b| serde_json::to_value(b).ok());
59 let shadow = Vec::new();
60 let mut refuse = Vec::new();
61 if (create || (update && r::changed(old.as_ref(), row, &["connector_id"]))) && !r::holds(&r::and(r::is_not_null(r::field(row, "connector_id")), r::not(r::is_empty(r::trim(r::field(row, "connector_id")))))) {
63 refuse.push(Violation::new("connector_id", "required", "connector_id is required"));
64 }
65 if (create || (update && r::changed(old.as_ref(), row, &["event_type"]))) && !r::holds(&r::and(r::is_not_null(r::field(row, "event_type")), r::not(r::is_empty(r::trim(r::field(row, "event_type")))))) {
67 refuse.push(Violation::new("event_type", "required", "event_type is required"));
68 }
69 if (create || (update && r::changed(old.as_ref(), row, &["event_type"]))) && !r::holds(&r::le(r::length(r::field(row, "event_type")), r::Val::Num(80.0_f64))) {
71 refuse.push(Violation::new("event_type", "max_length", "event_type must be at most 80 characters").with_param("max", 80_i64));
72 }
73 if (create || (update && r::changed(old.as_ref(), row, &["external_id"]))) && !r::holds(&r::and(r::is_not_null(r::field(row, "external_id")), r::not(r::is_empty(r::trim(r::field(row, "external_id")))))) {
75 refuse.push(Violation::new("external_id", "required", "external_id is required"));
76 }
77 if (create || (update && r::changed(old.as_ref(), row, &["external_id"]))) && !r::holds(&r::le(r::length(r::field(row, "external_id")), r::Val::Num(200.0_f64))) {
79 refuse.push(Violation::new("external_id", "max_length", "external_id must be at most 200 characters").with_param("max", 200_i64));
80 }
81 if (create || (update && r::changed(old.as_ref(), row, &["business_key"]))) && !r::holds(&r::and(r::is_not_null(r::field(row, "business_key")), r::not(r::is_empty(r::trim(r::field(row, "business_key")))))) {
83 refuse.push(Violation::new("business_key", "required", "business_key is required"));
84 }
85 if (create || (update && r::changed(old.as_ref(), row, &["business_key"]))) && !r::holds(&r::le(r::length(r::field(row, "business_key")), r::Val::Num(200.0_f64))) {
87 refuse.push(Violation::new("business_key", "max_length", "business_key must be at most 200 characters").with_param("max", 200_i64));
88 }
89 if (create || (update && r::changed(old.as_ref(), row, &["payload"]))) && !r::holds(&r::and(r::is_not_null(r::field(row, "payload")), r::not(r::is_empty(r::trim(r::field(row, "payload")))))) {
91 refuse.push(Violation::new("payload", "required", "payload is required"));
92 }
93 if (create || (update && r::changed(old.as_ref(), row, &["payload"]))) && !r::holds(&r::le(r::length(r::field(row, "payload")), r::Val::Num(20000.0_f64))) {
95 refuse.push(Violation::new("payload", "max_length", "payload must be at most 20000 characters").with_param("max", 20000_i64));
96 }
97 if (create || (update && r::changed(old.as_ref(), row, &["mapped_ref_type"]))) && !r::holds(&r::le(r::length(r::field(row, "mapped_ref_type")), r::Val::Num(60.0_f64))) {
99 refuse.push(Violation::new("mapped_ref_type", "max_length", "mapped_ref_type must be at most 60 characters").with_param("max", 60_i64));
100 }
101 if (create || (update && r::changed(old.as_ref(), row, &["error_detail"]))) && !r::holds(&r::le(r::length(r::field(row, "error_detail")), r::Val::Num(1000.0_f64))) {
103 refuse.push(Violation::new("error_detail", "max_length", "error_detail must be at most 1000 characters").with_param("max", 1000_i64));
104 }
105 GuardOutcome { refuse, shadow }
106 }
107}
108