zitadel/internal/command/instance_policy_mail_template_model.go

74 lines
2.1 KiB
Go
Raw Normal View History

2021-02-10 09:48:40 +00:00
package command
import (
"context"
"reflect"
"github.com/zitadel/zitadel/internal/api/authz"
"github.com/zitadel/zitadel/internal/eventstore"
"github.com/zitadel/zitadel/internal/repository/instance"
"github.com/zitadel/zitadel/internal/repository/policy"
2021-02-10 09:48:40 +00:00
)
type InstanceMailTemplateWriteModel struct {
2021-02-10 09:48:40 +00:00
MailTemplateWriteModel
}
func NewInstanceMailTemplateWriteModel(ctx context.Context) *InstanceMailTemplateWriteModel {
return &InstanceMailTemplateWriteModel{
2021-02-10 09:48:40 +00:00
MailTemplateWriteModel{
WriteModel: eventstore.WriteModel{
AggregateID: authz.GetInstance(ctx).InstanceID(),
ResourceOwner: authz.GetInstance(ctx).InstanceID(),
2021-02-10 09:48:40 +00:00
},
},
}
}
func (wm *InstanceMailTemplateWriteModel) AppendEvents(events ...eventstore.Event) {
2021-02-10 09:48:40 +00:00
for _, event := range events {
switch e := event.(type) {
case *instance.MailTemplateAddedEvent:
2021-02-10 09:48:40 +00:00
wm.MailTemplateWriteModel.AppendEvents(&e.MailTemplateAddedEvent)
case *instance.MailTemplateChangedEvent:
2021-02-10 09:48:40 +00:00
wm.MailTemplateWriteModel.AppendEvents(&e.MailTemplateChangedEvent)
}
}
}
func (wm *InstanceMailTemplateWriteModel) Reduce() error {
2021-02-10 09:48:40 +00:00
return wm.MailTemplateWriteModel.Reduce()
}
func (wm *InstanceMailTemplateWriteModel) Query() *eventstore.SearchQueryBuilder {
return eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent).
ResourceOwner(wm.ResourceOwner).
AddQuery().
AggregateTypes(instance.AggregateType).
AggregateIDs(wm.MailTemplateWriteModel.AggregateID).
EventTypes(
instance.MailTemplateAddedEventType,
instance.MailTemplateChangedEventType).
Builder()
2021-02-10 09:48:40 +00:00
}
func (wm *InstanceMailTemplateWriteModel) NewChangedEvent(
2021-02-10 09:48:40 +00:00
ctx context.Context,
aggregate *eventstore.Aggregate,
2021-02-10 09:48:40 +00:00
template []byte,
) (*instance.MailTemplateChangedEvent, bool) {
2021-02-10 09:48:40 +00:00
changes := make([]policy.MailTemplateChanges, 0)
if !reflect.DeepEqual(wm.Template, template) {
changes = append(changes, policy.ChangeTemplate(template))
}
if len(changes) == 0 {
return nil, false
}
changedEvent, err := instance.NewMailTemplateChangedEvent(ctx, aggregate, changes)
2021-02-10 09:48:40 +00:00
if err != nil {
return nil, false
}
return changedEvent, true
}