mirror of
https://github.com/zitadel/zitadel.git
synced 2024-12-12 02:54:20 +00:00
25ef3da9d5
chore(fmt): run gci on complete project Fix global import formatting in go code by running the `gci` command. This allows us to just use the command directly, instead of fixing the import order manually for the linter, on each PR. Co-authored-by: Elio Bischof <elio@zitadel.com>
74 lines
2.1 KiB
Go
74 lines
2.1 KiB
Go
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"
|
|
)
|
|
|
|
type InstanceMailTemplateWriteModel struct {
|
|
MailTemplateWriteModel
|
|
}
|
|
|
|
func NewInstanceMailTemplateWriteModel(ctx context.Context) *InstanceMailTemplateWriteModel {
|
|
return &InstanceMailTemplateWriteModel{
|
|
MailTemplateWriteModel{
|
|
WriteModel: eventstore.WriteModel{
|
|
AggregateID: authz.GetInstance(ctx).InstanceID(),
|
|
ResourceOwner: authz.GetInstance(ctx).InstanceID(),
|
|
InstanceID: authz.GetInstance(ctx).InstanceID(),
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
func (wm *InstanceMailTemplateWriteModel) AppendEvents(events ...eventstore.Event) {
|
|
for _, event := range events {
|
|
switch e := event.(type) {
|
|
case *instance.MailTemplateAddedEvent:
|
|
wm.MailTemplateWriteModel.AppendEvents(&e.MailTemplateAddedEvent)
|
|
case *instance.MailTemplateChangedEvent:
|
|
wm.MailTemplateWriteModel.AppendEvents(&e.MailTemplateChangedEvent)
|
|
}
|
|
}
|
|
}
|
|
|
|
func (wm *InstanceMailTemplateWriteModel) Reduce() error {
|
|
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()
|
|
}
|
|
|
|
func (wm *InstanceMailTemplateWriteModel) NewChangedEvent(
|
|
ctx context.Context,
|
|
aggregate *eventstore.Aggregate,
|
|
template []byte,
|
|
) (*instance.MailTemplateChangedEvent, bool) {
|
|
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)
|
|
if err != nil {
|
|
return nil, false
|
|
}
|
|
return changedEvent, true
|
|
}
|