mirror of
https://github.com/zitadel/zitadel.git
synced 2024-12-12 19:14:23 +00:00
a1f4a06d27
* feat: mail template projection * feat: message text projection * feat: message text projection tests * feat: custom text projection * feat: custom text projection
141 lines
4.8 KiB
Go
141 lines
4.8 KiB
Go
package projection
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/caos/logging"
|
|
"github.com/caos/zitadel/internal/domain"
|
|
"github.com/caos/zitadel/internal/errors"
|
|
"github.com/caos/zitadel/internal/eventstore"
|
|
"github.com/caos/zitadel/internal/eventstore/handler"
|
|
"github.com/caos/zitadel/internal/eventstore/handler/crdb"
|
|
"github.com/caos/zitadel/internal/repository/iam"
|
|
"github.com/caos/zitadel/internal/repository/org"
|
|
"github.com/caos/zitadel/internal/repository/policy"
|
|
)
|
|
|
|
type MailTemplateProjection struct {
|
|
crdb.StatementHandler
|
|
}
|
|
|
|
const (
|
|
MailTemplateTable = "zitadel.projections.mail_templates"
|
|
|
|
MailTemplateAggregateIDCol = "aggregate_id"
|
|
MailTemplateCreationDateCol = "creation_date"
|
|
MailTemplateChangeDateCol = "change_date"
|
|
MailTemplateSequenceCol = "sequence"
|
|
MailTemplateStateCol = "state"
|
|
MailTemplateTemplateCol = "template"
|
|
MailTemplateIsDefaultCol = "is_default"
|
|
)
|
|
|
|
func NewMailTemplateProjection(ctx context.Context, config crdb.StatementHandlerConfig) *MailTemplateProjection {
|
|
p := &MailTemplateProjection{}
|
|
config.ProjectionName = MailTemplateTable
|
|
config.Reducers = p.reducers()
|
|
p.StatementHandler = crdb.NewStatementHandler(ctx, config)
|
|
return p
|
|
}
|
|
|
|
func (p *MailTemplateProjection) reducers() []handler.AggregateReducer {
|
|
return []handler.AggregateReducer{
|
|
{
|
|
Aggregate: org.AggregateType,
|
|
EventRedusers: []handler.EventReducer{
|
|
{
|
|
Event: org.MailTemplateAddedEventType,
|
|
Reduce: p.reduceAdded,
|
|
},
|
|
{
|
|
Event: org.MailTemplateChangedEventType,
|
|
Reduce: p.reduceChanged,
|
|
},
|
|
{
|
|
Event: org.MailTemplateRemovedEventType,
|
|
Reduce: p.reduceRemoved,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
Aggregate: iam.AggregateType,
|
|
EventRedusers: []handler.EventReducer{
|
|
{
|
|
Event: iam.MailTemplateAddedEventType,
|
|
Reduce: p.reduceAdded,
|
|
},
|
|
{
|
|
Event: iam.MailTemplateChangedEventType,
|
|
Reduce: p.reduceChanged,
|
|
},
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
func (p *MailTemplateProjection) reduceAdded(event eventstore.EventReader) (*handler.Statement, error) {
|
|
var templateEvent policy.MailTemplateAddedEvent
|
|
var isDefault bool
|
|
switch e := event.(type) {
|
|
case *org.MailTemplateAddedEvent:
|
|
templateEvent = e.MailTemplateAddedEvent
|
|
isDefault = false
|
|
case *iam.MailTemplateAddedEvent:
|
|
templateEvent = e.MailTemplateAddedEvent
|
|
isDefault = true
|
|
default:
|
|
logging.LogWithFields("PROJE-94jfG", "seq", event.Sequence(), "expectedTypes", []eventstore.EventType{org.MailTemplateAddedEventType, iam.MailTemplateAddedEventType}).Error("wrong event type")
|
|
return nil, errors.ThrowInvalidArgument(nil, "PROJE-0pJ3f", "reduce.wrong.event.type")
|
|
}
|
|
return crdb.NewCreateStatement(
|
|
&templateEvent,
|
|
[]handler.Column{
|
|
handler.NewCol(MailTemplateAggregateIDCol, templateEvent.Aggregate().ID),
|
|
handler.NewCol(MailTemplateCreationDateCol, templateEvent.CreationDate()),
|
|
handler.NewCol(MailTemplateChangeDateCol, templateEvent.CreationDate()),
|
|
handler.NewCol(MailTemplateSequenceCol, templateEvent.Sequence()),
|
|
handler.NewCol(MailTemplateStateCol, domain.PolicyStateActive),
|
|
handler.NewCol(MailTemplateIsDefaultCol, isDefault),
|
|
handler.NewCol(MailTemplateTemplateCol, templateEvent.Template),
|
|
}), nil
|
|
}
|
|
|
|
func (p *MailTemplateProjection) reduceChanged(event eventstore.EventReader) (*handler.Statement, error) {
|
|
var policyEvent policy.MailTemplateChangedEvent
|
|
switch e := event.(type) {
|
|
case *org.MailTemplateChangedEvent:
|
|
policyEvent = e.MailTemplateChangedEvent
|
|
case *iam.MailTemplateChangedEvent:
|
|
policyEvent = e.MailTemplateChangedEvent
|
|
default:
|
|
logging.LogWithFields("PROJE-02J9f", "seq", event.Sequence(), "expectedTypes", []eventstore.EventType{org.MailTemplateChangedEventType, iam.MailTemplateChangedEventType}).Error("wrong event type")
|
|
return nil, errors.ThrowInvalidArgument(nil, "PROJE-gJ03f", "reduce.wrong.event.type")
|
|
}
|
|
cols := []handler.Column{
|
|
handler.NewCol(MailTemplateChangeDateCol, policyEvent.CreationDate()),
|
|
handler.NewCol(MailTemplateSequenceCol, policyEvent.Sequence()),
|
|
}
|
|
if policyEvent.Template != nil {
|
|
cols = append(cols, handler.NewCol(MailTemplateTemplateCol, *policyEvent.Template))
|
|
}
|
|
return crdb.NewUpdateStatement(
|
|
&policyEvent,
|
|
cols,
|
|
[]handler.Condition{
|
|
handler.NewCond(MailTemplateAggregateIDCol, policyEvent.Aggregate().ID),
|
|
}), nil
|
|
}
|
|
|
|
func (p *MailTemplateProjection) reduceRemoved(event eventstore.EventReader) (*handler.Statement, error) {
|
|
policyEvent, ok := event.(*org.MailTemplateRemovedEvent)
|
|
if !ok {
|
|
logging.LogWithFields("PROJE-2m0fp", "seq", event.Sequence(), "expectedType", org.MailTemplateRemovedEventType).Error("wrong event type")
|
|
return nil, errors.ThrowInvalidArgument(nil, "PROJE-3jJGs", "reduce.wrong.event.type")
|
|
}
|
|
return crdb.NewDeleteStatement(
|
|
policyEvent,
|
|
[]handler.Condition{
|
|
handler.NewCond(MailTemplateAggregateIDCol, policyEvent.Aggregate().ID),
|
|
}), nil
|
|
}
|