Skip to content

A continuation of HomeBox the inventory and organization system built for the Home User

Fix wipe inventory: delete maintenance records before items

copilot-swe-agent[bot]b99102e09382
diff --git a/backend/internal/data/repo/repo_items.go b/backend/internal/data/repo/repo_items.go
index 49943dc1..3d2390fc 100644
--- a/backend/internal/data/repo/repo_items.go
+++ b/backend/internal/data/repo/repo_items.go
@@ -810,6 +810,22 @@ func (e *ItemsRepository) DeleteByGroup(ctx context.Context, gid, id uuid.UUID)
}
func (e *ItemsRepository) WipeInventory(ctx context.Context, gid uuid.UUID, wipeLabels bool, wipeLocations bool, wipeMaintenance bool) (int, error) {
+ deleted := 0
+
+ // Wipe maintenance records if requested
+ // IMPORTANT: Must delete maintenance records BEFORE items since they are linked to items
+ if wipeMaintenance {
+ maintenanceCount, err := e.db.MaintenanceEntry.Delete().
+ Where(maintenanceentry.HasItemWith(item.HasGroupWith(group.ID(gid)))).
+ Exec(ctx)
+ if err != nil {
+ log.Err(err).Msg("failed to delete maintenance entries during wipe inventory")
+ } else {
+ log.Info().Int("count", maintenanceCount).Msg("deleted maintenance entries during wipe inventory")
+ deleted += maintenanceCount
+ }
+ }
+
// Get all items for the group
items, err := e.db.Item.Query().
Where(item.HasGroupWith(group.ID(gid))).
@@ -819,7 +835,6 @@ func (e *ItemsRepository) WipeInventory(ctx context.Context, gid uuid.UUID, wipe
return 0, err
}
- deleted := 0
// Delete each item with its attachments
// Note: We manually delete attachments and items instead of calling DeleteByGroup
// to continue processing remaining items even if some deletions fail
@@ -872,20 +887,6 @@ func (e *ItemsRepository) WipeInventory(ctx context.Context, gid uuid.UUID, wipe
}
}
- // Wipe maintenance records if requested
- if wipeMaintenance {
- // Maintenance entries are linked to items, so we query by items in the group
- maintenanceCount, err := e.db.MaintenanceEntry.Delete().
- Where(maintenanceentry.HasItemWith(item.HasGroupWith(group.ID(gid)))).
- Exec(ctx)
- if err != nil {
- log.Err(err).Msg("failed to delete maintenance entries during wipe inventory")
- } else {
- log.Info().Int("count", maintenanceCount).Msg("deleted maintenance entries during wipe inventory")
- deleted += maintenanceCount
- }
- }
-
e.publishMutationEvent(gid)
return deleted, nil
}