2021-02-10 10:48:40 +01:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"reflect"
|
|
|
|
|
2022-04-27 01:01:45 +02:00
|
|
|
"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 10:48:40 +01:00
|
|
|
)
|
|
|
|
|
2022-03-24 17:21:34 +01:00
|
|
|
type InstanceMailTemplateWriteModel struct {
|
2021-02-10 10:48:40 +01:00
|
|
|
MailTemplateWriteModel
|
|
|
|
}
|
|
|
|
|
2022-04-05 07:58:09 +02:00
|
|
|
func NewInstanceMailTemplateWriteModel(ctx context.Context) *InstanceMailTemplateWriteModel {
|
2022-03-24 17:21:34 +01:00
|
|
|
return &InstanceMailTemplateWriteModel{
|
2021-02-10 10:48:40 +01:00
|
|
|
MailTemplateWriteModel{
|
|
|
|
WriteModel: eventstore.WriteModel{
|
2022-04-05 07:58:09 +02:00
|
|
|
AggregateID: authz.GetInstance(ctx).InstanceID(),
|
|
|
|
ResourceOwner: authz.GetInstance(ctx).InstanceID(),
|
2023-10-19 12:19:10 +02:00
|
|
|
InstanceID: authz.GetInstance(ctx).InstanceID(),
|
2021-02-10 10:48:40 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-24 17:21:34 +01:00
|
|
|
func (wm *InstanceMailTemplateWriteModel) AppendEvents(events ...eventstore.Event) {
|
2021-02-10 10:48:40 +01:00
|
|
|
for _, event := range events {
|
|
|
|
switch e := event.(type) {
|
2022-03-24 17:21:34 +01:00
|
|
|
case *instance.MailTemplateAddedEvent:
|
2021-02-10 10:48:40 +01:00
|
|
|
wm.MailTemplateWriteModel.AppendEvents(&e.MailTemplateAddedEvent)
|
2022-03-24 17:21:34 +01:00
|
|
|
case *instance.MailTemplateChangedEvent:
|
2021-02-10 10:48:40 +01:00
|
|
|
wm.MailTemplateWriteModel.AppendEvents(&e.MailTemplateChangedEvent)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-24 17:21:34 +01:00
|
|
|
func (wm *InstanceMailTemplateWriteModel) Reduce() error {
|
2021-02-10 10:48:40 +01:00
|
|
|
return wm.MailTemplateWriteModel.Reduce()
|
|
|
|
}
|
|
|
|
|
2022-03-24 17:21:34 +01:00
|
|
|
func (wm *InstanceMailTemplateWriteModel) Query() *eventstore.SearchQueryBuilder {
|
2021-07-06 13:55:57 +02:00
|
|
|
return eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent).
|
2021-02-18 14:48:27 +01:00
|
|
|
ResourceOwner(wm.ResourceOwner).
|
2021-07-06 13:55:57 +02:00
|
|
|
AddQuery().
|
2022-03-24 17:21:34 +01:00
|
|
|
AggregateTypes(instance.AggregateType).
|
2021-07-06 13:55:57 +02:00
|
|
|
AggregateIDs(wm.MailTemplateWriteModel.AggregateID).
|
2021-02-18 14:48:27 +01:00
|
|
|
EventTypes(
|
2022-03-24 17:21:34 +01:00
|
|
|
instance.MailTemplateAddedEventType,
|
|
|
|
instance.MailTemplateChangedEventType).
|
2021-07-06 13:55:57 +02:00
|
|
|
Builder()
|
2021-02-10 10:48:40 +01:00
|
|
|
}
|
|
|
|
|
2022-03-24 17:21:34 +01:00
|
|
|
func (wm *InstanceMailTemplateWriteModel) NewChangedEvent(
|
2021-02-10 10:48:40 +01:00
|
|
|
ctx context.Context,
|
2021-02-18 14:48:27 +01:00
|
|
|
aggregate *eventstore.Aggregate,
|
2021-02-10 10:48:40 +01:00
|
|
|
template []byte,
|
2022-03-24 17:21:34 +01:00
|
|
|
) (*instance.MailTemplateChangedEvent, bool) {
|
2021-02-10 10:48:40 +01: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
|
|
|
|
}
|
2022-03-24 17:21:34 +01:00
|
|
|
changedEvent, err := instance.NewMailTemplateChangedEvent(ctx, aggregate, changes)
|
2021-02-10 10:48:40 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
return changedEvent, true
|
|
|
|
}
|