2022-02-16 15:49:17 +00:00
|
|
|
package projection
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
"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/project"
|
2022-02-16 15:49:17 +00:00
|
|
|
)
|
|
|
|
|
2022-03-23 08:02:39 +00:00
|
|
|
const (
|
|
|
|
SMTPConfigProjectionTable = "projections.smtp_configs"
|
|
|
|
|
|
|
|
SMTPConfigColumnAggregateID = "aggregate_id"
|
|
|
|
SMTPConfigColumnCreationDate = "creation_date"
|
|
|
|
SMTPConfigColumnChangeDate = "change_date"
|
|
|
|
SMTPConfigColumnSequence = "sequence"
|
|
|
|
SMTPConfigColumnResourceOwner = "resource_owner"
|
|
|
|
SMTPConfigColumnInstanceID = "instance_id"
|
|
|
|
SMTPConfigColumnTLS = "tls"
|
|
|
|
SMTPConfigColumnSenderAddress = "sender_address"
|
|
|
|
SMTPConfigColumnSenderName = "sender_name"
|
|
|
|
SMTPConfigColumnSMTPHost = "host"
|
|
|
|
SMTPConfigColumnSMTPUser = "username"
|
|
|
|
SMTPConfigColumnSMTPPassword = "password"
|
|
|
|
)
|
|
|
|
|
2022-02-16 15:49:17 +00:00
|
|
|
type SMTPConfigProjection struct {
|
|
|
|
crdb.StatementHandler
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewSMTPConfigProjection(ctx context.Context, config crdb.StatementHandlerConfig) *SMTPConfigProjection {
|
2022-02-25 15:05:06 +00:00
|
|
|
p := new(SMTPConfigProjection)
|
2022-02-16 15:49:17 +00:00
|
|
|
config.ProjectionName = SMTPConfigProjectionTable
|
|
|
|
config.Reducers = p.reducers()
|
2022-03-23 08:02:39 +00:00
|
|
|
config.InitCheck = crdb.NewMultiTableCheck(
|
|
|
|
crdb.NewTable([]*crdb.Column{
|
|
|
|
crdb.NewColumn(SMTPConfigColumnAggregateID, crdb.ColumnTypeText),
|
|
|
|
crdb.NewColumn(SMTPConfigColumnCreationDate, crdb.ColumnTypeTimestamp),
|
|
|
|
crdb.NewColumn(SMTPConfigColumnChangeDate, crdb.ColumnTypeTimestamp),
|
|
|
|
crdb.NewColumn(SMTPConfigColumnSequence, crdb.ColumnTypeInt64),
|
|
|
|
crdb.NewColumn(SMTPConfigColumnResourceOwner, crdb.ColumnTypeText),
|
|
|
|
crdb.NewColumn(SMTPConfigColumnInstanceID, crdb.ColumnTypeText),
|
|
|
|
crdb.NewColumn(SMTPConfigColumnTLS, crdb.ColumnTypeBool),
|
|
|
|
crdb.NewColumn(SMTPConfigColumnSenderAddress, crdb.ColumnTypeText),
|
|
|
|
crdb.NewColumn(SMTPConfigColumnSenderName, crdb.ColumnTypeText),
|
|
|
|
crdb.NewColumn(SMTPConfigColumnSMTPHost, crdb.ColumnTypeText),
|
|
|
|
crdb.NewColumn(SMTPConfigColumnSMTPUser, crdb.ColumnTypeText),
|
|
|
|
crdb.NewColumn(SMTPConfigColumnSMTPPassword, crdb.ColumnTypeJSONB, crdb.Nullable()),
|
|
|
|
},
|
|
|
|
crdb.NewPrimaryKey(SMTPConfigColumnInstanceID, SMTPConfigColumnAggregateID),
|
|
|
|
),
|
|
|
|
)
|
2022-02-16 15:49:17 +00:00
|
|
|
p.StatementHandler = crdb.NewStatementHandler(ctx, config)
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *SMTPConfigProjection) reducers() []handler.AggregateReducer {
|
|
|
|
return []handler.AggregateReducer{
|
|
|
|
{
|
|
|
|
Aggregate: project.AggregateType,
|
|
|
|
EventRedusers: []handler.EventReducer{
|
|
|
|
{
|
2022-03-24 16:21:34 +00:00
|
|
|
Event: instance.SMTPConfigAddedEventType,
|
2022-02-16 15:49:17 +00:00
|
|
|
Reduce: p.reduceSMTPConfigAdded,
|
|
|
|
},
|
|
|
|
{
|
2022-03-24 16:21:34 +00:00
|
|
|
Event: instance.SMTPConfigChangedEventType,
|
2022-02-16 15:49:17 +00:00
|
|
|
Reduce: p.reduceSMTPConfigChanged,
|
|
|
|
},
|
|
|
|
{
|
2022-03-24 16:21:34 +00:00
|
|
|
Event: instance.SMTPConfigPasswordChangedEventType,
|
2022-02-16 15:49:17 +00:00
|
|
|
Reduce: p.reduceSMTPConfigPasswordChanged,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *SMTPConfigProjection) reduceSMTPConfigAdded(event eventstore.Event) (*handler.Statement, error) {
|
2022-03-24 16:21:34 +00:00
|
|
|
e, ok := event.(*instance.SMTPConfigAddedEvent)
|
2022-02-16 15:49:17 +00:00
|
|
|
if !ok {
|
2022-03-24 16:21:34 +00:00
|
|
|
return nil, errors.ThrowInvalidArgumentf(nil, "HANDL-sk99F", "reduce.wrong.event.type %s", instance.SMTPConfigAddedEventType)
|
2022-02-16 15:49:17 +00:00
|
|
|
}
|
|
|
|
return crdb.NewCreateStatement(
|
|
|
|
e,
|
|
|
|
[]handler.Column{
|
|
|
|
handler.NewCol(SMTPConfigColumnAggregateID, e.Aggregate().ID),
|
|
|
|
handler.NewCol(SMTPConfigColumnCreationDate, e.CreationDate()),
|
|
|
|
handler.NewCol(SMTPConfigColumnChangeDate, e.CreationDate()),
|
|
|
|
handler.NewCol(SMTPConfigColumnResourceOwner, e.Aggregate().ResourceOwner),
|
2022-03-23 08:02:39 +00:00
|
|
|
handler.NewCol(SMTPConfigColumnInstanceID, e.Aggregate().InstanceID),
|
2022-02-16 15:49:17 +00:00
|
|
|
handler.NewCol(SMTPConfigColumnSequence, e.Sequence()),
|
|
|
|
handler.NewCol(SMTPConfigColumnTLS, e.TLS),
|
2022-03-23 08:02:39 +00:00
|
|
|
handler.NewCol(SMTPConfigColumnSenderAddress, e.SenderAddress),
|
|
|
|
handler.NewCol(SMTPConfigColumnSenderName, e.SenderName),
|
2022-02-16 15:49:17 +00:00
|
|
|
handler.NewCol(SMTPConfigColumnSMTPHost, e.Host),
|
|
|
|
handler.NewCol(SMTPConfigColumnSMTPUser, e.User),
|
|
|
|
handler.NewCol(SMTPConfigColumnSMTPPassword, e.Password),
|
|
|
|
},
|
|
|
|
), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *SMTPConfigProjection) reduceSMTPConfigChanged(event eventstore.Event) (*handler.Statement, error) {
|
2022-03-24 16:21:34 +00:00
|
|
|
e, ok := event.(*instance.SMTPConfigChangedEvent)
|
2022-02-16 15:49:17 +00:00
|
|
|
if !ok {
|
2022-03-24 16:21:34 +00:00
|
|
|
return nil, errors.ThrowInvalidArgumentf(nil, "HANDL-wl0wd", "reduce.wrong.event.type %s", instance.SMTPConfigChangedEventType)
|
2022-02-16 15:49:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
columns := make([]handler.Column, 0, 7)
|
|
|
|
columns = append(columns, handler.NewCol(SMTPConfigColumnChangeDate, e.CreationDate()),
|
|
|
|
handler.NewCol(SMTPConfigColumnSequence, e.Sequence()))
|
|
|
|
if e.TLS != nil {
|
|
|
|
columns = append(columns, handler.NewCol(SMTPConfigColumnTLS, *e.TLS))
|
|
|
|
}
|
|
|
|
if e.FromAddress != nil {
|
2022-03-23 08:02:39 +00:00
|
|
|
columns = append(columns, handler.NewCol(SMTPConfigColumnSenderAddress, *e.FromAddress))
|
2022-02-16 15:49:17 +00:00
|
|
|
}
|
|
|
|
if e.FromName != nil {
|
2022-03-23 08:02:39 +00:00
|
|
|
columns = append(columns, handler.NewCol(SMTPConfigColumnSenderName, *e.FromName))
|
2022-02-16 15:49:17 +00:00
|
|
|
}
|
|
|
|
if e.Host != nil {
|
|
|
|
columns = append(columns, handler.NewCol(SMTPConfigColumnSMTPHost, *e.Host))
|
|
|
|
}
|
|
|
|
if e.User != nil {
|
|
|
|
columns = append(columns, handler.NewCol(SMTPConfigColumnSMTPUser, *e.User))
|
|
|
|
}
|
|
|
|
return crdb.NewUpdateStatement(
|
|
|
|
e,
|
|
|
|
columns,
|
|
|
|
[]handler.Condition{
|
|
|
|
handler.NewCond(SMTPConfigColumnAggregateID, e.Aggregate().ID),
|
|
|
|
},
|
|
|
|
), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *SMTPConfigProjection) reduceSMTPConfigPasswordChanged(event eventstore.Event) (*handler.Statement, error) {
|
2022-03-24 16:21:34 +00:00
|
|
|
e, ok := event.(*instance.SMTPConfigPasswordChangedEvent)
|
2022-02-16 15:49:17 +00:00
|
|
|
if !ok {
|
2022-03-24 16:21:34 +00:00
|
|
|
return nil, errors.ThrowInvalidArgumentf(nil, "HANDL-fk02f", "reduce.wrong.event.type %s", instance.SMTPConfigChangedEventType)
|
2022-02-16 15:49:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return crdb.NewUpdateStatement(
|
|
|
|
e,
|
|
|
|
[]handler.Column{
|
|
|
|
handler.NewCol(SMTPConfigColumnChangeDate, e.CreationDate()),
|
|
|
|
handler.NewCol(SMTPConfigColumnSequence, e.Sequence()),
|
|
|
|
handler.NewCol(SMTPConfigColumnSMTPPassword, e.Password),
|
|
|
|
},
|
|
|
|
[]handler.Condition{
|
|
|
|
handler.NewCond(SMTPConfigColumnAggregateID, e.Aggregate().ID),
|
|
|
|
},
|
|
|
|
), nil
|
|
|
|
}
|