zitadel/internal/v2/org/added.go
Silvan 12be21a3ff
refactor(v2): init events (#7823)
creates events structures for initial projections and read models
2024-05-23 06:36:08 +02:00

63 lines
1.5 KiB
Go

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
}