Make the tests pass, hopefully.

This commit is contained in:
slid1amo2n3e4
2024-12-10 18:48:49 +01:00
parent daf07d4f35
commit 63eb287485
5 changed files with 97 additions and 66 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -1,36 +1,35 @@
<template>
<div v-if="!inline" class="form-control w-full">
<label class="label cursor-pointer">
<input v-model="value" type="checkbox" class="toggle toggle-primary" />
<span class="label-text"> {{ label }}</span>
</label>
</div>
<div v-else class="label cursor-pointer sm:grid sm:grid-cols-4 sm:items-start sm:gap-4">
<label>
<span class="label-text">
{{ label }}
</span>
</label>
<div v-if="!inline" class="form-control w-full">
<label class="label cursor-pointer">
<input v-model="value" type="checkbox" class="toggle toggle-primary" />
</div>
</template>
<script setup lang="ts">
const props = defineProps({
modelValue: {
type: Boolean,
default: false,
},
inline: {
type: Boolean,
default: false,
},
label: {
type: String,
default: "",
},
});
const value = useVModel(props, "modelValue");
</script>
<span class="label-text"> {{ label }}</span>
</label>
</div>
<div v-else class="label cursor-pointer sm:grid sm:grid-cols-4 sm:items-start sm:gap-4">
<label>
<span class="label-text">
{{ label }}
</span>
</label>
<input v-model="value" type="checkbox" class="toggle toggle-primary" />
</div>
</template>
<script setup lang="ts">
const props = defineProps({
modelValue: {
type: Boolean,
default: false,
},
inline: {
type: Boolean,
default: false,
},
label: {
type: String,
default: "",
},
});
const value = useVModel(props, "modelValue");
</script>

View File

@@ -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);

View File

@@ -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 @@
<div class="mb-6 grid gap-4 border-t px-5 pt-2 md:grid-cols-2">
<LocationSelector v-model="item.location" @update:model-value="informAboutDesyncingLocationFromParent()" />
<FormMultiselect v-model="item.labels" :label="$t('global.labels')" :items="labels ?? []" />
<FormToggle v-model="item.syncChildItemsLocations" label="Sync child items' locations" inline @update:model-value="syncChildItemsLocations()" />
<FormToggle
v-model="item.syncChildItemsLocations"
label="Sync child items' locations"
inline
@update:model-value="syncChildItemsLocations()"
/>
<Autocomplete
v-if="preferences.editorAdvancedView"
v-model="parent"