2020-10-20 17:10:23 +00:00
|
|
|
package model
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"time"
|
|
|
|
|
2021-03-25 13:41:07 +00:00
|
|
|
"github.com/caos/zitadel/internal/domain"
|
2020-10-20 17:10:23 +00:00
|
|
|
org_es_model "github.com/caos/zitadel/internal/org/repository/eventsourcing/model"
|
|
|
|
|
|
|
|
es_model "github.com/caos/zitadel/internal/iam/repository/eventsourcing/model"
|
|
|
|
|
|
|
|
"github.com/caos/logging"
|
|
|
|
caos_errs "github.com/caos/zitadel/internal/errors"
|
2021-02-23 14:13:04 +00:00
|
|
|
"github.com/caos/zitadel/internal/eventstore/v1/models"
|
2020-10-20 17:10:23 +00:00
|
|
|
"github.com/caos/zitadel/internal/iam/model"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
LabelPolicyKeyAggregateID = "aggregate_id"
|
|
|
|
)
|
|
|
|
|
|
|
|
type LabelPolicyView struct {
|
|
|
|
AggregateID string `json:"-" gorm:"column:aggregate_id;primary_key"`
|
|
|
|
CreationDate time.Time `json:"-" gorm:"column:creation_date"`
|
|
|
|
ChangeDate time.Time `json:"-" gorm:"column:change_date"`
|
|
|
|
State int32 `json:"-" gorm:"column:label_policy_state"`
|
|
|
|
|
2021-03-25 13:41:07 +00:00
|
|
|
PrimaryColor string `json:"primaryColor" gorm:"column:primary_color"`
|
|
|
|
SecondaryColor string `json:"secondaryColor" gorm:"column:secondary_color"`
|
|
|
|
HideLoginNameSuffix bool `json:"hideLoginNameSuffix" gorm:"column:hide_login_name_suffix"`
|
|
|
|
Default bool `json:"-" gorm:"-"`
|
2020-10-20 17:10:23 +00:00
|
|
|
|
|
|
|
Sequence uint64 `json:"-" gorm:"column:sequence"`
|
|
|
|
}
|
|
|
|
|
2021-03-25 13:41:07 +00:00
|
|
|
func (p *LabelPolicyView) ToDomain() *domain.LabelPolicy {
|
|
|
|
return &domain.LabelPolicy{
|
|
|
|
ObjectRoot: models.ObjectRoot{
|
|
|
|
AggregateID: p.AggregateID,
|
|
|
|
CreationDate: p.CreationDate,
|
|
|
|
ChangeDate: p.ChangeDate,
|
|
|
|
Sequence: p.Sequence,
|
|
|
|
},
|
|
|
|
Default: p.Default,
|
|
|
|
PrimaryColor: p.PrimaryColor,
|
|
|
|
SecondaryColor: p.SecondaryColor,
|
|
|
|
HideLoginNameSuffix: p.HideLoginNameSuffix,
|
2020-10-20 17:10:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func LabelPolicyViewToModel(policy *LabelPolicyView) *model.LabelPolicyView {
|
|
|
|
return &model.LabelPolicyView{
|
2021-03-25 13:41:07 +00:00
|
|
|
AggregateID: policy.AggregateID,
|
|
|
|
Sequence: policy.Sequence,
|
|
|
|
CreationDate: policy.CreationDate,
|
|
|
|
ChangeDate: policy.ChangeDate,
|
|
|
|
PrimaryColor: policy.PrimaryColor,
|
|
|
|
SecondaryColor: policy.SecondaryColor,
|
|
|
|
HideLoginNameSuffix: policy.HideLoginNameSuffix,
|
|
|
|
Default: policy.Default,
|
2020-10-20 17:10:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *LabelPolicyView) AppendEvent(event *models.Event) (err error) {
|
|
|
|
i.Sequence = event.Sequence
|
|
|
|
i.ChangeDate = event.CreationDate
|
|
|
|
switch event.Type {
|
|
|
|
case es_model.LabelPolicyAdded, org_es_model.LabelPolicyAdded:
|
|
|
|
i.setRootData(event)
|
|
|
|
i.CreationDate = event.CreationDate
|
|
|
|
err = i.SetData(event)
|
|
|
|
case es_model.LabelPolicyChanged, org_es_model.LabelPolicyChanged:
|
|
|
|
err = i.SetData(event)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *LabelPolicyView) setRootData(event *models.Event) {
|
|
|
|
r.AggregateID = event.AggregateID
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *LabelPolicyView) SetData(event *models.Event) error {
|
|
|
|
if err := json.Unmarshal(event.Data, r); err != nil {
|
|
|
|
logging.Log("MODEL-Flp9C").WithError(err).Error("could not unmarshal event data")
|
|
|
|
return caos_errs.ThrowInternal(err, "MODEL-Hs8uf", "Could not unmarshal data")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|