zitadel/internal/repository/iam/events_step.go

104 lines
2.4 KiB
Go
Raw Normal View History

2020-11-06 17:25:07 +01:00
package iam
import (
"context"
2020-11-06 22:09:19 +01:00
"encoding/json"
"strconv"
"github.com/caos/zitadel/internal/eventstore"
2020-11-06 17:25:07 +01:00
"github.com/caos/zitadel/internal/domain"
2020-11-06 22:09:19 +01:00
"github.com/caos/zitadel/internal/errors"
"github.com/caos/zitadel/internal/eventstore/repository"
2020-11-06 17:25:07 +01:00
)
const (
UniqueStepStarted = "stepstarted"
UniqueStepDone = "stepdone"
2020-11-06 17:25:07 +01:00
SetupDoneEventType eventstore.EventType = "iam.setup.done"
SetupStartedEventType eventstore.EventType = "iam.setup.started"
)
type SetupStepEvent struct {
eventstore.BaseEvent `json:"-"`
Step domain.Step `json:"Step"`
Done bool `json:"-"`
2020-11-06 17:25:07 +01:00
}
func NewAddSetupStepStartedUniqueConstraint(step domain.Step) *eventstore.EventUniqueConstraint {
return eventstore.NewAddEventUniqueConstraint(
UniqueStepStarted,
strconv.Itoa(int(step)),
"Errors.Step.Started.AlreadyExists")
}
func NewAddSetupStepDoneUniqueConstraint(step domain.Step) *eventstore.EventUniqueConstraint {
return eventstore.NewAddEventUniqueConstraint(
UniqueStepDone,
strconv.Itoa(int(step)),
"Errors.Step.Done.AlreadyExists")
}
2020-11-06 17:25:07 +01:00
func (e *SetupStepEvent) Data() interface{} {
return e
}
func (e *SetupStepEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
if e.Done {
return []*eventstore.EventUniqueConstraint{NewAddSetupStepDoneUniqueConstraint(e.Step)}
} else {
return []*eventstore.EventUniqueConstraint{NewAddSetupStepStartedUniqueConstraint(e.Step)}
}
}
2020-11-06 22:09:19 +01:00
func SetupStepMapper(event *repository.Event) (eventstore.EventReader, error) {
step := &SetupStepEvent{
BaseEvent: *eventstore.BaseEventFromRepo(event),
Done: eventstore.EventType(event.Type) == SetupDoneEventType,
Step: domain.Step1,
}
if len(event.Data) == 0 {
return step, nil
2020-11-06 22:09:19 +01:00
}
err := json.Unmarshal(event.Data, step)
if err != nil {
return nil, errors.ThrowInternal(err, "IAM-O6rVg", "unable to unmarshal step")
}
return step, nil
}
func NewSetupStepDoneEvent(
ctx context.Context,
aggregate *eventstore.Aggregate,
step domain.Step,
) *SetupStepEvent {
2020-11-06 17:25:07 +01:00
return &SetupStepEvent{
BaseEvent: *eventstore.NewBaseEventForPush(
ctx,
aggregate,
2020-11-06 17:25:07 +01:00
SetupDoneEventType,
),
Step: step,
Done: true,
2020-11-06 17:25:07 +01:00
}
}
func NewSetupStepStartedEvent(
ctx context.Context,
aggregate *eventstore.Aggregate,
step domain.Step,
) *SetupStepEvent {
2020-11-06 17:25:07 +01:00
return &SetupStepEvent{
BaseEvent: *eventstore.NewBaseEventForPush(
ctx,
aggregate,
2020-11-06 17:25:07 +01:00
SetupStartedEventType,
),
Step: step,
2020-11-06 17:25:07 +01:00
}
}