zitadel/internal/iam/repository/view/model/metadata.go
Fabi 7451ed58f2
feat: User metadata (#2025)
* feat: user meta data events

* feat: user meta data set tests

* feat: user meta data tests

* feat: user meta data in protos

* feat: user meta data command api

* feat: user meta data query side

* feat: proto correct order, fix handlers

* feat: proto correct order

* feat: fixes of pr comments

* feat: fixes of pr comments

* feat: value as byte array

* feat: metadata feature

* Update internal/auth/repository/eventsourcing/handler/meta_data.go

Co-authored-by: Silvan <silvan.reusser@gmail.com>

* Update internal/command/user_meta_data.go

Co-authored-by: Silvan <silvan.reusser@gmail.com>

* Update proto/zitadel/metadata.proto

Co-authored-by: Silvan <silvan.reusser@gmail.com>

* Update proto/zitadel/metadata.proto

Co-authored-by: Silvan <silvan.reusser@gmail.com>

* fix: rename metadata files and table

* fix: rename meta data to metadat in protos

* Update internal/domain/metadata.go

Co-authored-by: Silvan <silvan.reusser@gmail.com>

* fix: rename vars

* fix: rebiuld docs

* Update internal/iam/repository/view/metadata_view.go

Co-authored-by: Silvan <silvan.reusser@gmail.com>

Co-authored-by: Silvan <silvan.reusser@gmail.com>
2021-08-09 08:01:20 +02:00

80 lines
2.1 KiB
Go

package model
import (
"encoding/json"
"time"
"github.com/caos/zitadel/internal/domain"
usr_model "github.com/caos/zitadel/internal/user/repository/eventsourcing/model"
"github.com/caos/logging"
caos_errs "github.com/caos/zitadel/internal/errors"
"github.com/caos/zitadel/internal/eventstore/v1/models"
)
const (
MetadataKeyAggregateID = "aggregate_id"
MetadataKeyResourceOwner = "resource_owner"
MetadataKeyKey = "key"
MetadataKeyValue = "value"
)
type MetadataView struct {
AggregateID string `json:"-" gorm:"column:aggregate_id;primary_key"`
ResourceOwner string `json:"-" gorm:"column:resource_owner"`
CreationDate time.Time `json:"-" gorm:"column:creation_date"`
ChangeDate time.Time `json:"-" gorm:"column:change_date"`
Key string `json:"key" gorm:"column:key;primary_key"`
Value []byte `json:"value" gorm:"column:value"`
Sequence uint64 `json:"-" gorm:"column:sequence"`
}
func MetadataViewsToDomain(texts []*MetadataView) []*domain.Metadata {
result := make([]*domain.Metadata, len(texts))
for i, text := range texts {
result[i] = MetadataViewToDomain(text)
}
return result
}
func MetadataViewToDomain(data *MetadataView) *domain.Metadata {
return &domain.Metadata{
ObjectRoot: models.ObjectRoot{
AggregateID: data.AggregateID,
Sequence: data.Sequence,
CreationDate: data.CreationDate,
ChangeDate: data.ChangeDate,
},
Key: data.Key,
Value: data.Value,
}
}
func (md *MetadataView) AppendEvent(event *models.Event) (err error) {
md.Sequence = event.Sequence
switch event.Type {
case usr_model.UserMetadataSet:
md.setRootData(event)
err = md.SetData(event)
}
return err
}
func (md *MetadataView) setRootData(event *models.Event) {
md.AggregateID = event.AggregateID
md.ResourceOwner = event.ResourceOwner
md.ChangeDate = event.CreationDate
md.Sequence = event.Sequence
}
func (md *MetadataView) SetData(event *models.Event) error {
if err := json.Unmarshal(event.Data, md); err != nil {
logging.Log("MODEL-3n9fs").WithError(err).Error("could not unmarshal event data")
return caos_errs.ThrowInternal(err, "MODEL-5CVaR", "Could not unmarshal data")
}
return nil
}