2021-01-06 10:12:56 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-01-12 11:59:51 +00:00
|
|
|
|
2021-01-06 10:12:56 +00:00
|
|
|
"github.com/caos/zitadel/internal/eventstore/v2"
|
|
|
|
"github.com/caos/zitadel/internal/v2/domain"
|
|
|
|
"github.com/caos/zitadel/internal/v2/repository/user"
|
|
|
|
)
|
|
|
|
|
|
|
|
type MachineWriteModel struct {
|
|
|
|
eventstore.WriteModel
|
|
|
|
|
|
|
|
UserName string
|
|
|
|
|
|
|
|
Name string
|
|
|
|
Description string
|
|
|
|
UserState domain.UserState
|
|
|
|
}
|
|
|
|
|
2021-01-12 11:59:51 +00:00
|
|
|
func NewMachineWriteModel(userID, resourceOwner string) *MachineWriteModel {
|
2021-01-06 10:12:56 +00:00
|
|
|
return &MachineWriteModel{
|
|
|
|
WriteModel: eventstore.WriteModel{
|
2021-01-12 11:59:51 +00:00
|
|
|
AggregateID: userID,
|
|
|
|
ResourceOwner: resourceOwner,
|
2021-01-06 10:12:56 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *MachineWriteModel) AppendEvents(events ...eventstore.EventReader) {
|
2021-01-15 08:32:59 +00:00
|
|
|
wm.WriteModel.AppendEvents(events...)
|
2021-01-06 10:12:56 +00:00
|
|
|
}
|
|
|
|
|
2021-01-15 08:32:59 +00:00
|
|
|
//TODO: Compute OTPState? initial/active
|
2021-01-06 10:12:56 +00:00
|
|
|
func (wm *MachineWriteModel) Reduce() error {
|
|
|
|
for _, event := range wm.Events {
|
|
|
|
switch e := event.(type) {
|
|
|
|
case *user.MachineAddedEvent:
|
|
|
|
wm.UserName = e.UserName
|
|
|
|
wm.Name = e.Name
|
|
|
|
wm.Description = e.Description
|
|
|
|
wm.UserState = domain.UserStateActive
|
2021-01-07 15:06:45 +00:00
|
|
|
case *user.UsernameChangedEvent:
|
|
|
|
wm.UserName = e.UserName
|
2021-01-06 10:12:56 +00:00
|
|
|
case *user.MachineChangedEvent:
|
|
|
|
if e.Name != nil {
|
|
|
|
wm.Name = *e.Name
|
|
|
|
}
|
|
|
|
if e.Description != nil {
|
|
|
|
wm.Description = *e.Description
|
|
|
|
}
|
|
|
|
case *user.UserLockedEvent:
|
|
|
|
if wm.UserState != domain.UserStateDeleted {
|
|
|
|
wm.UserState = domain.UserStateLocked
|
|
|
|
}
|
|
|
|
case *user.UserUnlockedEvent:
|
|
|
|
if wm.UserState != domain.UserStateDeleted {
|
|
|
|
wm.UserState = domain.UserStateActive
|
|
|
|
}
|
|
|
|
case *user.UserDeactivatedEvent:
|
|
|
|
if wm.UserState != domain.UserStateDeleted {
|
|
|
|
wm.UserState = domain.UserStateInactive
|
|
|
|
}
|
|
|
|
case *user.UserReactivatedEvent:
|
|
|
|
if wm.UserState != domain.UserStateDeleted {
|
|
|
|
wm.UserState = domain.UserStateActive
|
|
|
|
}
|
|
|
|
case *user.UserRemovedEvent:
|
|
|
|
wm.UserState = domain.UserStateDeleted
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return wm.WriteModel.Reduce()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *MachineWriteModel) Query() *eventstore.SearchQueryBuilder {
|
|
|
|
return eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent, user.AggregateType).
|
2021-01-12 11:59:51 +00:00
|
|
|
AggregateIDs(wm.AggregateID).
|
|
|
|
ResourceOwner(wm.ResourceOwner)
|
2021-01-06 10:12:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *MachineWriteModel) NewChangedEvent(
|
|
|
|
ctx context.Context,
|
|
|
|
name,
|
|
|
|
description string,
|
|
|
|
) (*user.MachineChangedEvent, bool) {
|
|
|
|
hasChanged := false
|
|
|
|
changedEvent := user.NewMachineChangedEvent(ctx)
|
|
|
|
if wm.Name != name {
|
|
|
|
hasChanged = true
|
|
|
|
changedEvent.Name = &name
|
|
|
|
}
|
|
|
|
if wm.Description != description {
|
|
|
|
hasChanged = true
|
|
|
|
changedEvent.Description = &description
|
|
|
|
}
|
|
|
|
return changedEvent, hasChanged
|
|
|
|
}
|