backbone_accounting/application/service/
journal_line_service.rs1use backbone_core::GenericCrudService;
9
10use crate::presentation::dto::{CreateJournalLineDto, UpdateJournalLineDto};
11use crate::domain::entity::JournalLine;
12use crate::infrastructure::persistence::JournalLineRepository;
13
14pub type JournalLineService = GenericCrudService<
29 JournalLine,
30 CreateJournalLineDto,
31 UpdateJournalLineDto,
32 JournalLineRepository,
33>;
34
35pub struct JournalLineRowRules;
40
41#[async_trait::async_trait]
42impl backbone_core::write_guard::WriteGuard<JournalLine> for JournalLineRowRules {
43 async fn check(
44 &self,
45 ctx: &backbone_core::write_guard::WriteCtx<'_, JournalLine>,
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, &["journal_id"]))) && !r::holds(&r::and(r::is_not_null(r::field(row, "journal_id")), r::not(r::is_empty(r::trim(r::field(row, "journal_id")))))) {
63 refuse.push(Violation::new("journal_id", "required", "journal_id is required"));
64 }
65 if (create || (update && r::changed(old.as_ref(), row, &["line_number"]))) && !r::holds(&r::gt(r::field(row, "line_number"), r::Val::Num(0.0_f64))) {
67 refuse.push(Violation::new("line_number", "positive", "line_number must be positive"));
68 }
69 if (create || (update && r::changed(old.as_ref(), row, &["account_id"]))) && !r::holds(&r::and(r::is_not_null(r::field(row, "account_id")), r::not(r::is_empty(r::trim(r::field(row, "account_id")))))) {
71 refuse.push(Violation::new("account_id", "required", "account_id is required"));
72 }
73 if (create || (update && r::changed(old.as_ref(), row, &["account_number"]))) && !r::holds(&r::and(r::is_not_null(r::field(row, "account_number")), r::not(r::is_empty(r::trim(r::field(row, "account_number")))))) {
75 refuse.push(Violation::new("account_number", "required", "account_number is required"));
76 }
77 if (create || (update && r::changed(old.as_ref(), row, &["account_number"]))) && !r::holds(&r::le(r::length(r::field(row, "account_number")), r::Val::Num(20.0_f64))) {
79 refuse.push(Violation::new("account_number", "max_length", "account_number must be at most 20 characters").with_param("max", 20_i64));
80 }
81 if (create || (update && r::changed(old.as_ref(), row, &["account_name"]))) && !r::holds(&r::and(r::is_not_null(r::field(row, "account_name")), r::not(r::is_empty(r::trim(r::field(row, "account_name")))))) {
83 refuse.push(Violation::new("account_name", "required", "account_name is required"));
84 }
85 if (create || (update && r::changed(old.as_ref(), row, &["account_name"]))) && !r::holds(&r::le(r::length(r::field(row, "account_name")), r::Val::Num(100.0_f64))) {
87 refuse.push(Violation::new("account_name", "max_length", "account_name must be at most 100 characters").with_param("max", 100_i64));
88 }
89 if (create || (update && r::changed(old.as_ref(), row, &["debit_amount"]))) && !r::holds(&r::ge(r::field(row, "debit_amount"), r::Val::Num(0.0_f64))) {
91 refuse.push(Violation::new("debit_amount", "non_negative", "debit_amount must not be negative"));
92 }
93 if (create || (update && r::changed(old.as_ref(), row, &["credit_amount"]))) && !r::holds(&r::ge(r::field(row, "credit_amount"), r::Val::Num(0.0_f64))) {
95 refuse.push(Violation::new("credit_amount", "non_negative", "credit_amount must not be negative"));
96 }
97 if (create || (update && r::changed(old.as_ref(), row, &["currency"]))) && !r::holds(&r::eq(r::length(r::field(row, "currency")), r::Val::Num(3.0_f64))) {
99 refuse.push(Violation::new("currency", "length", "currency must be exactly 3 characters").with_param("length", 3_i64));
100 }
101 if (create || (update && r::changed(old.as_ref(), row, &["exchange_rate"]))) && !r::holds(&r::gt(r::field(row, "exchange_rate"), r::Val::Num(0.0_f64))) {
103 refuse.push(Violation::new("exchange_rate", "positive", "exchange_rate must be positive"));
104 }
105 if (create || (update && r::changed(old.as_ref(), row, &["base_debit_amount"]))) && !r::holds(&r::ge(r::field(row, "base_debit_amount"), r::Val::Num(0.0_f64))) {
107 refuse.push(Violation::new("base_debit_amount", "non_negative", "base_debit_amount must not be negative"));
108 }
109 if (create || (update && r::changed(old.as_ref(), row, &["base_credit_amount"]))) && !r::holds(&r::ge(r::field(row, "base_credit_amount"), r::Val::Num(0.0_f64))) {
111 refuse.push(Violation::new("base_credit_amount", "non_negative", "base_credit_amount must not be negative"));
112 }
113 if (create || (update && r::changed(old.as_ref(), row, &["description"]))) && !r::holds(&r::le(r::length(r::field(row, "description")), r::Val::Num(255.0_f64))) {
115 refuse.push(Violation::new("description", "max_length", "description must be at most 255 characters").with_param("max", 255_i64));
116 }
117 if (create || (update && r::changed(old.as_ref(), row, &["source_type"]))) && !r::holds(&r::le(r::length(r::field(row, "source_type")), r::Val::Num(50.0_f64))) {
119 refuse.push(Violation::new("source_type", "max_length", "source_type must be at most 50 characters").with_param("max", 50_i64));
120 }
121 if (create || (update && r::changed(old.as_ref(), row, &["source_reference"]))) && !r::holds(&r::le(r::length(r::field(row, "source_reference")), r::Val::Num(100.0_f64))) {
123 refuse.push(Violation::new("source_reference", "max_length", "source_reference must be at most 100 characters").with_param("max", 100_i64));
124 }
125 if (create || (update && r::changed(old.as_ref(), row, &["tax_rate"]))) && !r::holds(&r::and(r::ge(r::field(row, "tax_rate"), r::Val::Num(0.0_f64)), r::le(r::field(row, "tax_rate"), r::Val::Num(100.0_f64)))) {
127 refuse.push(Violation::new("tax_rate", "range", "tax_rate must be between 0 and 100").with_param("min", 0_i64).with_param("max", 100_i64));
128 }
129 if (create || (update && r::changed(old.as_ref(), row, &["tax_base_amount"]))) && !r::holds(&r::ge(r::field(row, "tax_base_amount"), r::Val::Num(0.0_f64))) {
131 refuse.push(Violation::new("tax_base_amount", "non_negative", "tax_base_amount must not be negative"));
132 }
133 if (create || (update && r::changed(old.as_ref(), row, &["unit"]))) && !r::holds(&r::le(r::length(r::field(row, "unit")), r::Val::Num(20.0_f64))) {
135 refuse.push(Violation::new("unit", "max_length", "unit must be at most 20 characters").with_param("max", 20_i64));
136 }
137 GuardOutcome { refuse, shadow }
138 }
139}
140