zitadel/internal/v2/repository/org/member.go

149 lines
2.9 KiB
Go
Raw Normal View History

package org
import (
"context"
"github.com/caos/zitadel/internal/eventstore/v2"
"github.com/caos/zitadel/internal/v2/repository/member"
"github.com/caos/zitadel/internal/v2/repository/members"
)
const (
orgEventTypePrefix = eventstore.EventType("org.")
)
var (
MemberAddedEventType = orgEventTypePrefix + member.AddedEventType
MemberChangedEventType = orgEventTypePrefix + member.ChangedEventType
MemberRemovedEventType = orgEventTypePrefix + member.RemovedEventType
)
2020-11-18 20:22:15 +00:00
type MemberWriteModel struct {
member.WriteModel
}
// func NewMemberAggregate(userID string) *MemberAggregate {
// return &MemberAggregate{
// Aggregate: member.NewAggregate(
// eventstore.NewAggregate(userID, MemberAggregateType, "RO", AggregateVersion, 0),
// ),
// // Aggregate: member.NewMemberAggregate(userID),
// }
// }
type MembersReadModel struct {
members.ReadModel
}
2020-11-25 13:12:44 +00:00
func (rm *MembersReadModel) AppendEvents(events ...eventstore.EventReader) {
for _, event := range events {
switch e := event.(type) {
case *MemberAddedEvent:
rm.ReadModel.AppendEvents(&e.AddedEvent)
case *MemberChangedEvent:
rm.ReadModel.AppendEvents(&e.ChangedEvent)
case *MemberRemovedEvent:
rm.ReadModel.AppendEvents(&e.RemovedEvent)
}
}
}
type MemberReadModel member.ReadModel
2020-11-25 13:12:44 +00:00
func (rm *MemberReadModel) AppendEvents(events ...eventstore.EventReader) {
for _, event := range events {
switch e := event.(type) {
case *MemberAddedEvent:
rm.ReadModel.AppendEvents(&e.AddedEvent)
case *MemberChangedEvent:
rm.ReadModel.AppendEvents(&e.ChangedEvent)
}
}
}
type MemberAddedEvent struct {
member.AddedEvent
}
type MemberChangedEvent struct {
member.ChangedEvent
}
type MemberRemovedEvent struct {
member.RemovedEvent
}
func NewMemberAddedEvent(
ctx context.Context,
userID string,
roles ...string,
) *MemberAddedEvent {
return &MemberAddedEvent{
AddedEvent: *member.NewAddedEvent(
eventstore.NewBaseEventForPush(
ctx,
MemberAddedEventType,
),
userID,
roles...,
),
}
}
2020-11-23 18:31:12 +00:00
func MemberChangedEventFromExisting(
ctx context.Context,
2020-11-23 18:31:12 +00:00
current *MemberWriteModel,
roles ...string,
2020-11-18 20:22:15 +00:00
) (*MemberChangedEvent, error) {
2020-11-23 18:31:12 +00:00
m, err := member.ChangeEventFromExisting(
2020-11-18 20:22:15 +00:00
eventstore.NewBaseEventForPush(
ctx,
MemberChangedEventType,
),
2020-11-18 20:22:15 +00:00
&current.WriteModel,
2020-11-23 18:31:12 +00:00
roles...,
2020-11-18 20:22:15 +00:00
)
if err != nil {
return nil, err
}
2020-11-23 18:31:12 +00:00
2020-11-18 20:22:15 +00:00
return &MemberChangedEvent{
2020-11-23 18:31:12 +00:00
ChangedEvent: *m,
2020-11-18 20:22:15 +00:00
}, nil
}
2020-11-23 18:31:12 +00:00
func NewMemberChangedEvent(
ctx context.Context,
userID string,
roles ...string,
) *MemberChangedEvent {
return &MemberChangedEvent{
ChangedEvent: *member.NewChangedEvent(
eventstore.NewBaseEventForPush(
ctx,
MemberChangedEventType,
),
userID,
roles...,
),
}
}
func NewMemberRemovedEvent(
ctx context.Context,
userID string,
) *MemberRemovedEvent {
return &MemberRemovedEvent{
RemovedEvent: *member.NewRemovedEvent(
eventstore.NewBaseEventForPush(
ctx,
MemberRemovedEventType,
),
userID,
),
}
}