backbone_bucket/application/service/
processing_job_service.rs1use backbone_core::GenericCrudService;
9
10use crate::presentation::dto::{CreateProcessingJobDto, UpdateProcessingJobDto};
11use crate::domain::entity::ProcessingJob;
12use crate::infrastructure::persistence::ProcessingJobRepository;
13
14pub type ProcessingJobService = GenericCrudService<
29 ProcessingJob,
30 CreateProcessingJobDto,
31 UpdateProcessingJobDto,
32 ProcessingJobRepository,
33>;
34
35pub struct ProcessingJobRowRules;
39
40#[async_trait::async_trait]
41impl backbone_core::write_guard::WriteGuard<ProcessingJob> for ProcessingJobRowRules {
42 async fn check(
43 &self,
44 ctx: &backbone_core::write_guard::WriteCtx<'_, ProcessingJob>,
45 ) -> backbone_core::write_guard::GuardOutcome {
46 use backbone_core::row_rule as r;
47 use backbone_core::violation::Violation;
48 use backbone_core::write_guard::{GuardOutcome, WriteKind};
49 let (create, update) = (ctx.kind == WriteKind::Create, ctx.kind == WriteKind::Update);
50 let Some(after) = ctx.after.filter(|_| create || update) else {
51 return GuardOutcome::allow();
52 };
53 let Ok(row) = serde_json::to_value(after) else {
54 return GuardOutcome::allow();
55 };
56 let row = &row;
57 let old = ctx.before.and_then(|b| serde_json::to_value(b).ok());
58 let mut shadow = Vec::new();
59 if update && ctx.transition.is_none() && r::changed(old.as_ref(), row, &["status"]) {
61 shadow.push(Violation::new("status", "state_field_verbs_only", "the state field moves only through its verbs"));
62 }
63 if (create || update) && !r::holds(&r::and(r::ge(r::field(row, "priority"), r::Val::Num(0.0_f64)), r::le(r::field(row, "priority"), r::Val::Num(10.0_f64)))) {
65 shadow.push(Violation::new("priority", "invalid_priority", "Priority must be 0-10"));
66 }
67 if (update) && !r::holds(&r::ne(r::field(row, "status"), r::text("completed"))) {
69 shadow.push(Violation::new("status", "job_completed", "Cannot modify completed jobs"));
70 }
71 GuardOutcome { refuse: Vec::new(), shadow }
72 }
73}
74