feat: add parent import ref to CSV export for parent-child item relationships (#62)#1467
Conversation
WalkthroughTwo files in the reporting service are modified to support exporting parent-child item relationships in CSV format. A new Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Security RecommendationsWhen implementing parent-child relationship imports from the CSV, ensure that:
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@backend/internal/core/services/reporting/io_sheet.go`:
- Around line 238-255: The code currently swallows errors from
repos.Entities.GetByID and does per-row reads; change the logic in the export
loop to (1) stop hiding lookup failures by returning or logging the error when
GetByID returns a non-nil err for a row that has a Parent (i.e., don't treat
errors as "no parent") and (2) eliminate the N+1 reads by adding a local cache
map[parentID]string (lookup key item.Parent.ID) that you consult before calling
repos.Entities.GetByID; populate the cache on first successful read and use
cached ImportRef for subsequent rows, and ensure the ParentImportRef assigned
into ExportCSVRow uses the cached value or surfaces the GetByID error instead of
silently leaving it empty.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: a828835e-26a7-4223-a242-97f3a2042004
📒 Files selected for processing (2)
backend/internal/core/services/reporting/io_row.gobackend/internal/core/services/reporting/io_sheet.go
| // Resolve parent import ref for CSV output | ||
| var parentImportRef string | ||
| if item.Parent != nil { | ||
| // Find the parent entity to get its import_ref | ||
| parentEntity, err := repos.Entities.GetByID(ctx, gid, item.Parent.ID) | ||
| if err == nil && parentEntity.ImportRef != "" { | ||
| parentImportRef = parentEntity.ImportRef | ||
| } | ||
| } | ||
|
|
||
| s.Rows[i] = ExportCSVRow{ | ||
| // fill struct | ||
| Location: locString, | ||
| TagStr: tagString, | ||
|
|
||
| ImportRef: item.ImportRef, | ||
| AssetID: item.AssetID, | ||
| Name: item.Name, | ||
| Quantity: item.Quantity, | ||
| Description: item.Description, | ||
| Insured: item.Insured, | ||
| Archived: item.Archived, | ||
| URL: url, | ||
| ImportRef: item.ImportRef, | ||
| ParentImportRef: parentImportRef, | ||
| AssetID: item.AssetID, |
There was a problem hiding this comment.
Don’t swallow parent lookup failures.
A failed GetByID call currently becomes an empty ParentImportRef, which is indistinguishable from “no parent” and can silently drop hierarchy data on re-import. This also adds a per-row repository read; if this stays in the export path, cache parent refs by ID to avoid the N+1 pattern.
🔧 Suggested fix
- parentEntity, err := repos.Entities.GetByID(ctx, gid, item.Parent.ID)
- if err == nil && parentEntity.ImportRef != "" {
- parentImportRef = parentEntity.ImportRef
- }
+ parentEntity, err := repos.Entities.GetByID(ctx, gid, item.Parent.ID)
+ if err != nil {
+ return err
+ }
+ parentImportRef = parentEntity.ImportRef🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@backend/internal/core/services/reporting/io_sheet.go` around lines 238 - 255,
The code currently swallows errors from repos.Entities.GetByID and does per-row
reads; change the logic in the export loop to (1) stop hiding lookup failures by
returning or logging the error when GetByID returns a non-nil err for a row that
has a Parent (i.e., don't treat errors as "no parent") and (2) eliminate the N+1
reads by adding a local cache map[parentID]string (lookup key item.Parent.ID)
that you consult before calling repos.Entities.GetByID; populate the cache on
first successful read and use cached ImportRef for subsequent rows, and ensure
the ParentImportRef assigned into ExportCSVRow uses the cached value or surfaces
the GetByID error instead of silently leaving it empty.
Description
Adds support for preserving parent-child item relationships in CSV export/import.
Changes
io_row.go— AddedParentImportRef stringfield withcsv:"HB.parent_import_ref"tag toExportCSVRowio_sheet.go— InReadItems, resolves parent entity'simport_reffor each item and setsParentImportRefin the export rowHow It Works
When exporting inventory to CSV, each item now includes a
HB.parent_import_refcolumn containing the parent item'simport_ref. If an item has no parent, the column is empty. This preserves the parent-child relationship so re-importing the CSV maintains the correct hierarchy.Closes #62
Summary by CodeRabbit
Release Notes
HB.parent_import_ref) displaying the parent import reference for each exported row. Parent references are automatically resolved from the related entity hierarchy.