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