2020-06-15 14:50:39 +02:00
|
|
|
package model
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2020-10-16 07:49:38 +02:00
|
|
|
"time"
|
|
|
|
|
2020-06-15 14:50:39 +02:00
|
|
|
"github.com/caos/logging"
|
2020-10-16 07:49:38 +02:00
|
|
|
|
2021-08-24 08:34:10 +02:00
|
|
|
"github.com/caos/zitadel/internal/domain"
|
2020-06-15 14:50:39 +02:00
|
|
|
caos_errs "github.com/caos/zitadel/internal/errors"
|
2021-02-23 15:13:04 +01:00
|
|
|
"github.com/caos/zitadel/internal/eventstore/v1/models"
|
2020-06-15 14:50:39 +02:00
|
|
|
"github.com/caos/zitadel/internal/project/model"
|
|
|
|
es_model "github.com/caos/zitadel/internal/project/repository/eventsourcing/model"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
ProjectKeyProjectID = "project_id"
|
|
|
|
ProjectKeyResourceOwner = "resource_owner"
|
|
|
|
ProjectKeyName = "project_name"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ProjectView struct {
|
2021-08-24 08:34:10 +02:00
|
|
|
ProjectID string `json:"-" gorm:"column:project_id;primary_key"`
|
|
|
|
Name string `json:"name" gorm:"column:project_name"`
|
|
|
|
CreationDate time.Time `json:"-" gorm:"column:creation_date"`
|
|
|
|
ChangeDate time.Time `json:"-" gorm:"column:change_date"`
|
|
|
|
State int32 `json:"-" gorm:"column:project_state"`
|
|
|
|
ResourceOwner string `json:"-" gorm:"column:resource_owner"`
|
|
|
|
ProjectRoleAssertion bool `json:"projectRoleAssertion" gorm:"column:project_role_assertion"`
|
|
|
|
ProjectRoleCheck bool `json:"projectRoleCheck" gorm:"column:project_role_check"`
|
|
|
|
HasProjectCheck bool `json:"hasProjectCheck" gorm:"column:has_project_check"`
|
|
|
|
PrivateLabelingSetting domain.PrivateLabelingSetting `json:"privateLabelingSetting" gorm:"column:private_labeling_setting"`
|
|
|
|
Sequence uint64 `json:"-" gorm:"column:sequence"`
|
2020-06-15 14:50:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func ProjectToModel(project *ProjectView) *model.ProjectView {
|
|
|
|
return &model.ProjectView{
|
2021-08-24 08:34:10 +02:00
|
|
|
ProjectID: project.ProjectID,
|
|
|
|
Name: project.Name,
|
|
|
|
ChangeDate: project.ChangeDate,
|
|
|
|
CreationDate: project.CreationDate,
|
|
|
|
State: model.ProjectState(project.State),
|
|
|
|
ResourceOwner: project.ResourceOwner,
|
|
|
|
ProjectRoleAssertion: project.ProjectRoleAssertion,
|
|
|
|
ProjectRoleCheck: project.ProjectRoleCheck,
|
|
|
|
HasProjectCheck: project.HasProjectCheck,
|
|
|
|
PrivateLabelingSetting: project.PrivateLabelingSetting,
|
|
|
|
Sequence: project.Sequence,
|
2020-06-15 14:50:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ProjectsToModel(projects []*ProjectView) []*model.ProjectView {
|
|
|
|
result := make([]*model.ProjectView, len(projects))
|
|
|
|
for i, p := range projects {
|
|
|
|
result[i] = ProjectToModel(p)
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *ProjectView) AppendEvent(event *models.Event) (err error) {
|
|
|
|
p.ChangeDate = event.CreationDate
|
|
|
|
p.Sequence = event.Sequence
|
|
|
|
switch event.Type {
|
|
|
|
case es_model.ProjectAdded:
|
2020-06-23 14:47:47 +02:00
|
|
|
p.State = int32(model.ProjectStateActive)
|
2020-06-15 14:50:39 +02:00
|
|
|
p.CreationDate = event.CreationDate
|
|
|
|
p.setRootData(event)
|
|
|
|
err = p.setData(event)
|
|
|
|
case es_model.ProjectChanged:
|
|
|
|
err = p.setData(event)
|
|
|
|
case es_model.ProjectDeactivated:
|
2020-06-23 14:47:47 +02:00
|
|
|
p.State = int32(model.ProjectStateInactive)
|
2020-06-15 14:50:39 +02:00
|
|
|
case es_model.ProjectReactivated:
|
2020-06-23 14:47:47 +02:00
|
|
|
p.State = int32(model.ProjectStateActive)
|
2020-10-07 08:16:42 +02:00
|
|
|
case es_model.ProjectRemoved:
|
|
|
|
p.State = int32(model.ProjectStateRemoved)
|
2020-06-15 14:50:39 +02:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *ProjectView) setRootData(event *models.Event) {
|
|
|
|
p.ProjectID = event.AggregateID
|
|
|
|
p.ResourceOwner = event.ResourceOwner
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *ProjectView) setData(event *models.Event) error {
|
|
|
|
if err := json.Unmarshal(event.Data, p); err != nil {
|
|
|
|
logging.Log("EVEN-dlo92").WithError(err).Error("could not unmarshal event data")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *ProjectView) setProjectData(event *models.Event) error {
|
|
|
|
project := new(ProjectView)
|
|
|
|
err := project.SetData(event)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *ProjectView) SetData(event *models.Event) error {
|
|
|
|
if err := json.Unmarshal(event.Data, p); err != nil {
|
|
|
|
logging.Log("EVEN-sk9Sj").WithError(err).Error("could not unmarshal event data")
|
|
|
|
return caos_errs.ThrowInternal(err, "MODEL-s9ols", "Could not unmarshal data")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|