2021-01-12 11:59:51 +00:00
|
|
|
package project
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
|
|
|
|
"github.com/caos/zitadel/internal/errors"
|
|
|
|
"github.com/caos/zitadel/internal/eventstore/v2"
|
|
|
|
"github.com/caos/zitadel/internal/eventstore/v2/repository"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2021-01-21 09:49:38 +00:00
|
|
|
uniqueProjectnameTable = "project_names"
|
2021-01-12 11:59:51 +00:00
|
|
|
projectEventTypePrefix = eventstore.EventType("project.")
|
|
|
|
ProjectAdded = projectEventTypePrefix + "added"
|
|
|
|
ProjectChanged = projectEventTypePrefix + "changed"
|
|
|
|
ProjectDeactivated = projectEventTypePrefix + "deactivated"
|
|
|
|
ProjectReactivated = projectEventTypePrefix + "reactivated"
|
|
|
|
ProjectRemoved = projectEventTypePrefix + "removed"
|
|
|
|
)
|
|
|
|
|
2021-01-21 09:49:38 +00:00
|
|
|
func NewAddProjectNameUniqueConstraint(projectName, resourceOwner string) *eventstore.EventUniqueConstraint {
|
|
|
|
return eventstore.NewAddEventUniqueConstraint(
|
|
|
|
uniqueProjectnameTable,
|
|
|
|
projectName+resourceOwner,
|
|
|
|
"Errors.Project.AlreadyExists")
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewRemoveProjectNameUniqueConstraint(projectName, resourceOwner string) *eventstore.EventUniqueConstraint {
|
|
|
|
return eventstore.NewRemoveEventUniqueConstraint(
|
|
|
|
uniqueProjectnameTable,
|
|
|
|
projectName+resourceOwner)
|
|
|
|
}
|
|
|
|
|
2021-01-12 11:59:51 +00:00
|
|
|
type ProjectAddedEvent struct {
|
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
|
|
|
Name string `json:"name,omitempty"`
|
|
|
|
ProjectRoleAssertion bool `json:"projectRoleAssertion,omitempty"`
|
|
|
|
ProjectRoleCheck bool `json:"projectRoleCheck,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *ProjectAddedEvent) Data() interface{} {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2021-01-21 09:49:38 +00:00
|
|
|
func (e *ProjectAddedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
|
|
|
return []*eventstore.EventUniqueConstraint{NewAddProjectNameUniqueConstraint(e.Name, e.ResourceOwner())}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewProjectAddedEvent(ctx context.Context, name, resourceOwner string) *ProjectAddedEvent {
|
2021-01-12 11:59:51 +00:00
|
|
|
return &ProjectAddedEvent{
|
2021-01-21 09:49:38 +00:00
|
|
|
BaseEvent: *eventstore.NewBaseEventForPushWithResourceOwner(
|
2021-01-12 11:59:51 +00:00
|
|
|
ctx,
|
|
|
|
ProjectAdded,
|
2021-01-21 09:49:38 +00:00
|
|
|
resourceOwner,
|
2021-01-12 11:59:51 +00:00
|
|
|
),
|
|
|
|
Name: name,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ProjectAddedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
|
|
|
|
e := &ProjectAddedEvent{
|
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
|
|
|
|
|
|
|
err := json.Unmarshal(event.Data, e)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.ThrowInternal(err, "PROJECT-Bfg2f", "unable to unmarshal project")
|
|
|
|
}
|
|
|
|
|
|
|
|
return e, nil
|
|
|
|
}
|