zitadel/internal/command/instance_policy_mail_template_model.go
Silvan b5564572bc
feat(eventstore): increase parallel write capabilities (#5940)
This implementation increases parallel write capabilities of the eventstore.
Please have a look at the technical advisories: [05](https://zitadel.com/docs/support/advisory/a10005) and  [06](https://zitadel.com/docs/support/advisory/a10006).
The implementation of eventstore.push is rewritten and stored events are migrated to a new table `eventstore.events2`.
If you are using cockroach: make sure that the database user of ZITADEL has `VIEWACTIVITY` grant. This is used to query events.
2023-10-19 12:19:10 +02:00

75 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
}