zitadel/internal/query/projection/mail_template.go
Livio Amstutz 56b916a2b0
feat: projections auto create their tables (#3324)
* begin init checks for projections

* first projection checks

* debug notification providers with query fixes

* more projections and first index

* more projections

* more projections

* finish projections

* fix tests (remove db name)

* create tables in setup

* fix logging / error handling

* add tenant to views

* rename tenant to instance_id

* add instance_id to all projections

* add instance_id to all queries

* correct instance_id on projections

* add instance_id to failed_events

* use separate context for instance

* implement features projection

* implement features projection

* remove unique constraint from setup when migration failed

* add error to failed setup event

* add instance_id to primary keys

* fix IAM projection

* remove old migrations folder

* fix keysFromYAML test
2022-03-23 09:02:39 +01:00

153 lines
5.3 KiB
Go

package projection
import (
"context"
"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"
)
const (
MailTemplateTable = "projections.mail_templates"
MailTemplateAggregateIDCol = "aggregate_id"
MailTemplateInstanceIDCol = "instance_id"
MailTemplateCreationDateCol = "creation_date"
MailTemplateChangeDateCol = "change_date"
MailTemplateSequenceCol = "sequence"
MailTemplateStateCol = "state"
MailTemplateIsDefaultCol = "is_default"
MailTemplateTemplateCol = "template"
)
type MailTemplateProjection struct {
crdb.StatementHandler
}
func NewMailTemplateProjection(ctx context.Context, config crdb.StatementHandlerConfig) *MailTemplateProjection {
p := new(MailTemplateProjection)
config.ProjectionName = MailTemplateTable
config.Reducers = p.reducers()
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),
},
crdb.NewPrimaryKey(MailTemplateInstanceIDCol, MailTemplateAggregateIDCol),
),
)
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.Event) (*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:
return nil, errors.ThrowInvalidArgumentf(nil, "PROJE-0pJ3f", "reduce.wrong.event.type, %v", []eventstore.EventType{org.MailTemplateAddedEventType, iam.MailTemplateAddedEventType})
}
return crdb.NewCreateStatement(
&templateEvent,
[]handler.Column{
handler.NewCol(MailTemplateAggregateIDCol, templateEvent.Aggregate().ID),
handler.NewCol(MailTemplateInstanceIDCol, templateEvent.Aggregate().InstanceID),
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.Event) (*handler.Statement, error) {
var policyEvent policy.MailTemplateChangedEvent
switch e := event.(type) {
case *org.MailTemplateChangedEvent:
policyEvent = e.MailTemplateChangedEvent
case *iam.MailTemplateChangedEvent:
policyEvent = e.MailTemplateChangedEvent
default:
return nil, errors.ThrowInvalidArgumentf(nil, "PROJE-gJ03f", "reduce.wrong.event.type, %v", []eventstore.EventType{org.MailTemplateChangedEventType, iam.MailTemplateChangedEventType})
}
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.Event) (*handler.Statement, error) {
policyEvent, ok := event.(*org.MailTemplateRemovedEvent)
if !ok {
return nil, errors.ThrowInvalidArgumentf(nil, "PROJE-3jJGs", "reduce.wrong.event.type %s", org.MailTemplateRemovedEventType)
}
return crdb.NewDeleteStatement(
policyEvent,
[]handler.Condition{
handler.NewCond(MailTemplateAggregateIDCol, policyEvent.Aggregate().ID),
}), nil
}