2020-11-06 16:25:07 +00:00
|
|
|
package iam
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-11-06 21:09:19 +00:00
|
|
|
"encoding/json"
|
2021-03-24 08:30:45 +00:00
|
|
|
|
2021-02-23 14:13:04 +00:00
|
|
|
"github.com/caos/zitadel/internal/eventstore"
|
2020-11-06 16:25:07 +00:00
|
|
|
|
2021-02-23 14:13:04 +00:00
|
|
|
"github.com/caos/zitadel/internal/domain"
|
2020-11-06 21:09:19 +00:00
|
|
|
"github.com/caos/zitadel/internal/errors"
|
2021-02-23 14:13:04 +00:00
|
|
|
"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:"-"`
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
Step domain.Step `json:"Step"`
|
|
|
|
Done bool `json:"-"`
|
2020-11-06 16:25:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *SetupStepEvent) Data() interface{} {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2021-01-21 09:49:38 +00:00
|
|
|
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,
|
2021-03-24 08:30:45 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-11-12 21:50:01 +00:00
|
|
|
func NewSetupStepDoneEvent(
|
|
|
|
ctx context.Context,
|
2021-02-18 13:48:27 +00:00
|
|
|
aggregate *eventstore.Aggregate,
|
2021-01-04 13:52:13 +00:00
|
|
|
step domain.Step,
|
2020-11-12 21:50:01 +00:00
|
|
|
) *SetupStepEvent {
|
|
|
|
|
2020-11-06 16:25:07 +00:00
|
|
|
return &SetupStepEvent{
|
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
2021-02-18 13:48:27 +00:00
|
|
|
aggregate,
|
2020-11-06 16:25:07 +00:00
|
|
|
SetupDoneEventType,
|
|
|
|
),
|
2020-11-12 21:50:01 +00:00
|
|
|
Step: step,
|
2020-11-06 16:25:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-12 21:50:01 +00:00
|
|
|
func NewSetupStepStartedEvent(
|
|
|
|
ctx context.Context,
|
2021-02-18 13:48:27 +00:00
|
|
|
aggregate *eventstore.Aggregate,
|
2021-01-04 13:52:13 +00:00
|
|
|
step domain.Step,
|
2020-11-12 21:50:01 +00:00
|
|
|
) *SetupStepEvent {
|
|
|
|
|
2020-11-06 16:25:07 +00:00
|
|
|
return &SetupStepEvent{
|
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
2021-02-18 13:48:27 +00:00
|
|
|
aggregate,
|
2020-11-06 16:25:07 +00:00
|
|
|
SetupStartedEventType,
|
|
|
|
),
|
2020-11-12 21:50:01 +00:00
|
|
|
Step: step,
|
2020-11-06 16:25:07 +00:00
|
|
|
}
|
|
|
|
}
|