2020-11-06 17:25:07 +01:00
|
|
|
package iam
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-11-06 22:09:19 +01:00
|
|
|
"encoding/json"
|
2021-03-31 11:10:06 +02:00
|
|
|
"strconv"
|
2021-03-24 09:30:45 +01:00
|
|
|
|
2021-02-23 15:13:04 +01:00
|
|
|
"github.com/caos/zitadel/internal/eventstore"
|
2020-11-06 17:25:07 +01:00
|
|
|
|
2021-02-23 15:13:04 +01:00
|
|
|
"github.com/caos/zitadel/internal/domain"
|
2020-11-06 22:09:19 +01:00
|
|
|
"github.com/caos/zitadel/internal/errors"
|
2021-02-23 15:13:04 +01:00
|
|
|
"github.com/caos/zitadel/internal/eventstore/repository"
|
2020-11-06 17:25:07 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2021-03-31 11:10:06 +02:00
|
|
|
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:"-"`
|
|
|
|
|
2021-01-04 14:52:13 +01:00
|
|
|
Step domain.Step `json:"Step"`
|
|
|
|
Done bool `json:"-"`
|
2020-11-06 17:25:07 +01:00
|
|
|
}
|
|
|
|
|
2021-03-31 11:10:06 +02: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
|
|
|
|
}
|
|
|
|
|
2021-01-21 10:49:38 +01:00
|
|
|
func (e *SetupStepEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
2021-03-31 11:10:06 +02:00
|
|
|
if e.Done {
|
|
|
|
return []*eventstore.EventUniqueConstraint{NewAddSetupStepDoneUniqueConstraint(e.Step)}
|
|
|
|
} else {
|
|
|
|
return []*eventstore.EventUniqueConstraint{NewAddSetupStepStartedUniqueConstraint(e.Step)}
|
|
|
|
}
|
2021-01-21 10:49:38 +01:00
|
|
|
}
|
|
|
|
|
2022-01-03 09:19:07 +01:00
|
|
|
func SetupStepMapper(event *repository.Event) (eventstore.Event, error) {
|
2020-11-06 22:09:19 +01:00
|
|
|
step := &SetupStepEvent{
|
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
Done: eventstore.EventType(event.Type) == SetupDoneEventType,
|
2021-03-24 09:30:45 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-11-12 22:50:01 +01:00
|
|
|
func NewSetupStepDoneEvent(
|
|
|
|
ctx context.Context,
|
2021-02-18 14:48:27 +01:00
|
|
|
aggregate *eventstore.Aggregate,
|
2021-01-04 14:52:13 +01:00
|
|
|
step domain.Step,
|
2020-11-12 22:50:01 +01:00
|
|
|
) *SetupStepEvent {
|
|
|
|
|
2020-11-06 17:25:07 +01:00
|
|
|
return &SetupStepEvent{
|
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
2021-02-18 14:48:27 +01:00
|
|
|
aggregate,
|
2020-11-06 17:25:07 +01:00
|
|
|
SetupDoneEventType,
|
|
|
|
),
|
2020-11-12 22:50:01 +01:00
|
|
|
Step: step,
|
2021-04-01 10:14:07 +02:00
|
|
|
Done: true,
|
2020-11-06 17:25:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-12 22:50:01 +01:00
|
|
|
func NewSetupStepStartedEvent(
|
|
|
|
ctx context.Context,
|
2021-02-18 14:48:27 +01:00
|
|
|
aggregate *eventstore.Aggregate,
|
2021-01-04 14:52:13 +01:00
|
|
|
step domain.Step,
|
2020-11-12 22:50:01 +01:00
|
|
|
) *SetupStepEvent {
|
|
|
|
|
2020-11-06 17:25:07 +01:00
|
|
|
return &SetupStepEvent{
|
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
2021-02-18 14:48:27 +01:00
|
|
|
aggregate,
|
2020-11-06 17:25:07 +01:00
|
|
|
SetupStartedEventType,
|
|
|
|
),
|
2020-11-12 22:50:01 +01:00
|
|
|
Step: step,
|
2020-11-06 17:25:07 +01:00
|
|
|
}
|
|
|
|
}
|