zitadel/internal/repository/iam/events_step.go

82 lines
1.6 KiB
Go
Raw Normal View History

2020-11-06 16:25:07 +00:00
package iam
import (
"context"
2020-11-06 21:09:19 +00:00
"encoding/json"
"github.com/caos/zitadel/internal/eventstore"
2020-11-06 16:25:07 +00:00
"github.com/caos/zitadel/internal/domain"
2020-11-06 21:09:19 +00:00
"github.com/caos/zitadel/internal/errors"
"github.com/caos/zitadel/internal/eventstore/repository"
2020-11-06 16:25:07 +00:00
)
const (
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 16:25:07 +00:00
}
func (e *SetupStepEvent) Data() interface{} {
return e
}
func (e *SetupStepEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
return nil
}
2020-11-06 21:09:19 +00: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 21:09:19 +00: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 16:25:07 +00:00
return &SetupStepEvent{
BaseEvent: *eventstore.NewBaseEventForPush(
ctx,
aggregate,
2020-11-06 16:25:07 +00:00
SetupDoneEventType,
),
Step: step,
2020-11-06 16:25:07 +00:00
}
}
func NewSetupStepStartedEvent(
ctx context.Context,
aggregate *eventstore.Aggregate,
step domain.Step,
) *SetupStepEvent {
2020-11-06 16:25:07 +00:00
return &SetupStepEvent{
BaseEvent: *eventstore.NewBaseEventForPush(
ctx,
aggregate,
2020-11-06 16:25:07 +00:00
SetupStartedEventType,
),
Step: step,
2020-11-06 16:25:07 +00:00
}
}