mirror of
https://github.com/zitadel/zitadel.git
synced 2025-08-12 01:37:31 +00:00
refactor(v2): init events (#7823)
creates events structures for initial projections and read models
This commit is contained in:
62
internal/v2/org/added.go
Normal file
62
internal/v2/org/added.go
Normal file
@@ -0,0 +1,62 @@
|
||||
package org
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
|
||||
"github.com/zitadel/zitadel/internal/api/authz"
|
||||
"github.com/zitadel/zitadel/internal/v2/eventstore"
|
||||
"github.com/zitadel/zitadel/internal/zerrors"
|
||||
)
|
||||
|
||||
const AddedType = eventTypePrefix + "added"
|
||||
|
||||
type addedPayload struct {
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
type AddedEvent eventstore.Event[addedPayload]
|
||||
|
||||
var _ eventstore.TypeChecker = (*AddedEvent)(nil)
|
||||
|
||||
// ActionType implements eventstore.Typer.
|
||||
func (c *AddedEvent) ActionType() string {
|
||||
return AddedType
|
||||
}
|
||||
|
||||
func AddedEventFromStorage(event *eventstore.StorageEvent) (e *AddedEvent, _ error) {
|
||||
if event.Type != e.ActionType() {
|
||||
return nil, zerrors.ThrowInvalidArgument(nil, "ORG-Nf3tr", "Errors.Invalid.Event.Type")
|
||||
}
|
||||
|
||||
payload, err := eventstore.UnmarshalPayload[addedPayload](event.Payload)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &AddedEvent{
|
||||
StorageEvent: event,
|
||||
Payload: payload,
|
||||
}, nil
|
||||
}
|
||||
|
||||
const uniqueOrgName = "org_name"
|
||||
|
||||
func NewAddedCommand(ctx context.Context, name string) (*eventstore.Command, error) {
|
||||
if name = strings.TrimSpace(name); name == "" {
|
||||
return nil, zerrors.ThrowInvalidArgument(nil, "ORG-mruNY", "Errors.Invalid.Argument")
|
||||
}
|
||||
return &eventstore.Command{
|
||||
Action: eventstore.Action[any]{
|
||||
Creator: authz.GetCtxData(ctx).UserID,
|
||||
Type: AddedType,
|
||||
Revision: 1,
|
||||
Payload: addedPayload{
|
||||
Name: name,
|
||||
},
|
||||
},
|
||||
UniqueConstraints: []*eventstore.UniqueConstraint{
|
||||
eventstore.NewAddEventUniqueConstraint(uniqueOrgName, name, "Errors.Org.AlreadyExists"),
|
||||
},
|
||||
}, nil
|
||||
}
|
22
internal/v2/org/aggregate.go
Normal file
22
internal/v2/org/aggregate.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package org
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/zitadel/zitadel/internal/api/authz"
|
||||
"github.com/zitadel/zitadel/internal/v2/eventstore"
|
||||
)
|
||||
|
||||
const (
|
||||
AggregateType = "org"
|
||||
eventTypePrefix = AggregateType + "."
|
||||
)
|
||||
|
||||
func NewAggregate(ctx context.Context, id string) *eventstore.Aggregate {
|
||||
return &eventstore.Aggregate{
|
||||
ID: id,
|
||||
Type: AggregateType,
|
||||
Instance: authz.GetInstance(ctx).InstanceID(),
|
||||
Owner: authz.GetCtxData(ctx).OrgID,
|
||||
}
|
||||
}
|
37
internal/v2/org/changed.go
Normal file
37
internal/v2/org/changed.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package org
|
||||
|
||||
import (
|
||||
"github.com/zitadel/zitadel/internal/v2/eventstore"
|
||||
"github.com/zitadel/zitadel/internal/zerrors"
|
||||
)
|
||||
|
||||
const ChangedType = eventTypePrefix + "changed"
|
||||
|
||||
type changedPayload struct {
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
type ChangedEvent eventstore.Event[changedPayload]
|
||||
|
||||
var _ eventstore.TypeChecker = (*ChangedEvent)(nil)
|
||||
|
||||
// ActionType implements eventstore.Typer.
|
||||
func (c *ChangedEvent) ActionType() string {
|
||||
return ChangedType
|
||||
}
|
||||
|
||||
func ChangedEventFromStorage(event *eventstore.StorageEvent) (e *ChangedEvent, _ error) {
|
||||
if event.Type != e.ActionType() {
|
||||
return nil, zerrors.ThrowInvalidArgument(nil, "ORG-pzOfP", "Errors.Invalid.Event.Type")
|
||||
}
|
||||
|
||||
payload, err := eventstore.UnmarshalPayload[changedPayload](event.Payload)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &ChangedEvent{
|
||||
StorageEvent: event,
|
||||
Payload: payload,
|
||||
}, nil
|
||||
}
|
27
internal/v2/org/deactivated.go
Normal file
27
internal/v2/org/deactivated.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package org
|
||||
|
||||
import (
|
||||
"github.com/zitadel/zitadel/internal/v2/eventstore"
|
||||
"github.com/zitadel/zitadel/internal/zerrors"
|
||||
)
|
||||
|
||||
const DeactivatedType = eventTypePrefix + "deactivated"
|
||||
|
||||
type DeactivatedEvent eventstore.Event[eventstore.EmptyPayload]
|
||||
|
||||
var _ eventstore.TypeChecker = (*DeactivatedEvent)(nil)
|
||||
|
||||
// ActionType implements eventstore.Typer.
|
||||
func (c *DeactivatedEvent) ActionType() string {
|
||||
return DeactivatedType
|
||||
}
|
||||
|
||||
func DeactivatedEventFromStorage(event *eventstore.StorageEvent) (e *DeactivatedEvent, _ error) {
|
||||
if event.Type != e.ActionType() {
|
||||
return nil, zerrors.ThrowInvalidArgument(nil, "ORG-4zeWH", "Errors.Invalid.Event.Type")
|
||||
}
|
||||
|
||||
return &DeactivatedEvent{
|
||||
StorageEvent: event,
|
||||
}, nil
|
||||
}
|
123
internal/v2/org/domain.go
Normal file
123
internal/v2/org/domain.go
Normal file
@@ -0,0 +1,123 @@
|
||||
package org
|
||||
|
||||
import (
|
||||
"github.com/zitadel/zitadel/internal/v2/domain"
|
||||
"github.com/zitadel/zitadel/internal/v2/eventstore"
|
||||
"github.com/zitadel/zitadel/internal/zerrors"
|
||||
)
|
||||
|
||||
const DomainAddedType = "org." + domain.AddedTypeSuffix
|
||||
|
||||
type DomainAddedPayload domain.AddedPayload
|
||||
|
||||
type DomainAddedEvent eventstore.Event[DomainAddedPayload]
|
||||
|
||||
var _ eventstore.TypeChecker = (*DomainAddedEvent)(nil)
|
||||
|
||||
// ActionType implements eventstore.Typer.
|
||||
func (c *DomainAddedEvent) ActionType() string {
|
||||
return DomainAddedType
|
||||
}
|
||||
|
||||
func DomainAddedEventFromStorage(event *eventstore.StorageEvent) (e *DomainAddedEvent, _ error) {
|
||||
if event.Type != e.ActionType() {
|
||||
return nil, zerrors.ThrowInvalidArgument(nil, "ORG-CXVe3", "Errors.Invalid.Event.Type")
|
||||
}
|
||||
|
||||
payload, err := eventstore.UnmarshalPayload[DomainAddedPayload](event.Payload)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &DomainAddedEvent{
|
||||
StorageEvent: event,
|
||||
Payload: payload,
|
||||
}, nil
|
||||
}
|
||||
|
||||
const DomainVerifiedType = "org." + domain.VerifiedTypeSuffix
|
||||
|
||||
type DomainVerifiedPayload domain.VerifiedPayload
|
||||
|
||||
type DomainVerifiedEvent eventstore.Event[DomainVerifiedPayload]
|
||||
|
||||
var _ eventstore.TypeChecker = (*DomainVerifiedEvent)(nil)
|
||||
|
||||
// ActionType implements eventstore.Typer.
|
||||
func (c *DomainVerifiedEvent) ActionType() string {
|
||||
return DomainVerifiedType
|
||||
}
|
||||
|
||||
func DomainVerifiedEventFromStorage(event *eventstore.StorageEvent) (e *DomainVerifiedEvent, _ error) {
|
||||
if event.Type != e.ActionType() {
|
||||
return nil, zerrors.ThrowInvalidArgument(nil, "ORG-RAwdb", "Errors.Invalid.Event.Type")
|
||||
}
|
||||
|
||||
payload, err := eventstore.UnmarshalPayload[DomainVerifiedPayload](event.Payload)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &DomainVerifiedEvent{
|
||||
StorageEvent: event,
|
||||
Payload: payload,
|
||||
}, nil
|
||||
}
|
||||
|
||||
const DomainPrimarySetType = "org." + domain.PrimarySetTypeSuffix
|
||||
|
||||
type DomainPrimarySetPayload domain.PrimarySetPayload
|
||||
|
||||
type DomainPrimarySetEvent eventstore.Event[DomainPrimarySetPayload]
|
||||
|
||||
var _ eventstore.TypeChecker = (*DomainPrimarySetEvent)(nil)
|
||||
|
||||
// ActionType implements eventstore.Typer.
|
||||
func (c *DomainPrimarySetEvent) ActionType() string {
|
||||
return DomainPrimarySetType
|
||||
}
|
||||
|
||||
func DomainPrimarySetEventFromStorage(event *eventstore.StorageEvent) (e *DomainPrimarySetEvent, _ error) {
|
||||
if event.Type != e.ActionType() {
|
||||
return nil, zerrors.ThrowInvalidArgument(nil, "ORG-7P3Iz", "Errors.Invalid.Event.Type")
|
||||
}
|
||||
|
||||
payload, err := eventstore.UnmarshalPayload[DomainPrimarySetPayload](event.Payload)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &DomainPrimarySetEvent{
|
||||
StorageEvent: event,
|
||||
Payload: payload,
|
||||
}, nil
|
||||
}
|
||||
|
||||
const DomainRemovedType = "org." + domain.RemovedTypeSuffix
|
||||
|
||||
type DomainRemovedPayload domain.RemovedPayload
|
||||
|
||||
type DomainRemovedEvent eventstore.Event[DomainRemovedPayload]
|
||||
|
||||
var _ eventstore.TypeChecker = (*DomainRemovedEvent)(nil)
|
||||
|
||||
// ActionType implements eventstore.Typer.
|
||||
func (c *DomainRemovedEvent) ActionType() string {
|
||||
return DomainRemovedType
|
||||
}
|
||||
|
||||
func DomainRemovedEventFromStorage(event *eventstore.StorageEvent) (e *DomainRemovedEvent, _ error) {
|
||||
if event.Type != e.ActionType() {
|
||||
return nil, zerrors.ThrowInvalidArgument(nil, "ORG-ndpL2", "Errors.Invalid.Event.Type")
|
||||
}
|
||||
|
||||
payload, err := eventstore.UnmarshalPayload[DomainRemovedPayload](event.Payload)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &DomainRemovedEvent{
|
||||
StorageEvent: event,
|
||||
Payload: payload,
|
||||
}, nil
|
||||
}
|
94
internal/v2/org/domain_policy.go
Normal file
94
internal/v2/org/domain_policy.go
Normal file
@@ -0,0 +1,94 @@
|
||||
package org
|
||||
|
||||
import (
|
||||
"github.com/zitadel/zitadel/internal/v2/eventstore"
|
||||
"github.com/zitadel/zitadel/internal/v2/policy"
|
||||
"github.com/zitadel/zitadel/internal/zerrors"
|
||||
)
|
||||
|
||||
const DomainPolicyAddedType = eventTypePrefix + policy.DomainPolicyAddedTypeSuffix
|
||||
|
||||
type DomainPolicyAddedPayload policy.DomainPolicyAddedPayload
|
||||
|
||||
type DomainPolicyAddedEvent eventstore.Event[DomainPolicyAddedPayload]
|
||||
|
||||
var _ eventstore.TypeChecker = (*DomainPolicyAddedEvent)(nil)
|
||||
|
||||
// ActionType implements eventstore.Typer.
|
||||
func (c *DomainPolicyAddedEvent) ActionType() string {
|
||||
return DomainPolicyAddedType
|
||||
}
|
||||
|
||||
func DomainPolicyAddedEventFromStorage(event *eventstore.StorageEvent) (e *DomainPolicyAddedEvent, _ error) {
|
||||
if event.Type != e.ActionType() {
|
||||
return nil, zerrors.ThrowInvalidArgument(nil, "ORG-asiSN", "Errors.Invalid.Event.Type")
|
||||
}
|
||||
|
||||
payload, err := eventstore.UnmarshalPayload[DomainPolicyAddedPayload](event.Payload)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &DomainPolicyAddedEvent{
|
||||
StorageEvent: event,
|
||||
Payload: payload,
|
||||
}, nil
|
||||
}
|
||||
|
||||
const DomainPolicyChangedType = eventTypePrefix + policy.DomainPolicyChangedTypeSuffix
|
||||
|
||||
type DomainPolicyChangedPayload policy.DomainPolicyChangedPayload
|
||||
|
||||
type DomainPolicyChangedEvent eventstore.Event[DomainPolicyChangedPayload]
|
||||
|
||||
var _ eventstore.TypeChecker = (*DomainPolicyChangedEvent)(nil)
|
||||
|
||||
// ActionType implements eventstore.Typer.
|
||||
func (c *DomainPolicyChangedEvent) ActionType() string {
|
||||
return DomainPolicyChangedType
|
||||
}
|
||||
|
||||
func DomainPolicyChangedEventFromStorage(event *eventstore.StorageEvent) (e *DomainPolicyChangedEvent, _ error) {
|
||||
if event.Type != e.ActionType() {
|
||||
return nil, zerrors.ThrowInvalidArgument(nil, "ORG-BmN6K", "Errors.Invalid.Event.Type")
|
||||
}
|
||||
|
||||
payload, err := eventstore.UnmarshalPayload[DomainPolicyChangedPayload](event.Payload)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &DomainPolicyChangedEvent{
|
||||
StorageEvent: event,
|
||||
Payload: payload,
|
||||
}, nil
|
||||
}
|
||||
|
||||
const DomainPolicyRemovedType = eventTypePrefix + policy.DomainPolicyRemovedTypeSuffix
|
||||
|
||||
type DomainPolicyRemovedPayload policy.DomainPolicyRemovedPayload
|
||||
|
||||
type DomainPolicyRemovedEvent eventstore.Event[DomainPolicyRemovedPayload]
|
||||
|
||||
var _ eventstore.TypeChecker = (*DomainPolicyRemovedEvent)(nil)
|
||||
|
||||
// ActionType implements eventstore.Typer.
|
||||
func (c *DomainPolicyRemovedEvent) ActionType() string {
|
||||
return DomainPolicyRemovedType
|
||||
}
|
||||
|
||||
func DomainPolicyRemovedEventFromStorage(event *eventstore.StorageEvent) (e *DomainPolicyRemovedEvent, _ error) {
|
||||
if event.Type != e.ActionType() {
|
||||
return nil, zerrors.ThrowInvalidArgument(nil, "ORG-nHy4z", "Errors.Invalid.Event.Type")
|
||||
}
|
||||
|
||||
payload, err := eventstore.UnmarshalPayload[DomainPolicyRemovedPayload](event.Payload)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &DomainPolicyRemovedEvent{
|
||||
StorageEvent: event,
|
||||
Payload: payload,
|
||||
}, nil
|
||||
}
|
27
internal/v2/org/reactivated.go
Normal file
27
internal/v2/org/reactivated.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package org
|
||||
|
||||
import (
|
||||
"github.com/zitadel/zitadel/internal/v2/eventstore"
|
||||
"github.com/zitadel/zitadel/internal/zerrors"
|
||||
)
|
||||
|
||||
const ReactivatedType = eventTypePrefix + "reactivated"
|
||||
|
||||
type ReactivatedEvent eventstore.Event[eventstore.EmptyPayload]
|
||||
|
||||
var _ eventstore.TypeChecker = (*ReactivatedEvent)(nil)
|
||||
|
||||
// ActionType implements eventstore.Typer.
|
||||
func (c *ReactivatedEvent) ActionType() string {
|
||||
return ReactivatedType
|
||||
}
|
||||
|
||||
func ReactivatedEventFromStorage(event *eventstore.StorageEvent) (e *ReactivatedEvent, _ error) {
|
||||
if event.Type != e.ActionType() {
|
||||
return nil, zerrors.ThrowInvalidArgument(nil, "ORG-cPWZw", "Errors.Invalid.Event.Type")
|
||||
}
|
||||
|
||||
return &ReactivatedEvent{
|
||||
StorageEvent: event,
|
||||
}, nil
|
||||
}
|
27
internal/v2/org/removed.go
Normal file
27
internal/v2/org/removed.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package org
|
||||
|
||||
import (
|
||||
"github.com/zitadel/zitadel/internal/v2/eventstore"
|
||||
"github.com/zitadel/zitadel/internal/zerrors"
|
||||
)
|
||||
|
||||
const RemovedType = eventTypePrefix + "removed"
|
||||
|
||||
type RemovedEvent eventstore.Event[eventstore.EmptyPayload]
|
||||
|
||||
var _ eventstore.TypeChecker = (*RemovedEvent)(nil)
|
||||
|
||||
// ActionType implements eventstore.Typer.
|
||||
func (c *RemovedEvent) ActionType() string {
|
||||
return RemovedType
|
||||
}
|
||||
|
||||
func RemovedEventFromStorage(event *eventstore.StorageEvent) (e *RemovedEvent, _ error) {
|
||||
if event.Type != e.ActionType() {
|
||||
return nil, zerrors.ThrowInvalidArgument(nil, "ORG-RSPYk", "Errors.Invalid.Event.Type")
|
||||
}
|
||||
|
||||
return &RemovedEvent{
|
||||
StorageEvent: event,
|
||||
}, nil
|
||||
}
|
36
internal/v2/org/state.go
Normal file
36
internal/v2/org/state.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package org
|
||||
|
||||
type State uint8
|
||||
|
||||
const (
|
||||
UndefinedState State = iota
|
||||
ActiveState
|
||||
InactiveState
|
||||
RemovedState
|
||||
maxState
|
||||
)
|
||||
|
||||
func (s State) IsValid() bool {
|
||||
return s != UndefinedState ||
|
||||
s < maxState
|
||||
}
|
||||
|
||||
func (s State) Is(state State) bool {
|
||||
return s == state
|
||||
}
|
||||
|
||||
func (s State) IsValidState(state State) bool {
|
||||
return s.IsValid() && s.Is(state)
|
||||
}
|
||||
|
||||
func (s State) IsValidStates(states ...State) bool {
|
||||
if !s.IsValid() {
|
||||
return false
|
||||
}
|
||||
for _, state := range states {
|
||||
if s.Is(state) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
Reference in New Issue
Block a user