diff --git a/server/internal/usecase/interactor/item.go b/server/internal/usecase/interactor/item.go index 3d689348b6..545d4360d8 100644 --- a/server/internal/usecase/interactor/item.go +++ b/server/internal/usecase/interactor/item.go @@ -296,7 +296,7 @@ func (i Item) Create(ctx context.Context, param interfaces.CreateItemParam, oper Item: vi.Value(), Model: m, Schema: s, - GroupSchemas: groupSchemas, + GroupSchemas: lo.Values(groupSchemas), ReferencedItems: i.getReferencedItems(ctx, fields), }, Operator: operator.Operator(), @@ -393,6 +393,9 @@ func (i Item) Update(ctx context.Context, param interfaces.UpdateItemParam, oper } } + sp := schema.NewPackage(s, nil, groupSchemas) + itv.Prune(sp) + if err := i.repos.Item.Save(ctx, itv); err != nil { return nil, err } @@ -410,7 +413,7 @@ func (i Item) Update(ctx context.Context, param interfaces.UpdateItemParam, oper Item: itv, Model: m, Schema: s, - GroupSchemas: groupSchemas, + GroupSchemas: lo.Values(groupSchemas), ReferencedItems: i.getReferencedItems(ctx, fields), Changes: item.CompareFields(itv.Fields(), oldFields), }, @@ -718,11 +721,14 @@ func (i Item) handleReferenceFields(ctx context.Context, s schema.Schema, it *it return nil } -func (i Item) handleGroupFields(ctx context.Context, params []interfaces.ItemFieldParam, s *schema.Schema, mId id.ModelID, itemFields item.Fields) (item.Fields, schema.List, error) { +func (i Item) handleGroupFields(ctx context.Context, params []interfaces.ItemFieldParam, s *schema.Schema, mId id.ModelID, itemFields item.Fields) (item.Fields, map[id.GroupID]*schema.Schema, error) { var res item.Fields - var groupSchemas schema.List + groupSchemas := map[id.GroupID]*schema.Schema{} for _, field := range itemFields.FieldsByType(value.TypeGroup) { sf := s.Field(field.FieldID()) + if sf == nil { + continue + } var fieldGroup *schema.FieldGroup sf.TypeProperty().Match(schema.TypePropertyMatch{ Group: func(f *schema.FieldGroup) { @@ -741,7 +747,7 @@ func (i Item) handleGroupFields(ctx context.Context, params []interfaces.ItemFie } if groupSchema != nil { - groupSchemas = append(groupSchemas, groupSchema) + groupSchemas[group.ID()] = groupSchema } mvg, ok := field.Value().ValuesGroup() diff --git a/server/pkg/item/item.go b/server/pkg/item/item.go index 5c35e4fcc1..0f546bddee 100644 --- a/server/pkg/item/item.go +++ b/server/pkg/item/item.go @@ -255,3 +255,22 @@ func (i *Item) SetMetadataItem(iid id.ItemID) { func (i *Item) SetOriginalItem(iid id.ItemID) { i.originalItem = &iid } + +func (i *Item) Prune(sp *schema.Package) { + var filtered, res Fields = lo.Filter(slices.Clone(i.Fields()), func(f *Field, index int) bool { + return sp.Field(f.FieldID()) != nil + }), Fields{} + for _, fd := range filtered { + if fd.Type() == value.TypeGroup { + gvl, _ := fd.Value().ValuesGroup() + for _, gv := range gvl { + res = append(res, filtered.FieldsByGroup(gv)...) + } + } + if fd.ItemGroup() == nil { + res = append(res, fd) + } + } + + i.fields = res +} diff --git a/server/pkg/schema/package.go b/server/pkg/schema/package.go index dce17511db..a9a58faaf1 100644 --- a/server/pkg/schema/package.go +++ b/server/pkg/schema/package.go @@ -52,9 +52,11 @@ func (p *Package) Field(fieldID id.FieldID) *Field { if f != nil { return f } - f = p.metaSchema.Field(fieldID) - if f != nil { - return f + if p.metaSchema != nil { + f = p.metaSchema.Field(fieldID) + if f != nil { + return f + } } for _, s := range p.groupSchemas { f = s.Field(fieldID)