BREP_plm 0.1.1

The BREP PLM server: parts, revisions, lifecycle, checkout, BOMs and change orders over an embedded SQLite store, with a browser UI and the document API the BREP CAD app stores through.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
//! Change orders — ECOs (round 9 item 5).
//!
//! A change order names revisions and what to do with each: release a Draft or
//! In-review revision, or obsolete a Released or Superseded one. It is
//! reviewed as a whole with the same machinery as a revision
//! ([`crate::review`]), and then released as a whole: every item's gates are
//! checked, every release hook asked, and every item applied in ONE store
//! transaction — all of them, or none.
//!
//! # Lifecycle
//!
//! `Draft → InReview → Approved → Released`, and `Cancelled` from any open
//! state. Submitting opens a review round with [`Settings::eco_review_rule`]
//! (0 approvals by default, so a submitted change order is approved at once).
//! When the round's rule is met the change order becomes `Approved`; a
//! rejection sends it back to `Draft`, and so does withdrawing it. Its items
//! change only in `Draft`, so a round always judges the items it was opened
//! on.
//!
//! # What a release checks, per item
//!
//! The same gates as releasing the revision alone — the lifecycle move, the
//! lock, a waiting bake, complete catalog values, and (when the setting is
//! on) released children, where a child released by the SAME change order
//! counts as released — then `beforeRelease` for each release item, outside
//! the lock. Inside the locked write every gate runs again, each document must
//! be the one its hook saw, and then everything is applied. The revisions'
//! own review rounds are not a gate here: the change order's approval covers
//! its items.
//!
//! A revision is in at most one open change order. Releasing or obsoleting
//! it on its own is allowed with a warning, unless
//! [`Settings::eco_holds_revisions`] is on.

use std::collections::BTreeSet;

use serde::{Deserialize, Serialize};
use serde_json::{json, Value};

use crate::bom;
use crate::db::{self, now, Db, State};
use crate::lifecycle;
use crate::model::{
    ChangeOrder, EcoAction, EcoItem, EcoPriority, EcoState, Lifecycle, NumberMode, Part, PartType, Revision,
    ReviewStatus, User, Verdict,
};
use crate::review::{self, CommentView, ReviewView, RoundChange};
use crate::scripting::{self, HookResult};
use crate::Error;

/// The longest title and text fields a change order keeps.
pub const MAX_TITLE: usize = 200;
pub const MAX_TEXT: usize = 20_000;

// ===========================================================================
// Rules on the state
// ===========================================================================

/// The open change order (other than `except`) that names `revision_id`.
pub fn holder<'a>(state: &'a State, revision_id: &str, except: &str) -> Option<&'a ChangeOrder> {
    state
        .change_orders
        .iter()
        .find(|e| e.id != except && e.state.is_open() && e.item(revision_id).is_some())
}

/// The change order with this id, or else this number (ignoring case).
pub fn find<'a>(state: &'a State, key: &str) -> Option<&'a ChangeOrder> {
    let key = key.trim();
    state
        .change_orders
        .iter()
        .find(|e| e.id == key)
        .or_else(|| state.change_orders.iter().find(|e| e.number.eq_ignore_ascii_case(key)))
}

fn find_mut<'a>(state: &'a mut State, key: &str) -> Result<&'a mut ChangeOrder, Error> {
    let id = find(state, key).map(|e| e.id.clone()).ok_or_else(|| Error::not_found("change order"))?;
    state.change_orders.iter_mut().find(|e| e.id == id).ok_or_else(|| Error::not_found("change order"))
}

/// What stops `item` of `eco` being applied now, if anything. `releasing` is
/// every revision the change order releases — a child among them counts as
/// released.
pub fn item_problem(state: &State, eco: &ChangeOrder, item: &EcoItem, releasing: &BTreeSet<String>) -> Option<String> {
    let Some(part) = state.part(&item.part_id) else {
        return Some("its part no longer exists".into());
    };
    let Some(revision) = part.revision(&item.revision_id) else {
        return Some(format!("{}: its revision no longer exists", part.number));
    };
    let name = format!("{} rev {}", part.number, revision.label);
    let problem = match item.action {
        EcoAction::Release => release_problem(state, part, revision, releasing),
        EcoAction::Obsolete => obsolete_problem(revision, releasing, part),
    };
    let problem = problem.or_else(|| {
        holder(state, &revision.id, &eco.id).map(|other| format!("it is also in {}", other.number))
    });
    problem.map(|p| format!("{name}: {p}"))
}

fn release_problem(state: &State, part: &Part, revision: &Revision, releasing: &BTreeSet<String>) -> Option<String> {
    if let Err(refusal) = lifecycle::check_transition(revision.lifecycle, Lifecycle::Released) {
        return Some(refusal);
    }
    if let Some(lock) = &revision.lock {
        let who = state.user(&lock.user_id).map(|u| u.username.clone()).unwrap_or_else(|| lock.user_id.clone());
        return Some(format!("it is checked out by {who} — check it in first"));
    }
    if revision.needs_bake() {
        return Some("it is waiting for a bake".into());
    }
    let missing = crate::catalog::release_problems(&state.categories, part);
    if !missing.is_empty() {
        return Some(format!("its catalog values are incomplete: {}", missing.join("; ")));
    }
    if state.settings.require_released_children {
        let unreleased = unreleased_children_after(state, revision, releasing);
        if !unreleased.is_empty() {
            return Some(format!("it uses unreleased parts: {}", unreleased.join("; ")));
        }
    }
    None
}

fn obsolete_problem(revision: &Revision, releasing: &BTreeSet<String>, part: &Part) -> Option<String> {
    // A revision this order also releases is not obsoleted by it; one that
    // the order's release will SUPERSEDE is still obsoleted afterwards.
    if releasing.contains(&revision.id) {
        return Some("the same change order releases it".into());
    }
    let _ = part;
    lifecycle::check_transition(revision.lifecycle, Lifecycle::Obsolete).err()
}

/// [`bom::unreleased_children`], counting as released every child that
/// `releasing` will release: a pinned child among them, or — for a floating
/// line — any revision of that child part among them.
pub fn unreleased_children_after(state: &State, revision: &Revision, releasing: &BTreeSet<String>) -> Vec<String> {
    let mut out = Vec::new();
    for line in &revision.uses {
        let Some(child) = state.part(&line.part) else {
            out.push(format!("a used part ({}) no longer exists", line.part));
            continue;
        };
        let released_here = if line.revision.is_empty() {
            child.revisions.iter().any(|r| releasing.contains(&r.id))
        } else {
            releasing.contains(&line.revision)
        };
        if released_here {
            continue;
        }
        match bom::resolve(child, line) {
            None => out.push(format!("{}: the revision it names no longer exists", child.number)),
            Some(_) if line.revision.is_empty() && child.current_release().is_none() => {
                out.push(format!("{} has no released revision", child.number))
            }
            Some(rev) if rev.lifecycle != Lifecycle::Released => {
                out.push(format!("{} rev {} is {}", child.number, rev.label, rev.lifecycle.as_str()))
            }
            Some(_) => {}
        }
    }
    out
}

/// Every revision the change order releases.
pub fn releasing(eco: &ChangeOrder) -> BTreeSet<String> {
    eco.items
        .iter()
        .filter(|i| i.action == EcoAction::Release)
        .map(|i| i.revision_id.clone())
        .collect()
}

/// Every problem that would stop `eco` releasing now.
pub fn problems(state: &State, eco: &ChangeOrder) -> Vec<String> {
    let releasing = releasing(eco);
    eco.items.iter().filter_map(|item| item_problem(state, eco, item, &releasing)).collect()
}

/// The revision a change order's release or obsolete of it would conflict
/// with, for `transition` of that revision alone: the open change order
/// naming it, if any.
pub fn standalone_holder<'a>(state: &'a State, revision_id: &str) -> Option<&'a ChangeOrder> {
    holder(state, revision_id, "")
}

fn check_text(what: &str, text: &str, max: usize) -> Result<String, Error> {
    let text = text.trim();
    if text.chars().count() > max {
        return Err(Error::bad_request(format!("the {what} is at most {max} characters")));
    }
    Ok(text.to_string())
}

pub fn priority(text: &str) -> Result<EcoPriority, Error> {
    match text.trim().to_ascii_lowercase().as_str() {
        "" | "normal" => Ok(EcoPriority::Normal),
        "low" => Ok(EcoPriority::Low),
        "high" => Ok(EcoPriority::High),
        "critical" => Ok(EcoPriority::Critical),
        other => Err(Error::bad_request(format!("'{other}' is not a priority — low, normal, high or critical"))),
    }
}

pub fn action(text: &str) -> Result<EcoAction, Error> {
    match text.trim().to_ascii_lowercase().as_str() {
        "" | "release" => Ok(EcoAction::Release),
        "obsolete" => Ok(EcoAction::Obsolete),
        other => Err(Error::bad_request(format!("'{other}' is not an action — release or obsolete"))),
    }
}

// ===========================================================================
// What the page shows
// ===========================================================================

/// A change order in a list.
#[derive(Debug, Serialize)]
pub struct EcoRow {
    pub id: String,
    pub number: String,
    pub title: String,
    pub state: &'static str,
    pub priority: &'static str,
    /// How many revisions it names. Not `items`: the detail view flattens
    /// this row beside its own `items` list, and a key twice in one object
    /// is refused by any strict reader (serde) and silently resolved by a
    /// lenient one.
    pub item_count: usize,
    pub created_by: String,
    pub created_at: u64,
    pub released_at: Option<u64>,
    /// The current round's status, if it has one.
    pub review: Option<&'static str>,
}

/// One item as the page shows it: what it is, what it changes, and what
/// would stop it.
#[derive(Debug, Serialize)]
pub struct ItemView {
    pub part_id: String,
    pub number: String,
    pub name: String,
    pub revision_id: String,
    pub label: String,
    pub lifecycle: &'static str,
    pub action: EcoAction,
    pub note: String,
    /// For a release: the revision it replaces as current, if there is one.
    pub replaces: Option<String>,
    pub replaces_id: Option<String>,
    /// For a release: whether the document differs from what it replaces.
    pub document_changed: Option<bool>,
    pub content_hash: String,
    pub previous_hash: String,
    /// For a release of an assembly: its uses list against the one it replaces.
    pub bom_diff: Option<bom::BomDiff>,
    /// For a release: catalog changes to the part since it last released.
    pub attribute_changes: Vec<AttributeChange>,
    /// Why it could not be applied now; empty when nothing stops it.
    pub problem: String,
}

/// One change to a part's catalog values, from the audit log.
#[derive(Debug, Serialize)]
pub struct AttributeChange {
    pub at: u64,
    pub by: String,
    pub field: String,
    pub before: Value,
    pub after: Value,
}

/// A change order as its page shows it.
#[derive(Debug, Serialize)]
pub struct EcoView {
    #[serde(flatten)]
    pub eco: EcoRow,
    pub description: String,
    pub reason: String,
    pub released_by: Option<String>,
    pub cancelled_by: Option<String>,
    pub cancelled_at: Option<u64>,
    pub items: Vec<ItemView>,
    pub current: Option<ReviewView>,
    pub history: Vec<ReviewView>,
    pub comments: Vec<CommentView>,
    pub rule: crate::model::ReviewRule,
    /// Every problem that would stop the release now.
    pub problems: Vec<String>,
    pub can_edit: bool,
    pub can_release: bool,
    pub can_comment: bool,
}

fn name_of(state: &State, id: &str) -> String {
    state.user(id).map(|u| u.username.clone()).unwrap_or_else(|| id.to_string())
}

pub fn row(state: &State, eco: &ChangeOrder) -> EcoRow {
    EcoRow {
        id: eco.id.clone(),
        number: eco.number.clone(),
        title: eco.title.clone(),
        state: eco.state.as_str(),
        priority: eco.priority.as_str(),
        item_count: eco.items.len(),
        created_by: name_of(state, &eco.created_by),
        created_at: eco.created_at,
        released_at: eco.released_at,
        review: eco.reviews.last().map(|r| r.status.as_str()),
    }
}

// ===========================================================================
// The store's operations
// ===========================================================================

/// What a new change order is made from.
#[derive(Debug, Clone, Default, Deserialize)]
pub struct NewEco {
    #[serde(default)]
    pub number: String,
    pub title: String,
    #[serde(default)]
    pub description: String,
    #[serde(default)]
    pub reason: String,
    #[serde(default)]
    pub priority: String,
}

/// A change to a change order's words. `None` leaves a field as it is.
#[derive(Debug, Clone, Default, Deserialize)]
pub struct EcoChange {
    #[serde(default)]
    pub title: Option<String>,
    #[serde(default)]
    pub description: Option<String>,
    #[serde(default)]
    pub reason: Option<String>,
    #[serde(default)]
    pub priority: Option<String>,
}

/// An item to add: a part by id or number, a revision by id or label.
#[derive(Debug, Clone, Default, Deserialize)]
pub struct NewItem {
    pub part: String,
    pub revision: String,
    #[serde(default)]
    pub action: String,
    #[serde(default)]
    pub note: String,
}

/// A change to the ECO numbering. `None` leaves a field as it is.
#[derive(Debug, Clone, Default, Deserialize)]
pub struct NumberingChange {
    #[serde(default)]
    pub prefix: Option<String>,
    #[serde(default)]
    pub digits: Option<u32>,
    #[serde(default)]
    pub next: Option<u64>,
    #[serde(default)]
    pub mode: Option<NumberMode>,
}

impl Db {
    // -- numbering ---------------------------------------------------------

    /// The number a new change order gets, decided outside the lock: a typed
    /// one checked against the mode, or the script's answer. `None` means
    /// "the counter decides, inside the lock".
    fn eco_number(&self, user: &User, numbering: &PartType, typed: &str, input: &NewEco) -> Result<Option<String>, Error> {
        let typed = typed.trim().to_string();
        let given = match &numbering.mode {
            NumberMode::Counter => {
                if !typed.is_empty() {
                    return Err(Error::bad_request("change orders number by a counter — leave the number empty"));
                }
                None
            }
            NumberMode::Free => {
                if typed.is_empty() {
                    return Err(Error::bad_request("change orders take the number you type — enter one"));
                }
                Some(typed)
            }
            NumberMode::Pattern { regex } => {
                if !db::full_match(regex)?.is_match(&typed) {
                    return Err(Error::bad_request(format!(
                        "'{typed}' does not match the change-order number pattern ({regex})"
                    )));
                }
                Some(typed)
            }
            NumberMode::Script { script } => {
                let request = json!({
                    "requested": typed,
                    "numbering": numbering,
                    "eco": { "title": input.title, "reason": input.reason, "priority": input.priority },
                    "user": scripting::user_json(user),
                });
                let answer = match scripting::run(self, user, script, "ecoNumber", &request)? {
                    HookResult::Absent => {
                        return Err(Error::conflict(format!(
                            "change orders number by the script '{script}', which is not in the scripts directory"
                        )))
                    }
                    HookResult::Refused(failure) => return Err(Error::conflict(format!("{script}: {}", failure.message))),
                    HookResult::Returned(success) => success.value,
                };
                Some(scripting::string_answer(&answer, "number").ok_or_else(|| {
                    Error::conflict(format!("{script}: ecoNumber() must return the number, or {{ number }} — it returned {answer}"))
                })?)
            }
        };
        if let Some(number) = &given {
            db::check_number(number)?;
        }
        Ok(given)
    }

    /// Change how change orders are numbered. Administrators only (the API
    /// checks).
    pub fn update_eco_numbering(&self, change: &NumberingChange) -> Result<PartType, Error> {
        if let Some(digits) = change.digits {
            if !(1..=18).contains(&digits) {
                return Err(Error::bad_request("digits must be between 1 and 18"));
            }
        }
        if let Some(mode) = &change.mode {
            match mode {
                NumberMode::Pattern { regex } => {
                    if regex.trim().is_empty() {
                        return Err(Error::bad_request("a pattern needs a pattern"));
                    }
                    db::full_match(regex)?;
                }
                NumberMode::Script { script } => {
                    self.scripts().resolve(script)?;
                }
                _ => {}
            }
        }
        let change = change.clone();
        self.mutate(move |state| {
            let mut numbering = state.eco_numbering.0.clone();
            if let Some(prefix) = change.prefix {
                numbering.prefix = prefix.trim().to_string();
            }
            if let Some(digits) = change.digits {
                numbering.digits = digits;
            }
            if let Some(next) = change.next {
                numbering.next = next.max(1);
            }
            if let Some(mode) = change.mode {
                numbering.mode = mode;
            }
            if numbering.mode == NumberMode::Counter && numbering.next > numbering.capacity() {
                return Err(Error::bad_request(format!(
                    "the next number {} does not fit in {} digits",
                    numbering.next, numbering.digits
                )));
            }
            state.eco_numbering.0 = numbering.clone();
            Ok(numbering)
        })
    }

    // -- creating and editing ----------------------------------------------

    /// Open a new change order in Draft.
    pub fn create_eco(&self, user: &User, spec: &NewEco) -> Result<ChangeOrder, Error> {
        if !user.can_author() {
            return Err(Error::forbidden("a change order is made by the author group"));
        }
        let title = check_text("title", &spec.title, MAX_TITLE)?;
        if title.is_empty() {
            return Err(Error::bad_request("a change order needs a title"));
        }
        let description = check_text("description", &spec.description, MAX_TEXT)?;
        let reason = check_text("reason", &spec.reason, MAX_TEXT)?;
        let priority = priority(&spec.priority)?;
        let numbering = self.read(|state| state.eco_numbering.0.clone());
        let given = self.eco_number(user, &numbering, &spec.number, spec)?;
        let author = user.id.clone();
        self.mutate(move |state| {
            if state.eco_numbering.mode != numbering.mode {
                return Err(Error::conflict("change-order numbering changed meanwhile — try again"));
            }
            let taken = |state: &State, number: &str| state.change_orders.iter().any(|e| e.number.eq_ignore_ascii_case(number));
            let number = match given {
                Some(number) => number,
                None => {
                    let current = state.eco_numbering.0.clone();
                    let mut sequence = current.next;
                    let mut number = current.format_number(sequence);
                    while sequence <= current.capacity() && taken(state, &number) {
                        sequence += 1;
                        number = current.format_number(sequence);
                    }
                    if sequence > current.capacity() {
                        return Err(Error::conflict(format!(
                            "change-order numbering is exhausted: {} digits cannot spell {sequence}",
                            current.digits
                        )));
                    }
                    state.eco_numbering.0.next = sequence + 1;
                    number
                }
            };
            if taken(state, &number) {
                return Err(Error::conflict(format!("change order {number} already exists")));
            }
            let stamp = now();
            let eco = ChangeOrder {
                id: crate::auth::new_id(),
                number,
                title,
                description,
                reason,
                priority,
                state: EcoState::Draft,
                items: Vec::new(),
                created_by: author,
                created_at: stamp,
                modified_at: stamp,
                released_by: None,
                released_at: None,
                cancelled_by: None,
                cancelled_at: None,
                reviews: Vec::new(),
                comments: Vec::new(),
            };
            state.change_orders.push(eco.clone());
            Ok(eco)
        })
    }

    /// Change a change order's title, description, reason or priority —
    /// while it is still open.
    pub fn update_eco(&self, user: &User, key: &str, change: &EcoChange) -> Result<ChangeOrder, Error> {
        if !user.can_author() {
            return Err(Error::forbidden("a change order is edited by the author group"));
        }
        let title = change.title.as_deref().map(|t| check_text("title", t, MAX_TITLE)).transpose()?;
        if title.as_deref() == Some("") {
            return Err(Error::bad_request("a change order needs a title"));
        }
        let description = change.description.as_deref().map(|t| check_text("description", t, MAX_TEXT)).transpose()?;
        let reason = change.reason.as_deref().map(|t| check_text("reason", t, MAX_TEXT)).transpose()?;
        let priority = change.priority.as_deref().map(priority).transpose()?;
        self.mutate(move |state| {
            let eco = find_mut(state, key)?;
            if !eco.state.is_open() {
                return Err(Error::conflict(format!("{} is {} and cannot change", eco.number, eco.state.as_str())));
            }
            if let Some(t) = title {
                eco.title = t;
            }
            if let Some(d) = description {
                eco.description = d;
            }
            if let Some(r) = reason {
                eco.reason = r;
            }
            if let Some(p) = priority {
                eco.priority = p;
            }
            eco.modified_at = now();
            Ok(eco.clone())
        })
    }

    /// Add an item to a Draft change order.
    pub fn add_eco_item(&self, user: &User, key: &str, item: &NewItem) -> Result<ChangeOrder, Error> {
        if !user.can_author() {
            return Err(Error::forbidden("a change order is edited by the author group"));
        }
        let action = action(&item.action)?;
        let note = check_text("note", &item.note, MAX_TITLE)?;
        let item = item.clone();
        self.mutate(move |state| {
            let part = state.part_by_id_or_number(&item.part).ok_or_else(|| Error::not_found(format!("part '{}'", item.part)))?;
            let revision = bom::find_revision(part, &item.revision)
                .ok_or_else(|| Error::not_found(format!("revision '{}' of {}", item.revision, part.number)))?;
            let name = format!("{} rev {}", part.number, revision.label);
            match action {
                EcoAction::Release if !revision.lifecycle.is_editable() => {
                    return Err(Error::conflict(format!(
                        "{name} is {} — only a revision in work can be released",
                        revision.lifecycle.as_str()
                    )))
                }
                EcoAction::Obsolete if !matches!(revision.lifecycle, Lifecycle::Released | Lifecycle::Superseded) => {
                    return Err(Error::conflict(format!(
                        "{name} is {} — only a released or superseded revision can be obsoleted",
                        revision.lifecycle.as_str()
                    )))
                }
                _ => {}
            }
            let (part_id, revision_id) = (part.id.clone(), revision.id.clone());
            let this = find(state, key).ok_or_else(|| Error::not_found("change order"))?;
            let this_id = this.id.clone();
            if let Some(other) = holder(state, &revision_id, &this_id) {
                return Err(Error::conflict(format!("{name} is already in {}", other.number)));
            }
            let eco = find_mut(state, &this_id)?;
            if eco.state != EcoState::Draft {
                return Err(Error::conflict(format!(
                    "{} is {} — items change only in a draft; withdraw it first",
                    eco.number,
                    eco.state.as_str()
                )));
            }
            if eco.item(&revision_id).is_some() {
                return Err(Error::conflict(format!("{name} is already in this change order")));
            }
            if action == EcoAction::Release
                && eco.items.iter().any(|i| i.part_id == part_id && i.action == EcoAction::Release)
            {
                return Err(Error::conflict(format!(
                    "this change order already releases a revision of {} — one release per part",
                    name.split(" rev ").next().unwrap_or("")
                )));
            }
            eco.items.push(EcoItem { part_id, revision_id, action, note, replaced: None });
            eco.modified_at = now();
            Ok(eco.clone())
        })
    }

    /// Take an item out of a Draft change order.
    pub fn remove_eco_item(&self, user: &User, key: &str, revision_id: &str) -> Result<ChangeOrder, Error> {
        if !user.can_author() {
            return Err(Error::forbidden("a change order is edited by the author group"));
        }
        self.mutate(move |state| {
            let eco = find_mut(state, key)?;
            if eco.state != EcoState::Draft {
                return Err(Error::conflict(format!(
                    "{} is {} — items change only in a draft",
                    eco.number,
                    eco.state.as_str()
                )));
            }
            let before = eco.items.len();
            eco.items.retain(|i| i.revision_id != revision_id);
            if eco.items.len() == before {
                return Err(Error::not_found("that item"));
            }
            eco.modified_at = now();
            Ok(eco.clone())
        })
    }

    // -- review --------------------------------------------------------------

    /// Submit a Draft change order: open a review round with the change-order
    /// rule. A round approved on opening (0 approvals) approves the order.
    pub fn submit_eco(&self, user: &User, key: &str, extra_reviewers: Option<&Value>, note: &str) -> Result<Vec<String>, Error> {
        if !user.can_author() {
            return Err(Error::forbidden("submitting a change order needs the author group"));
        }
        let eco = self.read(|state| find(state, key).cloned()).ok_or_else(|| Error::not_found("change order"))?;
        if eco.state != EcoState::Draft {
            return Err(Error::conflict(format!("{} is {}, not a draft", eco.number, eco.state.as_str())));
        }
        let mut rule = self.read(|state| state.settings.eco_review_rule.clone());
        if let Some(extra) = extra_reviewers {
            for reviewer in self.read(|state| review::parse_reviewers(state, extra))? {
                if !rule.reviewers.contains(&reviewer) {
                    rule.reviewers.push(reviewer);
                }
            }
        }
        let mut round = review::open_round(&rule, "settings:eco", &user.id, now());
        let input = json!({
            "kind": "eco",
            "eco": eco,
            "rule": {
                "required_approvals": round.required_approvals,
                "reviewers": round.reviewers,
                "due": round.due,
                "allow_self_approval": round.allow_self_approval,
                "source": "settings:eco",
            },
            "user": scripting::user_json(user),
        });
        self.shape_round(user, &mut round, &input)?;
        let (id, author, note) = (eco.id.clone(), user.clone(), note.to_string());
        self.mutate(move |state| {
            let eco = find_mut(state, &id)?;
            if eco.state != EcoState::Draft {
                return Err(Error::conflict(format!("{} is {}, not a draft", eco.number, eco.state.as_str())));
            }
            if eco.items.is_empty() {
                return Err(Error::conflict(format!("{} names no revisions yet — add what it changes first", eco.number)));
            }
            eco.state = if round.status == ReviewStatus::Approved { EcoState::Approved } else { EcoState::InReview };
            eco.reviews.push(round);
            if !note.trim().is_empty() {
                review::push_comment(&mut eco.comments, &author, &note, "", now())?;
            }
            eco.modified_at = now();
            Ok(())
        })?;
        Ok(self.eco_event(user, "opened", &eco.id, None))
    }

    /// Ask `reviewers.js` about a round about to open, and apply its answer.
    pub(crate) fn shape_round(&self, user: &User, round: &mut crate::model::Review, input: &Value) -> Result<(), Error> {
        match scripting::run(self, user, scripting::REVIEWERS, "reviewers", input)? {
            HookResult::Absent => Ok(()),
            HookResult::Refused(failure) => Err(Error::conflict(format!("{}: {}", scripting::REVIEWERS, failure.message))),
            HookResult::Returned(success) => match &success.value {
                Value::Null => Ok(()),
                Value::Object(answer) => {
                    if let Some(reviewers) = answer.get("reviewers") {
                        round.reviewers = self
                            .read(|state| review::parse_reviewers(state, reviewers))
                            .map_err(|e| Error::conflict(format!("{}: {}", scripting::REVIEWERS, e.message)))?;
                    }
                    if let Some(required) = answer.get("required_approvals") {
                        round.required_approvals = required
                            .as_u64()
                            .filter(|n| *n <= u64::from(review::MAX_APPROVALS))
                            .ok_or_else(|| {
                                Error::conflict(format!(
                                    "{}: required_approvals must be a whole number up to {}",
                                    scripting::REVIEWERS,
                                    review::MAX_APPROVALS
                                ))
                            })? as u32;
                    }
                    if let Some(due) = answer.get("due") {
                        round.due = due.as_u64().filter(|d| *d > 0);
                    }
                    if let Some(own) = answer.get("allow_self_approval").and_then(Value::as_bool) {
                        round.allow_self_approval = own;
                    }
                    round.rule_source.push_str("+script");
                    review::refresh(round, "");
                    Ok(())
                }
                other => Err(Error::conflict(format!(
                    "{}: reviewers() must return {{ reviewers, required_approvals, due }} or null — it returned {other}",
                    scripting::REVIEWERS
                ))),
            },
        }
    }

    /// Approve or reject a change order under review. A rejection ends the
    /// round and returns it to Draft; the approval that meets the rule
    /// approves it.
    pub fn decide_eco(&self, user: &User, key: &str, verdict: Verdict, comment: &str) -> Result<Vec<String>, Error> {
        let (decider, comment) = (user.clone(), comment.to_string());
        let id = self.mutate(move |state| {
            let eco = find_mut(state, key)?;
            if !matches!(eco.state, EcoState::InReview | EcoState::Approved) {
                return Err(Error::conflict(format!("{} is {}, not in review", eco.number, eco.state.as_str())));
            }
            let number = eco.number.clone();
            let snapshot = eco.clone();
            let current = review::eco_subject_hash(state, &snapshot);
            let eco = find_mut(state, key)?;
            let Some(round) = eco.reviews.last_mut().filter(|r| r.is_live()) else {
                return Err(Error::conflict(format!("{number} has no review open")));
            };
            review::record_decision(round, &decider, verdict, &comment, now(), &current)?;
            eco.state = match round.status {
                ReviewStatus::Rejected => EcoState::Draft,
                ReviewStatus::Approved => EcoState::Approved,
                _ => EcoState::InReview,
            };
            eco.modified_at = now();
            Ok(eco.id.clone())
        })?;
        Ok(self.eco_event(user, verdict.as_str(), &id, None))
    }

    /// Change a change order's live round.
    pub fn change_eco_review(&self, user: &User, key: &str, change: &RoundChange) -> Result<Vec<String>, Error> {
        let (editor, change) = (user.clone(), change.clone());
        let id = self.mutate(move |state| {
            let eco = find(state, key).ok_or_else(|| Error::not_found("change order"))?;
            let Some(mut round) = eco.reviews.last().cloned() else {
                return Err(Error::conflict("this change order has no review"));
            };
            let current = review::eco_subject_hash(state, eco);
            review::change_round(state, &mut round, &editor, &change, &current)?;
            let eco = find_mut(state, key)?;
            if eco.state.is_open() && eco.state != EcoState::Draft {
                eco.state = if round.status == ReviewStatus::Approved { EcoState::Approved } else { EcoState::InReview };
            }
            if let Some(last) = eco.reviews.last_mut() {
                *last = round;
            }
            eco.modified_at = now();
            Ok(eco.id.clone())
        })?;
        Ok(self.eco_event(user, "updated", &id, None))
    }

    /// Pull a change order under review back to Draft, ending its round.
    pub fn withdraw_eco(&self, user: &User, key: &str) -> Result<Vec<String>, Error> {
        if !user.can_author() {
            return Err(Error::forbidden("withdrawing a change order needs the author group"));
        }
        let by = user.id.clone();
        let id = self.mutate(move |state| {
            let eco = find_mut(state, key)?;
            if !matches!(eco.state, EcoState::InReview | EcoState::Approved) {
                return Err(Error::conflict(format!("{} is {}, not in review", eco.number, eco.state.as_str())));
            }
            if let Some(round) = eco.reviews.last_mut() {
                review::withdraw(round, &by, now());
            }
            eco.state = EcoState::Draft;
            eco.modified_at = now();
            Ok(eco.id.clone())
        })?;
        Ok(self.eco_event(user, "withdrawn", &id, None))
    }

    /// Cancel an open change order. Its revisions are left as they are.
    pub fn cancel_eco(&self, user: &User, key: &str) -> Result<Vec<String>, Error> {
        if !user.can_author() {
            return Err(Error::forbidden("cancelling a change order needs the author group"));
        }
        let by = user.id.clone();
        let id = self.mutate(move |state| {
            let eco = find_mut(state, key)?;
            if !eco.state.is_open() {
                return Err(Error::conflict(format!("{} is already {}", eco.number, eco.state.as_str())));
            }
            let stamp = now();
            if let Some(round) = eco.reviews.last_mut() {
                review::withdraw(round, &by, stamp);
            }
            eco.state = EcoState::Cancelled;
            eco.cancelled_by = Some(by);
            eco.cancelled_at = Some(stamp);
            eco.modified_at = stamp;
            Ok(eco.id.clone())
        })?;
        Ok(self.eco_event(user, "cancelled", &id, None))
    }

    /// Add a comment to a change order's discussion.
    pub fn comment_on_eco(&self, user: &User, key: &str, body: &str, parent: &str) -> Result<(crate::model::Comment, Vec<String>), Error> {
        let (author, body, parent) = (user.clone(), body.to_string(), parent.to_string());
        let (comment, id) = self.mutate(move |state| {
            let eco = find_mut(state, key)?;
            let comment = review::push_comment(&mut eco.comments, &author, &body, &parent, now())?;
            Ok((comment, eco.id.clone()))
        })?;
        let warnings = self.eco_event(user, "comment", &id, Some(&comment));
        Ok((comment, warnings))
    }

    // -- release -------------------------------------------------------------

    /// Release an approved change order: every item applied in ONE store
    /// transaction, or none of them.
    pub fn release_eco(&self, user: &User, key: &str) -> Result<Vec<String>, Error> {
        if !user.can_checkin() {
            return Err(Error::forbidden("releasing a change order needs the check-in group"));
        }
        // On a snapshot first, so a hook is never asked about a release that
        // cannot happen.
        let eco = self.read(|state| find(state, key).cloned()).ok_or_else(|| Error::not_found("change order"))?;
        self.read(|state| check_releasable(state, &eco))?;
        let found = self.read(|state| problems(state, &eco));
        if !found.is_empty() {
            return Err(Error::conflict(format!("{} cannot release: {}", eco.number, found.join("; "))));
        }
        let eco_summary = json!({ "id": eco.id, "number": eco.number, "title": eco.title, "reason": eco.reason });
        let mut seen: Vec<(String, String, String)> = Vec::new();
        for item in eco.items.iter().filter(|i| i.action == EcoAction::Release) {
            let (part, revision) = self.snapshot(&item.part_id, &item.revision_id)?;
            let mut input = self.read(|state| db::release_input(state, &part, &revision, user));
            input["eco"] = eco_summary.clone();
            match scripting::run(self, user, scripting::BEFORE_RELEASE, "beforeRelease", &input)? {
                HookResult::Absent | HookResult::Returned(_) => {}
                HookResult::Refused(failure) => {
                    return Err(Error::conflict(format!(
                        "{} cannot release: {} refused {} rev {}: {}",
                        eco.number,
                        scripting::BEFORE_RELEASE,
                        part.number,
                        revision.label,
                        failure.message
                    )))
                }
            }
            seen.push((item.part_id.clone(), item.revision_id.clone(), revision.content_hash.clone()));
        }

        let (releaser, eco_id) = (user.id.clone(), eco.id.clone());
        self.mutate(move |state| {
            // Everything again, INSIDE the lock, against the state as it is.
            let eco = find(state, &eco_id).cloned().ok_or_else(|| Error::not_found("change order"))?;
            check_releasable(state, &eco)?;
            let found = problems(state, &eco);
            if !found.is_empty() {
                return Err(Error::conflict(format!("{} cannot release: {}", eco.number, found.join("; "))));
            }
            for (part_id, revision_id, hash) in &seen {
                let part = state.part(part_id).ok_or_else(|| Error::not_found("part"))?;
                let revision = part.revision(revision_id).ok_or_else(|| Error::not_found("revision"))?;
                if &revision.content_hash != hash {
                    return Err(Error::conflict(format!(
                        "{} rev {} changed while {} was checking it — release {} again",
                        part.number,
                        revision.label,
                        scripting::BEFORE_RELEASE,
                        eco.number
                    )));
                }
            }
            let stamp = now();
            // Releases first — each supersedes its part's older release —
            // then obsoletes, which may land on a revision just superseded.
            let mut replaced: Vec<(String, Option<String>)> = Vec::new();
            for item in eco.items.iter().filter(|i| i.action == EcoAction::Release) {
                let part = state.parts.get_mut(&item.part_id).ok_or_else(|| Error::not_found("part"))?;
                replaced.push((item.revision_id.clone(), part.current_release().map(|r| r.id.clone())));
                for other in part.revisions.iter_mut() {
                    if other.id != item.revision_id && other.lifecycle == Lifecycle::Released {
                        other.lifecycle = Lifecycle::Superseded;
                    }
                }
                let revision = part.revision_mut(&item.revision_id).ok_or_else(|| Error::not_found("revision"))?;
                lifecycle::check_transition(revision.lifecycle, Lifecycle::Released).map_err(Error::conflict)?;
                review::close_on_release(revision, &releaser, stamp);
                revision.lifecycle = Lifecycle::Released;
                revision.released_by = Some(releaser.clone());
                revision.released_at = Some(stamp);
                revision.lock = None;
            }
            for item in eco.items.iter().filter(|i| i.action == EcoAction::Obsolete) {
                let (number, revision) = db::find_revision_mut(state, &item.part_id, &item.revision_id)?;
                lifecycle::check_transition(revision.lifecycle, Lifecycle::Obsolete)
                    .map_err(|e| Error::conflict(format!("{number} rev {}: {e}", revision.label)))?;
                revision.lifecycle = Lifecycle::Obsolete;
            }
            // The index rows of every part the order moved (the feed names them).
            for item in eco.items.iter() {
                state.touch_part(&item.part_id);
            }
            let eco = find_mut(state, &eco_id)?;
            if let Some(round) = eco.reviews.last_mut() {
                round.closed_by = Some(releaser.clone());
                round.closed_at = Some(stamp);
            }
            for item in eco.items.iter_mut() {
                if let Some((_, before)) = replaced.iter().find(|(id, _)| *id == item.revision_id) {
                    item.replaced = before.clone();
                }
            }
            eco.state = EcoState::Released;
            eco.released_by = Some(releaser);
            eco.released_at = Some(stamp);
            eco.modified_at = stamp;
            Ok(())
        })?;

        let mut warnings = Vec::new();
        for item in eco.items.iter().filter(|i| i.action == EcoAction::Release) {
            let (part, revision) = self.snapshot(&item.part_id, &item.revision_id)?;
            let mut input = self.read(|state| db::release_input(state, &part, &revision, user));
            input["eco"] = eco_summary.clone();
            match scripting::run(self, user, scripting::AFTER_RELEASE, "afterRelease", &input) {
                Ok(HookResult::Absent) | Ok(HookResult::Returned(_)) => {}
                Ok(HookResult::Refused(failure)) => warnings.push(format!(
                    "{} rev {} released, but {} failed: {}",
                    part.number,
                    revision.label,
                    scripting::AFTER_RELEASE,
                    failure.message
                )),
                Err(error) => warnings.push(format!(
                    "{} rev {} released, but {} could not run: {}",
                    part.number,
                    revision.label,
                    scripting::AFTER_RELEASE,
                    error.message
                )),
            }
        }
        warnings.extend(self.eco_event(user, "released", &eco.id, None));
        Ok(warnings)
    }

    // -- reading -------------------------------------------------------------

    /// Change orders, newest first, optionally only those in `state_filter`.
    pub fn list_ecos(&self, state_filter: &str) -> Vec<EcoRow> {
        self.read(|state| {
            state
                .change_orders
                .iter()
                .rev()
                .filter(|e| match state_filter.trim() {
                    "" => true,
                    "open" => e.state.is_open(),
                    other => e.state.as_str() == other,
                })
                .map(|e| row(state, e))
                .collect()
        })
    }

    /// The open change orders a revision could be added to: Drafts.
    pub fn draft_ecos(&self) -> Vec<EcoRow> {
        self.list_ecos("draft")
    }

    /// One change order as its page shows it.
    pub fn eco_view(&self, viewer: &User, key: &str) -> Result<EcoView, Error> {
        let eco = self.read(|state| find(state, key).cloned()).ok_or_else(|| Error::not_found("change order"))?;
        // Catalog changes come from the audit log, outside the store lock.
        let mut attribute_changes: Vec<Vec<AttributeChange>> = Vec::new();
        for item in &eco.items {
            attribute_changes.push(if item.action == EcoAction::Release {
                // Before the release: since the part's current release. After
                // it: between the release it replaced and this one.
                let (since, until) = if eco.state == EcoState::Released {
                    let since = self.read(|state| {
                        let part = state.part(&item.part_id)?;
                        part.revision(item.replaced.as_deref()?)?.released_at
                    });
                    (since, eco.released_at)
                } else {
                    (self.read(|state| state.part(&item.part_id)?.current_release()?.released_at), None)
                };
                self.attribute_changes_between(&item.part_id, since, until)
            } else {
                Vec::new()
            });
        }
        self.read(|state| {
            let releasing = releasing(&eco);
            let items = eco
                .items
                .iter()
                .zip(attribute_changes)
                .map(|(item, changes)| item_view(state, &eco, item, &releasing, changes))
                .collect();
            let current = review::eco_subject_hash(state, &eco);
            let mut rounds: Vec<ReviewView> = eco.reviews.iter().map(|r| review::view(state, r, viewer, &current)).collect();
            let current = rounds.pop();
            rounds.reverse();
            Ok(EcoView {
                eco: row(state, &eco),
                description: eco.description.clone(),
                reason: eco.reason.clone(),
                released_by: eco.released_by.as_deref().map(|id| name_of(state, id)),
                cancelled_by: eco.cancelled_by.as_deref().map(|id| name_of(state, id)),
                cancelled_at: eco.cancelled_at,
                items,
                current,
                history: rounds,
                comments: review::comment_views(state, &eco.comments),
                rule: state.settings.eco_review_rule.clone(),
                problems: if eco.state.is_open() { problems(state, &eco) } else { Vec::new() },
                can_edit: viewer.can_author() && eco.state == EcoState::Draft,
                can_release: viewer.can_checkin() && eco.state == EcoState::Approved,
                can_comment: review::may_comment(viewer),
            })
        })
    }

    /// Catalog-value changes to a part between two times (from the start,
    /// or to now, when one is missing), from the audit log.
    fn attribute_changes_between(&self, part_id: &str, since: Option<u64>, until: Option<u64>) -> Vec<AttributeChange> {
        let filter = crate::audit::AuditFilter {
            kind: "part".into(),
            entity: part_id.to_string(),
            since,
            until,
            limit: Some(200),
            ..Default::default()
        };
        let events = self.audit_query(&filter).unwrap_or_default();
        let mut out = Vec::new();
        for event in events.into_iter().rev() {
            for field in ["attributes", "category"] {
                if let Some(change) = event.changes.get(field) {
                    out.push(AttributeChange {
                        at: event.at,
                        by: event.actor.username.clone(),
                        field: field.to_string(),
                        before: change.before.clone(),
                        after: change.after.clone(),
                    });
                }
            }
        }
        out
    }

    /// Tell the `review-event` hook about a change order's event.
    fn eco_event(&self, user: &User, event: &str, eco_id: &str, comment: Option<&crate::model::Comment>) -> Vec<String> {
        let input = self.read(|state| {
            let eco = find(state, eco_id)?;
            Some(json!({
                "event": event,
                "subject": {
                    "kind": "eco",
                    "id": eco.id,
                    "number": eco.number,
                    "title": eco.title,
                    "state": eco.state.as_str(),
                },
                "review": eco.reviews.last(),
                "comment": comment,
                "user": scripting::user_json(user),
            }))
        });
        match input {
            Some(input) => review::hook_warning(self, user, &input),
            None => Vec::new(),
        }
    }
}

/// A change order may release only when approved, with its rule still met by
/// approvals given on its items' documents as they are now (D8).
fn check_releasable(state: &State, eco: &ChangeOrder) -> Result<(), Error> {
    let current = review::eco_subject_hash(state, eco);
    if eco.state != EcoState::Approved {
        // Back in review because an item was saved after approval: say so.
        let stale = eco.reviews.last().filter(|r| r.is_live()).map(|r| review::stale_clause(r, &current)).unwrap_or_default();
        return Err(Error::conflict(format!(
            "{} is {} — a change order releases once it is approved{stale}",
            eco.number,
            eco.state.as_str()
        )));
    }
    match eco.reviews.last() {
        Some(round) if round.is_live() && round.is_met(&current) => {}
        Some(round) => {
            return Err(Error::conflict(format!(
                "{}'s review is not approved{}",
                eco.number,
                review::stale_clause(round, &current)
            )))
        }
        None => return Err(Error::conflict(format!("{}'s review is not approved", eco.number))),
    }
    if eco.items.is_empty() {
        return Err(Error::conflict(format!("{} names no revisions", eco.number)));
    }
    Ok(())
}

fn item_view(
    state: &State,
    eco: &ChangeOrder,
    item: &EcoItem,
    releasing: &BTreeSet<String>,
    attribute_changes: Vec<AttributeChange>,
) -> ItemView {
    let part = state.part(&item.part_id);
    let revision = part.and_then(|p| p.revision(&item.revision_id));
    // What a release replaces as current: once released, the revision it
    // recorded; before, the part's release now, unless that is the item.
    let replaces = match (part, item.action) {
        (Some(p), EcoAction::Release) if eco.state == EcoState::Released => {
            item.replaced.as_deref().and_then(|id| p.revision(id))
        }
        (Some(p), EcoAction::Release) => p.current_release().filter(|r| r.id != item.revision_id),
        _ => None,
    };
    let bom_diff = match (part, revision, replaces) {
        (Some(p), Some(r), Some(old)) if !r.uses.is_empty() || !old.uses.is_empty() => Some(bom::diff(state, p, old, r)),
        _ => None,
    };
    ItemView {
        part_id: item.part_id.clone(),
        number: part.map(|p| p.number.clone()).unwrap_or_default(),
        name: part.map(|p| p.name.clone()).unwrap_or_default(),
        revision_id: item.revision_id.clone(),
        label: revision.map(|r| r.label.clone()).unwrap_or_default(),
        lifecycle: revision.map(|r| r.lifecycle.as_str()).unwrap_or(""),
        action: item.action,
        note: item.note.clone(),
        replaces: replaces.map(|r| r.label.clone()),
        replaces_id: replaces.map(|r| r.id.clone()),
        document_changed: match (revision, replaces) {
            (Some(r), Some(old)) => Some(r.content_hash != old.content_hash),
            _ => None,
        },
        content_hash: revision.map(|r| r.content_hash.clone()).unwrap_or_default(),
        previous_hash: replaces.map(|r| r.content_hash.clone()).unwrap_or_default(),
        bom_diff,
        attribute_changes,
        problem: if eco.state.is_open() {
            item_problem(state, eco, item, releasing).unwrap_or_default()
        } else {
            String::new()
        },
    }
}