Skip to content

feat: add parent import ref to CSV export for parent-child item relationships (#62)#1467

Open
yihangwu539-png wants to merge 1 commit into
sysadminsmedia:mainfrom
yihangwu539-png:feat/csv-export-parent-child
Open

feat: add parent import ref to CSV export for parent-child item relationships (#62)#1467
yihangwu539-png wants to merge 1 commit into
sysadminsmedia:mainfrom
yihangwu539-png:feat/csv-export-parent-child

Conversation

@yihangwu539-png
Copy link
Copy Markdown

@yihangwu539-png yihangwu539-png commented Apr 27, 2026

Description

Adds support for preserving parent-child item relationships in CSV export/import.

Changes

  • io_row.go — Added ParentImportRef string field with csv:"HB.parent_import_ref" tag to ExportCSVRow
  • io_sheet.go — In ReadItems, resolves parent entity's import_ref for each item and sets ParentImportRef in the export row

How It Works

When exporting inventory to CSV, each item now includes a HB.parent_import_ref column containing the parent item's import_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

  • New Features
    • CSV exports now include a new column (HB.parent_import_ref) displaying the parent import reference for each exported row. Parent references are automatically resolved from the related entity hierarchy.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Apr 27, 2026

Walkthrough

Two files in the reporting service are modified to support exporting parent-child item relationships in CSV format. A new ParentImportRef field is added to the export row struct and mapped to the CSV column HB.parent_import_ref. The export logic is extended to resolve parent entities during CSV generation.

Changes

Cohort / File(s) Summary
Parent Import Reference Export
backend/internal/core/services/reporting/io_row.go
Adds ParentImportRef string field to ExportCSVRow struct with CSV tag mapping to HB.parent_import_ref column. Preserves existing field mappings and updates struct field ordering.
Parent Lookup Logic
backend/internal/core/services/reporting/io_sheet.go
Extends ReadItems to resolve parent entities via repos.Entities.GetByID for each row, extracts parent's ImportRef, and populates the new ExportCSVRow.ParentImportRef field. Silently handles lookup errors.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Security Recommendations

When implementing parent-child relationship imports from the CSV, ensure that:

  1. Validation: Verify that the ParentImportRef values reference valid, existing entities before establishing parent-child relationships. Prevent users from creating circular references or orphaned relationships.

  2. Authorization: Confirm that users have appropriate permissions to establish parent-child relationships for the entities they're importing. Don't allow privilege escalation through CSV imports.

  3. Data Integrity: Log parent-child relationship changes during import operations for audit purposes, especially when modifying existing hierarchies.

Poem

📦 CSV columns now keep families together,
Parents and children, bound by their tether,
No more lonely items when import returns,
Family trees flourish as knowledge discerns! 🌳

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main change: adding parent import reference to CSV export for preserving parent-child item relationships.
Description check ✅ Passed The description includes PR type (feature), detailed explanation of changes with file names, how the feature works, and links to issue #62.
Linked Issues check ✅ Passed The PR directly addresses issue #62 by implementing the proposed solution: adding a CSV column to export and preserve parent-child relationships during export/import cycles.
Out of Scope Changes check ✅ Passed All code changes are directly scoped to the stated objective of adding parent import reference support to CSV export functionality.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

📥 Commits

Reviewing files that changed from the base of the PR and between 0e0c171 and e36f438.

📒 Files selected for processing (2)
  • backend/internal/core/services/reporting/io_row.go
  • backend/internal/core/services/reporting/io_sheet.go

Comment on lines +238 to +255
// 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,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Exporting inventory with parent and child items

1 participant