2021-10-29 14:11:47 +00:00
|
|
|
package projection
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
|
|
"github.com/zitadel/zitadel/internal/errors"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore/handler"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore/handler/crdb"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/instance"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/org"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/policy"
|
2021-10-29 14:11:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2022-11-30 16:01:17 +00:00
|
|
|
MailTemplateTable = "projections.mail_templates2"
|
2021-10-29 14:11:47 +00:00
|
|
|
|
|
|
|
MailTemplateAggregateIDCol = "aggregate_id"
|
2022-03-23 08:02:39 +00:00
|
|
|
MailTemplateInstanceIDCol = "instance_id"
|
2021-10-29 14:11:47 +00:00
|
|
|
MailTemplateCreationDateCol = "creation_date"
|
|
|
|
MailTemplateChangeDateCol = "change_date"
|
|
|
|
MailTemplateSequenceCol = "sequence"
|
|
|
|
MailTemplateStateCol = "state"
|
|
|
|
MailTemplateIsDefaultCol = "is_default"
|
2022-03-23 08:02:39 +00:00
|
|
|
MailTemplateTemplateCol = "template"
|
2022-11-30 16:01:17 +00:00
|
|
|
MailTemplateOwnerRemovedCol = "owner_removed"
|
2021-10-29 14:11:47 +00:00
|
|
|
)
|
|
|
|
|
2022-06-14 05:51:00 +00:00
|
|
|
type mailTemplateProjection struct {
|
2022-03-23 08:02:39 +00:00
|
|
|
crdb.StatementHandler
|
|
|
|
}
|
|
|
|
|
2022-06-14 05:51:00 +00:00
|
|
|
func newMailTemplateProjection(ctx context.Context, config crdb.StatementHandlerConfig) *mailTemplateProjection {
|
|
|
|
p := new(mailTemplateProjection)
|
2021-10-29 14:11:47 +00:00
|
|
|
config.ProjectionName = MailTemplateTable
|
|
|
|
config.Reducers = p.reducers()
|
2022-03-23 08:02:39 +00:00
|
|
|
config.InitCheck = crdb.NewTableCheck(
|
|
|
|
crdb.NewTable([]*crdb.Column{
|
|
|
|
crdb.NewColumn(MailTemplateAggregateIDCol, crdb.ColumnTypeText),
|
|
|
|
crdb.NewColumn(MailTemplateInstanceIDCol, crdb.ColumnTypeText),
|
|
|
|
crdb.NewColumn(MailTemplateCreationDateCol, crdb.ColumnTypeTimestamp),
|
|
|
|
crdb.NewColumn(MailTemplateChangeDateCol, crdb.ColumnTypeTimestamp),
|
|
|
|
crdb.NewColumn(MailTemplateSequenceCol, crdb.ColumnTypeInt64),
|
|
|
|
crdb.NewColumn(MailTemplateStateCol, crdb.ColumnTypeEnum),
|
|
|
|
crdb.NewColumn(MailTemplateIsDefaultCol, crdb.ColumnTypeBool, crdb.Default(false)),
|
|
|
|
crdb.NewColumn(MailTemplateTemplateCol, crdb.ColumnTypeBytes),
|
2022-11-30 16:01:17 +00:00
|
|
|
crdb.NewColumn(MailTemplateOwnerRemovedCol, crdb.ColumnTypeBool, crdb.Default(false)),
|
2022-03-23 08:02:39 +00:00
|
|
|
},
|
|
|
|
crdb.NewPrimaryKey(MailTemplateInstanceIDCol, MailTemplateAggregateIDCol),
|
2022-11-30 16:01:17 +00:00
|
|
|
crdb.WithIndex(crdb.NewIndex("owner_removed", []string{MailTemplateOwnerRemovedCol})),
|
2022-03-23 08:02:39 +00:00
|
|
|
),
|
|
|
|
)
|
2021-10-29 14:11:47 +00:00
|
|
|
p.StatementHandler = crdb.NewStatementHandler(ctx, config)
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2022-06-14 05:51:00 +00:00
|
|
|
func (p *mailTemplateProjection) reducers() []handler.AggregateReducer {
|
2021-10-29 14:11:47 +00:00
|
|
|
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,
|
|
|
|
},
|
2022-11-30 16:01:17 +00:00
|
|
|
{
|
|
|
|
Event: org.OrgRemovedEventType,
|
|
|
|
Reduce: p.reduceOwnerRemoved,
|
|
|
|
},
|
2021-10-29 14:11:47 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-03-24 16:21:34 +00:00
|
|
|
Aggregate: instance.AggregateType,
|
2021-10-29 14:11:47 +00:00
|
|
|
EventRedusers: []handler.EventReducer{
|
|
|
|
{
|
2022-03-24 16:21:34 +00:00
|
|
|
Event: instance.MailTemplateAddedEventType,
|
2021-10-29 14:11:47 +00:00
|
|
|
Reduce: p.reduceAdded,
|
|
|
|
},
|
|
|
|
{
|
2022-03-24 16:21:34 +00:00
|
|
|
Event: instance.MailTemplateChangedEventType,
|
2021-10-29 14:11:47 +00:00
|
|
|
Reduce: p.reduceChanged,
|
|
|
|
},
|
2022-10-20 12:36:52 +00:00
|
|
|
{
|
|
|
|
Event: instance.InstanceRemovedEventType,
|
|
|
|
Reduce: reduceInstanceRemovedHelper(MailTemplateInstanceIDCol),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-10-29 14:11:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-14 05:51:00 +00:00
|
|
|
func (p *mailTemplateProjection) reduceAdded(event eventstore.Event) (*handler.Statement, error) {
|
2021-10-29 14:11:47 +00:00
|
|
|
var templateEvent policy.MailTemplateAddedEvent
|
|
|
|
var isDefault bool
|
|
|
|
switch e := event.(type) {
|
|
|
|
case *org.MailTemplateAddedEvent:
|
|
|
|
templateEvent = e.MailTemplateAddedEvent
|
|
|
|
isDefault = false
|
2022-03-24 16:21:34 +00:00
|
|
|
case *instance.MailTemplateAddedEvent:
|
2021-10-29 14:11:47 +00:00
|
|
|
templateEvent = e.MailTemplateAddedEvent
|
|
|
|
isDefault = true
|
|
|
|
default:
|
2022-03-24 16:21:34 +00:00
|
|
|
return nil, errors.ThrowInvalidArgumentf(nil, "PROJE-0pJ3f", "reduce.wrong.event.type, %v", []eventstore.EventType{org.MailTemplateAddedEventType, instance.MailTemplateAddedEventType})
|
2021-10-29 14:11:47 +00:00
|
|
|
}
|
|
|
|
return crdb.NewCreateStatement(
|
|
|
|
&templateEvent,
|
|
|
|
[]handler.Column{
|
|
|
|
handler.NewCol(MailTemplateAggregateIDCol, templateEvent.Aggregate().ID),
|
2022-03-23 08:02:39 +00:00
|
|
|
handler.NewCol(MailTemplateInstanceIDCol, templateEvent.Aggregate().InstanceID),
|
2021-10-29 14:11:47 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-06-14 05:51:00 +00:00
|
|
|
func (p *mailTemplateProjection) reduceChanged(event eventstore.Event) (*handler.Statement, error) {
|
2021-10-29 14:11:47 +00:00
|
|
|
var policyEvent policy.MailTemplateChangedEvent
|
|
|
|
switch e := event.(type) {
|
|
|
|
case *org.MailTemplateChangedEvent:
|
|
|
|
policyEvent = e.MailTemplateChangedEvent
|
2022-03-24 16:21:34 +00:00
|
|
|
case *instance.MailTemplateChangedEvent:
|
2021-10-29 14:11:47 +00:00
|
|
|
policyEvent = e.MailTemplateChangedEvent
|
|
|
|
default:
|
2022-03-24 16:21:34 +00:00
|
|
|
return nil, errors.ThrowInvalidArgumentf(nil, "PROJE-gJ03f", "reduce.wrong.event.type, %v", []eventstore.EventType{org.MailTemplateChangedEventType, instance.MailTemplateChangedEventType})
|
2021-10-29 14:11:47 +00:00
|
|
|
}
|
|
|
|
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),
|
2022-11-10 10:59:33 +00:00
|
|
|
handler.NewCond(MailTemplateInstanceIDCol, policyEvent.Aggregate().InstanceID),
|
2021-10-29 14:11:47 +00:00
|
|
|
}), nil
|
|
|
|
}
|
|
|
|
|
2022-06-14 05:51:00 +00:00
|
|
|
func (p *mailTemplateProjection) reduceRemoved(event eventstore.Event) (*handler.Statement, error) {
|
2021-10-29 14:11:47 +00:00
|
|
|
policyEvent, ok := event.(*org.MailTemplateRemovedEvent)
|
|
|
|
if !ok {
|
2022-03-23 08:02:39 +00:00
|
|
|
return nil, errors.ThrowInvalidArgumentf(nil, "PROJE-3jJGs", "reduce.wrong.event.type %s", org.MailTemplateRemovedEventType)
|
2021-10-29 14:11:47 +00:00
|
|
|
}
|
|
|
|
return crdb.NewDeleteStatement(
|
|
|
|
policyEvent,
|
|
|
|
[]handler.Condition{
|
|
|
|
handler.NewCond(MailTemplateAggregateIDCol, policyEvent.Aggregate().ID),
|
2022-11-10 10:59:33 +00:00
|
|
|
handler.NewCond(MailTemplateInstanceIDCol, policyEvent.Aggregate().InstanceID),
|
2021-10-29 14:11:47 +00:00
|
|
|
}), nil
|
|
|
|
}
|
2022-11-30 16:01:17 +00:00
|
|
|
|
|
|
|
func (p *mailTemplateProjection) reduceOwnerRemoved(event eventstore.Event) (*handler.Statement, error) {
|
|
|
|
e, ok := event.(*org.OrgRemovedEvent)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.ThrowInvalidArgumentf(nil, "PROJE-CThXR", "reduce.wrong.event.type %s", org.OrgRemovedEventType)
|
|
|
|
}
|
|
|
|
|
|
|
|
return crdb.NewUpdateStatement(
|
|
|
|
e,
|
|
|
|
[]handler.Column{
|
|
|
|
handler.NewCol(MailTemplateChangeDateCol, e.CreationDate()),
|
|
|
|
handler.NewCol(MailTemplateSequenceCol, e.Sequence()),
|
|
|
|
handler.NewCol(MailTemplateOwnerRemovedCol, true),
|
|
|
|
},
|
|
|
|
[]handler.Condition{
|
|
|
|
handler.NewCond(MailTemplateInstanceIDCol, e.Aggregate().InstanceID),
|
|
|
|
handler.NewCond(MailTemplateAggregateIDCol, e.Aggregate().ID),
|
|
|
|
},
|
|
|
|
), nil
|
|
|
|
}
|