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/eventstore"
|
2023-10-19 10:19:10 +00:00
|
|
|
old_handler "github.com/zitadel/zitadel/internal/eventstore/handler"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore/handler/v2"
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/repository/instance"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/org"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/policy"
|
2023-12-08 14:30:55 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
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
|
|
|
)
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
type mailTemplateProjection struct{}
|
|
|
|
|
|
|
|
func newMailTemplateProjection(ctx context.Context, config handler.Config) *handler.Handler {
|
|
|
|
return handler.NewHandler(ctx, &config, new(mailTemplateProjection))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*mailTemplateProjection) Name() string {
|
|
|
|
return MailTemplateTable
|
2022-03-23 08:02:39 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (*mailTemplateProjection) Init() *old_handler.Check {
|
|
|
|
return handler.NewTableCheck(
|
|
|
|
handler.NewTable([]*handler.InitColumn{
|
|
|
|
handler.NewColumn(MailTemplateAggregateIDCol, handler.ColumnTypeText),
|
|
|
|
handler.NewColumn(MailTemplateInstanceIDCol, handler.ColumnTypeText),
|
|
|
|
handler.NewColumn(MailTemplateCreationDateCol, handler.ColumnTypeTimestamp),
|
|
|
|
handler.NewColumn(MailTemplateChangeDateCol, handler.ColumnTypeTimestamp),
|
|
|
|
handler.NewColumn(MailTemplateSequenceCol, handler.ColumnTypeInt64),
|
|
|
|
handler.NewColumn(MailTemplateStateCol, handler.ColumnTypeEnum),
|
|
|
|
handler.NewColumn(MailTemplateIsDefaultCol, handler.ColumnTypeBool, handler.Default(false)),
|
|
|
|
handler.NewColumn(MailTemplateTemplateCol, handler.ColumnTypeBytes),
|
|
|
|
handler.NewColumn(MailTemplateOwnerRemovedCol, handler.ColumnTypeBool, handler.Default(false)),
|
2022-03-23 08:02:39 +00:00
|
|
|
},
|
2023-10-19 10:19:10 +00:00
|
|
|
handler.NewPrimaryKey(MailTemplateInstanceIDCol, MailTemplateAggregateIDCol),
|
|
|
|
handler.WithIndex(handler.NewIndex("owner_removed", []string{MailTemplateOwnerRemovedCol})),
|
2022-03-23 08:02:39 +00:00
|
|
|
),
|
|
|
|
)
|
2021-10-29 14:11:47 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (p *mailTemplateProjection) Reducers() []handler.AggregateReducer {
|
2021-10-29 14:11:47 +00:00
|
|
|
return []handler.AggregateReducer{
|
|
|
|
{
|
|
|
|
Aggregate: org.AggregateType,
|
2023-10-19 10:19:10 +00:00
|
|
|
EventReducers: []handler.EventReducer{
|
2021-10-29 14:11:47 +00:00
|
|
|
{
|
|
|
|
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,
|
2023-10-19 10:19:10 +00:00
|
|
|
EventReducers: []handler.EventReducer{
|
2021-10-29 14:11:47 +00:00
|
|
|
{
|
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:
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInvalidArgumentf(nil, "PROJE-0pJ3f", "reduce.wrong.event.type, %v", []eventstore.EventType{org.MailTemplateAddedEventType, instance.MailTemplateAddedEventType})
|
2021-10-29 14:11:47 +00:00
|
|
|
}
|
2023-10-19 10:19:10 +00:00
|
|
|
return handler.NewCreateStatement(
|
2021-10-29 14:11:47 +00:00
|
|
|
&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:
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.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))
|
|
|
|
}
|
2023-10-19 10:19:10 +00:00
|
|
|
return handler.NewUpdateStatement(
|
2021-10-29 14:11:47 +00:00
|
|
|
&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 {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInvalidArgumentf(nil, "PROJE-3jJGs", "reduce.wrong.event.type %s", org.MailTemplateRemovedEventType)
|
2021-10-29 14:11:47 +00:00
|
|
|
}
|
2023-10-19 10:19:10 +00:00
|
|
|
return handler.NewDeleteStatement(
|
2021-10-29 14:11:47 +00:00
|
|
|
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 {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInvalidArgumentf(nil, "PROJE-CThXR", "reduce.wrong.event.type %s", org.OrgRemovedEventType)
|
2022-11-30 16:01:17 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
return handler.NewDeleteStatement(
|
2022-11-30 16:01:17 +00:00
|
|
|
e,
|
|
|
|
[]handler.Condition{
|
|
|
|
handler.NewCond(MailTemplateInstanceIDCol, e.Aggregate().InstanceID),
|
|
|
|
handler.NewCond(MailTemplateAggregateIDCol, e.Aggregate().ID),
|
|
|
|
},
|
|
|
|
), nil
|
|
|
|
}
|