zitadel/internal/command/org_policy_mail_text_model.go

96 lines
2.5 KiB
Go
Raw Normal View History

2021-02-10 09:48:40 +00:00
package command
import (
"context"
"github.com/caos/zitadel/internal/eventstore"
"github.com/caos/zitadel/internal/repository/org"
"github.com/caos/zitadel/internal/repository/policy"
2021-02-10 09:48:40 +00:00
)
type OrgMailTextWriteModel struct {
MailTextWriteModel
}
func NewOrgMailTextWriteModel(orgID, mailTextType, language string) *OrgMailTextWriteModel {
return &OrgMailTextWriteModel{
MailTextWriteModel{
WriteModel: eventstore.WriteModel{
AggregateID: orgID,
ResourceOwner: orgID,
},
MailTextType: mailTextType,
Language: language,
},
}
}
func (wm *OrgMailTextWriteModel) AppendEvents(events ...eventstore.EventReader) {
for _, event := range events {
switch e := event.(type) {
case *org.MailTextAddedEvent:
wm.MailTextWriteModel.AppendEvents(&e.MailTextAddedEvent)
case *org.MailTextChangedEvent:
wm.MailTextWriteModel.AppendEvents(&e.MailTextChangedEvent)
case *org.MailTextRemovedEvent:
wm.MailTextWriteModel.AppendEvents(&e.MailTextRemovedEvent)
2021-02-10 09:48:40 +00:00
}
}
}
func (wm *OrgMailTextWriteModel) Reduce() error {
return wm.MailTextWriteModel.Reduce()
}
func (wm *OrgMailTextWriteModel) Query() *eventstore.SearchQueryBuilder {
query := eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent, org.AggregateType).
AggregateIDs(wm.MailTextWriteModel.AggregateID).
EventTypes(org.MailTextAddedEventType,
org.MailTextChangedEventType,
org.MailTextRemovedEventType)
2021-02-10 09:48:40 +00:00
if wm.ResourceOwner != "" {
query.ResourceOwner(wm.ResourceOwner)
}
return query
}
func (wm *OrgMailTextWriteModel) NewChangedEvent(
ctx context.Context,
aggregate *eventstore.Aggregate,
2021-02-10 09:48:40 +00:00
mailTextType,
language,
title,
preHeader,
subject,
greeting,
text,
buttonText string,
) (*org.MailTextChangedEvent, bool) {
changes := make([]policy.MailTextChanges, 0)
if wm.Title != title {
changes = append(changes, policy.ChangeTitle(title))
}
if wm.PreHeader != preHeader {
changes = append(changes, policy.ChangePreHeader(preHeader))
}
if wm.Subject != subject {
changes = append(changes, policy.ChangeSubject(subject))
}
if wm.Greeting != greeting {
changes = append(changes, policy.ChangeGreeting(greeting))
}
if wm.Text != text {
changes = append(changes, policy.ChangeText(text))
}
if wm.ButtonText != buttonText {
changes = append(changes, policy.ChangeButtonText(buttonText))
}
if len(changes) == 0 {
return nil, false
}
changedEvent, err := org.NewMailTextChangedEvent(ctx, aggregate, mailTextType, language, changes)
2021-02-10 09:48:40 +00:00
if err != nil {
return nil, false
}
return changedEvent, true
}