diff --git a/backend/internal/data/ent/item_create.go b/backend/internal/data/ent/item_create.go index 97ed01ea..a9b79cdd 100644 --- a/backend/internal/data/ent/item_create.go +++ b/backend/internal/data/ent/item_create.go @@ -159,6 +159,20 @@ func (ic *ItemCreate) SetNillableAssetID(i *int) *ItemCreate { return ic } +// SetSyncChildItemsLocations sets the "sync_child_items_locations" field. +func (ic *ItemCreate) SetSyncChildItemsLocations(b bool) *ItemCreate { + ic.mutation.SetSyncChildItemsLocations(b) + return ic +} + +// SetNillableSyncChildItemsLocations sets the "sync_child_items_locations" field if the given value is not nil. +func (ic *ItemCreate) SetNillableSyncChildItemsLocations(b *bool) *ItemCreate { + if b != nil { + ic.SetSyncChildItemsLocations(*b) + } + return ic +} + // SetSerialNumber sets the "serial_number" field. func (ic *ItemCreate) SetSerialNumber(s string) *ItemCreate { ic.mutation.SetSerialNumber(s) @@ -538,6 +552,10 @@ func (ic *ItemCreate) defaults() { v := item.DefaultAssetID ic.mutation.SetAssetID(v) } + if _, ok := ic.mutation.SyncChildItemsLocations(); !ok { + v := item.DefaultSyncChildItemsLocations + ic.mutation.SetSyncChildItemsLocations(v) + } if _, ok := ic.mutation.LifetimeWarranty(); !ok { v := item.DefaultLifetimeWarranty ic.mutation.SetLifetimeWarranty(v) @@ -599,6 +617,9 @@ func (ic *ItemCreate) check() error { if _, ok := ic.mutation.AssetID(); !ok { return &ValidationError{Name: "asset_id", err: errors.New(`ent: missing required field "Item.asset_id"`)} } + if _, ok := ic.mutation.SyncChildItemsLocations(); !ok { + return &ValidationError{Name: "sync_child_items_locations", err: errors.New(`ent: missing required field "Item.sync_child_items_locations"`)} + } if v, ok := ic.mutation.SerialNumber(); ok { if err := item.SerialNumberValidator(v); err != nil { return &ValidationError{Name: "serial_number", err: fmt.Errorf(`ent: validator failed for field "Item.serial_number": %w`, err)} @@ -711,6 +732,10 @@ func (ic *ItemCreate) createSpec() (*Item, *sqlgraph.CreateSpec) { _spec.SetField(item.FieldAssetID, field.TypeInt, value) _node.AssetID = value } + if value, ok := ic.mutation.SyncChildItemsLocations(); ok { + _spec.SetField(item.FieldSyncChildItemsLocations, field.TypeBool, value) + _node.SyncChildItemsLocations = value + } if value, ok := ic.mutation.SerialNumber(); ok { _spec.SetField(item.FieldSerialNumber, field.TypeString, value) _node.SerialNumber = value diff --git a/backend/internal/data/repo/repo_items.go b/backend/internal/data/repo/repo_items.go index ff3562aa..7cb91553 100644 --- a/backend/internal/data/repo/repo_items.go +++ b/backend/internal/data/repo/repo_items.go @@ -55,11 +55,11 @@ type ( } ItemCreate struct { - ImportRef string `json:"-"` - ParentID uuid.UUID `json:"parentId" extensions:"x-nullable"` - Name string `json:"name" validate:"required,min=1,max=255"` - Description string `json:"description" validate:"max=1000"` - AssetID AssetID `json:"-"` + ImportRef string `json:"-"` + ParentID uuid.UUID `json:"parentId" extensions:"x-nullable"` + Name string `json:"name" validate:"required,min=1,max=255"` + Description string `json:"description" validate:"max=1000"` + AssetID AssetID `json:"-"` // Edges LocationID uuid.UUID `json:"locationId"` @@ -67,11 +67,11 @@ type ( } ItemUpdate struct { - ParentID uuid.UUID `json:"parentId" extensions:"x-nullable,x-omitempty"` + ParentID uuid.UUID `json:"parentId" extensions:"x-nullable,x-omitempty"` ID uuid.UUID `json:"id"` - AssetID AssetID `json:"assetId" swaggertype:"string"` - Name string `json:"name" validate:"required,min=1,max=255"` - Description string `json:"description" validate:"max=1000"` + AssetID AssetID `json:"assetId" swaggertype:"string"` + Name string `json:"name" validate:"required,min=1,max=255"` + Description string `json:"description" validate:"max=1000"` Quantity int `json:"quantity"` Insured bool `json:"insured"` Archived bool `json:"archived"` @@ -93,12 +93,12 @@ type ( // Purchase PurchaseTime types.Date `json:"purchaseTime"` - PurchaseFrom string `json:"purchaseFrom" validate:"max=255"` + PurchaseFrom string `json:"purchaseFrom" validate:"max=255"` PurchasePrice float64 `json:"purchasePrice" extensions:"x-nullable,x-omitempty"` // Sold SoldTime types.Date `json:"soldTime"` - SoldTo string `json:"soldTo" validate:"max=255"` + SoldTo string `json:"soldTo" validate:"max=255"` SoldPrice float64 `json:"soldPrice" extensions:"x-nullable,x-omitempty"` SoldNotes string `json:"soldNotes"` @@ -651,7 +651,7 @@ func (e *ItemsRepository) UpdateByGroup(ctx context.Context, gid uuid.UUID, data return ItemOut{}, err } - if location != child_location.ID { + if location != childLocation.ID { err = child.Update().SetLocationID(location).Exec(ctx) if err != nil { return ItemOut{}, err diff --git a/frontend/components/Form/Toggle.vue b/frontend/components/Form/Toggle.vue index 2e68d63e..e22decd5 100644 --- a/frontend/components/Form/Toggle.vue +++ b/frontend/components/Form/Toggle.vue @@ -1,36 +1,35 @@ - - - \ No newline at end of file + {{ label }} + + +
+ + +
+ + + diff --git a/frontend/lib/api/__test__/user/items.test.ts b/frontend/lib/api/__test__/user/items.test.ts index 83736a1b..196c6c51 100644 --- a/frontend/lib/api/__test__/user/items.test.ts +++ b/frontend/lib/api/__test__/user/items.test.ts @@ -205,7 +205,7 @@ describe("user should be able to create an item and add an attachment", () => { }); expect(parentResponse.status).toBe(201); expect(parent.id).toBeTruthy(); - + const { response: child1Response, data: child1Item } = await api.items.create({ name: "child1-item", labelIds: [], @@ -217,9 +217,9 @@ describe("user should be able to create an item and add an attachment", () => { parentId: parent.id, ...child1Item, locationId: child1Item.location?.id, - labelIds: [] + labelIds: [], }; - const { response: child1UpdatedResponse, data: child1UpdatedData } = await api.items.update(child1Item.id, child1ItemUpdate as ItemUpdate); + const { response: child1UpdatedResponse } = await api.items.update(child1Item.id, child1ItemUpdate as ItemUpdate); expect(child1UpdatedResponse.status).toBe(200); const { response: child2Response, data: child2Item } = await api.items.create({ @@ -233,9 +233,9 @@ describe("user should be able to create an item and add an attachment", () => { parentId: parent.id, ...child2Item, locationId: child2Item.location?.id, - labelIds: [] - } - const { response: child2UpdatedResponse, data: child2UpdatedData } = await api.items.update(child2Item.id, child2ItemUpdate as ItemUpdate); + labelIds: [], + }; + const { response: child2UpdatedResponse } = await api.items.update(child2Item.id, child2ItemUpdate as ItemUpdate); expect(child2UpdatedResponse.status).toBe(200); const itemUpdate = { @@ -243,9 +243,9 @@ describe("user should be able to create an item and add an attachment", () => { ...parent, locationId: parentLocation.id, labelIds: [], - syncChildItemsLocations: true + syncChildItemsLocations: true, }; - const { response: updateResponse, data: updateData } = await api.items.update(parent.id, itemUpdate) + const { response: updateResponse } = await api.items.update(parent.id, itemUpdate); expect(updateResponse.status).toBe(200); const { response: child1FinalResponse, data: child1FinalData } = await api.items.get(child1Item.id); diff --git a/frontend/pages/item/[id]/index/edit.vue b/frontend/pages/item/[id]/index/edit.vue index 5bda88b9..27799fc2 100644 --- a/frontend/pages/item/[id]/index/edit.vue +++ b/frontend/pages/item/[id]/index/edit.vue @@ -468,16 +468,17 @@ async function maybeSyncWithParentLocation() { if (parent.value && parent.value.id) { - const { data, error } = await api.items.get(parent.value.id); + const { data, error } = await api.items.get(parent.value.id); - if (error) { - toast.error("Something went wrong trying to load parent data"); - } else { - if (data.syncChildItemsLocations) { - toast.info("Selected parent syncs its children's locations to its own. The location has been updated."); - item.value.location = data.location - } - } + if (error) { + toast.error("Something went wrong trying to load parent data"); + return; + } + + if (data.syncChildItemsLocations) { + toast.info("Selected parent syncs its children's locations to its own. The location has been updated."); + item.value.location = data.location; + } } } @@ -487,6 +488,7 @@ if (error) { toast.error("Something went wrong trying to load parent data"); + return; } if (data.syncChildItemsLocations) { @@ -500,7 +502,7 @@ toast.error("Failed to save item: no location selected"); return; } - + const payload: ItemUpdate = { ...item.value, locationId: item.value.location?.id, @@ -517,7 +519,7 @@ } if (!item.value.syncChildItemsLocations) { - toast.success("Child items' locations will no longer be synced with this item.") + toast.success("Child items' locations will no longer be synced with this item."); } else { toast.success("Child items' locations have been synced with this item"); } @@ -592,7 +594,12 @@
- +