BREP_plm 0.22.0

The next.BREP.io PLM server for the Rust B-rep CAD kernel: parts, revisions, lifecycle, checkout, BOMs and configurable workflows over an embedded SQLite store, with a browser UI and the document API the 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
//! The jobs floor queue: serialized builds of released manufacturing
//! instructions, signed off step by step on the shop floor.
//!
//! One job is one build of one released instructions revision. Its steps are
//! a snapshot of that revision's step order (reading order: a step's children
//! before it), taken when the job is made; a released document never changes, so
//! the snapshot is what the floor builds. Each step is signed once, in order,
//! by an operator (or an author or admin), who records the serial of every
//! unit the step adds of a part marked `Serial_Tracked` and the values of the
//! step's fields (what its author asked to be recorded there: ticks, choices,
//! text, measurements, serial numbers), which the server reads against the
//! field definitions it snapshot with the step: a required field left empty,
//! a value the field cannot hold, or a measurement outside limits that must
//! be inside refuses the sign-off, whatever the page checked. A serial of a part
//! is in at most one job that is not cancelled: `GET /api/serials` answers
//! which build took it. Only the check-in group reopens a signed step (and every step after
//! it), and the audit keeps both.
//!
//! No inventory: no stock, no locations, no consumption. The queue is a step
//! towards an ERP system, not one.
//!
//! Every write below is one read-modify-write inside [`Db::mutate`], which
//! holds the store's write lock from the first read to the commit: two
//! tablets never both read a step unsigned, and no write is lost. Jobs are
//! journaled ([`crate::journal::Journaled`]): a write stores and audits only
//! the jobs it touched, one `meta` row each.

use std::collections::BTreeSet;

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

use crate::db::{now, Db, State};
use crate::model::{groups, DocumentClass, Instructs, Lifecycle, Timestamp, User};
use crate::Error;

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum JobStatus {
    Queued,
    InProgress,
    Done,
    Cancelled,
}

impl JobStatus {
    pub fn as_str(self) -> &'static str {
        match self {
            JobStatus::Queued => "queued",
            JobStatus::InProgress => "in_progress",
            JobStatus::Done => "done",
            JobStatus::Cancelled => "cancelled",
        }
    }

    /// Queued or in progress: its serials are taken.
    pub fn is_live(self) -> bool {
        matches!(self, JobStatus::Queued | JobStatus::InProgress)
    }
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Job {
    /// `JOB000001`: handed out in order, never reused.
    pub id: String,
    pub instructions: Instructs,
    pub assembly: Instructs,
    /// The serial of the unit being built, when it has one.
    #[serde(default)]
    pub serial: String,
    pub status: JobStatus,
    /// The queue's order: lowest first, ties by number.
    pub position: i64,
    /// A user id, or empty for anyone.
    #[serde(default)]
    pub assigned_to: String,
    pub created_by: String,
    pub created_at: Timestamp,
    #[serde(default)]
    pub started_at: Option<Timestamp>,
    #[serde(default)]
    pub finished_at: Option<Timestamp>,
    /// The store sequence of the job's last change: a floor tablet polls
    /// the queue and re-reads what moved.
    #[serde(default)]
    pub modified_seq: u64,
    pub steps: Vec<JobStep>,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct JobStep {
    /// The instructions' step id.
    pub step: String,
    /// The sub-assembly the step helps build (the occurrence its parent step
    /// places): empty at the top level. Named for the chapters that held a
    /// sub-assembly's steps before the step tree.
    #[serde(default)]
    pub chapter: String,
    #[serde(default)]
    pub title: String,
    /// What the step adds, with the units that need a serial.
    #[serde(default)]
    pub adds: Vec<JobAdd>,
    #[serde(default)]
    pub signed_by: Option<String>,
    #[serde(default)]
    pub signed_at: Option<Timestamp>,
    #[serde(default)]
    pub note: String,
    #[serde(default)]
    pub serials: Vec<SerialEntry>,
    /// The step's fields as the instructions define them (a snapshot, like
    /// the adds): what the sign-off's values are read against. Empty for a
    /// step without fields and for a job made before fields existed.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub field_defs: Vec<brep_instructions::fields::StepField>,
    /// The values recorded at sign-off, one per field that was filled in.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub fields: Vec<FieldEntry>,
}

/// One field's recorded value.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct FieldEntry {
    /// The field's id in the instructions (`MF12`).
    pub id: String,
    /// Its label when the step was signed.
    #[serde(default)]
    pub label: String,
    /// `true` for a tick, the option (or an array of them) for a choice,
    /// text, or a number for a measurement.
    pub value: Value,
    /// A measurement's unit.
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub unit: String,
    /// A measurement with limits: whether the value was inside them, as
    /// the server judged it.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub in_limits: Option<bool>,
}

/// One field's value as a sign-off sends it. A page may send the label,
/// unit and its own in-limits reading beside it; the server takes only the
/// id and the value and works the rest out from the step's definitions.
#[derive(Debug, Clone, Deserialize)]
pub struct FieldInput {
    pub id: String,
    #[serde(default)]
    pub value: Value,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct JobAdd {
    /// One occurrence path per unit (`ACOMP1:ACOMP3`).
    pub occurrences: Vec<String>,
    /// The PLM part id when the add names a part this server knows, else
    /// the document's text.
    #[serde(default)]
    pub part: String,
    /// One serial per occurrence at sign-off: the part's `Serial_Tracked`
    /// attribute, or the document marks the add.
    pub serial_tracked: bool,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct SerialEntry {
    pub occurrence: String,
    /// The PLM part. A sign-off need not send it: the server takes it from
    /// the step's add for that occurrence.
    #[serde(default)]
    pub part: String,
    pub serial: String,
}

/// Whether a part (its newest revision's type attributes, then its catalog
/// attributes) is marked `Serial_Tracked`, the attribute name the app and
/// server agreed (matched without regard to case).
pub fn serial_tracked(state: &State, part_id: &str) -> bool {
    let Some(part) = state.part(part_id) else { return false };
    let truthy = |map: &std::collections::BTreeMap<String, Value>| {
        map.iter().find(|(k, _)| k.eq_ignore_ascii_case("serial_tracked")).is_some_and(|(_, v)| {
            v.as_bool().unwrap_or(false) || v.as_str().is_some_and(|s| matches!(s.trim().to_ascii_lowercase().as_str(), "true" | "yes" | "1"))
        })
    };
    part.latest().is_some_and(|r| truthy(&r.attributes)) || truthy(&part.attributes)
}

/// The sub-assembly a step helps build: the occurrence its parent step
/// places; empty at the top level.
fn group_of(instructions: &brep_instructions::model::Instructions, step_id: &str) -> String {
    instructions
        .parent_id(step_id)
        .flatten()
        .and_then(|parent| instructions.step(parent))
        .and_then(|parent| parent.adds.iter().find(|add| add.subassembly).and_then(|add| add.occurrences.first().cloned()))
        .unwrap_or_default()
}

/// The steps a job signs, read with the instructions crate's own model (a
/// schema-1 document's chapters read as the tree): every step in reading
/// order (a step's children before it, as the wizard orders them), leaving
/// out a step that needs no sign-off and records nothing (a step with fields
/// is on the checklist: its values are captured with its sign-off).
pub fn snapshot_steps(state: &State, document: &Value) -> Result<Vec<JobStep>, Error> {
    let block = document.get(brep_instructions::model::BLOCK_KEY).ok_or_else(|| Error::conflict("the document holds no instructions"))?;
    let instructions = brep_instructions::model::Instructions::from_value(block)
        .map_err(|e| Error::conflict(format!("the instructions do not read: {e}")))?;
    let steps: Vec<JobStep> = instructions
        .steps()
        // A step that needs no sign-off and has no field to record is not on
        // the floor's checklist.
        .filter(|(_, step)| step.on_checklist())
        .map(|(_, step)| JobStep {
            step: step.id.clone(),
            chapter: group_of(&instructions, &step.id),
            title: step.title.clone(),
            adds: step
                .adds
                .iter()
                .map(|add| {
                    let known = state.part_by_id_or_number(&add.part).map(|p| p.id.clone());
                    JobAdd {
                        occurrences: add.occurrences.clone(),
                        serial_tracked: add.serial_tracked || known.as_deref().is_some_and(|id| serial_tracked(state, id)),
                        part: known.unwrap_or_else(|| add.part.clone()),
                    }
                })
                .collect(),
            signed_by: None,
            signed_at: None,
            note: String::new(),
            serials: Vec::new(),
            field_defs: step.fields.clone(),
            fields: Vec::new(),
        })
        .collect();
    if steps.is_empty() {
        return Err(Error::conflict("the instructions have no steps to build"));
    }
    let mut seen = BTreeSet::new();
    if let Some(twice) = steps.iter().find(|s| !seen.insert(s.step.clone())) {
        return Err(Error::conflict(format!("step id {} is used twice", twice.step)));
    }
    Ok(steps)
}

/// Who may sign a step: operators, authors and admins.
pub fn can_sign(user: &User) -> bool {
    [groups::OPERATOR, groups::AUTHOR, groups::ADMIN].iter().any(|g| user.groups.iter().any(|u| u == g))
}

/// Who may reopen a signed step: the check-in group and admins.
pub fn can_reopen(user: &User) -> bool {
    [groups::CHECKIN, groups::ADMIN].iter().any(|g| user.groups.iter().any(|u| u == g))
}

/// Where a part's serial is taken: the job (live or done; a cancelled job
/// frees its serials) and the step that took it.
pub fn serial_holder<'a>(state: &'a State, part: &str, serial: &str) -> Option<(&'a Job, &'a JobStep)> {
    state.jobs.iter().filter(|j| j.status != JobStatus::Cancelled).find_map(|job| {
        job.steps
            .iter()
            .find(|s| s.serials.iter().any(|e| e.part == part && e.serial.eq_ignore_ascii_case(serial)))
            .map(|s| (job, s))
    })
}

#[derive(Debug, Clone, Deserialize)]
pub struct NewJob {
    /// The instructions part, by id or number.
    pub instructions: String,
    /// Its revision, by id or label: a released one.
    pub revision: String,
    #[serde(default)]
    pub serial: String,
    /// How many jobs to queue (each its own build). One by default.
    #[serde(default)]
    pub quantity: Option<u32>,
}

#[derive(Debug, Clone, Deserialize)]
pub struct SignOff {
    #[serde(default)]
    pub serials: Vec<SerialEntry>,
    #[serde(default)]
    pub note: String,
    /// The values of the step's fields: `[{id, value}]`.
    #[serde(default)]
    pub fields: Vec<FieldInput>,
}

/// The sign-off's field values read against the step's definitions: one
/// entry per field that was filled in, in the step's order. Refused: a value
/// for a field the step does not have, a field given twice, a value its
/// field cannot hold, a required field left empty, and a measurement outside
/// limits that must be inside.
pub fn read_fields(step: &JobStep, given: &[FieldInput]) -> Result<Vec<FieldEntry>, Error> {
    use brep_instructions::fields::{blocking, evaluate};
    for (i, input) in given.iter().enumerate() {
        if !step.field_defs.iter().any(|f| f.id == input.id) {
            return Err(Error::conflict(format!("step {} has no field '{}'", step.step, input.id)));
        }
        if given[..i].iter().any(|earlier| earlier.id == input.id) {
            return Err(Error::bad_request(format!("field '{}' is given twice", input.id)));
        }
    }
    let mut entries = Vec::new();
    let mut open = Vec::new();
    for field in &step.field_defs {
        let value = given.iter().find(|input| input.id == field.id).map(|input| &input.value);
        let read = evaluate(field, value).map_err(Error::bad_request)?;
        if let Some(reason) = blocking(field, &read) {
            open.push(reason);
        }
        if let Some(value) = read.value {
            entries.push(FieldEntry { id: field.id.clone(), label: field.label.clone(), value, unit: field.unit().to_string(), in_limits: read.in_limits });
        }
    }
    if !open.is_empty() {
        return Err(Error::conflict(format!("step {} is not complete: {}", step.step, open.join("; "))));
    }
    Ok(entries)
}

impl Db {
    /// Queue `quantity` jobs building one released instructions revision.
    pub fn create_jobs(&self, user: &User, request: &NewJob) -> Result<Vec<Job>, Error> {
        let quantity = request.quantity.unwrap_or(1);
        if !(1..=500).contains(&quantity) {
            return Err(Error::bad_request("a request queues 1 to 500 jobs"));
        }
        let serial = request.serial.trim().to_string();
        if quantity > 1 && !serial.is_empty() {
            return Err(Error::bad_request("a serial names one build: queue one job for it"));
        }
        // The document is read outside the lock: a released revision's bytes
        // never change, and the lifecycle is checked again inside it.
        let (part_id, revision_id, path) = self.read(|state| {
            let part = state.part_by_id_or_number(&request.instructions).ok_or_else(|| Error::not_found("instructions"))?;
            if part.document_class != DocumentClass::Instructions {
                return Err(Error::bad_request(format!("{} is not manufacturing instructions", part.number)));
            }
            let revision = crate::bom::find_revision(part, &request.revision).ok_or_else(|| Error::not_found("revision"))?;
            Ok((part.id.clone(), revision.id.clone(), crate::identity::model_path(&part.id, &revision.id)))
        })?;
        let document: Value = std::fs::read_to_string(self.root().join(path))
            .ok()
            .and_then(|text| serde_json::from_str(&text).ok())
            .ok_or_else(|| Error::conflict("the instructions revision has no readable document"))?;
        let user_id = user.id.clone();
        self.mutate(move |state| {
            let part = state.part(&part_id).ok_or_else(|| Error::not_found("instructions"))?;
            let revision = part.revision(&revision_id).ok_or_else(|| Error::not_found("revision"))?;
            if revision.lifecycle != Lifecycle::Released {
                return Err(Error::conflict(format!(
                    "{} rev {} is {}: the floor builds released instructions only",
                    part.number,
                    revision.label,
                    revision.lifecycle.as_str()
                )));
            }
            let assembly = revision.instructs.clone().ok_or_else(|| Error::conflict("the instructions instruct no PLM assembly"))?;
            let steps = snapshot_steps(state, &document)?;
            if !serial.is_empty() {
                if let Some(job) = state.jobs.iter().find(|j| j.status != JobStatus::Cancelled && j.assembly.part == assembly.part && j.serial.eq_ignore_ascii_case(&serial)) {
                    return Err(Error::conflict(format!("serial {serial} is {} by {}", if job.status == JobStatus::Done { "already built" } else { "already being built" }, job.id)));
                }
            }
            let mut position = state.jobs.iter().filter(|j| j.status.is_live()).map(|j| j.position).max().unwrap_or(0);
            let stamp = now();
            let mut made = Vec::new();
            for _ in 0..quantity {
                state.next_job += 1;
                position += 1;
                let job = Job {
                    id: format!("JOB{:06}", state.next_job),
                    instructions: Instructs { part: part_id.clone(), revision: revision_id.clone() },
                    assembly: assembly.clone(),
                    serial: serial.clone(),
                    status: JobStatus::Queued,
                    position,
                    assigned_to: String::new(),
                    created_by: user_id.clone(),
                    created_at: stamp,
                    started_at: None,
                    finished_at: None,
                    modified_seq: state.seq,
                    steps: steps.clone(),
                };
                state.jobs.push(job.clone());
                made.push(job);
            }
            Ok(made)
        })
    }

    /// Change a job's place in the queue, who it is assigned to, or cancel
    /// it (`status: "cancelled"`; a cancelled job frees its serials).
    pub fn update_job(&self, id: &str, fields: &Value) -> Result<Job, Error> {
        let fields = fields.as_object().cloned().ok_or_else(|| Error::bad_request("a job change is a JSON object"))?;
        if let Some(key) = fields.keys().find(|k| !["position", "assigned_to", "status"].contains(&k.as_str())) {
            return Err(Error::bad_request(format!("a job has no field '{key}' to change")));
        }
        let id = id.to_string();
        self.mutate(move |state| {
            if let Some(user) = fields.get("assigned_to").and_then(Value::as_str).filter(|u| !u.is_empty()) {
                if state.user(user).is_none() {
                    return Err(Error::bad_request(format!("no user '{user}'")));
                }
            }
            let seq = state.seq;
            let job = state.jobs.get_mut(&id).ok_or_else(|| Error::not_found("job"))?;
            if !job.status.is_live() {
                return Err(Error::conflict(format!("{} is {}", job.id, job.status.as_str())));
            }
            if let Some(position) = fields.get("position") {
                job.position = position.as_i64().ok_or_else(|| Error::bad_request("position is a whole number"))?;
            }
            if let Some(user) = fields.get("assigned_to") {
                job.assigned_to = user.as_str().ok_or_else(|| Error::bad_request("assigned_to is a user id"))?.to_string();
            }
            match fields.get("status").map(|s| s.as_str()) {
                None => {}
                Some(Some("cancelled")) => {
                    job.status = JobStatus::Cancelled;
                    job.finished_at = Some(now());
                }
                Some(_) => return Err(Error::bad_request("a job's status changes only to cancelled; sign-off moves the rest")),
            }
            job.modified_seq = seq;
            Ok(job.clone())
        })
    }

    /// Sign one step of a job, in order, with the serials it captures.
    pub fn sign_step(&self, user: &User, id: &str, step: &str, signoff: &SignOff) -> Result<JobStep, Error> {
        if !can_sign(user) {
            return Err(Error::forbidden("signing a step needs the operator, author or admin group"));
        }
        let (id, step, user_id) = (id.to_string(), step.to_string(), user.id.clone());
        let mut signoff = signoff.clone();
        for entry in &mut signoff.serials {
            entry.serial = entry.serial.trim().to_string();
            if entry.serial.is_empty() || entry.serial.chars().count() > 120 {
                return Err(Error::bad_request("a serial is 1 to 120 characters"));
            }
        }
        if signoff.note.chars().count() > 2000 {
            return Err(Error::bad_request("a sign-off note is at most 2000 characters"));
        }
        self.mutate(move |state| {
            let job = state.jobs.get(&id).ok_or_else(|| Error::not_found("job"))?;
            if !job.status.is_live() {
                return Err(Error::conflict(format!("{} is {}: nothing more is signed", job.id, job.status.as_str())));
            }
            let at = job.steps.iter().position(|s| s.step == step).ok_or_else(|| Error::not_found("step"))?;
            let this = &job.steps[at];
            if let (Some(by), Some(when)) = (&this.signed_by, this.signed_at) {
                let name = state.user(by).map_or_else(|| "someone".to_string(), |u| u.username.clone());
                return Err(Error::conflict(format!("step {} of {} was signed by {name} at {when}", this.step, job.id)));
            }
            if let Some(earlier) = job.steps[..at].iter().find(|s| s.signed_at.is_none()) {
                return Err(Error::conflict(format!("step {} of {} is not signed yet: steps are signed in order", earlier.step, job.id)));
            }
            // Exactly one serial per occurrence of each serial-tracked add,
            // and no serial for anything else.
            let add_of = |occurrence: &str| this.adds.iter().find(|a| a.occurrences.iter().any(|o| o == occurrence));
            if let Some(stray) = signoff.serials.iter().find(|e| add_of(&e.occurrence).is_none_or(|a| !a.serial_tracked)) {
                return Err(Error::conflict(format!("step {} takes no serial for '{}'", this.step, stray.occurrence)));
            }
            for add in this.adds.iter().filter(|a| a.serial_tracked) {
                for occurrence in &add.occurrences {
                    let given = signoff.serials.iter().filter(|e| &e.occurrence == occurrence).count();
                    if given != 1 {
                        return Err(Error::conflict(format!("{occurrence} takes one serial at step {}; {given} given", this.step)));
                    }
                }
            }
            let fields = read_fields(this, &signoff.fields)?;
            let mut serials = Vec::new();
            for entry in &signoff.serials {
                let part = add_of(&entry.occurrence).map(|a| a.part.clone()).unwrap_or_default();
                if serials.iter().any(|e: &SerialEntry| e.part == part && e.serial.eq_ignore_ascii_case(&entry.serial)) {
                    return Err(Error::conflict(format!("serial {} is given twice", entry.serial)));
                }
                if let Some((holder, held_at)) = serial_holder(state, &part, &entry.serial) {
                    return Err(Error::conflict(format!("serial {} of {part} was taken by {} at step {}", entry.serial, holder.id, held_at.step)));
                }
                serials.push(SerialEntry { occurrence: entry.occurrence.clone(), part, serial: entry.serial.clone() });
            }
            let (seq, stamp) = (state.seq, now());
            let job = state.jobs.get_mut(&id).expect("the job was read under this lock");
            let signed = &mut job.steps[at];
            signed.signed_by = Some(user_id);
            signed.signed_at = Some(stamp);
            signed.note = signoff.note.trim().to_string();
            signed.serials = serials;
            signed.fields = fields;
            let signed = signed.clone();
            if job.status == JobStatus::Queued {
                job.status = JobStatus::InProgress;
                job.started_at = Some(stamp);
            }
            if job.steps.iter().all(|s| s.signed_at.is_some()) {
                job.status = JobStatus::Done;
                job.finished_at = Some(stamp);
            }
            job.modified_seq = seq;
            Ok(signed)
        })
    }

    /// Reopen a signed step and every step after it: the check-in group, with
    /// a reason the audit keeps. A done job is in progress again.
    pub fn reopen_step(&self, user: &User, id: &str, step: &str, reason: &str) -> Result<Job, Error> {
        if !can_reopen(user) {
            return Err(Error::forbidden("reopening a signed step needs the check-in group"));
        }
        let reason = reason.trim().to_string();
        if reason.is_empty() || reason.chars().count() > 2000 {
            return Err(Error::bad_request("a reopen gives its reason (at most 2000 characters)"));
        }
        let (id, step) = (id.to_string(), step.to_string());
        self.mutate(move |state| {
            let seq = state.seq;
            let job = state.jobs.get_mut(&id).ok_or_else(|| Error::not_found("job"))?;
            if job.status == JobStatus::Cancelled {
                return Err(Error::conflict(format!("{} is cancelled", job.id)));
            }
            let at = job.steps.iter().position(|s| s.step == step).ok_or_else(|| Error::not_found("step"))?;
            if job.steps[at].signed_at.is_none() {
                return Err(Error::conflict(format!("step {step} of {} is not signed", job.id)));
            }
            for later in &mut job.steps[at..] {
                later.signed_by = None;
                later.signed_at = None;
                later.serials.clear();
                later.fields.clear();
                later.note = String::new();
            }
            job.steps[at].note = format!("reopened: {reason}");
            job.status = if job.steps.iter().any(|s| s.signed_at.is_some()) { JobStatus::InProgress } else { JobStatus::Queued };
            job.finished_at = None;
            job.modified_seq = seq;
            Ok(job.clone())
        })
    }
}