feat(revisions): auto-create revision on item metadata changes #177
Reference in New Issue
Block a user
Delete Branch "feat/auto-revision-on-update"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Summary
Closes #173
When updating an item via
PUT /api/items/{partNumber}, any change to metadata fields now creates a new revision for full audit trail.Problem
Previously,
HandleUpdateItemonly created a revision when thepropertiesJSONB field was explicitly included in the request body. Changes to item metadata (part_number, description, item_type, sourcing_type, long_description) were applied in-place with no trace in revision history.Solution
Change detection in
HandleUpdateItemBefore calling
Update(), compare each request field against the current item values. Track which fields actually changed in ametadataChangesslice.Auto-revision creation
"updated description")SSE + job triggers
When a revision is auto-created, publish
revision.createdSSE event and trigger auto-jobs — matching the pattern inHandleCreateRevision.Files Changed
internal/db/items.goGetLatestRevision()— efficiently fetches current revision properties viaORDER BY revision_number DESC LIMIT 1internal/api/handlers.goHandleUpdateItem()— add change detection, auto-revision, SSE event, job triggerNot Changed
POST /api/items(HandleCreateItem) — already creates revision 1POST /api/items/{pn}/revisions(HandleCreateRevision) — explicit revision creationPOST /api/items/{pn}/file(HandleUploadFile) — file upload pathitems.Create()which already makes revision 1Testing
go build ./...— passesgo test ./...— all tests pass, no regressionsWhen updating an item via PUT /api/items/{partNumber}, any change to metadata fields (part_number, item_type, description, sourcing_type, long_description) now creates a new revision for audit trail. Previously, revisions were only created when the 'properties' JSONB field was explicitly included in the request body. Metadata-only changes were invisible in revision history. New behavior: - Detect which metadata fields actually changed vs current values - If metadata changed without properties, carry forward properties from the latest revision and auto-generate a comment (e.g. 'updated description') - If properties changed, use the provided properties (existing behavior) - If both changed, create a single revision capturing both - If nothing actually changed (identical values), skip revision creation - Publish revision.created SSE event and trigger auto-jobs Also adds GetLatestRevision() to ItemRepository for efficiently fetching the current revision's properties without loading all revisions. Closes #173