From c3f492ee8fa1278fc1643308d7e7f2d71fe625cc Mon Sep 17 00:00:00 2001 From: fork Date: Sun, 22 Nov 2026 20:06:04 +0100 Subject: [PATCH 1/2] Check-in: "Update the BOM from the CAD file", ticked by default (#407) A check-in can update the BOM of the revision from the CAD references of the version being checked in, in the check-in's transaction: - services/bom_references.rs: plan() split into plan_on() on one connection (sees the references the check-in just stored; counts from file_references.instances, no parsing under the lock); the add/update loop shared by apply() and the new apply_on_checkin() (new children added, CAD-counted quantities corrected, never an assumed 1, circular refused, nothing removed; kept_not_in_cad / not_in_teamcraft reported; audit bom.from_references with source checkin). SolidCraft designs with placements count as structure files. - services/files.rs: check_in_with() with CheckInOptions; check_in() keeps its behaviour. uploads::finish passes the option on. - API: multipart field bom_from_references on POST .../checkin, body field on POST /uploads/{id}/finish (off by default), answer adds bom. - Web: the box in the check-in form (assembly items or CAD structure), the flash message says what changed. - Desktop: the box in Check in... (js/checkin-bom.js), bomFromReferences through workspace_checkin, the result line in the toast; never on Park. - Tests: tests/checkin_bom.rs, unit tests, scripts/desktop-checkin-bom.test.mjs. - Docs: guides (structure, desktop, vault), API reference, CHANGELOG, README, desktop/README, OPEN_WORK. Co-Authored-By: Claude Opus 5.5 --- .github/workflows/desktop.yml | 4 + CHANGELOG.md | 22 ++ README.md | 4 + desktop/README.md | 2 +- desktop/src/commands.rs | 5 + desktop/src/workspace.rs | 33 +- desktop/ui/js/checkin-bom.js | 52 +++ desktop/ui/js/workspace.js | 18 +- docs/OPEN_WORK.md | 3 + docs/src/guide/desktop.md | 18 +- docs/src/guide/structure.md | 41 +- docs/src/guide/vault.md | 3 + docs/src/reference/api.md | 34 +- scripts/desktop-checkin-bom.test.mjs | 61 +++ src/api/items_api.rs | 25 +- src/api/mod.rs | 4 +- src/api/uploads_api.rs | 20 +- src/services/bom_references.rs | 453 ++++++++++++++++++--- src/services/files.rs | 72 +++- src/services/references.rs | 17 +- src/services/uploads.rs | 17 +- src/web/revisions.rs | 24 +- templates/revisions/show.html | 4 + tests/checkin_bom.rs | 570 +++++++++++++++++++++++++++ 24 files changed, 1407 insertions(+), 99 deletions(-) create mode 100755 desktop/ui/js/checkin-bom.js create mode 100755 scripts/desktop-checkin-bom.test.mjs create mode 100755 tests/checkin_bom.rs diff --git a/.github/workflows/desktop.yml b/.github/workflows/desktop.yml index 0a792851..d4bb1368 100755 --- a/.github/workflows/desktop.yml +++ b/.github/workflows/desktop.yml @@ -107,6 +107,10 @@ jobs: # where the file is, the duplicate findings, the worklist suggestions - name: Test the rules of new items from local files run: node scripts/desktop-new-files.test.mjs + # Update the BOM from the CAD file at check-in (#407): when the box is + # offered, the result line + - name: Test the check-in BOM rules + run: node scripts/desktop-checkin-bom.test.mjs # the mesh check of the shared 3D viewer: empty, cut-off or invalid # models end in a message, not in an empty canvas (#290) - name: Test the 3D viewer's mesh check diff --git a/CHANGELOG.md b/CHANGELOG.md index 8bb48d14..e76c765b 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,28 @@ see the notices in the README. ## [Unreleased] +### Added + +- **Check-in: *Update the BOM from the CAD file*** (#407): a box in the web + check-in form and the desktop app's *Check in…*, ticked by default every + time, shown when the revision has an assembly. The plan of *Create BOM from + CAD references…* is applied to the version being checked in, in the + check-in's transaction: new children added, quantities the CAD file counts + corrected (never from an *assumed 1*), circular children refused, no line + removed – lines the CAD file no longer uses are kept and reported, as are + referenced files TeamCraft does not have. The result says what changed + (*BOM: 4 lines added, 1 quantity changed · …*). REST API: the multipart + field `bom_from_references` on `POST …/checkin` and in the body of + `POST /uploads/{id}/finish` (off by default), the answer adds `bom`; + audit `bom.from_references` with `"source": "checkin"`. *Park* never + changes the BOM. + +### Changed + +- BOM from CAD references: SolidCraft designs with linked designs count as + structure files (their placements are the quantities); the guides now name + Inventor and SolidCraft as sources of instance counts too. + ## [0.2.12] - 2026-11-22 ### Security diff --git a/README.md b/README.md index 6b9bde78..97fd6582 100755 --- a/README.md +++ b/README.md @@ -238,6 +238,10 @@ end-to-end tests against a real PostgreSQL. - **Alternates and supersession** – alternate parts, superseded parts and *Replace in assemblies* with a preview. - **BOM import** – items and BOMs from CAD BOM tables (CSV, Excel, ODS) or a STEP assembly; preview first, one transaction. +- **BOM from the CAD file** – the parts an assembly uses, with the instance + counts of Inventor, SOLIDWORKS, NX, STEP and SolidCraft files; updated at + check-in when *Update the BOM from the CAD file* is ticked (the default), + never removing a line. - **Pack and Go** – a ZIP of a whole assembly with all its files. **Change and release** diff --git a/desktop/README.md b/desktop/README.md index 3491c674..bc8b7e19 100755 --- a/desktop/README.md +++ b/desktop/README.md @@ -29,7 +29,7 @@ TeamCraft server – it is a client, not an offline copy. | Tab order | Drag a tab to move it (pointer events, an insertion bar shows the landing place, the strip scrolls at its edges, `Esc` cancels; a plain click still activates, a dragged tab keeps its state and is not activated). Keyboard: arrow keys, `Home`, `End` move the focus between the tabs (WAI-ARIA tabs, roving tabindex, `Enter`/`Space` activates), `Ctrl+Shift+PageUp/PageDown` move the active tab. Right click (or the menu key): *Move to start*, *Move to end*, *Close*, *Close others*, *Close tabs to the right*. The order is kept while the app runs (open tabs are not restored on start). Rules in `ui/js/tab-order.js`, tested by `scripts/desktop-tab-order.test.mjs` (`node`); the dragging itself is checked by hand | | Revision tabs | Overview (with *At a glance*: the preview image from the CAD file, files, BOM lines, where used, references, change orders, open requests – and a link to the similar parts in the browser), Files (versions, restore, open), Structure (multi-level BOM, revision rules, roll-up), Where used, References, **Graph** (see below), Changes, History, **3D viewer** (STEP, meshes, point clouds – converted by the server), Workspace | | Graph | A navigable reference/BOM graph around the revision: the focused item in the middle, its BOM children and references below, the assemblies that use it above. Node **colour** = lifecycle state, **size** and the ⚙ badge = the item's own BOM complexity, **×n in the top-left corner** = how many instances of that part or sub-assembly the assembly contains (summed over its BOM lines, ×1 included). Every node is clickable and takes the tab to that item – a sub-assembly shows its own graph, anything else its overview, the item in the middle its own overview – with a **back control that stays in the same place** (the left of the sub-tab row) and leads back the way you came, one step per click; `Ctrl`/`⌘`+click opens a node in a tab of its own. Mouse-wheel zooms on the cursor; drag to pan; the view fits itself while details load until you zoom or pan, a double-click on the background fits it again. More than 8 neighbours on one side are laid out in rows; above 48 only the first 48 are fetched for preview and complexity (one request each), the rest keep a basic card. Preview images and filenames on the nodes; pure SVG, no external engine | -| Working folder | One folder per project group that **mirrors the folder structure of the files** (`///`, the folder is stored with every file on the server), one copy of each file – the revision is metadata in `/.teamcraft/state.json` (item, revision, file id, version, SHA-256, checked out). Files you have not checked out are **read-only** on disk; *Check out* takes the lock and makes them writable; *Check in…* uploads the chosen files as new versions, releases the lock in one step and makes them read-only again (files above 32 MiB go first as chunked, resumable uploads, then join the same check-in transaction). The check-in dialog shows the progress, and **Cancel** stops the check-in until its last byte is sent – before the next STEP export, between two chunks (the upload sessions are discarded, `DELETE /uploads/{id}`) or half-way through the streamed request with the small files: nothing is stored, the revision stays checked out (`src/abortable.rs`). Every check-in names the version each file is based on, so the server refuses (409) to overwrite a version somebody else checked in meanwhile. The Workspace tab shows *up to date* / *modified* / *outdated* / *conflict* / *new* / *missing* / *name clash* per file (from the server's file status) with its path, read-only flag and the lock owner. Two items with the same folder and file name are reported, never overwritten. Lock and backup files of the CAD programs are skipped; a SolidCraft design that is open in SolidCraft (a fresh `.~.lock` beside it, `src/cadlock.rs`) is marked *Open in SolidCraft* with the computer and user, and the check-in dialog warns that unsaved changes there are not checked in; Creo iterations count as one file. A tracked revision the server no longer shows (you left a restricted project group) is listed as *No longer accessible*: its local copies stay untouched and are never synced again – delete them yourself if you must In *Check in…*, a new part or assembly file next to a revision that has a model already is offered unticked, with *New item from this file* (#359) | +| Working folder | One folder per project group that **mirrors the folder structure of the files** (`///`, the folder is stored with every file on the server), one copy of each file – the revision is metadata in `/.teamcraft/state.json` (item, revision, file id, version, SHA-256, checked out). Files you have not checked out are **read-only** on disk; *Check out* takes the lock and makes them writable; *Check in…* uploads the chosen files as new versions, releases the lock in one step and makes them read-only again (files above 32 MiB go first as chunked, resumable uploads, then join the same check-in transaction). The check-in dialog shows the progress, and **Cancel** stops the check-in until its last byte is sent – before the next STEP export, between two chunks (the upload sessions are discarded, `DELETE /uploads/{id}`) or half-way through the streamed request with the small files: nothing is stored, the revision stays checked out (`src/abortable.rs`). Every check-in names the version each file is based on, so the server refuses (409) to overwrite a version somebody else checked in meanwhile. The Workspace tab shows *up to date* / *modified* / *outdated* / *conflict* / *new* / *missing* / *name clash* per file (from the server's file status) with its path, read-only flag and the lock owner. Two items with the same folder and file name are reported, never overwritten. Lock and backup files of the CAD programs are skipped; a SolidCraft design that is open in SolidCraft (a fresh `.~.lock` beside it, `src/cadlock.rs`) is marked *Open in SolidCraft* with the computer and user, and the check-in dialog warns that unsaved changes there are not checked in; Creo iterations count as one file. A tracked revision the server no longer shows (you left a restricted project group) is listed as *No longer accessible*: its local copies stay untouched and are never synced again – delete them yourself if you must In *Check in…*, a new part or assembly file next to a revision that has a model already is offered unticked, with *New item from this file* (#359). *Update the BOM from the CAD file* in *Check in…* (#407): ticked by default every time, shown when a file of the revision may be an assembly (`js/checkin-bom.js`, tested by `scripts/desktop-checkin-bom.test.mjs`); sends `bom_from_references` with the check-in (never with *Park*) and adds the server's `bom` summary to the result | | Get | *Get…* on a revision: the item with its BOM (revision rule *latest* or *released*) and the parts its CAD files reference, resolved by the server (`…/workset`); only missing and outdated files are downloaded (SHA-256, checked after the download), recorded one by one so an interrupted Get continues where it stopped – also inside a large file: the bytes received stay in `..teamcraft-download` and the next Get asks only for the rest (`Range` / `If-Range`, `src/resume.rs`), checks the SHA-256 of the whole file and renames it into place; progress and *Cancel* in the status bar. No browser downloads | | Add files | *Add files…* (Files section and *More ▾*): file picker → the files are **copied** into the working folder of the revision (checked out first if needed; sub-folder selectable, originals untouched) and listed as *new* in *Check in…* (option *Check in now*; remembered in `.teamcraft/added.json` until checked in). A name that exists is a new version and asks first. The Files section says why it is unavailable (locked by someone else, in review, released = frozen → Revise). Pure logic and tests in `src/addfiles.rs`; drag and drop onto the window is not supported (`dragDropEnabled` is off) | | Old workspaces | Folders of the older layout `///` (and `//`) are found after the sign-in; the app offers to move the files into the new layout – never overwriting a file, keeping files whose place is taken and listing them, writing every step to `.teamcraft/migration.log` | diff --git a/desktop/src/commands.rs b/desktop/src/commands.rs index d55a5a99..14287928 100755 --- a/desktop/src/commands.rs +++ b/desktop/src/commands.rs @@ -824,7 +824,10 @@ pub async fn workspace_status( } /// Check in the chosen files (new versions) and release the lock. +/// `bom_from_references` (`bomFromReferences` in JS, off when left out): also +/// update the BOM from the CAD file on the server (#407). #[tauri::command] +#[allow(clippy::too_many_arguments)] pub async fn workspace_checkin( app: AppHandle, state: State<'_, AppState>, @@ -833,6 +836,7 @@ pub async fn workspace_checkin( code: String, files: Vec, comment: String, + bom_from_references: Option, ) -> CmdResult { usage(&state, |u| u.count("checkin")); let api = signed_in_api(&state)?; @@ -851,6 +855,7 @@ pub async fn workspace_checkin( &code, &files, &comment, + bom_from_references.unwrap_or(false), &transfer, ) .await diff --git a/desktop/src/workspace.rs b/desktop/src/workspace.rs index 8329d65c..d0448163 100755 --- a/desktop/src/workspace.rs +++ b/desktop/src/workspace.rs @@ -1064,6 +1064,9 @@ pub async fn locate( /// [`crate::api::CHUNKED_ABOVE`] are sent before in resumable chunks (see /// [`Api::send_files`]), with progress and cancel through `transfer`. /// Afterwards the files are read-only again. +/// With `bom_from_references` the server also updates the BOM of the +/// revision from the CAD references of the checked-in version, in the same +/// transaction (#407); the answer then carries `bom`. #[allow(clippy::too_many_arguments)] pub async fn check_in( api: &Api, @@ -1073,6 +1076,7 @@ pub async fn check_in( code: &str, names: &[String], comment: &str, + bom_from_references: bool, transfer: &Transfer<'_>, ) -> Result { send_local( @@ -1082,7 +1086,9 @@ pub async fn check_in( names, comment, transfer, - SendMode::CheckIn, + SendMode::CheckIn { + bom_from_references, + }, ) .await } @@ -1125,8 +1131,9 @@ pub async fn park( /// What [`send_local`] does with the lock. #[derive(Clone, Copy, PartialEq, Eq)] enum SendMode { - /// `…/checkin`: new versions and the lock released in one transaction. - CheckIn, + /// `…/checkin`: new versions and the lock released in one transaction + /// (with `bom_from_references`: the BOM updated from the CAD file, #407). + CheckIn { bom_from_references: bool }, /// `…/files`: new versions, the lock stays. Park, } @@ -1248,20 +1255,28 @@ async fn send_local( // Large files go first as resumable chunked uploads (with their base and // folder) and join this check-in by id – one transaction on the server. let endpoint = match send { - SendMode::CheckIn => "checkin", + SendMode::CheckIn { .. } => "checkin", SendMode::Park => "files", }; + let mut fields = vec![ + ("comment", comment.to_string()), + ("bases", Value::Object(bases).to_string()), + ("folders", Value::Object(folders).to_string()), + ]; + // never on a park: the revision stays checked out, nothing is final yet + if let SendMode::CheckIn { + bom_from_references: true, + } = send + { + fields.push(("bom_from_references", "true".to_string())); + } let result = api .send_files( &format!("{}/{endpoint}", rev_path(item, code)), item, code, &files, - &[ - ("comment", comment.to_string()), - ("bases", Value::Object(bases).to_string()), - ("folders", Value::Object(folders).to_string()), - ], + &fields, transfer, ) .await?; diff --git a/desktop/ui/js/checkin-bom.js b/desktop/ui/js/checkin-bom.js new file mode 100755 index 00000000..6eacbe5d --- /dev/null +++ b/desktop/ui/js/checkin-bom.js @@ -0,0 +1,52 @@ +// SPDX-License-Identifier: MPL-2.0 +// TeamCraft Desktop – *Update the BOM from the CAD file* at check-in (#407): +// the pure part (no DOM, no server calls), so it can be checked on its own. +// workspace.js (the *Check in…* dialog) uses it. +// +// The server updates the BOM from the references of the checked-in version +// (new lines added, counted quantities corrected, nothing removed). The box +// is offered only when a file of the revision can tell the structure: +// assemblies (Inventor, SOLIDWORKS, …), NX parts and SolidCraft designs (an +// assembly by their content) and STEP files (assemblies with external +// parts) – the server checks the content and leaves the BOM alone otherwise. + +/** Extensions whose files may be assemblies by content, whatever their kind. */ +export var BOM_STRUCTURE_EXTENSIONS = ["prt", "stp", "step", "sc"]; + +/** Does one of the file names lead to a BOM from the CAD file? `kinds` is + * the extension → format kind map of GET /formats. */ +export function bomStructureFile(names, kinds) { + return (names || []).some(function (name) { + var dot = String(name || "").lastIndexOf("."); + if (dot <= 0) return false; + var ext = String(name).slice(dot + 1).toLowerCase(); + // Creo iterations: bracket.asm.7 → asm + if (/^\d+$/.test(ext)) { + var base = String(name).slice(0, dot); + var inner = base.lastIndexOf("."); + if (inner <= 0) return false; + ext = base.slice(inner + 1).toLowerCase(); + } + return (kinds && kinds[ext] === "assembly") || BOM_STRUCTURE_EXTENSIONS.indexOf(ext) >= 0; + }); +} + +function plural(n, one, many) { return n + " " + (n === 1 ? one : many); } + +/** The `bom` part of the check-in answer as one line: "BOM: 4 lines added, + * 1 quantity changed · not in TeamCraft: Lager.ipt · no longer in the CAD + * file: PRT-000012 (kept)". Empty when no line was added, changed or + * refused (nothing changed: no line), and for no `bom` at all. */ +export function checkinBomText(bom) { + if (!bom) return ""; + var added = bom.added || [], updated = bom.updated || [], refused = bom.refused || []; + if (!added.length && !updated.length && !refused.length) return ""; + var changes = []; + if (added.length) changes.push(plural(added.length, "line", "lines") + " added"); + if (updated.length) changes.push(plural(updated.length, "quantity", "quantities") + " changed"); + var parts = [changes.length ? "BOM: " + changes.join(", ") : "BOM unchanged"]; + if (refused.length) parts.push("refused (circular): " + refused.map(function (r) { return r.child; }).join(", ")); + if ((bom.not_in_teamcraft || []).length) parts.push("not in TeamCraft: " + bom.not_in_teamcraft.join(", ")); + if ((bom.kept_not_in_cad || []).length) parts.push("no longer in the CAD file: " + bom.kept_not_in_cad.join(", ") + " (kept)"); + return parts.join(" · "); +} diff --git a/desktop/ui/js/workspace.js b/desktop/ui/js/workspace.js index 6818bbd1..44b1269b 100755 --- a/desktop/ui/js/workspace.js +++ b/desktop/ui/js/workspace.js @@ -16,6 +16,7 @@ import { incompleteGet, notOpenedText } from "./get-rules.js"; import { formatKinds } from "./folder-import.js"; import { isNewModelOfItsOwn, workspacePath } from "./new-files.js"; import { newItemDialog } from "./dialogs.js"; +import { bomStructureFile, checkinBomText } from "./checkin-bom.js"; // ---------------------------------------------------------------- 3D viewer @@ -480,6 +481,11 @@ export function checkinDialog(tab, park) { .filter(function (v, i, a) { return a.indexOf(v) === i; }).join(" / ") + " for the 3D preview (" + exportable.length + (exportable.length === 1 ? " file" : " files") + ") – your files are uploaded first, a slow export can be skipped"]) : null; + // #407: on by default every time (the choice is not remembered); only on + // a check-in – Park keeps the revision checked out, nothing is final yet + var withBom = h("input", { type: "checkbox", checked: true }); + var bomNote = bomStructureFile(ws.files.map(function (f) { return f.name; }), kinds) ? h("label.check", null, [withBom, + "Update the BOM from the CAD file – adds the parts the assembly uses and corrects counted quantities, never removes a line (not when parking)"]) : null; var body = h("div.stack", null, [ h("p.small.muted", null, ["From ", h("span.mono", { text: ws.dir })]), candidates.length ? h("div.stack", null, boxes.map(function (b) { @@ -498,6 +504,7 @@ export function checkinDialog(tab, park) { boxes.some(function (b) { return b.state === "conflict"; }) ? h("p.notice.error", { text: "Conflict: these files changed on the server since you got them. " + "The server refuses to overwrite the newer version (nothing is stored) – save your copy under another name, get the current files and merge." }) : null, stepNote, + bomNote, field("Comment", comment, "stored with every new version"), progressLine ]); @@ -543,11 +550,18 @@ export function checkinDialog(tab, park) { var ref = rev.item_number + "/" + rev.code; var args = { project: rev.project_code, item: rev.item_number, code: rev.code, comment: comment.value }; function show(text) { if (!cancelled) { progressLine.textContent = text; progressLine.classList.remove("hidden"); } } + var bomResult = null; // the `bom` part of the check-in answer (#407) function upload(command, names, text) { phase = "upload"; show(text); uploadWatcher = function (p) { show(uploadLine(p)); }; - return call(command, Object.assign({}, args, { files: names })).then(function (r) { uploadWatcher = null; return r; }); + var extra = { files: names }; + if (command === "workspace_checkin") extra.bomFromReferences = !!(bomNote && withBom.checked); + return call(command, Object.assign({}, args, extra)).then(function (r) { + uploadWatcher = null; + if (command === "workspace_checkin" && r) bomResult = r.bom || null; + return r; + }); } var steps = [], exportFailures = [], stored = false; // one export after the other: the CAD program handles one document at a @@ -592,8 +606,10 @@ export function checkinDialog(tab, park) { setStatus(""); d.close(); var all = files.concat(steps); + var bomLine = parking ? "" : checkinBomText(bomResult); toast((parking ? "Parked " + all.join(", ") + " on the server – " + ref + " stays checked out to you" : "Checked in " + ref + (all.length ? " with " + all.join(", ") : "")) + + (bomLine ? ". " + bomLine : "") + (cancelled ? " – the server already had every file when you cancelled" : "") + (exportFailures.length ? ". No STEP export for " + exportFailures.join("; ") : ""), cancelled || exportFailures.length ? "info" : null); diff --git a/docs/OPEN_WORK.md b/docs/OPEN_WORK.md index df153896..f6e57aa3 100755 --- a/docs/OPEN_WORK.md +++ b/docs/OPEN_WORK.md @@ -757,6 +757,9 @@ needs an issue with a reason first (see *Direction*). and *Get TeamCraft Desktop*, #412 preview at the top of the revision page, #413 no hash column, #414 revision vs version explained), #421 section nav of the revision page, #417 right-click menus in the desktop app; left: the daily CAD loop (#415, #416, #418–#420) and onboarding (#422–#424) +- [x] Check-in: *Update the BOM from the CAD file*, ticked by default (#407) – web form, desktop *Check in…*, API + field `bom_from_references` (also on chunked `finish`), in the check-in's transaction, nothing removed, the result + says what changed; left: tried with real Inventor/SOLIDWORKS check-ins from the desktop app - [ ] Responsive pages on large data (1M files, 100k items, 10k-line BOMs – see the scale targets) - [ ] Faster opening of large CAD files and assemblies - [ ] Configurable lifecycle states and site-specific allowed transitions instead of one built-in state machine diff --git a/docs/src/guide/desktop.md b/docs/src/guide/desktop.md index 6118bbfb..9c61deb3 100755 --- a/docs/src/guide/desktop.md +++ b/docs/src/guide/desktop.md @@ -80,8 +80,8 @@ check-outs and reviews, and a **local working folder**: numbering scheme), and whether items with the same number are reused, are chosen in the dialog; * *Quantities from CAD…* (next to *Import BOM…*) compares the BOM with the - instances counted in the revision's CAD files (SOLIDWORKS 2015+, NX, STEP - assemblies with external part files): missing lines and lines whose + instances counted in the revision's CAD files (SOLIDWORKS 2015+, NX, + Inventor, SolidCraft, STEP assemblies with external part files): missing lines and lines whose quantity differs – such as the quantity 1 of an assembly imported before the counts existed – are ticked; *Apply* adds and corrects them in one step (the same plan as *Create BOM from CAD references…* in the web UI). A @@ -130,6 +130,20 @@ check-outs and reviews, and a **local working folder**: `Bracket.ipt` – the same name stem makes it a candidate) is offered **unticked**, with *New item from this file*: it is usually an item of its own (#359); +* **Update the BOM from the CAD file** (#407) – a box in *Check in…*, + **ticked by default** every time (the choice is not remembered) and shown + when the revision has an assembly whose references are read (Inventor + `.iam`, SOLIDWORKS, an NX `.prt`, a STEP file, a SolidCraft `.sc`). The + server applies the plan of *Quantities from CAD…* to the version being + checked in, in the same transaction: missing children are added, a + quantity the CAD file counts (*CAD instances*) is corrected – an *assumed + 1* never replaces an existing quantity –, circular children are refused, + and **no line is removed**: lines whose part the assembly no longer uses + (glue, cable ties, bought parts) are kept and reported. The result says + what changed, e.g. *BOM: 4 lines added, 1 quantity changed · not in + TeamCraft: Lager.ipt · no longer in the CAD file: PRT-000012 (kept)*; + nothing changed – no line. Unticking it keeps the BOM as it is for that + one check-in; *Park* never changes the BOM; * the worklist in the navigator follows what you do in the tabs: checking out, cancelling a check-out, checking in or acknowledging a dependent updates it at once, while it stays open on the left; diff --git a/docs/src/guide/structure.md b/docs/src/guide/structure.md index 27d5824f..fade2267 100755 --- a/docs/src/guide/structure.md +++ b/docs/src/guide/structure.md @@ -64,7 +64,9 @@ the BOM is empty) shows what the BOM would be, one row per child: * item, name, **quantity** and where it comes from – *CAD instances* (NX: the placed instances of the component, hidden layers left out; SOLIDWORKS: - the instances in the component tree without *exclude from BOM*; STEP + the instances in the component tree without *exclude from BOM*; Inventor: + the occurrences of each part, suppressed ones left out; SolidCraft: the + placements of each linked design; STEP assemblies with external part files: the assembly occurrences of each file) or *assumed 1* (the file does not tell; the line gets the note *quantity not known from the CAD file – check it*); @@ -73,8 +75,8 @@ the BOM is empty) shows what the BOM would be, one row per child: 4 – changed only when you tick it and *Also change the quantity*), or *circular* (refused, as when adding a line by hand). -Only assembly files count (NX files only when they have component -instances, STEP files only when their occurrences lead to external part +Only assembly files count (NX files and SolidCraft designs only when they +have component instances, STEP files only when their occurrences lead to external part files); a drawing's link to its model stays a *drawing of* reference. Referenced files TeamCraft does not have are listed as *not in TeamCraft* and never created; parts in project groups you cannot see are only counted. *Add the ticked lines* needs the revision @@ -84,6 +86,39 @@ REST API has the same two steps: `GET …/bom/from-references` returns the plan, `POST …/bom/from-references` with `{"children": ["PRT-000004"], "update_quantities": false}` applies it. +### At check-in: *Update the BOM from the CAD file* + +The check-in form of the web UI and the desktop app's *Check in…* have a +box **Update the BOM from the CAD file**, ticked by default every time +(#407). It is shown when the revision has an assembly (in the web UI: an +assembly item or a revision whose CAD files lead to other items; in the +desktop app: an Inventor `.iam`, a SOLIDWORKS assembly, an NX `.prt`, a STEP +file or a SolidCraft `.sc`). With the box ticked, the plan above is applied +to the version being checked in, **in the check-in's transaction** – the +files and the BOM change together, or neither (a refused upload, 409, leaves +the BOM as it was): + +* children with the status *new* are added; +* a quantity that *differs* is corrected only where the CAD file counts it + (*CAD instances*); an *assumed 1* never replaces an existing quantity; +* *circular* children are refused, as above; +* lines whose part the CAD files no longer reference are **kept** (a BOM + carries lines without CAD geometry: glue, cable ties, bought parts) and + reported as *no longer in the CAD file*; +* referenced files TeamCraft does not have are reported as *not in + TeamCraft*, never created. + +The check-in message says what changed – *BOM: 4 lines added, 1 quantity +changed · not in TeamCraft: Lager.ipt · no longer in the CAD file: +PRT-000012 (kept)* – and says nothing when nothing changed. The permission +is that of the check-in (the holder of the check-out); the audit log gets +`bom.add` / `bom.update` per line and one `bom.from_references` with +`"source": "checkin"`. The counts are the ones the reference scan stored +with the new version. Unticking the box keeps the BOM as it is for that one +check-in; a park (upload without check-in) never changes it. On the REST API +the field `bom_from_references` is off by default, so existing clients +behave as before. + ## Comparing BOMs *Compare…* on the BOM page lists the lines that were **added**, **removed** diff --git a/docs/src/guide/vault.md b/docs/src/guide/vault.md index 4113e05a..223458b1 100755 --- a/docs/src/guide/vault.md +++ b/docs/src/guide/vault.md @@ -61,6 +61,9 @@ results, reports; CAD model, drawing, STEP). The desktop app has *Add files…* * **Check in** with files: files with the same name become the next version (Creo's iteration suffixes `bracket.prt.7` are folded into versions); identical content is recognised and not stored again. + For an assembly, *Update the BOM from the CAD file* (ticked by default) + also adds the parts it uses to the BOM in the same step – see + [Structure](structure.md#at-check-in-update-the-bom-from-the-cad-file). * Every file has a **folder** inside its project group (`chassis/brackets`; empty = the top of the group). It is the file's place in the desktop working folder, so assemblies find their parts. The folder import sets it diff --git a/docs/src/reference/api.md b/docs/src/reference/api.md index f9e87045..c07a0568 100755 --- a/docs/src/reference/api.md +++ b/docs/src/reference/api.md @@ -146,6 +146,36 @@ curl -s -X POST -H "$H" -H "Content-Type: application/json" \ $API/items/PRT-000001/revisions/A/cancel-checkout ``` +### Updating the BOM from the CAD file at check-in + +The multipart field `bom_from_references=true` on `POST …/checkin` (and +`"bom_from_references": true` next to `"check_in": true` in the body of +`POST /uploads/{id}/finish`) also updates the BOM of the revision from the +CAD references of the version being checked in, **in the check-in's +transaction** (#407; the plan of `GET …/bom/from-references`, see the +[structure guide](../guide/structure.md#at-check-in-update-the-bom-from-the-cad-file)): +new children are added, quantities that differ are corrected where the CAD +file counts them (never from an *assumed 1*), circular children are refused, +no line is removed. It is off by default, so clients that do not send it +behave as before; a refused upload (409) changes neither files nor BOM. The +answer adds `bom`: + +```sh +curl -s -X POST -H "$H" -F file=@pump.stp -F bom_from_references=true \ + $API/items/ASM-000001/revisions/A/checkin +# {"revision": {…}, "files": […], +# "bom": {"added": ["PRT-000007"], "updated": ["PRT-000004"], +# "kept_not_in_cad": ["PRT-000012"], "not_in_teamcraft": ["Lager.ipt"], +# "refused": [{"child": "ASM-000002", "reason": "Adding … would create a circular BOM structure."}]}} +``` + +`added`, `updated`, `kept_not_in_cad` (lines whose child the CAD files no +longer reference – kept) and `refused[].child` are item numbers, +`not_in_teamcraft` referenced file names. `bom` is `null` without the field +and when the revision has no file whose references describe a structure +(an assembly, an NX part or SolidCraft design with component instances, a +STEP assembly with external part files). + ### Concurrency: base versions and file status A check-in or upload can name the version each file is **based on** in the @@ -211,7 +241,7 @@ curl -s -X POST -H "$H" -H 'Content-Type: application/json' \ | `POST /uploads` | `{item, revision, filename, size, sha256?, content_type?, base?, folder?, refuse_clash?, refuse_same_name?}` → **201** with the session (`folder` = place in the project group, see below; omitted = keep the file's folder; `refuse_clash`/`refuse_same_name` = the [duplicate guard](#duplicates-of-new-files-check-and-guard), kept with the session and applied when the file is stored); 413 above `TEAMCRAFT_MAX_UPLOAD_MB`, 409 if the revision is not checked out by you, `base` is outdated or the guard refuses the new file already | | `PATCH /uploads/{id}` | header `Upload-Offset`, body = the next chunk (≤ `chunk_bytes`) → the session with the new `offset`; **409** with the server's `Upload-Offset` header at any other offset, 400 beyond `size` | | `HEAD`/`GET /uploads/{id}` | headers `Upload-Offset`, `Upload-Length`; `GET` also as JSON (`offset`, `size`, `complete`, `expires_at`) | -| `POST /uploads/{id}/finish` | body optional `{comment?, check_in?}`: verify size and SHA-256, store the new file or version (`check_in: true` also releases the check-out) → `{files, revision}`; 409 while incomplete, 400 if the content does not match (the upload is reset to offset 0) | +| `POST /uploads/{id}/finish` | body optional `{comment?, check_in?, bom_from_references?}`: verify size and SHA-256, store the new file or version (`check_in: true` also releases the check-out, with `bom_from_references: true` also updates the BOM from the CAD file) → `{files, revision, bom}`; 409 while incomplete, 400 if the content does not match (the upload is reset to offset 0) | | `DELETE /uploads/{id}` | cancel; the received data is deleted | Instead of `finish`, complete uploads can join a multipart **check-in** or @@ -475,7 +505,7 @@ the caller cannot read are left out; a successor there is a placeholder with | Uploads | `POST /uploads`, `PATCH/HEAD/GET/DELETE /uploads/{id}`, `POST /uploads/{id}/finish` – chunked, resumable uploads of large files | | BOM | `GET/POST …/bom` (`rule=latest\|released\|as_released`, `expand=true`), `PATCH/DELETE /bom-lines/{id}`, `…/package` (Pack and Go ZIP); the expanded tree has `project_id` per line (`null` for restricted placeholders) | | BOM import | `POST /bom-imports` (new assembly), `POST …/bom/import` (into a checked-out revision); multipart `file` = CSV/Excel table or STEP assembly, `dry_run=true` returns the plan (with `attribute_columns`, `unused_columns`, per item `classification` and `attributes`) | -| BOM from CAD references | `GET …/bom/from-references` (plan: children with instance-count quantities, status `new`/`already_in_bom`/`differs`/`circular`, `not_in_teamcraft`, `hidden`), `POST …/bom/from-references` `{children, update_quantities?}` (revision checked out by the caller; one transaction; `added`/`updated`/`skipped`/`refused`) | +| BOM from CAD references | `GET …/bom/from-references` (plan: children with instance-count quantities, status `new`/`already_in_bom`/`differs`/`circular`, `not_in_teamcraft`, `hidden`), `POST …/bom/from-references` `{children, update_quantities?}` (revision checked out by the caller; one transaction; `added`/`updated`/`skipped`/`refused`); at check-in: `bom_from_references` (see above) | | References | `GET/POST …/references`, `DELETE /references/{id}`, `GET /impacts`, `POST /impacts/{id}/acknowledge`, `POST …/impacts/acknowledge` | | Change requests | `GET/POST /change-requests`, `GET/PATCH /change-requests/{number}`, `comments`, `items`, `triage`, `assign`, `decide`, `reopen`, `withdraw`, `create-change`, `link-change`, `attachments`, `watch` | | Simulations | `GET/POST /projects/{code}/result-templates`, `PUT/DELETE /result-templates/{id}`, `GET …/simulation`, `GET/POST …/simulation/bases`, `DELETE /simulation-bases/{id}`, `GET/POST/PUT …/simulation/results`, `…/results/import`, `PATCH/DELETE /simulation-results/{id}`, `…/simulation/compare`, `GET /items/{number}/simulations` | diff --git a/scripts/desktop-checkin-bom.test.mjs b/scripts/desktop-checkin-bom.test.mjs new file mode 100755 index 00000000..7eb92ab8 --- /dev/null +++ b/scripts/desktop-checkin-bom.test.mjs @@ -0,0 +1,61 @@ +// SPDX-License-Identifier: MPL-2.0 +// Unit tests of desktop/ui/js/checkin-bom.js (*Update the BOM from the CAD +// file* at check-in, #407: when the box is offered, the result line). +// Plain Node.js, no packages; run by the CI job `ui`: +// +// node scripts/desktop-checkin-bom.test.mjs +// +// The module sits in a folder without package.json, so it is imported as a +// data: URL module instead of by path. +import assert from "node:assert/strict"; +import fs from "node:fs"; +import path from "node:path"; +import { fileURLToPath } from "node:url"; + +const source = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../desktop/ui/js/checkin-bom.js"); +const { bomStructureFile, checkinBomText } = + await import("data:text/javascript;base64," + fs.readFileSync(source).toString("base64")); + +let passed = 0; +function test(name, fn) { + fn(); + passed++; + console.log("ok - " + name); +} + +const kinds = { iam: "assembly", sldasm: "assembly", asm: "assembly", ipt: "part", prt: "model", stp: "exchange", pdf: "document" }; + +test("assemblies, NX parts, STEP files and SolidCraft designs offer the box", () => { + assert.equal(bomStructureFile(["Wagon.iam"], kinds), true); + assert.equal(bomStructureFile(["Pump.SLDASM"], kinds), true); + assert.equal(bomStructureFile(["frame.prt"], {}), true, "NX: an assembly by content"); + assert.equal(bomStructureFile(["pump.STEP"], {}), true); + assert.equal(bomStructureFile(["cart.sc"], {}), true); + assert.equal(bomStructureFile(["bracket.asm.7"], kinds), true, "a Creo iteration"); +}); + +test("parts, drawings and documents do not", () => { + assert.equal(bomStructureFile(["Bolt.ipt", "Bolt.pdf"], kinds), false); + assert.equal(bomStructureFile([], kinds), false); + assert.equal(bomStructureFile(["README"], kinds), false); + assert.equal(bomStructureFile(["bolt.ipt.3"], kinds), false); + assert.equal(bomStructureFile(null, kinds), false); +}); + +test("the result line says what changed", () => { + assert.equal(checkinBomText(null), ""); + assert.equal(checkinBomText({ added: [], updated: [], kept_not_in_cad: [], not_in_teamcraft: [], refused: [] }), ""); + assert.equal(checkinBomText({ + added: ["PRT-1", "PRT-2", "PRT-3", "PRT-4"], updated: ["PRT-5"], kept_not_in_cad: ["PRT-000012"], + not_in_teamcraft: ["Lager.ipt"], refused: [] + }), "BOM: 4 lines added, 1 quantity changed · not in TeamCraft: Lager.ipt · no longer in the CAD file: PRT-000012 (kept)"); +}); + +test("nothing changed: no line, even with something to report", () => { + assert.equal(checkinBomText({ added: [], updated: [], kept_not_in_cad: ["PRT-9"], not_in_teamcraft: ["x.ipt"], refused: [] }), ""); + assert.equal(checkinBomText({ added: [], updated: [], refused: [{ child: "ASM-1", reason: "circular" }] }), + "BOM unchanged · refused (circular): ASM-1"); + assert.equal(checkinBomText({ added: ["PRT-1"], updated: ["PRT-2", "PRT-3"] }), "BOM: 1 line added, 2 quantities changed"); +}); + +console.log(passed + " tests passed"); diff --git a/src/api/items_api.rs b/src/api/items_api.rs index 710fa388..71c59aaa 100755 --- a/src/api/items_api.rs +++ b/src/api/items_api.rs @@ -607,7 +607,9 @@ pub(super) async fn checkout( /// Check-in. Without a body it just releases the lock; with a /// `multipart/form-data` body (fields `file` and optional `comment`) the files -/// become new versions and the lock is released in the same transaction. +/// become new versions and the lock is released in the same transaction; the +/// field `bom_from_references=true` also updates the BOM from the CAD file +/// there (#407, answered as `bom`). pub(super) async fn checkin( State(st): State, ApiUser(me): ApiUser, @@ -642,15 +644,30 @@ pub(super) async fn checkin( &mut received, ) .await?; - let (rev, stored) = - files::check_in(&st.db, &me, rev.id, &received.uploads, &received.comment).await?; + let options = files::CheckInOptions::from_field( + received + .fields + .get("bom_from_references") + .map(String::as_str), + ); + let (rev, stored, bom) = files::check_in_with( + &st.db, + &me, + rev.id, + &received.uploads, + &received.comment, + options, + ) + .await?; uploads::forget(&st.db, &st.vault, &sessions).await; web::warm_previews(&st, &received.uploads); let changed: Vec = stored .iter() .map(|s| json!({ "file_id": s.file_id, "filename": s.filename, "version": s.version, "changed": s.changed })) .collect(); - Ok(Json(json!({ "revision": rev, "files": changed }))) + Ok(Json( + json!({ "revision": rev, "files": changed, "bom": bom }), + )) } #[derive(Deserialize)] diff --git a/src/api/mod.rs b/src/api/mod.rs index 76cafef8..6c9bc584 100755 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -571,11 +571,11 @@ async fn index() -> Json { "GET /api/v1/items/{number}/revisions/{code|latest}", "PATCH /api/v1/items/{number}/revisions/{code} {name?, description?, material?, mass_kg?, unit_cost?, attributes?: {key: value|null}}", "POST /api/v1/items/{number}/revisions/{code}/checkout", - "POST /api/v1/items/{number}/revisions/{code}/checkin optional multipart: 'file' (new versions), 'comment', 'bases' = {\"name\": version | sha256} → 409 if a file moved on, 'folder' or 'folders' = {\"name\": \"folder\"}, 'uploads' = [upload id] (chunked uploads)", + "POST /api/v1/items/{number}/revisions/{code}/checkin optional multipart: 'file' (new versions), 'comment', 'bases' = {\"name\": version | sha256} → 409 if a file moved on, 'folder' or 'folders' = {\"name\": \"folder\"}, 'uploads' = [upload id] (chunked uploads), 'bom_from_references' = true → also update the BOM from the CAD file (answer: bom)", "POST /api/v1/uploads {item, revision, filename, size, sha256?, content_type?, base?, folder?, refuse_clash?, refuse_same_name?} – start a chunked, resumable upload (checked-out revision); the refuse flags are the duplicate guard of the multipart upload, applied when the file is stored", "GET /api/v1/uploads/{id} state and offset to resume from (also HEAD: Upload-Offset, Upload-Length)", "PATCH /api/v1/uploads/{id} header Upload-Offset, body = next chunk (≤ chunk_bytes) → 409 at any other offset", - "POST /api/v1/uploads/{id}/finish {comment?, check_in?} – verify size and sha256, store the new version (and release the check-out)", + "POST /api/v1/uploads/{id}/finish {comment?, check_in?, bom_from_references?} – verify size and sha256, store the new version (and release the check-out; with bom_from_references also update the BOM)", "DELETE /api/v1/uploads/{id} cancel the upload", "GET /api/v1/items/{number}/revisions/{code}/file-status current version, sha256 and lock of every file, item_folder; released = {by_name, at, reason, message} when someone else released my check-out", "POST /api/v1/items/{number}/revisions/{code}/file-status {files: [{filename, sha256, base_version? | base_sha256?}]} → up_to_date | modified | outdated | conflict | new | missing", diff --git a/src/api/uploads_api.rs b/src/api/uploads_api.rs index fe3eaef4..c0d05057 100755 --- a/src/api/uploads_api.rs +++ b/src/api/uploads_api.rs @@ -230,6 +230,9 @@ pub(super) struct FinishBody { /// Also release the check-out (like a check-in with this one file). #[serde(default)] check_in: bool, + /// With `check_in`: update the BOM from the CAD file (#407); off by default. + #[serde(default)] + bom_from_references: bool, } pub(super) async fn finish( @@ -245,8 +248,19 @@ pub(super) async fn finish( serde_json::from_slice(&body) .map_err(|e| AppError::BadRequest(format!("Invalid JSON body: {e}")))? }; - let finished = - uploads::finish(&st.db, &st.vault, &me, id, &body.comment, body.check_in).await?; + let options = crate::services::files::CheckInOptions { + bom_from_references: body.bom_from_references, + }; + let finished = uploads::finish( + &st.db, + &st.vault, + &me, + id, + &body.comment, + body.check_in, + options, + ) + .await?; web::warm_previews(&st, std::slice::from_ref(&finished.upload)); let files: Vec = finished .stored @@ -257,7 +271,7 @@ pub(super) async fn finish( }) .collect(); Ok(Json( - json!({ "files": files, "revision": finished.revision }), + json!({ "files": files, "revision": finished.revision, "bom": finished.bom }), )) } diff --git a/src/services/bom_references.rs b/src/services/bom_references.rs index c9801345..4622db91 100755 --- a/src/services/bom_references.rs +++ b/src/services/bom_references.rs @@ -7,19 +7,20 @@ //! //! # Which references count //! -//! * Files of an **assembly** format (SOLIDWORKS `.SLDASM`, Inventor, …) and -//! NX files **that have component instances** (an NX `.prt` is a part or an -//! assembly by its content, not by its extension), and STEP assemblies -//! whose occurrences lead to **external files**. Drawings and every other -//! file do not count: a drawing's link to its model is a *drawing of* -//! reference. +//! * Files of an **assembly** format (SOLIDWORKS `.SLDASM`, Inventor, …), +//! NX files and SolidCraft designs **that have component instances** (an +//! NX `.prt` or a SolidCraft `.sc` is a part or an assembly by its content, +//! not by its extension), and STEP assemblies whose occurrences lead to +//! **external files**. Drawings and every other file do not count: a +//! drawing's link to its model is a *drawing of* reference. //! * **Quantity:** NX – the placed instances of the component (hidden layers //! left out, as in the preview); SOLIDWORKS – the instances of the //! component tree without `swExcludeFromBOM` (a component with only //! excluded instances is left out); STEP – the assembly occurrences of the //! external file; Inventor – the occurrences in the file descriptor table //! (suppressed ones left out; a part with only suppressed ones is left -//! out); otherwise 1, flagged *assumed*. +//! out); SolidCraft – the placements of each linked design; otherwise 1, +//! flagged *assumed*. //! * A child is listed once per item. Unmatched names are listed as *not in //! TeamCraft* (never created). Children in project groups the actor may not //! see are only counted. @@ -37,20 +38,29 @@ //! * **Audit:** `bom.add` per line (as `bom::add_tx`), `bom.update` per //! changed quantity and one `bom.from_references` with the counts. //! * **Atomic:** one transaction. +//! +//! # At check-in (#407) +//! +//! A check-in with *Update the BOM from the CAD file* +//! (`bom_from_references`) runs [`apply_on_checkin`] in the check-in's +//! transaction, after the new versions and their references are stored: +//! new children are added, CAD-counted quantities corrected, circular +//! children refused, nothing removed (see its invariants). use std::collections::{HashMap, HashSet}; use serde::{Deserialize, Serialize}; use serde_json::json; -use sqlx::PgPool; +use sqlx::PgConnection; use uuid::Uuid; use crate::{ auth::Actor, cadformat::FormatKind, error::{AppError, AppResult}, + models::Revision, services::{ - audit, bom, + access, audit, bom, references::{self, FileWithReferences}, revisions, }, @@ -141,6 +151,9 @@ pub struct Plan { pub hidden: i64, /// Components of the CAD files that are excluded from the BOM there. pub excluded: Vec, + /// The revision has a file whose references describe its structure. + #[serde(skip)] + pub structure: bool, } /// A child before it is compared with the BOM. @@ -174,8 +187,8 @@ pub(crate) struct Collected { fn is_structure(src: &SourceFile<'_>) -> bool { match src.file.format_kind { Some(FormatKind::Assembly) => true, - // NX: an assembly by content (it has component instances) - Some(FormatKind::Model) if src.format == "nx-part" => { + // NX, SolidCraft: an assembly by content (it has component instances) + Some(FormatKind::Model) if matches!(src.format, "nx-part" | "solidcraft-design") => { src.counts.is_some_and(|c| !c.is_empty()) } // STEP: an assembly with external references (occurrences counted) @@ -269,6 +282,32 @@ struct FileRow { format: String, } +/// Where the instance counts of a plan come from. +enum Counts<'a> { + /// Read again from the stored files (the manual flow; it also covers + /// files whose references were scanned before the counts were stored). + Read(&'a AppState), + /// The counts the reference scan stored with the references + /// (`file_references.instances`): the check-in, which never parses a + /// file while it holds the revision lock. + Stored, +} + +/// The stored instance counts of one file, by lower case name (`None`: the +/// file told none). +fn stored_counts(file: &FileWithReferences) -> Option> { + let counts: HashMap = file + .references + .iter() + .filter_map(|r| { + r.instances + .and_then(|n| u32::try_from(n).ok()) + .map(|n| (r.name.to_lowercase(), n)) + }) + .collect(); + (!counts.is_empty()).then_some(counts) +} + /// Instance counts of the components of one file, read off the async runtime. async fn read_counts( st: &AppState, @@ -285,7 +324,7 @@ async fn read_counts( /// Hidden items (project groups the actor may not see) among the names. async fn hidden_names( - pool: &PgPool, + conn: &mut PgConnection, actor: &Actor, revision_id: Uuid, names: &[String], @@ -307,7 +346,7 @@ async fn hidden_names( .bind(&keys) .bind(revision_id) .bind(&actor.hidden_projects) - .fetch_all(pool) + .fetch_all(&mut *conn) .await?) } @@ -316,21 +355,40 @@ async fn hidden_names( /// Read-only; the actor must be able to read the revision. pub async fn plan(st: &AppState, actor: &Actor, revision_id: Uuid) -> AppResult { let rev = revisions::get(&st.db, actor, revision_id).await?; - let files = references::files_of_revision(&st.db, actor, revision_id).await?; + let mut conn = st.db.acquire().await?; + plan_on(&mut conn, Counts::Read(st), actor, &rev).await +} + +/// The plan of `rev`, read on one connection: inside a transaction it sees +/// the references and BOM lines that transaction wrote. +async fn plan_on( + conn: &mut PgConnection, + counts_from: Counts<'_>, + actor: &Actor, + rev: &Revision, +) -> AppResult { + access::ensure_visible(actor, rev.project_id, access::not_found_revision())?; + let revision_id = rev.id; + let files = references::files_of_revision_on(conn, actor, revision_id).await?; let ids: Vec = files.iter().map(|f| f.file_id).collect(); let rows = sqlx::query_as::<_, FileRow>( "SELECT id, sha256, COALESCE(format, '') AS format FROM files WHERE id = ANY($1)", ) .bind(&ids) - .fetch_all(&st.db) + .fetch_all(&mut *conn) .await?; let meta: HashMap = rows.iter().map(|r| (r.id, r)).collect(); let mut counts: HashMap> = HashMap::new(); for f in &files { - if let Some(row) = meta.get(&f.file_id) { - if let Some(c) = read_counts(st, &row.sha256, &row.format).await? { - counts.insert(f.file_id, c); - } + let found = match &counts_from { + Counts::Read(st) => match meta.get(&f.file_id) { + Some(row) => read_counts(st, &row.sha256, &row.format).await?, + None => None, + }, + Counts::Stored => stored_counts(f), + }; + if let Some(c) = found { + counts.insert(f.file_id, c); } } let sources: Vec> = files @@ -341,19 +399,22 @@ pub async fn plan(st: &AppState, actor: &Actor, revision_id: Uuid) -> AppResult< counts: counts.get(&f.file_id), }) .collect(); + let structure = sources.iter().any(is_structure); let mut collected = collect(&sources); - let existing: HashMap = bom::lines(&st.db, actor, revision_id) - .await? - .into_iter() - .map(|l| (l.child_item_id, l.quantity)) - .collect(); - let mut conn = st.db.acquire().await?; + let existing: HashMap = sqlx::query_as::<_, (Uuid, f64)>( + "SELECT child_item_id, quantity FROM bom_lines WHERE parent_revision_id = $1", + ) + .bind(revision_id) + .fetch_all(&mut *conn) + .await? + .into_iter() + .collect(); let mut children = Vec::new(); for c in collected.children { let bom_quantity = existing.get(&c.item_id).copied(); let circular = - bom_quantity.is_none() && bom::creates_cycle(&mut conn, rev.item_id, c.item_id).await?; + bom_quantity.is_none() && bom::creates_cycle(conn, rev.item_id, c.item_id).await?; let status = status_of(bom_quantity, c.quantity, circular); children.push(PlannedChild { item_id: c.item_id, @@ -371,11 +432,10 @@ pub async fn plan(st: &AppState, actor: &Actor, revision_id: Uuid) -> AppResult< selected: status == ChildStatus::New, }); } - drop(conn); // names no visible item has: some may belong to hidden items let names: Vec = collected.unmatched.iter().map(|u| u.name.clone()).collect(); - let hidden = hidden_names(&st.db, actor, revision_id, &names).await?; + let hidden = hidden_names(conn, actor, revision_id, &names).await?; let hidden_keys: HashSet<&str> = hidden.iter().map(|(k, _)| k.as_str()).collect(); collected .unmatched @@ -387,6 +447,7 @@ pub async fn plan(st: &AppState, actor: &Actor, revision_id: Uuid) -> AppResult< not_in_teamcraft: collected.unmatched, hidden: hidden_items.len() as i64, excluded: collected.excluded, + structure, }) } @@ -400,7 +461,7 @@ pub struct ApplyRequest { pub update_quantities: bool, } -#[derive(Clone, Debug, Default, Serialize)] +#[derive(Clone, Debug, Default, Serialize, PartialEq, Eq)] pub struct Refused { pub child: String, pub reason: String, @@ -453,6 +514,46 @@ pub async fn apply( let mut tx = st.db.begin().await?; let parent = revisions::lock(&mut tx, revision_id).await?; revisions::ensure_editable(&parent, actor)?; + let result = + apply_children(&mut tx, actor, &parent, &chosen, |_| req.update_quantities).await?; + audit::record_related( + &mut tx, + Some(actor), + "bom.from_references", + "revision", + Some(parent.id), + None, + format!( + "Created BOM lines of {} from the CAD references: {} added, {} updated", + revisions::label(&parent), + result.added.len(), + result.updated.len() + ), + json!({ + "item_id": parent.item_id, + "source": "cad_references", + "added": result.added.len(), + "updated": result.updated.len(), + "skipped": result.skipped.len(), + "refused": result.refused.len(), + }), + ) + .await?; + tx.commit().await?; + Ok(result) +} + +/// The shared step of [`apply`] and [`apply_on_checkin`], on the locked +/// parent: adds the chosen children that have no line yet, changes the +/// quantity of a differing line where `update` says so, refuses a child that +/// would make the BOM circular. Never removes a line. +async fn apply_children( + tx: &mut PgConnection, + actor: &Actor, + parent: &Revision, + chosen: &[&PlannedChild], + update: impl Fn(&PlannedChild) -> bool, +) -> AppResult { // decided under the lock: another session may have added lines meanwhile let existing: HashMap = sqlx::query_as::<_, (Uuid, Uuid, f64)>( "SELECT child_item_id, id, quantity FROM bom_lines WHERE parent_revision_id = $1", @@ -464,7 +565,7 @@ pub async fn apply( .map(|(child, id, q)| (child, (id, q))) .collect(); let mut result = ApplyResult::default(); - for child in chosen { + for child in chosen.iter().copied() { let candidate = Candidate { item_id: child.item_id, item_number: child.item_number.clone(), @@ -474,15 +575,15 @@ pub async fn apply( via: child.via.clone(), }; match existing.get(&child.item_id) { - Some((line_id, q)) if (q - child.quantity).abs() >= 1e-9 && req.update_quantities => { + Some((line_id, q)) if (q - child.quantity).abs() >= 1e-9 && update(child) => { sqlx::query("UPDATE bom_lines SET quantity = $2 WHERE id = $1") .bind(line_id) .bind(child.quantity) .execute(&mut *tx) .await?; - bom::touch(&mut tx, parent.id).await?; + bom::touch(&mut *tx, parent.id).await?; audit::record_related( - &mut tx, + &mut *tx, Some(actor), "bom.update", "revision", @@ -491,7 +592,7 @@ pub async fn apply( format!( "Changed the quantity of {} in {} from {} to {} (CAD references)", child.item_number, - revisions::label(&parent), + revisions::label(parent), util::format_number(*q, 4), util::format_number(child.quantity, 4) ), @@ -503,9 +604,9 @@ pub async fn apply( Some(_) => result.skipped.push(child.item_number.clone()), None => { match bom::add_tx( - &mut tx, + &mut *tx, actor, - &parent, + parent, child.item_id, &child.item_number, child.quantity, @@ -526,33 +627,188 @@ pub async fn apply( } } } - audit::record_related( - &mut tx, - Some(actor), - "bom.from_references", - "revision", - Some(parent.id), - None, - format!( - "Created BOM lines of {} from the CAD references: {} added, {} updated", - revisions::label(&parent), - result.added.len(), - result.updated.len() - ), - json!({ - "item_id": parent.item_id, - "source": "cad_references", - "added": result.added.len(), - "updated": result.updated.len(), - "skipped": result.skipped.len(), - "refused": result.refused.len(), - }), - ) - .await?; - tx.commit().await?; Ok(result) } +/// What the BOM step of a check-in changed (#407). Item numbers, except +/// `not_in_teamcraft` (referenced file names). +#[derive(Clone, Debug, Default, Serialize, PartialEq, Eq)] +pub struct CheckinBom { + /// New lines (children with the status *new*). + pub added: Vec, + /// Lines whose quantity was corrected to the counted CAD instances. + pub updated: Vec, + /// Lines whose child the CAD files no longer reference – kept. + pub kept_not_in_cad: Vec, + /// Referenced files TeamCraft does not have – never created. + pub not_in_teamcraft: Vec, + /// Children refused because they would make the BOM circular. + pub refused: Vec, +} + +impl CheckinBom { + /// Nothing changed and nothing to report. + pub fn is_empty(&self) -> bool { + self.added.is_empty() + && self.updated.is_empty() + && self.kept_not_in_cad.is_empty() + && self.not_in_teamcraft.is_empty() + && self.refused.is_empty() + } + + /// One line for the check-in result: `BOM: 4 lines added, 1 quantity + /// changed · not in TeamCraft: Lager.ipt · no longer in the CAD file: + /// PRT-000012 (kept)`; `None` when no line was added, changed or refused + /// (nothing changed: no line, even with lines kept or files missing). + pub fn summary(&self) -> Option { + if self.added.is_empty() && self.updated.is_empty() && self.refused.is_empty() { + return None; + } + fn plural(n: usize, one: &str, many: &str) -> String { + format!("{n} {}", if n == 1 { one } else { many }) + } + let mut changes = Vec::new(); + if !self.added.is_empty() { + changes.push(format!( + "{} added", + plural(self.added.len(), "line", "lines") + )); + } + if !self.updated.is_empty() { + changes.push(format!( + "{} changed", + plural(self.updated.len(), "quantity", "quantities") + )); + } + let mut parts = vec![if changes.is_empty() { + "BOM unchanged".to_string() + } else { + format!("BOM: {}", changes.join(", ")) + }]; + if !self.refused.is_empty() { + let names: Vec<&str> = self.refused.iter().map(|r| r.child.as_str()).collect(); + parts.push(format!("refused (circular): {}", names.join(", "))); + } + if !self.not_in_teamcraft.is_empty() { + parts.push(format!( + "not in TeamCraft: {}", + self.not_in_teamcraft.join(", ") + )); + } + if !self.kept_not_in_cad.is_empty() { + parts.push(format!( + "no longer in the CAD file: {} (kept)", + self.kept_not_in_cad.join(", ") + )); + } + Some(parts.join(" · ")) + } +} + +/// The visible children of existing lines that are not among `children` +/// (item numbers, sorted). +async fn lines_not_in( + conn: &mut PgConnection, + actor: &Actor, + parent_revision_id: Uuid, + children: &[Uuid], +) -> AppResult> { + Ok(sqlx::query_scalar::<_, String>(&format!( + "SELECT DISTINCT i.number + FROM bom_lines bl + JOIN items i ON i.id = bl.child_item_id + WHERE bl.parent_revision_id = $1 AND NOT (bl.child_item_id = ANY($2)) + AND {} + ORDER BY i.number", + access::item_visible("i", 3) + )) + .bind(parent_revision_id) + .bind(children) + .bind(&actor.hidden_projects) + .fetch_all(&mut *conn) + .await?) +} + +/// The BOM step of a check-in with *Update the BOM from the CAD file* (#407): +/// the plan of the version being checked in, applied inside the check-in's +/// transaction (`files::check_in_with`). +/// +/// # Invariants +/// +/// * **Permission:** that of the check-in – `parent` is the revision row the +/// caller locked, checked out by the actor and editable +/// (`revisions::ensure_editable`). +/// * **Counts:** the instance counts the reference scan stored with the +/// references of the new versions (no file is parsed under the lock). +/// * **Lines:** children with the status *new* are added; the quantity of a +/// differing line is changed only when the CAD file counts it (*CAD +/// instances*, never an *assumed 1*); a circular child is refused; no line +/// is ever removed – lines whose child is no longer referenced are reported +/// as `kept_not_in_cad`, referenced files TeamCraft does not have as +/// `not_in_teamcraft`. +/// * **No structure:** a revision without a structure file (assembly, NX or +/// STEP assembly with counts) is left alone: `None`. +/// * **Audit:** `bom.add` / `bom.update` per line and one +/// `bom.from_references` with `"source": "checkin"`, only when a line was +/// added or changed. +/// * **Atomic:** runs in the caller's transaction. +pub(crate) async fn apply_on_checkin( + tx: &mut PgConnection, + actor: &Actor, + parent: &Revision, +) -> AppResult> { + revisions::ensure_editable(parent, actor)?; + let plan = plan_on(&mut *tx, Counts::Stored, actor, parent).await?; + if !plan.structure { + return Ok(None); + } + let chosen: Vec<&PlannedChild> = plan + .children + .iter() + .filter(|c| c.status != ChildStatus::AlreadyInBom) + .collect(); + let applied = apply_children(&mut *tx, actor, parent, &chosen, |c| { + c.quantity_source == QuantitySource::CadInstances + }) + .await?; + let referenced: Vec = plan.children.iter().map(|c| c.item_id).collect(); + let kept_not_in_cad = lines_not_in(&mut *tx, actor, parent.id, &referenced).await?; + let result = CheckinBom { + added: applied.added, + updated: applied.updated, + kept_not_in_cad, + not_in_teamcraft: plan.not_in_teamcraft.into_iter().map(|u| u.name).collect(), + refused: applied.refused, + }; + if !result.added.is_empty() || !result.updated.is_empty() { + audit::record_related( + &mut *tx, + Some(actor), + "bom.from_references", + "revision", + Some(parent.id), + None, + format!( + "Updated the BOM of {} from the CAD file at check-in: {} added, {} updated", + revisions::label(parent), + result.added.len(), + result.updated.len() + ), + json!({ + "item_id": parent.item_id, + "source": "checkin", + "added": result.added.len(), + "updated": result.updated.len(), + "kept_not_in_cad": result.kept_not_in_cad.len(), + "not_in_teamcraft": result.not_in_teamcraft.len(), + "refused": result.refused.len(), + }), + ) + .await?; + } + Ok(Some(result)) +} + /// Number of matched, not yet used structure children (for the hint on the /// revision page): children found in the CAD references with the status new. pub fn new_children(plan: &Plan) -> usize { @@ -716,6 +972,83 @@ mod tests { assert_eq!(c.children[0].source, QuantitySource::CadInstances); } + #[test] + fn solidcraft_designs_count_only_with_placements() { + let design = file( + "frame.sc", + FormatKind::Model, + vec![("wheel.sc", vec![m(1, "PRT-1", "wheel.sc", false)])], + ); + let source = |counts| SourceFile { + file: &design, + format: "solidcraft-design", + counts, + }; + assert!(collect(&[source(None)]).children.is_empty()); + let counts: HashMap = [("wheel.sc".to_string(), 2)].into(); + let c = collect(&[source(Some(&counts))]); + assert_eq!(c.children.len(), 1); + assert_eq!(c.children[0].quantity, 2.0); + } + + #[test] + fn stored_counts_come_from_the_references() { + let mut asm = file( + "A.iam", + FormatKind::Assembly, + vec![ + ("Bolt.ipt", vec![m(1, "PRT-1", "Bolt.ipt", false)]), + ("glue.ipt", vec![]), + ], + ); + assert_eq!(stored_counts(&asm), None, "no counts stored: none known"); + asm.references[0].instances = Some(4); + asm.references[1].instances = Some(0); + let counts = stored_counts(&asm).unwrap(); + assert_eq!(counts.get("bolt.ipt"), Some(&4)); + assert_eq!(counts.get("glue.ipt"), Some(&0)); + } + + #[test] + fn the_checkin_summary_says_what_changed() { + assert_eq!(CheckinBom::default().summary(), None); + let bom = CheckinBom { + added: vec![ + "PRT-1".into(), + "PRT-2".into(), + "PRT-3".into(), + "PRT-4".into(), + ], + updated: vec!["PRT-5".into()], + kept_not_in_cad: vec!["PRT-000012".into()], + not_in_teamcraft: vec!["Lager.ipt".into()], + refused: Vec::new(), + }; + assert_eq!( + bom.summary().unwrap(), + "BOM: 4 lines added, 1 quantity changed · not in TeamCraft: Lager.ipt · \ + no longer in the CAD file: PRT-000012 (kept)" + ); + // nothing changed: no line, even with something to report + let only_report = CheckinBom { + not_in_teamcraft: vec!["Lager.ipt".into()], + kept_not_in_cad: vec!["PRT-000012".into()], + ..CheckinBom::default() + }; + assert_eq!(only_report.summary(), None); + let refused = CheckinBom { + refused: vec![Refused { + child: "ASM-1".into(), + reason: "circular".into(), + }], + ..CheckinBom::default() + }; + assert_eq!( + refused.summary().unwrap(), + "BOM unchanged · refused (circular): ASM-1" + ); + } + #[test] fn status_compares_with_the_bom() { assert_eq!(status_of(None, 2.0, false), ChildStatus::New); diff --git a/src/services/files.rs b/src/services/files.rs index ff4768e1..a51fa213 100755 --- a/src/services/files.rs +++ b/src/services/files.rs @@ -21,7 +21,7 @@ use crate::{ error::{AppError, AppResult}, models::{FileRecord, FileVersion, Revision}, services::{ - access, audit, + access, audit, bom_references, file_check::{self, ClashGuard}, references, revisions, }, @@ -1286,6 +1286,9 @@ pub async fn attach( /// * **Webhooks:** none. /// * **Atomic:** one transaction (`tests/atomic_processes.rs`); two check-ins /// at once give one winner and a 409. +/// +/// The BOM is never touched; [`check_in_with`] can update it from the CAD +/// file in the same transaction. pub async fn check_in( pool: &PgPool, actor: &Actor, @@ -1293,6 +1296,64 @@ pub async fn check_in( uploads: &[Upload], comment: &str, ) -> AppResult<(Revision, Vec)> { + let (rev, stored, _) = check_in_with( + pool, + actor, + revision_id, + uploads, + comment, + CheckInOptions::default(), + ) + .await?; + Ok((rev, stored)) +} + +/// Options of a check-in ([`check_in_with`]). +#[derive(Clone, Copy, Debug, Default)] +pub struct CheckInOptions { + /// *Update the BOM from the CAD file* (#407): off by default on the API. + pub bom_from_references: bool, +} + +impl CheckInOptions { + /// From the text field `bom_from_references` of a multipart check-in + /// (`true`, `1`, `on`, `yes`; anything else or no field: off). + pub fn from_field(value: Option<&str>) -> CheckInOptions { + CheckInOptions { + bom_from_references: value.is_some_and(|v| { + matches!( + v.trim().to_ascii_lowercase().as_str(), + "true" | "1" | "on" | "yes" + ) + }), + } + } +} + +/// [`check_in`] with options: with `bom_from_references` the BOM of the +/// revision is updated from the CAD references of the checked-in version +/// (`bom_references::apply_on_checkin`) – `None` when that was not asked for +/// or the revision has no structure file. +/// +/// # Invariants +/// +/// As [`check_in`], and with `bom_from_references`: +/// +/// * **Permission:** the same as the check-in (the holder of the check-out, +/// `revisions::ensure_editable`). +/// * **Audit:** in addition `bom.add` / `bom.update` per changed line and one +/// `bom.from_references` (`"source": "checkin"`) when a line changed. +/// * **Atomic:** the BOM step runs in the check-in's transaction after the +/// new versions and their references are stored: a refused upload (409) +/// leaves the BOM as it was, a failing BOM step stores no file. +pub async fn check_in_with( + pool: &PgPool, + actor: &Actor, + revision_id: Uuid, + uploads: &[Upload], + comment: &str, + options: CheckInOptions, +) -> AppResult<(Revision, Vec, Option)> { let comment = validate_comment(comment)?; let mut tx = pool.begin().await?; let rev = revisions::lock(&mut tx, revision_id).await?; @@ -1305,6 +1366,13 @@ pub async fn check_in( } revisions::ensure_editable(&rev, actor)?; let stored = store_uploads(&mut tx, actor, &rev, uploads, &comment).await?; + // the references of the new versions are stored: the BOM follows them + // in the same transaction (#407) + let bom = if options.bom_from_references { + bom_references::apply_on_checkin(&mut tx, actor, &rev).await? + } else { + None + }; sqlx::query("UPDATE revisions SET checked_out_by = NULL, checked_out_at = NULL WHERE id = $1") .bind(rev.id) .execute(&mut *tx) @@ -1353,7 +1421,7 @@ pub async fn check_in( } tx.commit().await?; let rev = revisions::get(pool, actor, revision_id).await?; - Ok((rev, stored)) + Ok((rev, stored, bom)) } /// Rollback: makes the content of an older version the newest version. diff --git a/src/services/references.rs b/src/services/references.rs index c558e29f..5f925e2c 100755 --- a/src/services/references.rs +++ b/src/services/references.rs @@ -315,6 +315,17 @@ pub async fn files_of_revision( pool: &PgPool, actor: &Actor, revision_id: Uuid, +) -> AppResult> { + let mut conn = pool.acquire().await?; + files_of_revision_on(&mut conn, actor, revision_id).await +} + +/// [`files_of_revision`] on a connection – inside a transaction it sees the +/// references the transaction stored (the check-in's BOM step, #407). +pub(crate) async fn files_of_revision_on( + conn: &mut PgConnection, + actor: &Actor, + revision_id: Uuid, ) -> AppResult> { #[derive(sqlx::FromRow)] struct Row { @@ -334,7 +345,7 @@ pub async fn files_of_revision( ORDER BY lower(f.filename), lower(fr.name)", ) .bind(revision_id) - .fetch_all(pool) + .fetch_all(&mut *conn) .await?; if rows.is_empty() { return Ok(Vec::new()); @@ -361,7 +372,7 @@ pub async fn files_of_revision( .bind(&keys) .bind(revision_id) .bind(&actor.hidden_projects) - .fetch_all(pool) + .fetch_all(&mut *conn) .await?; let mut by_key: HashMap> = HashMap::new(); for m in found { @@ -392,7 +403,7 @@ pub async fn files_of_revision( .bind(&unmatched) .bind(revision_id) .bind(&actor.hidden_projects) - .fetch_all(pool) + .fetch_all(&mut *conn) .await?; for m in weak { by_key.entry(m.key.clone()).or_default().push(m); diff --git a/src/services/uploads.rs b/src/services/uploads.rs index 57b93954..e416d306 100755 --- a/src/services/uploads.rs +++ b/src/services/uploads.rs @@ -132,6 +132,8 @@ pub struct Finished { pub stored: Vec, pub upload: files::Upload, pub revision: Option, + /// What the check-in changed in the BOM (`bom_from_references`, #407). + pub bom: Option, } /// Checks a chunk against the session state and returns the new offset. @@ -645,6 +647,9 @@ pub async fn forget(pool: &PgPool, vault: &Vault, ids: &[Uuid]) { /// storing succeeded. /// * **Audit, notifications, webhooks:** those of [`files::check_in`] / /// [`files::attach`] (`file.upload` / `file.version`, `revision.checkin`). +/// * **BOM:** only with `check_in` and `options.bom_from_references`, in the +/// check-in's transaction ([`files::check_in_with`], #407); never without +/// `check_in`. pub async fn finish( pool: &PgPool, vault: &Vault, @@ -652,6 +657,7 @@ pub async fn finish( id: Uuid, comment: &str, check_in: bool, + options: files::CheckInOptions, ) -> AppResult { let session = get(pool, actor, id).await?; let revision_id = session.revision_id.ok_or_else(|| { @@ -659,18 +665,21 @@ pub async fn finish( })?; let upload = complete(pool, vault, actor, id, None).await?; let uploads = std::slice::from_ref(&upload); - let (stored, revision) = if check_in { - let (rev, stored) = files::check_in(pool, actor, revision_id, uploads, comment).await?; - (stored, Some(rev)) + let (stored, revision, bom) = if check_in { + let (rev, stored, bom) = + files::check_in_with(pool, actor, revision_id, uploads, comment, options).await?; + (stored, Some(rev), bom) } else { + // an upload without check-in (park) never touches the BOM let stored = files::attach(pool, actor, revision_id, uploads, comment).await?; - (stored, None) + (stored, None, None) }; forget(pool, vault, &[id]).await; Ok(Finished { stored, upload, revision, + bom, }) } diff --git a/src/web/revisions.rs b/src/web/revisions.rs index e0777447..03008eb8 100755 --- a/src/web/revisions.rs +++ b/src/web/revisions.rs @@ -723,8 +723,21 @@ pub async fn checkin( // Large files sent before in chunks by app.js (field `uploads`). let sessions = take_session_uploads(&st, &ctx.me, uploads::Scope::Revision(id), &mut received).await?; - let result = - files::check_in(&st.db, &ctx.me, id, &received.uploads, &received.comment).await; + let options = files::CheckInOptions::from_field( + received + .fields + .get("bom_from_references") + .map(String::as_str), + ); + let result = files::check_in_with( + &st.db, + &ctx.me, + id, + &received.uploads, + &received.comment, + options, + ) + .await; if result.is_ok() { uploads::forget(&st.db, &st.vault, &sessions).await; warm_previews(&st, &received.uploads); @@ -732,7 +745,7 @@ pub async fn checkin( result } .await; - finish(&st, result, &back, |(rev, stored)| { + finish(&st, result, &back, |(rev, stored, bom)| { let changed: Vec = stored .iter() .filter(|s| s.changed) @@ -747,6 +760,11 @@ pub async fn checkin( changed.join(", ") ) }; + // what the BOM step changed (#407); nothing changed: no line + let message = match bom.as_ref().and_then(|b| b.summary()) { + Some(summary) => format!("{message} · {summary}"), + None => message, + }; (back.clone(), message) }) } diff --git a/templates/revisions/show.html b/templates/revisions/show.html index 72955f67..e6f46504 100755 --- a/templates/revisions/show.html +++ b/templates/revisions/show.html @@ -146,6 +146,10 @@

Check in {{ rev.item_number }}/{{ rev.code }}

+ {% if cad_structure or rev.item_type == "assembly" %} + + {% endif %}
diff --git a/tests/checkin_bom.rs b/tests/checkin_bom.rs new file mode 100755 index 00000000..6d8aafe8 --- /dev/null +++ b/tests/checkin_bom.rs @@ -0,0 +1,570 @@ +//! *Update the BOM from the CAD file* at check-in (#407): the field +//! `bom_from_references` of `POST …/checkin` (and of `check_in` on +//! `POST /uploads/{id}/finish`) applies the plan of `…/bom/from-references` +//! to the version being checked in, in the check-in's transaction – new +//! children added, CAD-counted quantities corrected, an *assumed 1* never +//! applied over a line, unreferenced lines kept and reported, missing files +//! reported, circular children refused. Without the field, on a park and on +//! a refused upload the BOM stays as it was. + +mod common; + +use axum::{ + body::Body, + http::{header, Method, Request, StatusCode}, +}; +use serde_json::{json, Value}; +use teamcraft::domain::Role; +use uuid::Uuid; + +macro_rules! app_or_skip { + () => { + match common::setup().await { + Some(app) => app, + None => return, + } + }; +} + +/// A STEP assembly `name` that places each external file `count` times +/// (the counts are stored with its references, `cadrefs::instance_counts`). +fn step_assembly(name: &str, children: &[(&str, u32)]) -> Vec { + assert!(children.len() <= 8, "product ids stay below the usage ids"); + let product = |n: usize| { + format!( + "#{n}1=PRODUCT('P{n}','P{n}','',(#9));\n\ + #{n}2=PRODUCT_DEFINITION_FORMATION('','',#{n}1);\n\ + #{n}3=PRODUCT_DEFINITION('design','',#{n}2,#8);\n" + ) + }; + // 1 = the assembly, 2.. = the external files + let mut data = (1..=children.len() + 1).map(product).collect::(); + let mut usage = 100; + for (i, (file, count)) in children.iter().enumerate() { + let k = i + 2; + let doc = 900 + 10 * i; + data += &format!( + "#{doc}=DOCUMENT_FILE('{file}','',$,#1,'','');\n\ + #{}=APPLIED_DOCUMENT_REFERENCE(#{doc},'',(#{k}3));\n", + doc + 1 + ); + for _ in 0..*count { + data += + &format!("#{usage}=NEXT_ASSEMBLY_USAGE_OCCURRENCE('{usage}','','',#13,#{k}3,$);\n"); + usage += 1; + } + } + format!( + "ISO-10303-21;\nHEADER;\nFILE_DESCRIPTION((''),'2;1');\n\ + FILE_NAME('{name}','2026-11-11T10:00:00',(''),(''),'','','');\n\ + FILE_SCHEMA(('AUTOMOTIVE_DESIGN'));\nENDSEC;\nDATA;\n{data}ENDSEC;\nEND-ISO-10303-21;\n" + ) + .into_bytes() +} + +fn utf16(s: &str) -> Vec { + s.encode_utf16().flat_map(u16::to_le_bytes).collect() +} + +/// A SOLIDWORKS assembly file (old format: file names as UTF-16 strings, no +/// instance counts – every quantity is an *assumed 1*). +fn sldasm(names: &[&str]) -> Vec { + let mut out = b"\x00\x01".to_vec(); + for name in names { + out.extend(utf16(&format!("C:\\Lib\\{name}"))); + out.extend([0, 0, 7, 0]); + } + out +} + +async fn item(app: &common::TestApp, token: &str, kind: &str) -> String { + app.create_item( + token, + json!({ "item_type": kind, "name": format!("{kind} in ROCKET"), "project": "ROCKET" }), + ) + .await +} + +/// `POST …/checkin` with files and text fields. +async fn checkin( + app: &common::TestApp, + token: &str, + number: &str, + files: &[(&str, &[u8])], + fields: &[(&str, &str)], +) -> common::Response { + app.api_multipart( + &format!("/api/v1/items/{number}/revisions/A/checkin"), + token, + files, + fields, + ) + .await +} + +/// The first check-in of a new item (checked out to its author), without +/// the BOM field. +async fn first(app: &common::TestApp, token: &str, number: &str, filename: &str, bytes: &[u8]) { + let r = checkin(app, token, number, &[(filename, bytes)], &[]).await; + assert_eq!(r.status, StatusCode::OK, "{filename}: {}", r.text()); + assert!( + r.json()["bom"].is_null(), + "no field, no BOM step: {}", + r.text() + ); +} + +async fn checkout(app: &common::TestApp, token: &str, number: &str) { + let r = app + .api( + Method::POST, + &format!("/api/v1/items/{number}/revisions/A/checkout"), + token, + None, + ) + .await; + assert_eq!(r.status, StatusCode::OK, "{}", r.text()); +} + +async fn add_line(app: &common::TestApp, token: &str, parent: &str, child: &str, quantity: f64) { + let r = app + .api( + Method::POST, + &format!("/api/v1/items/{parent}/revisions/A/bom"), + token, + Some(json!({ "child": child, "quantity": quantity })), + ) + .await; + assert_eq!(r.status, StatusCode::CREATED, "{}", r.text()); +} + +/// The BOM as `child number → quantity`, sorted by number. +async fn bom(app: &common::TestApp, token: &str, number: &str) -> Vec<(String, f64)> { + let r = app + .api( + Method::GET, + &format!("/api/v1/items/{number}/revisions/A/bom"), + token, + None, + ) + .await; + assert_eq!(r.status, StatusCode::OK, "{}", r.text()); + let mut lines: Vec<(String, f64)> = r + .json() + .as_array() + .unwrap() + .iter() + .map(|l| { + ( + l["child_number"].as_str().unwrap().to_string(), + l["quantity"].as_f64().unwrap(), + ) + }) + .collect(); + lines.sort_by(|a, b| a.0.cmp(&b.0)); + lines +} + +async fn revision_id(app: &common::TestApp, number: &str) -> Uuid { + sqlx::query_scalar( + "SELECT r.id FROM revisions r JOIN items i ON i.id = r.item_id + WHERE i.number = $1 AND r.code = 'A'", + ) + .bind(number) + .fetch_one(&app.pool) + .await + .unwrap() +} + +async fn checked_out(app: &common::TestApp, number: &str) -> bool { + sqlx::query_scalar( + "SELECT r.checked_out_by IS NOT NULL FROM revisions r JOIN items i ON i.id = r.item_id + WHERE i.number = $1 AND r.code = 'A'", + ) + .bind(number) + .fetch_one(&app.pool) + .await + .unwrap() +} + +/// The `details` of the `bom.from_references` audit entries of a revision. +async fn bom_audits(app: &common::TestApp, revision: Uuid) -> Vec { + sqlx::query_scalar( + "SELECT details FROM audit_log + WHERE action = 'bom.from_references' AND entity_id = $1 ORDER BY id", + ) + .bind(revision) + .fetch_all(&app.pool) + .await + .unwrap() +} + +fn strings(v: &Value) -> Vec { + v.as_array() + .unwrap_or_else(|| panic!("not a list: {v}")) + .iter() + .map(|s| s.as_str().unwrap().to_string()) + .collect() +} + +/// New children added, a differing CAD count corrected, an unreferenced line +/// kept and reported, a missing file reported – in the check-in's +/// transaction, with one `bom.from_references` audit entry. +#[tokio::test] +async fn a_checkin_with_the_box_updates_the_bom_from_the_cad_file() { + let app = app_or_skip!(); + let (_, eng) = app.user_in("eng", Role::Editor, &["ROCKET"]).await; + let bolt = item(&app, &eng, "part").await; + first(&app, &eng, &bolt, "bolt.stp", b"ISO-10303-21;\n").await; + let nut = item(&app, &eng, "part").await; + first(&app, &eng, &nut, "nut.stp", b"ISO-10303-21;\n").await; + // glue has no CAD geometry: a BOM line only + let glue = item(&app, &eng, "part").await; + first(&app, &eng, &glue, "glue.txt", b"2K epoxy").await; + + // version 1 places the bolt once; checked in without the field: no BOM + let asm = item(&app, &eng, "assembly").await; + first( + &app, + &eng, + &asm, + "pump.stp", + &step_assembly("pump.stp", &[("bolt.stp", 1)]), + ) + .await; + assert!(bom(&app, &eng, &asm).await.is_empty(), "API default: off"); + + checkout(&app, &eng, &asm).await; + add_line(&app, &eng, &asm, &bolt, 1.0).await; + add_line(&app, &eng, &asm, &glue, 2.0).await; + // version 2: four bolts, two nuts and a file TeamCraft does not have + let v2 = step_assembly( + "pump.stp", + &[("bolt.stp", 4), ("nut.stp", 2), ("ghost.stp", 1)], + ); + let r = checkin( + &app, + &eng, + &asm, + &[("pump.stp", v2.as_slice())], + &[("comment", "more bolts"), ("bom_from_references", "true")], + ) + .await; + assert_eq!(r.status, StatusCode::OK, "{}", r.text()); + let body = r.json(); + let result = &body["bom"]; + assert_eq!(strings(&result["added"]), [nut.clone()], "{body}"); + assert_eq!(strings(&result["updated"]), [bolt.clone()], "{body}"); + assert_eq!( + strings(&result["kept_not_in_cad"]), + [glue.clone()], + "{body}" + ); + assert_eq!( + strings(&result["not_in_teamcraft"]), + ["ghost.stp"], + "{body}" + ); + assert_eq!(result["refused"], json!([]), "{body}"); + assert!(body["revision"]["checked_out_by"].is_null(), "{body}"); + + let mut expected = vec![(bolt.clone(), 4.0), (glue.clone(), 2.0), (nut.clone(), 2.0)]; + expected.sort_by(|a, b| a.0.cmp(&b.0)); + assert_eq!(bom(&app, &eng, &asm).await, expected, "glue is kept"); + + let rev = revision_id(&app, &asm).await; + let audits = bom_audits(&app, rev).await; + assert_eq!(audits.len(), 1, "{audits:?}"); + assert_eq!(audits[0]["source"], "checkin"); + assert_eq!(audits[0]["added"], 1); + assert_eq!(audits[0]["updated"], 1); + let actions = app.audit_actions(rev).await; + assert!(actions.contains(&"bom.update".to_string()), "{actions:?}"); + assert!( + actions.contains(&"revision.checkin".to_string()), + "{actions:?}" + ); + + // the same check-in again changes nothing: no further audit entry + checkout(&app, &eng, &asm).await; + let r = checkin(&app, &eng, &asm, &[], &[("bom_from_references", "true")]).await; + assert_eq!(r.status, StatusCode::OK, "{}", r.text()); + let again = r.json(); + assert_eq!(again["bom"]["added"], json!([]), "{again}"); + assert_eq!(again["bom"]["updated"], json!([]), "{again}"); + assert_eq!(strings(&again["bom"]["kept_not_in_cad"]), [glue.clone()]); + assert_eq!(bom_audits(&app, rev).await.len(), 1); + app.teardown().await; +} + +/// An *assumed 1* (a file without instance counts) adds a line, but never +/// replaces the quantity of an existing one. +#[tokio::test] +async fn an_assumed_quantity_never_overwrites_an_existing_line() { + let app = app_or_skip!(); + let (_, eng) = app.user_in("eng", Role::Editor, &["ROCKET"]).await; + let plate = item(&app, &eng, "part").await; + first(&app, &eng, &plate, "Plate.SLDPRT", b"\x00plate").await; + let bolt = item(&app, &eng, "part").await; + first(&app, &eng, &bolt, "Bolt.SLDPRT", b"\x00bolt").await; + let asm = item(&app, &eng, "assembly").await; + first(&app, &eng, &asm, "Stage.SLDASM", &sldasm(&["Plate.SLDPRT"])).await; + + checkout(&app, &eng, &asm).await; + add_line(&app, &eng, &asm, &plate, 3.0).await; + let r = checkin( + &app, + &eng, + &asm, + &[( + "Stage.SLDASM", + sldasm(&["Plate.SLDPRT", "Bolt.SLDPRT"]).as_slice(), + )], + &[("bom_from_references", "true")], + ) + .await; + assert_eq!(r.status, StatusCode::OK, "{}", r.text()); + let body = r.json(); + assert_eq!(strings(&body["bom"]["added"]), [bolt.clone()], "{body}"); + assert_eq!(body["bom"]["updated"], json!([]), "assumed: {body}"); + let mut expected = vec![(bolt.clone(), 1.0), (plate.clone(), 3.0)]; + expected.sort_by(|a, b| a.0.cmp(&b.0)); + assert_eq!(bom(&app, &eng, &asm).await, expected); + app.teardown().await; +} + +/// A child that would make the BOM circular is refused; the other children +/// are still added. +#[tokio::test] +async fn a_circular_child_is_refused_at_checkin() { + let app = app_or_skip!(); + let (_, eng) = app.user_in("eng", Role::Editor, &["ROCKET"]).await; + let part = item(&app, &eng, "part").await; + first(&app, &eng, &part, "Plate.SLDPRT", b"\x00plate").await; + let top = item(&app, &eng, "assembly").await; + let sub = item(&app, &eng, "assembly").await; + first(&app, &eng, &sub, "Sub.SLDASM", &sldasm(&["Plate.SLDPRT"])).await; + // the sub-assembly already contains the top assembly + checkout(&app, &eng, &sub).await; + add_line(&app, &eng, &sub, &top, 1.0).await; + + let r = checkin( + &app, + &eng, + &top, + &[( + "Top.SLDASM", + sldasm(&["Sub.SLDASM", "Plate.SLDPRT"]).as_slice(), + )], + &[("bom_from_references", "true")], + ) + .await; + assert_eq!(r.status, StatusCode::OK, "{}", r.text()); + let body = r.json(); + assert_eq!(strings(&body["bom"]["added"]), [part.clone()], "{body}"); + assert_eq!(body["bom"]["refused"][0]["child"], json!(sub), "{body}"); + assert!(body["bom"]["refused"][0]["reason"] + .as_str() + .unwrap() + .contains("circular")); + assert_eq!(bom(&app, &eng, &top).await, [(part.clone(), 1.0)]); + app.teardown().await; +} + +/// A park (upload without check-in) never touches the BOM, and a refused +/// check-in (409: the file moved on since its base version) leaves files and +/// BOM as they were – the revision stays checked out. +#[tokio::test] +async fn park_and_a_refused_checkin_leave_the_bom_alone() { + let app = app_or_skip!(); + let (_, eng) = app.user_in("eng", Role::Editor, &["ROCKET"]).await; + let bolt = item(&app, &eng, "part").await; + first(&app, &eng, &bolt, "bolt.stp", b"ISO-10303-21;\n").await; + let asm = item(&app, &eng, "assembly").await; + first( + &app, + &eng, + &asm, + "pump.stp", + &step_assembly("pump.stp", &[("bolt.stp", 1)]), + ) + .await; + + // park version 2 (four bolts): the files endpoint, the lock stays + checkout(&app, &eng, &asm).await; + let v2 = step_assembly("pump.stp", &[("bolt.stp", 4)]); + let r = app + .api_multipart( + &format!("/api/v1/items/{asm}/revisions/A/files"), + &eng, + &[("pump.stp", v2.as_slice())], + &[("bom_from_references", "true")], + ) + .await; + assert!(r.status.is_success(), "{}", r.text()); + assert!( + bom(&app, &eng, &asm).await.is_empty(), + "park: BOM untouched" + ); + assert!(checked_out(&app, &asm).await); + + // a check-in based on version 1 is refused: nothing changes + let v3 = step_assembly("pump.stp", &[("bolt.stp", 6)]); + let bases = json!({ "pump.stp": 1 }).to_string(); + let r = checkin( + &app, + &eng, + &asm, + &[("pump.stp", v3.as_slice())], + &[("bases", bases.as_str()), ("bom_from_references", "true")], + ) + .await; + assert_eq!(r.status, StatusCode::CONFLICT, "{}", r.text()); + assert!( + bom(&app, &eng, &asm).await.is_empty(), + "refused: BOM untouched" + ); + assert!(checked_out(&app, &asm).await, "the lock stays"); + let rev = revision_id(&app, &asm).await; + assert!(bom_audits(&app, rev).await.is_empty()); + + // the check-in of the parked version with the box: four bolts + let r = checkin(&app, &eng, &asm, &[], &[("bom_from_references", "true")]).await; + assert_eq!(r.status, StatusCode::OK, "{}", r.text()); + assert_eq!(bom(&app, &eng, &asm).await, [(bolt.clone(), 4.0)]); + app.teardown().await; +} + +/// A revision without a structure file is left alone (`bom` is null), and +/// a field that is not "true" counts as unticked. +#[tokio::test] +async fn no_structure_file_or_an_unticked_box_leaves_the_bom_alone() { + let app = app_or_skip!(); + let (_, eng) = app.user_in("eng", Role::Editor, &["ROCKET"]).await; + let bolt = item(&app, &eng, "part").await; + first(&app, &eng, &bolt, "bolt.stp", b"ISO-10303-21;\n").await; + let doc = item(&app, &eng, "part").await; + first(&app, &eng, &doc, "notes.txt", b"bolt.stp").await; + checkout(&app, &eng, &doc).await; + let r = checkin(&app, &eng, &doc, &[], &[("bom_from_references", "true")]).await; + assert_eq!(r.status, StatusCode::OK, "{}", r.text()); + assert!(r.json()["bom"].is_null(), "{}", r.text()); + + let asm = item(&app, &eng, "assembly").await; + let v1 = step_assembly("pump.stp", &[("bolt.stp", 2)]); + let r = checkin( + &app, + &eng, + &asm, + &[("pump.stp", v1.as_slice())], + &[("bom_from_references", "false")], + ) + .await; + assert_eq!(r.status, StatusCode::OK, "{}", r.text()); + assert!(r.json()["bom"].is_null()); + assert!(bom(&app, &eng, &asm).await.is_empty()); + app.teardown().await; +} + +/// The chunked path: `check_in` with `bom_from_references` on +/// `POST /uploads/{id}/finish`. +#[tokio::test] +async fn a_chunked_checkin_updates_the_bom_too() { + let app = app_or_skip!(); + let (_, eng) = app.user_in("eng", Role::Editor, &["ROCKET"]).await; + let bolt = item(&app, &eng, "part").await; + first(&app, &eng, &bolt, "bolt.stp", b"ISO-10303-21;\n").await; + let asm = item(&app, &eng, "assembly").await; + let content = step_assembly("pump.stp", &[("bolt.stp", 3)]); + let r = app + .api( + Method::POST, + "/api/v1/uploads", + &eng, + Some( + json!({ "item": asm, "revision": "A", "filename": "pump.stp", + "size": content.len() }), + ), + ) + .await; + assert_eq!(r.status, StatusCode::CREATED, "{}", r.text()); + let id = r.json()["id"].as_str().unwrap().to_string(); + let request = Request::builder() + .method(Method::PATCH) + .uri(format!("/api/v1/uploads/{id}")) + .header(header::AUTHORIZATION, format!("Bearer {eng}")) + .header(header::CONTENT_TYPE, "application/offset+octet-stream") + .header("Upload-Offset", "0") + .body(Body::from(content.clone())) + .unwrap(); + let r = app.send(request).await; + assert_eq!(r.status, StatusCode::OK, "{}", r.text()); + let r = app + .api( + Method::POST, + &format!("/api/v1/uploads/{id}/finish"), + &eng, + Some(json!({ "check_in": true, "bom_from_references": true })), + ) + .await; + assert_eq!(r.status, StatusCode::OK, "{}", r.text()); + let body = r.json(); + assert_eq!(strings(&body["bom"]["added"]), [bolt.clone()], "{body}"); + assert_eq!(bom(&app, &eng, &asm).await, [(bolt.clone(), 3.0)]); + app.teardown().await; +} + +/// The web check-in form offers the box (ticked) on an assembly and the +/// form's field updates the BOM. +#[tokio::test] +async fn the_web_checkin_form_has_the_box_ticked() { + let app = app_or_skip!(); + let (_, eng) = app.user_in("eng", Role::Editor, &["ROCKET"]).await; + let bolt = item(&app, &eng, "part").await; + first(&app, &eng, &bolt, "bolt.stp", b"ISO-10303-21;\n").await; + let asm = item(&app, &eng, "assembly").await; + first( + &app, + &eng, + &asm, + "pump.stp", + &step_assembly("pump.stp", &[("bolt.stp", 2)]), + ) + .await; + checkout(&app, &eng, &asm).await; + let rev = revision_id(&app, &asm).await; + let cookie = app.login("eng").await; + let page = app + .get_page(&format!("/revisions/{rev}"), Some(&cookie)) + .await; + assert_eq!(page.status, StatusCode::OK); + let html = page.text(); + assert!( + html.contains(r#"name="bom_from_references" value="true" checked"#), + "the box, ticked" + ); + + let boundary = "teamcraft-test-boundary"; + let body = format!( + "--{boundary}\r\nContent-Disposition: form-data; name=\"comment\"\r\n\r\nweb\r\n\ + --{boundary}\r\nContent-Disposition: form-data; name=\"bom_from_references\"\r\n\r\ntrue\r\n\ + --{boundary}--\r\n" + ); + let request = Request::builder() + .method(Method::POST) + .uri(format!("/revisions/{rev}/checkin")) + .header(header::HOST, "localhost:8080") + .header(header::ORIGIN, "http://localhost:8080") + .header(header::COOKIE, &cookie) + .header( + header::CONTENT_TYPE, + format!("multipart/form-data; boundary={boundary}"), + ) + .body(Body::from(body)) + .unwrap(); + let r = app.send(request).await; + assert_eq!(r.status, StatusCode::SEE_OTHER, "{}", r.text()); + assert_eq!(bom(&app, &eng, &asm).await, [(bolt.clone(), 2.0)]); + assert!(!checked_out(&app, &asm).await); + app.teardown().await; +} From 1de8f31e0e77076c10391a27227c1de2c4e4c2ec Mon Sep 17 00:00:00 2001 From: fork Date: Sun, 22 Nov 2026 20:57:36 +0100 Subject: [PATCH 2/2] Check-in BOM box: server decides, excluded parts kept quietly (#407) Review findings on PR #450: - file-status answers bom_structure (bom_references::has_structure: the structure rule on the stored reference counts, no parsing); the desktop shows *Update the BOM from the CAD file* for that flag, an assembly item, an assembly file or a SolidCraft design (.sc and .solidcraft) - no longer for every NX/Creo .prt or STEP export. - Web: the check-in box also uses has_structure; cad_structure counts STEP assemblies (Exchange) that lead to other items. - Check-in: items referenced only through components excluded from the BOM in the CAD file are no longer reported as "no longer in the CAD file" (Collected/Plan.excluded_items, unit tests). - plan(): the CAD files are parsed for their counts with no pooled connection held (load_files / compare_on split; the check-in keeps everything on its transaction). - Docs: structure.md, desktop.md, desktop/README, api.md, CHANGELOG. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 9 +- desktop/README.md | 2 +- desktop/src/scbridge.rs | 1 + desktop/src/workspace.rs | 6 + desktop/ui/js/checkin-bom.js | 27 +++- desktop/ui/js/workspace.js | 4 +- docs/src/guide/desktop.md | 9 +- docs/src/guide/structure.md | 15 +- docs/src/reference/api.md | 6 +- scripts/desktop-checkin-bom.test.mjs | 20 ++- src/services/bom_references.rs | 216 +++++++++++++++++++++------ src/services/checkouts.rs | 12 +- src/web/revisions.rs | 14 +- templates/revisions/show.html | 2 +- 14 files changed, 263 insertions(+), 80 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e76c765b..32499345 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,7 +17,10 @@ see the notices in the README. - **Check-in: *Update the BOM from the CAD file*** (#407): a box in the web check-in form and the desktop app's *Check in…*, ticked by default every - time, shown when the revision has an assembly. The plan of *Create BOM from + time, shown when the revision has an assembly whose references are read (an + assembly item, an assembly file, or an NX part, STEP file or SolidCraft + design with counted instances – `bom_structure` in `file-status` for the + desktop app; not for a plain part check-in). The plan of *Create BOM from CAD references…* is applied to the version being checked in, in the check-in's transaction: new children added, quantities the CAD file counts corrected (never from an *assumed 1*), circular children refused, no line @@ -27,7 +30,9 @@ see the notices in the README. field `bom_from_references` on `POST …/checkin` and in the body of `POST /uploads/{id}/finish` (off by default), the answer adds `bom`; audit `bom.from_references` with `"source": "checkin"`. *Park* never - changes the BOM. + changes the BOM. Parts still in the assembly but only excluded from its BOM + there are not reported as *no longer in the CAD file*; the manual plan no + longer holds a database connection while it reads the CAD files. ### Changed diff --git a/desktop/README.md b/desktop/README.md index bc8b7e19..78d4536c 100755 --- a/desktop/README.md +++ b/desktop/README.md @@ -29,7 +29,7 @@ TeamCraft server – it is a client, not an offline copy. | Tab order | Drag a tab to move it (pointer events, an insertion bar shows the landing place, the strip scrolls at its edges, `Esc` cancels; a plain click still activates, a dragged tab keeps its state and is not activated). Keyboard: arrow keys, `Home`, `End` move the focus between the tabs (WAI-ARIA tabs, roving tabindex, `Enter`/`Space` activates), `Ctrl+Shift+PageUp/PageDown` move the active tab. Right click (or the menu key): *Move to start*, *Move to end*, *Close*, *Close others*, *Close tabs to the right*. The order is kept while the app runs (open tabs are not restored on start). Rules in `ui/js/tab-order.js`, tested by `scripts/desktop-tab-order.test.mjs` (`node`); the dragging itself is checked by hand | | Revision tabs | Overview (with *At a glance*: the preview image from the CAD file, files, BOM lines, where used, references, change orders, open requests – and a link to the similar parts in the browser), Files (versions, restore, open), Structure (multi-level BOM, revision rules, roll-up), Where used, References, **Graph** (see below), Changes, History, **3D viewer** (STEP, meshes, point clouds – converted by the server), Workspace | | Graph | A navigable reference/BOM graph around the revision: the focused item in the middle, its BOM children and references below, the assemblies that use it above. Node **colour** = lifecycle state, **size** and the ⚙ badge = the item's own BOM complexity, **×n in the top-left corner** = how many instances of that part or sub-assembly the assembly contains (summed over its BOM lines, ×1 included). Every node is clickable and takes the tab to that item – a sub-assembly shows its own graph, anything else its overview, the item in the middle its own overview – with a **back control that stays in the same place** (the left of the sub-tab row) and leads back the way you came, one step per click; `Ctrl`/`⌘`+click opens a node in a tab of its own. Mouse-wheel zooms on the cursor; drag to pan; the view fits itself while details load until you zoom or pan, a double-click on the background fits it again. More than 8 neighbours on one side are laid out in rows; above 48 only the first 48 are fetched for preview and complexity (one request each), the rest keep a basic card. Preview images and filenames on the nodes; pure SVG, no external engine | -| Working folder | One folder per project group that **mirrors the folder structure of the files** (`///`, the folder is stored with every file on the server), one copy of each file – the revision is metadata in `/.teamcraft/state.json` (item, revision, file id, version, SHA-256, checked out). Files you have not checked out are **read-only** on disk; *Check out* takes the lock and makes them writable; *Check in…* uploads the chosen files as new versions, releases the lock in one step and makes them read-only again (files above 32 MiB go first as chunked, resumable uploads, then join the same check-in transaction). The check-in dialog shows the progress, and **Cancel** stops the check-in until its last byte is sent – before the next STEP export, between two chunks (the upload sessions are discarded, `DELETE /uploads/{id}`) or half-way through the streamed request with the small files: nothing is stored, the revision stays checked out (`src/abortable.rs`). Every check-in names the version each file is based on, so the server refuses (409) to overwrite a version somebody else checked in meanwhile. The Workspace tab shows *up to date* / *modified* / *outdated* / *conflict* / *new* / *missing* / *name clash* per file (from the server's file status) with its path, read-only flag and the lock owner. Two items with the same folder and file name are reported, never overwritten. Lock and backup files of the CAD programs are skipped; a SolidCraft design that is open in SolidCraft (a fresh `.~.lock` beside it, `src/cadlock.rs`) is marked *Open in SolidCraft* with the computer and user, and the check-in dialog warns that unsaved changes there are not checked in; Creo iterations count as one file. A tracked revision the server no longer shows (you left a restricted project group) is listed as *No longer accessible*: its local copies stay untouched and are never synced again – delete them yourself if you must In *Check in…*, a new part or assembly file next to a revision that has a model already is offered unticked, with *New item from this file* (#359). *Update the BOM from the CAD file* in *Check in…* (#407): ticked by default every time, shown when a file of the revision may be an assembly (`js/checkin-bom.js`, tested by `scripts/desktop-checkin-bom.test.mjs`); sends `bom_from_references` with the check-in (never with *Park*) and adds the server's `bom` summary to the result | +| Working folder | One folder per project group that **mirrors the folder structure of the files** (`///`, the folder is stored with every file on the server), one copy of each file – the revision is metadata in `/.teamcraft/state.json` (item, revision, file id, version, SHA-256, checked out). Files you have not checked out are **read-only** on disk; *Check out* takes the lock and makes them writable; *Check in…* uploads the chosen files as new versions, releases the lock in one step and makes them read-only again (files above 32 MiB go first as chunked, resumable uploads, then join the same check-in transaction). The check-in dialog shows the progress, and **Cancel** stops the check-in until its last byte is sent – before the next STEP export, between two chunks (the upload sessions are discarded, `DELETE /uploads/{id}`) or half-way through the streamed request with the small files: nothing is stored, the revision stays checked out (`src/abortable.rs`). Every check-in names the version each file is based on, so the server refuses (409) to overwrite a version somebody else checked in meanwhile. The Workspace tab shows *up to date* / *modified* / *outdated* / *conflict* / *new* / *missing* / *name clash* per file (from the server's file status) with its path, read-only flag and the lock owner. Two items with the same folder and file name are reported, never overwritten. Lock and backup files of the CAD programs are skipped; a SolidCraft design that is open in SolidCraft (a fresh `.~.lock` beside it, `src/cadlock.rs`) is marked *Open in SolidCraft* with the computer and user, and the check-in dialog warns that unsaved changes there are not checked in; Creo iterations count as one file. A tracked revision the server no longer shows (you left a restricted project group) is listed as *No longer accessible*: its local copies stay untouched and are never synced again – delete them yourself if you must In *Check in…*, a new part or assembly file next to a revision that has a model already is offered unticked, with *New item from this file* (#359). *Update the BOM from the CAD file* in *Check in…* (#407): ticked by default every time, shown when the server reports a structure file (`bom_structure` of `file-status`), the item is an assembly, or a file is an assembly or a SolidCraft design (`js/checkin-bom.js`, tested by `scripts/desktop-checkin-bom.test.mjs`); sends `bom_from_references` with the check-in (never with *Park*) and adds the server's `bom` summary to the result | | Get | *Get…* on a revision: the item with its BOM (revision rule *latest* or *released*) and the parts its CAD files reference, resolved by the server (`…/workset`); only missing and outdated files are downloaded (SHA-256, checked after the download), recorded one by one so an interrupted Get continues where it stopped – also inside a large file: the bytes received stay in `..teamcraft-download` and the next Get asks only for the rest (`Range` / `If-Range`, `src/resume.rs`), checks the SHA-256 of the whole file and renames it into place; progress and *Cancel* in the status bar. No browser downloads | | Add files | *Add files…* (Files section and *More ▾*): file picker → the files are **copied** into the working folder of the revision (checked out first if needed; sub-folder selectable, originals untouched) and listed as *new* in *Check in…* (option *Check in now*; remembered in `.teamcraft/added.json` until checked in). A name that exists is a new version and asks first. The Files section says why it is unavailable (locked by someone else, in review, released = frozen → Revise). Pure logic and tests in `src/addfiles.rs`; drag and drop onto the window is not supported (`dragDropEnabled` is off) | | Old workspaces | Folders of the older layout `///` (and `//`) are found after the sign-in; the app offers to move the files into the new layout – never overwriting a file, keeping files whose place is taken and listing them, writing every step to `.teamcraft/migration.log` | diff --git a/desktop/src/scbridge.rs b/desktop/src/scbridge.rs index f5eeddd4..a5c52da9 100755 --- a/desktop/src/scbridge.rs +++ b/desktop/src/scbridge.rs @@ -343,6 +343,7 @@ mod tests { files: Vec::new(), inaccessible: false, released: None, + bom_structure: false, }; let s = state(5, "PRT-000042", "A", &status); assert_eq!(s["checked_out"]["by_name"], "Kim Lee"); diff --git a/desktop/src/workspace.rs b/desktop/src/workspace.rs index d0448163..4259a04d 100755 --- a/desktop/src/workspace.rs +++ b/desktop/src/workspace.rs @@ -728,6 +728,10 @@ pub struct Status { /// last checked out (the server's `released`: `by_name`, `at`, `reason`, /// `message`): local changes stay, but need a new check-out. pub released: Option, + /// The server knows a structure file of the revision (`bom_structure` of + /// `file-status`, false from older servers): *Check in…* offers *Update + /// the BOM from the CAD file* (#407). + pub bom_structure: bool, } /// A local file of a revision: where it is and what the state says. @@ -840,6 +844,7 @@ pub fn inaccessible_status(root: &Path, project: Option<&str>, item: &str, code: locked_by_name: None, inaccessible: true, released: None, + bom_structure: false, files, } } @@ -999,6 +1004,7 @@ pub async fn status( locked_by_name: rev.locked_by_name, inaccessible: false, released: Some(server["released"].clone()).filter(Value::is_object), + bom_structure: server["bom_structure"].as_bool().unwrap_or(false), files: out, }) } diff --git a/desktop/ui/js/checkin-bom.js b/desktop/ui/js/checkin-bom.js index 6eacbe5d..fe5ec6f1 100755 --- a/desktop/ui/js/checkin-bom.js +++ b/desktop/ui/js/checkin-bom.js @@ -5,13 +5,28 @@ // // The server updates the BOM from the references of the checked-in version // (new lines added, counted quantities corrected, nothing removed). The box -// is offered only when a file of the revision can tell the structure: -// assemblies (Inventor, SOLIDWORKS, …), NX parts and SolidCraft designs (an -// assembly by their content) and STEP files (assemblies with external -// parts) – the server checks the content and leaves the BOM alone otherwise. +// is offered only for a revision with an assembly whose references are read: +// the server knows a structure file of it (`bom_structure` of file-status: +// an assembly, or an NX part, STEP file or SolidCraft design whose +// references carry instance counts), the item is an assembly, or a file is +// of an assembly format (Inventor, SOLIDWORKS, Creo, …) or a SolidCraft +// design (the desktop's own CAD program; a part or an assembly by content). +// NX `.prt` and STEP files are left to the server: every Creo part is a +// `.prt` too, and *Check in…* exports a STEP of every part. -/** Extensions whose files may be assemblies by content, whatever their kind. */ -export var BOM_STRUCTURE_EXTENSIONS = ["prt", "stp", "step", "sc"]; +/** Extensions whose files may be assemblies by content, whatever their kind + * (SolidCraft designs, both extensions of `solidcraft-design`). */ +export var BOM_STRUCTURE_EXTENSIONS = ["sc", "solidcraft"]; + +/** Is *Update the BOM from the CAD file* offered? `status` is the answer of + * workspace_status (`bom_structure`, `files`), `rev` the revision (its + * `item_type`), `kinds` the extension → format kind map of GET /formats. */ +export function offerBomUpdate(status, rev, kinds) { + if (status && status.bom_structure) return true; + if (rev && rev.item_type === "assembly") return true; + var names = ((status && status.files) || []).map(function (f) { return f.name; }); + return bomStructureFile(names, kinds); +} /** Does one of the file names lead to a BOM from the CAD file? `kinds` is * the extension → format kind map of GET /formats. */ diff --git a/desktop/ui/js/workspace.js b/desktop/ui/js/workspace.js index 44b1269b..ed2eef1f 100755 --- a/desktop/ui/js/workspace.js +++ b/desktop/ui/js/workspace.js @@ -16,7 +16,7 @@ import { incompleteGet, notOpenedText } from "./get-rules.js"; import { formatKinds } from "./folder-import.js"; import { isNewModelOfItsOwn, workspacePath } from "./new-files.js"; import { newItemDialog } from "./dialogs.js"; -import { bomStructureFile, checkinBomText } from "./checkin-bom.js"; +import { offerBomUpdate, checkinBomText } from "./checkin-bom.js"; // ---------------------------------------------------------------- 3D viewer @@ -484,7 +484,7 @@ export function checkinDialog(tab, park) { // #407: on by default every time (the choice is not remembered); only on // a check-in – Park keeps the revision checked out, nothing is final yet var withBom = h("input", { type: "checkbox", checked: true }); - var bomNote = bomStructureFile(ws.files.map(function (f) { return f.name; }), kinds) ? h("label.check", null, [withBom, + var bomNote = offerBomUpdate(ws, rev, kinds) ? h("label.check", null, [withBom, "Update the BOM from the CAD file – adds the parts the assembly uses and corrects counted quantities, never removes a line (not when parking)"]) : null; var body = h("div.stack", null, [ h("p.small.muted", null, ["From ", h("span.mono", { text: ws.dir })]), diff --git a/docs/src/guide/desktop.md b/docs/src/guide/desktop.md index 9c61deb3..6c932b7a 100755 --- a/docs/src/guide/desktop.md +++ b/docs/src/guide/desktop.md @@ -132,8 +132,13 @@ check-outs and reviews, and a **local working folder**: own (#359); * **Update the BOM from the CAD file** (#407) – a box in *Check in…*, **ticked by default** every time (the choice is not remembered) and shown - when the revision has an assembly whose references are read (Inventor - `.iam`, SOLIDWORKS, an NX `.prt`, a STEP file, a SolidCraft `.sc`). The + when the revision has an assembly whose references are read: the server + knows a structure file of it (`bom_structure` of `file-status` – an + assembly, or an NX part, STEP file or SolidCraft design with counted + instances), the item is an assembly, or a file is an assembly (Inventor + `.iam`, SOLIDWORKS, Creo `.asm`) or a SolidCraft `.sc` / `.solidcraft` + design. A plain part check-in (an NX or Creo part, its STEP export) shows + no box. The server applies the plan of *Quantities from CAD…* to the version being checked in, in the same transaction: missing children are added, a quantity the CAD file counts (*CAD instances*) is corrected – an *assumed diff --git a/docs/src/guide/structure.md b/docs/src/guide/structure.md index fade2267..da96af35 100755 --- a/docs/src/guide/structure.md +++ b/docs/src/guide/structure.md @@ -90,10 +90,13 @@ plan, `POST …/bom/from-references` with The check-in form of the web UI and the desktop app's *Check in…* have a box **Update the BOM from the CAD file**, ticked by default every time -(#407). It is shown when the revision has an assembly (in the web UI: an -assembly item or a revision whose CAD files lead to other items; in the -desktop app: an Inventor `.iam`, a SOLIDWORKS assembly, an NX `.prt`, a STEP -file or a SolidCraft `.sc`). With the box ticked, the plan above is applied +(#407). It is shown when the revision has an assembly whose references are +read: an assembly item, a file of an assembly format, or an NX part, STEP +file or SolidCraft design whose stored references carry instance counts (in +the web UI also a revision whose CAD files lead to other items; in the +desktop app also a file of an assembly format or a SolidCraft `.sc` / +`.solidcraft` design in the working folder, a part or an assembly by +content). Any other plain part check-in shows no box. With the box ticked, the plan above is applied to the version being checked in, **in the check-in's transaction** – the files and the BOM change together, or neither (a refused upload, 409, leaves the BOM as it was): @@ -104,7 +107,9 @@ the BOM as it was): * *circular* children are refused, as above; * lines whose part the CAD files no longer reference are **kept** (a BOM carries lines without CAD geometry: glue, cable ties, bought parts) and - reported as *no longer in the CAD file*; + reported as *no longer in the CAD file*; a part still in the assembly but + only excluded from its BOM there (SOLIDWORKS *exclude from BOM*, suppressed + Inventor occurrences) is kept without that report; * referenced files TeamCraft does not have are reported as *not in TeamCraft*, never created. diff --git a/docs/src/reference/api.md b/docs/src/reference/api.md index c07a0568..dab98eff 100755 --- a/docs/src/reference/api.md +++ b/docs/src/reference/api.md @@ -196,7 +196,11 @@ curl -s -X POST -H "$H" -F file=@hub.step -F 'bases={"hub.step": 3}' \ `GET …/revisions/{code}/file-status` returns every file with its current `version`, `sha256`, uploader and the lock (`locked_by`, `locked_by_name`, -`locked_at`, `locked_by_me`, `locked_by_other`). When somebody else released *your* check-out of the revision and nobody +`locked_at`, `locked_by_me`, `locked_by_other`). `bom_structure` is `true` when a +current file of the revision describes its structure by what the reference +scan stored (an assembly, or an NX part, STEP file or SolidCraft design whose +references carry instance counts) – the desktop app then offers *Update the +BOM from the CAD file* at check-in. When somebody else released *your* check-out of the revision and nobody has checked it out since, `released` says so (`by_name`, `at`, `reason` and the `message` *Released by X; your local changes are kept and need a new check-out.*); otherwise it is diff --git a/scripts/desktop-checkin-bom.test.mjs b/scripts/desktop-checkin-bom.test.mjs index 7eb92ab8..c8016384 100755 --- a/scripts/desktop-checkin-bom.test.mjs +++ b/scripts/desktop-checkin-bom.test.mjs @@ -13,7 +13,7 @@ import path from "node:path"; import { fileURLToPath } from "node:url"; const source = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../desktop/ui/js/checkin-bom.js"); -const { bomStructureFile, checkinBomText } = +const { bomStructureFile, offerBomUpdate, checkinBomText } = await import("data:text/javascript;base64," + fs.readFileSync(source).toString("base64")); let passed = 0; @@ -25,12 +25,11 @@ function test(name, fn) { const kinds = { iam: "assembly", sldasm: "assembly", asm: "assembly", ipt: "part", prt: "model", stp: "exchange", pdf: "document" }; -test("assemblies, NX parts, STEP files and SolidCraft designs offer the box", () => { +test("assembly formats and SolidCraft designs offer the box", () => { assert.equal(bomStructureFile(["Wagon.iam"], kinds), true); assert.equal(bomStructureFile(["Pump.SLDASM"], kinds), true); - assert.equal(bomStructureFile(["frame.prt"], {}), true, "NX: an assembly by content"); - assert.equal(bomStructureFile(["pump.STEP"], {}), true); assert.equal(bomStructureFile(["cart.sc"], {}), true); + assert.equal(bomStructureFile(["cart.solidcraft"], {}), true, "the second SolidCraft extension"); assert.equal(bomStructureFile(["bracket.asm.7"], kinds), true, "a Creo iteration"); }); @@ -40,6 +39,19 @@ test("parts, drawings and documents do not", () => { assert.equal(bomStructureFile(["README"], kinds), false); assert.equal(bomStructureFile(["bolt.ipt.3"], kinds), false); assert.equal(bomStructureFile(null, kinds), false); + // NX and Creo parts are .prt, every part gets a STEP export: the server decides + assert.equal(bomStructureFile(["frame.prt"], kinds), false); + assert.equal(bomStructureFile(["pump.STEP", "pump.stp"], kinds), false); +}); + +test("the server's structure flag and assembly items offer the box", () => { + const files = (...names) => names.map((name) => ({ name })); + assert.equal(offerBomUpdate({ bom_structure: true, files: files("frame.prt") }, { item_type: "part" }, kinds), true); + assert.equal(offerBomUpdate({ bom_structure: false, files: files("frame.prt", "frame.stp") }, { item_type: "part" }, kinds), false); + assert.equal(offerBomUpdate({ files: files("frame.prt") }, { item_type: "assembly" }, kinds), true, "an assembly item"); + assert.equal(offerBomUpdate({ files: files("Wagon.iam") }, { item_type: "part" }, kinds), true); + assert.equal(offerBomUpdate({ files: files("cart.solidcraft") }, null, {}), true); + assert.equal(offerBomUpdate(null, null, kinds), false); }); test("the result line says what changed", () => { diff --git a/src/services/bom_references.rs b/src/services/bom_references.rs index 4622db91..54fdcd1d 100755 --- a/src/services/bom_references.rs +++ b/src/services/bom_references.rs @@ -51,7 +51,7 @@ use std::collections::{HashMap, HashSet}; use serde::{Deserialize, Serialize}; use serde_json::json; -use sqlx::PgConnection; +use sqlx::{PgConnection, PgPool}; use uuid::Uuid; use crate::{ @@ -154,6 +154,10 @@ pub struct Plan { /// The revision has a file whose references describe its structure. #[serde(skip)] pub structure: bool, + /// Items the structure files reference only through components excluded + /// from the BOM there (`excluded`): still in the CAD file. + #[serde(skip)] + pub excluded_items: Vec, } /// A child before it is compared with the BOM. @@ -181,24 +185,60 @@ pub(crate) struct Collected { pub children: Vec, pub unmatched: Vec, pub excluded: Vec, + /// The items behind `excluded` (other revisions' files only). + pub excluded_items: Vec, } -/// Does this file's references describe the structure of the revision? -fn is_structure(src: &SourceFile<'_>) -> bool { - match src.file.format_kind { +/// The structure rule: a file with references describes the structure of +/// its revision when it is of an assembly format, or an NX part, SolidCraft +/// design or STEP file whose references carry instance counts (`counted`: +/// an assembly by content, not by extension). +fn structure_rule(kind: Option, format: &str, counted: bool) -> bool { + match kind { Some(FormatKind::Assembly) => true, // NX, SolidCraft: an assembly by content (it has component instances) - Some(FormatKind::Model) if matches!(src.format, "nx-part" | "solidcraft-design") => { - src.counts.is_some_and(|c| !c.is_empty()) - } + Some(FormatKind::Model) if matches!(format, "nx-part" | "solidcraft-design") => counted, // STEP: an assembly with external references (occurrences counted) - Some(FormatKind::Exchange) if src.format == "step" => { - src.counts.is_some_and(|c| !c.is_empty()) - } + Some(FormatKind::Exchange) if format == "step" => counted, _ => false, } } +/// Does this file's references describe the structure of the revision? +fn is_structure(src: &SourceFile<'_>) -> bool { + structure_rule( + src.file.format_kind, + src.format, + src.counts.is_some_and(|c| !c.is_empty()), + ) +} + +/// Has the revision a structure file, by what the reference scan stored +/// (`file_references.instances`)? Cheap – no file is parsed and no name is +/// matched; the caller has checked that the actor may read the revision. +/// Offers *Update the BOM from the CAD file* at check-in (web form, desktop +/// `file-status`). +pub async fn has_structure(pool: &PgPool, revision_id: Uuid) -> AppResult { + let rows = sqlx::query_as::<_, (String, bool)>( + "SELECT COALESCE(f.format, ''), COALESCE(bool_or(fr.instances >= 0), FALSE) + FROM files f + JOIN file_versions fv ON fv.file_id = f.id AND fv.version = f.current_version + JOIN file_references fr ON fr.file_version_id = fv.id + WHERE f.revision_id = $1 + GROUP BY f.id, f.format", + ) + .bind(revision_id) + .fetch_all(pool) + .await?; + Ok(rows.iter().any(|(format, counted)| { + structure_rule( + crate::cadformat::by_id(format).map(|f| f.kind), + format, + *counted, + ) + })) +} + /// The children of the structure files: the first match of every referenced /// name that is another item, once per item. pub(crate) fn collect(files: &[SourceFile<'_>]) -> Collected { @@ -213,6 +253,12 @@ pub(crate) fn collect(files: &[SourceFile<'_>]) -> Collected { if !out.excluded.contains(&name.name) { out.excluded.push(name.name.clone()); } + // still in the CAD file, only not in its BOM + for m in name.matches.iter().filter(|m| !m.same_revision) { + if !out.excluded_items.contains(&m.item_id) { + out.excluded_items.push(m.item_id); + } + } continue; } if name.matches.is_empty() { @@ -282,17 +328,6 @@ struct FileRow { format: String, } -/// Where the instance counts of a plan come from. -enum Counts<'a> { - /// Read again from the stored files (the manual flow; it also covers - /// files whose references were scanned before the counts were stored). - Read(&'a AppState), - /// The counts the reference scan stored with the references - /// (`file_references.instances`): the check-in, which never parses a - /// file while it holds the revision lock. - Stored, -} - /// The stored instance counts of one file, by lower case name (`None`: the /// file told none). fn stored_counts(file: &FileWithReferences) -> Option> { @@ -352,23 +387,51 @@ async fn hidden_names( /// What the BOM of the revision would be, from the CAD references. /// -/// Read-only; the actor must be able to read the revision. +/// Read-only; the actor must be able to read the revision. The files are +/// parsed for their instance counts with no pooled connection held: a plan +/// of a large assembly never keeps a connection busy while it reads. pub async fn plan(st: &AppState, actor: &Actor, revision_id: Uuid) -> AppResult { let rev = revisions::get(&st.db, actor, revision_id).await?; + access::ensure_visible(actor, rev.project_id, access::not_found_revision())?; + let (files, rows) = { + let mut conn = st.db.acquire().await?; + load_files(&mut conn, actor, revision_id).await? + }; + let meta: HashMap = rows.iter().map(|r| (r.id, r)).collect(); + let mut counts: HashMap> = HashMap::new(); + for f in &files { + if let Some(row) = meta.get(&f.file_id) { + if let Some(c) = read_counts(st, &row.sha256, &row.format).await? { + counts.insert(f.file_id, c); + } + } + } let mut conn = st.db.acquire().await?; - plan_on(&mut conn, Counts::Read(st), actor, &rev).await + compare_on(&mut conn, actor, &rev, &files, &meta, &counts).await } -/// The plan of `rev`, read on one connection: inside a transaction it sees -/// the references and BOM lines that transaction wrote. -async fn plan_on( +/// The plan of `rev` with the stored instance counts, read on one +/// connection: inside a transaction it sees the references and BOM lines +/// that transaction wrote (the check-in, which never parses a file while it +/// holds the revision lock). +async fn plan_on(conn: &mut PgConnection, actor: &Actor, rev: &Revision) -> AppResult { + access::ensure_visible(actor, rev.project_id, access::not_found_revision())?; + let (files, rows) = load_files(conn, actor, rev.id).await?; + let meta: HashMap = rows.iter().map(|r| (r.id, r)).collect(); + let counts: HashMap> = files + .iter() + .filter_map(|f| stored_counts(f).map(|c| (f.file_id, c))) + .collect(); + compare_on(conn, actor, rev, &files, &meta, &counts).await +} + +/// The files of the revision with their matched references, and their +/// content hash and format. +async fn load_files( conn: &mut PgConnection, - counts_from: Counts<'_>, actor: &Actor, - rev: &Revision, -) -> AppResult { - access::ensure_visible(actor, rev.project_id, access::not_found_revision())?; - let revision_id = rev.id; + revision_id: Uuid, +) -> AppResult<(Vec, Vec)> { let files = references::files_of_revision_on(conn, actor, revision_id).await?; let ids: Vec = files.iter().map(|f| f.file_id).collect(); let rows = sqlx::query_as::<_, FileRow>( @@ -377,20 +440,20 @@ async fn plan_on( .bind(&ids) .fetch_all(&mut *conn) .await?; - let meta: HashMap = rows.iter().map(|r| (r.id, r)).collect(); - let mut counts: HashMap> = HashMap::new(); - for f in &files { - let found = match &counts_from { - Counts::Read(st) => match meta.get(&f.file_id) { - Some(row) => read_counts(st, &row.sha256, &row.format).await?, - None => None, - }, - Counts::Stored => stored_counts(f), - }; - if let Some(c) = found { - counts.insert(f.file_id, c); - } - } + Ok((files, rows)) +} + +/// The plan from the loaded files and their counts, compared with the BOM +/// of `rev` on `conn`. +async fn compare_on( + conn: &mut PgConnection, + actor: &Actor, + rev: &Revision, + files: &[FileWithReferences], + meta: &HashMap, + counts: &HashMap>, +) -> AppResult { + let revision_id = rev.id; let sources: Vec> = files .iter() .map(|f| SourceFile { @@ -448,6 +511,7 @@ async fn plan_on( hidden: hidden_items.len() as i64, excluded: collected.excluded, structure, + excluded_items: collected.excluded_items, }) } @@ -744,8 +808,9 @@ async fn lines_not_in( /// differing line is changed only when the CAD file counts it (*CAD /// instances*, never an *assumed 1*); a circular child is refused; no line /// is ever removed – lines whose child is no longer referenced are reported -/// as `kept_not_in_cad`, referenced files TeamCraft does not have as -/// `not_in_teamcraft`. +/// as `kept_not_in_cad` (a child whose components are only excluded from +/// the BOM in the CAD file is still referenced: kept, not reported), +/// referenced files TeamCraft does not have as `not_in_teamcraft`. /// * **No structure:** a revision without a structure file (assembly, NX or /// STEP assembly with counts) is left alone: `None`. /// * **Audit:** `bom.add` / `bom.update` per line and one @@ -758,7 +823,7 @@ pub(crate) async fn apply_on_checkin( parent: &Revision, ) -> AppResult> { revisions::ensure_editable(parent, actor)?; - let plan = plan_on(&mut *tx, Counts::Stored, actor, parent).await?; + let plan = plan_on(&mut *tx, actor, parent).await?; if !plan.structure { return Ok(None); } @@ -771,7 +836,10 @@ pub(crate) async fn apply_on_checkin( c.quantity_source == QuantitySource::CadInstances }) .await?; - let referenced: Vec = plan.children.iter().map(|c| c.item_id).collect(); + // children still in the CAD file: counted ones and those only excluded + // from its BOM there (not "no longer in the CAD file") + let mut referenced: Vec = plan.children.iter().map(|c| c.item_id).collect(); + referenced.extend(plan.excluded_items.iter().copied()); let kept_not_in_cad = lines_not_in(&mut *tx, actor, parent.id, &referenced).await?; let result = CheckinBom { added: applied.added, @@ -915,6 +983,56 @@ mod tests { ); assert_eq!(c.children[1].source, QuantitySource::Assumed); assert_eq!(c.excluded, ["glue.sldprt"]); + // still in the assembly, only excluded from its BOM: never "no + // longer in the CAD file" at check-in + assert_eq!(c.excluded_items, [Uuid::from_u128(3)]); + } + + #[test] + fn excluded_components_of_the_own_revision_are_no_items() { + let asm = file( + "A.iam", + FormatKind::Assembly, + vec![ + ("own.ipt", vec![m(2, "ASM-1", "own.ipt", true)]), + ("ghost.ipt", vec![]), + ], + ); + let counts: HashMap = + [("own.ipt".to_string(), 0), ("ghost.ipt".to_string(), 0)].into(); + let c = collect(&[SourceFile { + file: &asm, + format: "inventor-assembly", + counts: Some(&counts), + }]); + assert_eq!(c.excluded, ["own.ipt", "ghost.ipt"]); + assert!(c.excluded_items.is_empty()); + assert!(c.children.is_empty() && c.unmatched.is_empty()); + } + + #[test] + fn the_structure_rule_needs_counts_for_models_and_step() { + assert!(structure_rule( + Some(FormatKind::Assembly), + "inventor-assembly", + false + )); + assert!(!structure_rule(Some(FormatKind::Model), "nx-part", false)); + assert!(structure_rule(Some(FormatKind::Model), "nx-part", true)); + assert!(structure_rule( + Some(FormatKind::Model), + "solidcraft-design", + true + )); + assert!(!structure_rule(Some(FormatKind::Model), "creo-part", true)); + assert!(!structure_rule(Some(FormatKind::Exchange), "step", false)); + assert!(structure_rule(Some(FormatKind::Exchange), "step", true)); + assert!(!structure_rule( + Some(FormatKind::Drawing), + "inventor-drawing", + true + )); + assert!(!structure_rule(None, "", true)); } #[test] diff --git a/src/services/checkouts.rs b/src/services/checkouts.rs index e4c59ebf..c1d01114 100755 --- a/src/services/checkouts.rs +++ b/src/services/checkouts.rs @@ -18,7 +18,7 @@ use crate::{ domain::{self, BaseVersion, FileSyncState, ServerFile}, error::{AppError, AppResult}, models::Revision, - services::{access, files, projects, revisions}, + services::{access, bom_references, files, projects, revisions}, }; /// A file of the client's workspace, as the client reports it. @@ -103,6 +103,10 @@ pub struct RevisionStatus { /// The item's folder in its group (`items.folder`): where a revision /// without files has its place in the working folder. pub item_folder: String, + /// The revision has a structure file by what the reference scan stored + /// (`bom_references::has_structure`): the desktop app offers *Update the + /// BOM from the CAD file* in *Check in…* (#407). + pub bom_structure: bool, pub files: Vec, } @@ -184,9 +188,10 @@ pub async fn file_status( client: &[ClientFile], ) -> AppResult { let rev = revisions::get(pool, actor, revision_id).await?; - let (list, versions) = tokio::try_join!( + let (list, versions, bom_structure) = tokio::try_join!( files::list(pool, actor, revision_id), - files::versions_of_revision(pool, revision_id) + files::versions_of_revision(pool, revision_id), + bom_references::has_structure(pool, revision_id) )?; let mut history: HashMap> = HashMap::new(); for v in versions { @@ -285,6 +290,7 @@ pub async fn file_status( locked_by_me: rev.checked_out_by == Some(actor.id), locked_by_other: rev.checked_out_by.is_some_and(|id| id != actor.id), item_folder: rev.item_folder.clone(), + bom_structure, files: out, }) } diff --git a/src/web/revisions.rs b/src/web/revisions.rs index 03008eb8..357a06a8 100755 --- a/src/web/revisions.rs +++ b/src/web/revisions.rs @@ -26,7 +26,7 @@ use crate::{ preview::PreviewFormat, services::{ access_log::{self, AccessCtx}, - as_built, attributes, audit, bom, changes, + as_built, attributes, audit, bom, bom_references, changes, file_check::ClashGuard, files, impacts, interchange, items, notifications, ownership, projects, references, requests, result_proposals, revisions, similar, simulations, uploads, @@ -405,17 +405,22 @@ pub async fn show( "add_to_change": can_work && open_change.is_none(), "acknowledge": me.can_edit() && me.can_edit_project(item.project_id), }); - // CAD files of this revision that lead to other items (assemblies and - // NX models): offers "Create BOM from CAD references" + // CAD files of this revision that lead to other items (assemblies, NX + // models, STEP assemblies with external parts): offers "Create BOM from + // CAD references" let cad_structure = file_refs.iter().any(|f| { matches!( f.format_kind, - Some(crate::cadformat::FormatKind::Assembly | crate::cadformat::FormatKind::Model) + Some(FormatKind::Assembly | FormatKind::Model | FormatKind::Exchange) ) && f .references .iter() .any(|r| r.matches.iter().any(|m| !m.same_revision)) }); + // the check-in box (#407): also a structure file by the rule of the + // check-in step (e.g. an assembly whose parts are not in TeamCraft yet) + let bom_structure = + rev.checked_out_by == Some(me.id) && bom_references::has_structure(&st.db, rev.id).await?; let files_hint = domain::files_hint( state, rev.checked_out_by, @@ -527,6 +532,7 @@ pub async fn show( "similar": similar_parts, "lines": lines, "cad_structure": cad_structure, + "bom_structure": bom_structure, "where_used": where_used, "interchange": interchange, "hidden": { diff --git a/templates/revisions/show.html b/templates/revisions/show.html index e6f46504..8cc6d542 100755 --- a/templates/revisions/show.html +++ b/templates/revisions/show.html @@ -146,7 +146,7 @@

Check in {{ rev.item_number }}/{{ rev.code }}

- {% if cad_structure or rev.item_type == "assembly" %} + {% if cad_structure or bom_structure or rev.item_type == "assembly" %} {% endif %}