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",
}
}
pub fn is_live(self) -> bool {
matches!(self, JobStatus::Queued | JobStatus::InProgress)
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Job {
pub id: String,
pub instructions: Instructs,
pub assembly: Instructs,
#[serde(default)]
pub serial: String,
pub status: JobStatus,
pub position: i64,
#[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>,
#[serde(default)]
pub modified_seq: u64,
pub steps: Vec<JobStep>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct JobStep {
pub step: String,
#[serde(default)]
pub chapter: String,
#[serde(default)]
pub title: String,
#[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>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub field_defs: Vec<brep_instructions::fields::StepField>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub fields: Vec<FieldEntry>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct FieldEntry {
pub id: String,
#[serde(default)]
pub label: String,
pub value: Value,
#[serde(default, skip_serializing_if = "String::is_empty")]
pub unit: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub in_limits: Option<bool>,
}
#[derive(Debug, Clone, Deserialize)]
pub struct FieldInput {
pub id: String,
#[serde(default)]
pub value: Value,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct JobAdd {
pub occurrences: Vec<String>,
#[serde(default)]
pub part: String,
pub serial_tracked: bool,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct SerialEntry {
pub occurrence: String,
#[serde(default)]
pub part: String,
pub serial: String,
}
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)
}
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()
}
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()
.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)
}
pub fn can_sign(user: &User) -> bool {
[groups::OPERATOR, groups::AUTHOR, groups::ADMIN].iter().any(|g| user.groups.iter().any(|u| u == g))
}
pub fn can_reopen(user: &User) -> bool {
[groups::CHECKIN, groups::ADMIN].iter().any(|g| user.groups.iter().any(|u| u == g))
}
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 {
pub instructions: String,
pub revision: String,
#[serde(default)]
pub serial: String,
#[serde(default)]
pub quantity: Option<u32>,
}
#[derive(Debug, Clone, Deserialize)]
pub struct SignOff {
#[serde(default)]
pub serials: Vec<SerialEntry>,
#[serde(default)]
pub note: String,
#[serde(default)]
pub fields: Vec<FieldInput>,
}
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 {
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"));
}
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)
})
}
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())
})
}
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)));
}
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)
})
}
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())
})
}
}