backbone_accounting/application/service/
journal_service.rs1use backbone_core::GenericCrudService;
9
10use crate::presentation::dto::{CreateJournalDto, UpdateJournalDto};
11use crate::domain::entity::Journal;
12use crate::infrastructure::persistence::JournalRepository;
13
14pub type JournalService = GenericCrudService<
29 Journal,
30 CreateJournalDto,
31 UpdateJournalDto,
32 JournalRepository,
33>;
34
35pub struct JournalRowRules;
40
41#[async_trait::async_trait]
42impl backbone_core::write_guard::WriteGuard<Journal> for JournalRowRules {
43 async fn check(
44 &self,
45 ctx: &backbone_core::write_guard::WriteCtx<'_, Journal>,
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 update && ctx.transition.is_none() && r::changed(old.as_ref(), row, &["status"]) {
63 refuse.push(Violation::new("status", "state_field_verbs_only", "the state field moves only through its verbs"));
64 }
65 if (create || (update && r::changed(old.as_ref(), row, &["journal_number"]))) && !r::holds(&r::and(r::is_not_null(r::field(row, "journal_number")), r::not(r::is_empty(r::trim(r::field(row, "journal_number")))))) {
67 refuse.push(Violation::new("journal_number", "required", "journal_number is required"));
68 }
69 if (create || (update && r::changed(old.as_ref(), row, &["journal_number"]))) && !r::holds(&r::le(r::length(r::field(row, "journal_number")), r::Val::Num(30.0_f64))) {
71 refuse.push(Violation::new("journal_number", "max_length", "journal_number must be at most 30 characters").with_param("max", 30_i64));
72 }
73 if (create || (update && r::changed(old.as_ref(), row, &["reference_number"]))) && !r::holds(&r::le(r::length(r::field(row, "reference_number")), r::Val::Num(50.0_f64))) {
75 refuse.push(Violation::new("reference_number", "max_length", "reference_number must be at most 50 characters").with_param("max", 50_i64));
76 }
77 if (create || (update && r::changed(old.as_ref(), row, &["fiscal_year"]))) && !r::holds(&r::ge(r::field(row, "fiscal_year"), r::Val::Num(2020.0_f64))) {
79 refuse.push(Violation::new("fiscal_year", "min", "fiscal_year must be at least 2020").with_param("min", 2020_i64));
80 }
81 if (create || (update && r::changed(old.as_ref(), row, &["fiscal_year"]))) && !r::holds(&r::le(r::field(row, "fiscal_year"), r::Val::Num(2100.0_f64))) {
83 refuse.push(Violation::new("fiscal_year", "max", "fiscal_year must be at most 2100").with_param("max", 2100_i64));
84 }
85 if (create || (update && r::changed(old.as_ref(), row, &["fiscal_month"]))) && !r::holds(&r::ge(r::field(row, "fiscal_month"), r::Val::Num(1.0_f64))) {
87 refuse.push(Violation::new("fiscal_month", "min", "fiscal_month must be at least 1").with_param("min", 1_i64));
88 }
89 if (create || (update && r::changed(old.as_ref(), row, &["fiscal_month"]))) && !r::holds(&r::le(r::field(row, "fiscal_month"), r::Val::Num(12.0_f64))) {
91 refuse.push(Violation::new("fiscal_month", "max", "fiscal_month must be at most 12").with_param("max", 12_i64));
92 }
93 if (create || (update && r::changed(old.as_ref(), row, &["description"]))) && !r::holds(&r::and(r::is_not_null(r::field(row, "description")), r::not(r::is_empty(r::trim(r::field(row, "description")))))) {
95 refuse.push(Violation::new("description", "required", "description is required"));
96 }
97 if (create || (update && r::changed(old.as_ref(), row, &["description"]))) && !r::holds(&r::le(r::length(r::field(row, "description")), r::Val::Num(500.0_f64))) {
99 refuse.push(Violation::new("description", "max_length", "description must be at most 500 characters").with_param("max", 500_i64));
100 }
101 if (create || (update && r::changed(old.as_ref(), row, &["notes"]))) && !r::holds(&r::le(r::length(r::field(row, "notes")), r::Val::Num(1000.0_f64))) {
103 refuse.push(Violation::new("notes", "max_length", "notes must be at most 1000 characters").with_param("max", 1000_i64));
104 }
105 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))) {
107 refuse.push(Violation::new("currency", "length", "currency must be exactly 3 characters").with_param("length", 3_i64));
108 }
109 if (create || (update && r::changed(old.as_ref(), row, &["total_debit"]))) && !r::holds(&r::ge(r::field(row, "total_debit"), r::Val::Num(0.0_f64))) {
111 refuse.push(Violation::new("total_debit", "non_negative", "total_debit must not be negative"));
112 }
113 if (create || (update && r::changed(old.as_ref(), row, &["total_credit"]))) && !r::holds(&r::ge(r::field(row, "total_credit"), r::Val::Num(0.0_f64))) {
115 refuse.push(Violation::new("total_credit", "non_negative", "total_credit must not be negative"));
116 }
117 if (create || (update && r::changed(old.as_ref(), row, &["line_count"]))) && !r::holds(&r::ge(r::field(row, "line_count"), r::Val::Num(0.0_f64))) {
119 refuse.push(Violation::new("line_count", "non_negative", "line_count must not be negative"));
120 }
121 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))) {
123 refuse.push(Violation::new("source_type", "max_length", "source_type must be at most 50 characters").with_param("max", 50_i64));
124 }
125 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(255.0_f64))) {
127 refuse.push(Violation::new("source_reference", "max_length", "source_reference must be at most 255 characters").with_param("max", 255_i64));
128 }
129 if (create || (update && r::changed(old.as_ref(), row, &["reversal_reason"]))) && !r::holds(&r::le(r::length(r::field(row, "reversal_reason")), r::Val::Num(255.0_f64))) {
131 refuse.push(Violation::new("reversal_reason", "max_length", "reversal_reason must be at most 255 characters").with_param("max", 255_i64));
132 }
133 if (create || (update && r::changed(old.as_ref(), row, &["rejection_reason"]))) && !r::holds(&r::le(r::length(r::field(row, "rejection_reason")), r::Val::Num(500.0_f64))) {
135 refuse.push(Violation::new("rejection_reason", "max_length", "rejection_reason must be at most 500 characters").with_param("max", 500_i64));
136 }
137 if (create || (update && r::changed(old.as_ref(), row, &["void_reason"]))) && !r::holds(&r::le(r::length(r::field(row, "void_reason")), r::Val::Num(500.0_f64))) {
139 refuse.push(Violation::new("void_reason", "max_length", "void_reason must be at most 500 characters").with_param("max", 500_i64));
140 }
141 GuardOutcome { refuse, shadow }
142 }
143}
144