2021-01-12 11:59:51 +00:00
|
|
|
package project
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
2021-01-28 05:35:26 +00:00
|
|
|
"fmt"
|
2021-02-23 14:13:04 +00:00
|
|
|
"github.com/caos/zitadel/internal/eventstore"
|
2021-01-12 11:59:51 +00:00
|
|
|
|
|
|
|
"github.com/caos/zitadel/internal/errors"
|
2021-02-23 14:13:04 +00:00
|
|
|
"github.com/caos/zitadel/internal/eventstore/repository"
|
2021-01-12 11:59:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2021-02-15 12:31:24 +00:00
|
|
|
UniqueAppNameType = "appname"
|
2021-01-12 11:59:51 +00:00
|
|
|
applicationEventTypePrefix = projectEventTypePrefix + "application."
|
2021-01-28 05:35:26 +00:00
|
|
|
ApplicationAddedType = applicationEventTypePrefix + "added"
|
|
|
|
ApplicationChangedType = applicationEventTypePrefix + "changed"
|
|
|
|
ApplicationDeactivatedType = applicationEventTypePrefix + "deactivated"
|
|
|
|
ApplicationReactivatedType = applicationEventTypePrefix + "reactivated"
|
|
|
|
ApplicationRemovedType = applicationEventTypePrefix + "removed"
|
2021-01-12 11:59:51 +00:00
|
|
|
)
|
|
|
|
|
2021-01-28 05:35:26 +00:00
|
|
|
func NewAddApplicationUniqueConstraint(name, projectID string) *eventstore.EventUniqueConstraint {
|
|
|
|
return eventstore.NewAddEventUniqueConstraint(
|
2021-02-15 12:31:24 +00:00
|
|
|
UniqueAppNameType,
|
2021-01-28 05:35:26 +00:00
|
|
|
fmt.Sprintf("%s:%s", name, projectID),
|
|
|
|
"Errors.Project.App.AlreadyExists")
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewRemoveApplicationUniqueConstraint(name, projectID string) *eventstore.EventUniqueConstraint {
|
|
|
|
return eventstore.NewRemoveEventUniqueConstraint(
|
2021-02-15 12:31:24 +00:00
|
|
|
UniqueAppNameType,
|
2021-01-28 05:35:26 +00:00
|
|
|
fmt.Sprintf("%s:%s", name, projectID))
|
|
|
|
}
|
|
|
|
|
2021-01-12 11:59:51 +00:00
|
|
|
type ApplicationAddedEvent struct {
|
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
2021-03-15 11:51:15 +00:00
|
|
|
AppID string `json:"appId,omitempty"`
|
|
|
|
Name string `json:"name,omitempty"`
|
2021-01-12 11:59:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *ApplicationAddedEvent) Data() interface{} {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2021-01-21 09:49:38 +00:00
|
|
|
func (e *ApplicationAddedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
2021-03-15 11:51:15 +00:00
|
|
|
return []*eventstore.EventUniqueConstraint{NewAddApplicationUniqueConstraint(e.Name, e.Aggregate().ID)}
|
2021-01-21 09:49:38 +00:00
|
|
|
}
|
|
|
|
|
2021-02-18 13:48:27 +00:00
|
|
|
func NewApplicationAddedEvent(
|
|
|
|
ctx context.Context,
|
|
|
|
aggregate *eventstore.Aggregate,
|
|
|
|
appID,
|
2021-03-15 11:51:15 +00:00
|
|
|
name string,
|
2021-02-18 13:48:27 +00:00
|
|
|
) *ApplicationAddedEvent {
|
2021-01-12 11:59:51 +00:00
|
|
|
return &ApplicationAddedEvent{
|
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
2021-02-18 13:48:27 +00:00
|
|
|
aggregate,
|
2021-01-28 05:35:26 +00:00
|
|
|
ApplicationAddedType,
|
2021-01-12 11:59:51 +00:00
|
|
|
),
|
2021-03-15 11:51:15 +00:00
|
|
|
AppID: appID,
|
|
|
|
Name: name,
|
2021-01-12 11:59:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ApplicationAddedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
|
|
|
|
e := &ApplicationAddedEvent{
|
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
|
|
|
|
|
|
|
err := json.Unmarshal(event.Data, e)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.ThrowInternal(err, "APPLICATION-Nffg2", "unable to unmarshal application")
|
|
|
|
}
|
|
|
|
|
|
|
|
return e, nil
|
|
|
|
}
|
2021-01-28 05:35:26 +00:00
|
|
|
|
|
|
|
type ApplicationChangedEvent struct {
|
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
2021-03-15 11:51:15 +00:00
|
|
|
AppID string `json:"appId,omitempty"`
|
|
|
|
Name string `json:"name,omitempty"`
|
|
|
|
oldName string
|
2021-01-28 05:35:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *ApplicationChangedEvent) Data() interface{} {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *ApplicationChangedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
|
|
|
return []*eventstore.EventUniqueConstraint{
|
2021-03-15 11:51:15 +00:00
|
|
|
NewRemoveApplicationUniqueConstraint(e.oldName, e.Aggregate().ID),
|
|
|
|
NewAddApplicationUniqueConstraint(e.Name, e.Aggregate().ID),
|
2021-01-28 05:35:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-18 13:48:27 +00:00
|
|
|
func NewApplicationChangedEvent(
|
|
|
|
ctx context.Context,
|
|
|
|
aggregate *eventstore.Aggregate,
|
|
|
|
appID,
|
|
|
|
oldName,
|
2021-03-15 11:51:15 +00:00
|
|
|
newName string,
|
2021-02-18 13:48:27 +00:00
|
|
|
) *ApplicationChangedEvent {
|
2021-01-28 05:35:26 +00:00
|
|
|
return &ApplicationChangedEvent{
|
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
2021-02-18 13:48:27 +00:00
|
|
|
aggregate,
|
2021-01-28 05:35:26 +00:00
|
|
|
ApplicationChangedType,
|
|
|
|
),
|
2021-03-15 11:51:15 +00:00
|
|
|
AppID: appID,
|
|
|
|
Name: newName,
|
|
|
|
oldName: oldName,
|
2021-01-28 05:35:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ApplicationChangedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
|
|
|
|
e := &ApplicationChangedEvent{
|
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
|
|
|
|
|
|
|
err := json.Unmarshal(event.Data, e)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.ThrowInternal(err, "APPLICATION-9l0cs", "unable to unmarshal application")
|
|
|
|
}
|
|
|
|
|
|
|
|
return e, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type ApplicationDeactivatedEvent struct {
|
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
|
|
|
AppID string `json:"appId,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *ApplicationDeactivatedEvent) Data() interface{} {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *ApplicationDeactivatedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-18 13:48:27 +00:00
|
|
|
func NewApplicationDeactivatedEvent(
|
|
|
|
ctx context.Context,
|
|
|
|
aggregate *eventstore.Aggregate,
|
|
|
|
appID string,
|
|
|
|
) *ApplicationDeactivatedEvent {
|
2021-01-28 05:35:26 +00:00
|
|
|
return &ApplicationDeactivatedEvent{
|
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
2021-02-18 13:48:27 +00:00
|
|
|
aggregate,
|
2021-01-28 05:35:26 +00:00
|
|
|
ApplicationDeactivatedType,
|
|
|
|
),
|
|
|
|
AppID: appID,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ApplicationDeactivatedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
|
|
|
|
e := &ApplicationDeactivatedEvent{
|
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
|
|
|
|
|
|
|
err := json.Unmarshal(event.Data, e)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.ThrowInternal(err, "APPLICATION-0p9fB", "unable to unmarshal application")
|
|
|
|
}
|
|
|
|
|
|
|
|
return e, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type ApplicationReactivatedEvent struct {
|
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
|
|
|
AppID string `json:"appId,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *ApplicationReactivatedEvent) Data() interface{} {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *ApplicationReactivatedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-18 13:48:27 +00:00
|
|
|
func NewApplicationReactivatedEvent(
|
|
|
|
ctx context.Context,
|
|
|
|
aggregate *eventstore.Aggregate,
|
|
|
|
appID string,
|
|
|
|
) *ApplicationReactivatedEvent {
|
2021-01-28 05:35:26 +00:00
|
|
|
return &ApplicationReactivatedEvent{
|
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
2021-02-18 13:48:27 +00:00
|
|
|
aggregate,
|
2021-01-28 05:35:26 +00:00
|
|
|
ApplicationReactivatedType,
|
|
|
|
),
|
|
|
|
AppID: appID,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ApplicationReactivatedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
|
|
|
|
e := &ApplicationReactivatedEvent{
|
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
|
|
|
|
|
|
|
err := json.Unmarshal(event.Data, e)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.ThrowInternal(err, "APPLICATION-1m9e3", "unable to unmarshal application")
|
|
|
|
}
|
|
|
|
|
|
|
|
return e, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type ApplicationRemovedEvent struct {
|
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
2021-03-15 11:51:15 +00:00
|
|
|
AppID string `json:"appId,omitempty"`
|
|
|
|
name string
|
2021-01-28 05:35:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *ApplicationRemovedEvent) Data() interface{} {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *ApplicationRemovedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
2021-03-15 11:51:15 +00:00
|
|
|
return []*eventstore.EventUniqueConstraint{NewRemoveApplicationUniqueConstraint(e.name, e.Aggregate().ID)}
|
2021-01-28 05:35:26 +00:00
|
|
|
}
|
|
|
|
|
2021-02-18 13:48:27 +00:00
|
|
|
func NewApplicationRemovedEvent(
|
|
|
|
ctx context.Context,
|
|
|
|
aggregate *eventstore.Aggregate,
|
|
|
|
appID,
|
2021-03-15 11:51:15 +00:00
|
|
|
name string,
|
2021-02-18 13:48:27 +00:00
|
|
|
) *ApplicationRemovedEvent {
|
2021-01-28 05:35:26 +00:00
|
|
|
return &ApplicationRemovedEvent{
|
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
2021-02-18 13:48:27 +00:00
|
|
|
aggregate,
|
2021-01-28 05:35:26 +00:00
|
|
|
ApplicationRemovedType,
|
|
|
|
),
|
2021-03-15 11:51:15 +00:00
|
|
|
AppID: appID,
|
|
|
|
name: name,
|
2021-01-28 05:35:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ApplicationRemovedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
|
|
|
|
e := &ApplicationRemovedEvent{
|
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
|
|
|
|
|
|
|
err := json.Unmarshal(event.Data, e)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.ThrowInternal(err, "APPLICATION-1m9e3", "unable to unmarshal application")
|
|
|
|
}
|
|
|
|
|
|
|
|
return e, nil
|
|
|
|
}
|