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