2022-02-16 15:49:17 +00:00
|
|
|
package projection
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2024-04-11 07:16:10 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
2022-04-26 23:01:45 +00:00
|
|
|
"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"
|
2022-02-16 15:49:17 +00:00
|
|
|
)
|
|
|
|
|
2022-03-23 08:02:39 +00:00
|
|
|
const (
|
2024-10-04 09:34:44 +00:00
|
|
|
SMTPConfigProjectionTable = "projections.smtp_configs5"
|
2024-09-12 04:27:29 +00:00
|
|
|
SMTPConfigTable = SMTPConfigProjectionTable + "_" + smtpConfigSMTPTableSuffix
|
|
|
|
SMTPConfigHTTPTable = SMTPConfigProjectionTable + "_" + smtpConfigHTTPTableSuffix
|
|
|
|
|
|
|
|
SMTPConfigColumnInstanceID = "instance_id"
|
|
|
|
SMTPConfigColumnResourceOwner = "resource_owner"
|
|
|
|
SMTPConfigColumnAggregateID = "aggregate_id"
|
|
|
|
SMTPConfigColumnID = "id"
|
|
|
|
SMTPConfigColumnCreationDate = "creation_date"
|
|
|
|
SMTPConfigColumnChangeDate = "change_date"
|
|
|
|
SMTPConfigColumnSequence = "sequence"
|
|
|
|
SMTPConfigColumnState = "state"
|
|
|
|
SMTPConfigColumnDescription = "description"
|
|
|
|
|
|
|
|
smtpConfigSMTPTableSuffix = "smtp"
|
|
|
|
SMTPConfigSMTPColumnInstanceID = "instance_id"
|
|
|
|
SMTPConfigSMTPColumnID = "id"
|
|
|
|
SMTPConfigSMTPColumnTLS = "tls"
|
|
|
|
SMTPConfigSMTPColumnSenderAddress = "sender_address"
|
|
|
|
SMTPConfigSMTPColumnSenderName = "sender_name"
|
|
|
|
SMTPConfigSMTPColumnReplyToAddress = "reply_to_address"
|
|
|
|
SMTPConfigSMTPColumnHost = "host"
|
|
|
|
SMTPConfigSMTPColumnUser = "username"
|
|
|
|
SMTPConfigSMTPColumnPassword = "password"
|
|
|
|
|
|
|
|
smtpConfigHTTPTableSuffix = "http"
|
|
|
|
SMTPConfigHTTPColumnInstanceID = "instance_id"
|
|
|
|
SMTPConfigHTTPColumnID = "id"
|
|
|
|
SMTPConfigHTTPColumnEndpoint = "endpoint"
|
2022-03-23 08:02:39 +00:00
|
|
|
)
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
type smtpConfigProjection struct{}
|
|
|
|
|
|
|
|
func newSMTPConfigProjection(ctx context.Context, config handler.Config) *handler.Handler {
|
|
|
|
return handler.NewHandler(ctx, &config, new(smtpConfigProjection))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*smtpConfigProjection) Name() string {
|
|
|
|
return SMTPConfigProjectionTable
|
2022-02-16 15:49:17 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (*smtpConfigProjection) Init() *old_handler.Check {
|
2024-09-12 04:27:29 +00:00
|
|
|
return handler.NewMultiTableCheck(
|
2023-10-19 10:19:10 +00:00
|
|
|
handler.NewTable([]*handler.InitColumn{
|
2024-04-11 07:16:10 +00:00
|
|
|
handler.NewColumn(SMTPConfigColumnID, handler.ColumnTypeText),
|
2024-09-12 04:27:29 +00:00
|
|
|
handler.NewColumn(SMTPConfigColumnAggregateID, handler.ColumnTypeText),
|
2023-10-19 10:19:10 +00:00
|
|
|
handler.NewColumn(SMTPConfigColumnCreationDate, handler.ColumnTypeTimestamp),
|
|
|
|
handler.NewColumn(SMTPConfigColumnChangeDate, handler.ColumnTypeTimestamp),
|
|
|
|
handler.NewColumn(SMTPConfigColumnSequence, handler.ColumnTypeInt64),
|
|
|
|
handler.NewColumn(SMTPConfigColumnResourceOwner, handler.ColumnTypeText),
|
|
|
|
handler.NewColumn(SMTPConfigColumnInstanceID, handler.ColumnTypeText),
|
2024-04-11 07:16:10 +00:00
|
|
|
handler.NewColumn(SMTPConfigColumnDescription, handler.ColumnTypeText),
|
2024-09-12 04:27:29 +00:00
|
|
|
handler.NewColumn(SMTPConfigColumnState, handler.ColumnTypeEnum),
|
|
|
|
},
|
|
|
|
handler.NewPrimaryKey(SMTPConfigColumnInstanceID, SMTPConfigColumnID),
|
|
|
|
),
|
|
|
|
handler.NewSuffixedTable([]*handler.InitColumn{
|
|
|
|
handler.NewColumn(SMTPConfigSMTPColumnID, handler.ColumnTypeText),
|
|
|
|
handler.NewColumn(SMTPConfigSMTPColumnInstanceID, handler.ColumnTypeText),
|
|
|
|
handler.NewColumn(SMTPConfigSMTPColumnTLS, handler.ColumnTypeBool),
|
|
|
|
handler.NewColumn(SMTPConfigSMTPColumnSenderAddress, handler.ColumnTypeText),
|
|
|
|
handler.NewColumn(SMTPConfigSMTPColumnSenderName, handler.ColumnTypeText),
|
|
|
|
handler.NewColumn(SMTPConfigSMTPColumnReplyToAddress, handler.ColumnTypeText),
|
|
|
|
handler.NewColumn(SMTPConfigSMTPColumnHost, handler.ColumnTypeText),
|
|
|
|
handler.NewColumn(SMTPConfigSMTPColumnUser, handler.ColumnTypeText),
|
|
|
|
handler.NewColumn(SMTPConfigSMTPColumnPassword, handler.ColumnTypeJSONB, handler.Nullable()),
|
2022-03-23 08:02:39 +00:00
|
|
|
},
|
2024-09-12 04:27:29 +00:00
|
|
|
handler.NewPrimaryKey(SMTPConfigSMTPColumnInstanceID, SMTPConfigSMTPColumnID),
|
|
|
|
smtpConfigSMTPTableSuffix,
|
|
|
|
handler.WithForeignKey(handler.NewForeignKeyOfPublicKeys()),
|
|
|
|
),
|
|
|
|
handler.NewSuffixedTable([]*handler.InitColumn{
|
|
|
|
handler.NewColumn(SMTPConfigHTTPColumnID, handler.ColumnTypeText),
|
|
|
|
handler.NewColumn(SMTPConfigHTTPColumnInstanceID, handler.ColumnTypeText),
|
|
|
|
handler.NewColumn(SMTPConfigHTTPColumnEndpoint, handler.ColumnTypeText),
|
|
|
|
},
|
|
|
|
handler.NewPrimaryKey(SMTPConfigHTTPColumnInstanceID, SMTPConfigHTTPColumnID),
|
|
|
|
smtpConfigHTTPTableSuffix,
|
|
|
|
handler.WithForeignKey(handler.NewForeignKeyOfPublicKeys()),
|
2022-03-23 08:02:39 +00:00
|
|
|
),
|
|
|
|
)
|
2022-02-16 15:49:17 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (p *smtpConfigProjection) Reducers() []handler.AggregateReducer {
|
2022-02-16 15:49:17 +00:00
|
|
|
return []handler.AggregateReducer{
|
|
|
|
{
|
2022-05-13 12:13:07 +00:00
|
|
|
Aggregate: instance.AggregateType,
|
2023-10-19 10:19:10 +00:00
|
|
|
EventReducers: []handler.EventReducer{
|
2022-02-16 15:49:17 +00:00
|
|
|
{
|
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,
|
|
|
|
},
|
2024-09-12 04:27:29 +00:00
|
|
|
{
|
|
|
|
Event: instance.SMTPConfigHTTPAddedEventType,
|
|
|
|
Reduce: p.reduceSMTPConfigHTTPAdded,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: instance.SMTPConfigHTTPChangedEventType,
|
|
|
|
Reduce: p.reduceSMTPConfigHTTPChanged,
|
|
|
|
},
|
2024-04-11 07:16:10 +00:00
|
|
|
{
|
|
|
|
Event: instance.SMTPConfigActivatedEventType,
|
|
|
|
Reduce: p.reduceSMTPConfigActivated,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: instance.SMTPConfigDeactivatedEventType,
|
|
|
|
Reduce: p.reduceSMTPConfigDeactivated,
|
|
|
|
},
|
2023-08-18 12:22:57 +00:00
|
|
|
{
|
|
|
|
Event: instance.SMTPConfigRemovedEventType,
|
|
|
|
Reduce: p.reduceSMTPConfigRemoved,
|
|
|
|
},
|
2022-10-20 12:36:52 +00:00
|
|
|
{
|
|
|
|
Event: instance.InstanceRemovedEventType,
|
|
|
|
Reduce: reduceInstanceRemovedHelper(SMTPConfigColumnInstanceID),
|
|
|
|
},
|
2022-02-16 15:49:17 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-14 05:51:00 +00:00
|
|
|
func (p *smtpConfigProjection) reduceSMTPConfigAdded(event eventstore.Event) (*handler.Statement, error) {
|
2024-09-12 04:27:29 +00:00
|
|
|
e, err := assertEvent[*instance.SMTPConfigAddedEvent](event)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2022-02-16 15:49:17 +00:00
|
|
|
}
|
2024-04-11 07:16:10 +00:00
|
|
|
|
|
|
|
description := e.Description
|
|
|
|
state := domain.SMTPConfigStateInactive
|
|
|
|
if e.ID == "" {
|
|
|
|
description = "generic"
|
|
|
|
state = domain.SMTPConfigStateActive
|
|
|
|
}
|
|
|
|
|
2024-09-12 04:27:29 +00:00
|
|
|
return handler.NewMultiStatement(
|
2022-02-16 15:49:17 +00:00
|
|
|
e,
|
2024-09-12 04:27:29 +00:00
|
|
|
handler.AddCreateStatement(
|
|
|
|
[]handler.Column{
|
|
|
|
handler.NewCol(SMTPConfigColumnCreationDate, e.CreationDate()),
|
|
|
|
handler.NewCol(SMTPConfigColumnChangeDate, e.CreationDate()),
|
|
|
|
handler.NewCol(SMTPConfigColumnInstanceID, e.Aggregate().InstanceID),
|
|
|
|
handler.NewCol(SMTPConfigColumnResourceOwner, e.Aggregate().ResourceOwner),
|
|
|
|
handler.NewCol(SMTPConfigColumnAggregateID, e.Aggregate().ID),
|
2024-10-04 09:34:44 +00:00
|
|
|
handler.NewCol(SMTPConfigColumnID, getSMTPConfigID(e.ID, e.Aggregate())),
|
2024-09-12 04:27:29 +00:00
|
|
|
handler.NewCol(SMTPConfigColumnSequence, e.Sequence()),
|
|
|
|
handler.NewCol(SMTPConfigColumnState, state),
|
|
|
|
handler.NewCol(SMTPConfigColumnDescription, description),
|
|
|
|
},
|
|
|
|
),
|
|
|
|
handler.AddCreateStatement(
|
|
|
|
[]handler.Column{
|
|
|
|
handler.NewCol(SMTPConfigSMTPColumnInstanceID, e.Aggregate().InstanceID),
|
2024-10-04 09:34:44 +00:00
|
|
|
handler.NewCol(SMTPConfigSMTPColumnID, getSMTPConfigID(e.ID, e.Aggregate())),
|
2024-09-12 04:27:29 +00:00
|
|
|
handler.NewCol(SMTPConfigSMTPColumnTLS, e.TLS),
|
|
|
|
handler.NewCol(SMTPConfigSMTPColumnSenderAddress, e.SenderAddress),
|
|
|
|
handler.NewCol(SMTPConfigSMTPColumnSenderName, e.SenderName),
|
|
|
|
handler.NewCol(SMTPConfigSMTPColumnReplyToAddress, e.ReplyToAddress),
|
|
|
|
handler.NewCol(SMTPConfigSMTPColumnHost, e.Host),
|
|
|
|
handler.NewCol(SMTPConfigSMTPColumnUser, e.User),
|
|
|
|
handler.NewCol(SMTPConfigSMTPColumnPassword, e.Password),
|
|
|
|
},
|
|
|
|
handler.WithTableSuffix(smtpConfigSMTPTableSuffix),
|
|
|
|
),
|
2022-02-16 15:49:17 +00:00
|
|
|
), nil
|
|
|
|
}
|
|
|
|
|
2024-09-12 04:27:29 +00:00
|
|
|
func (p *smtpConfigProjection) reduceSMTPConfigHTTPAdded(event eventstore.Event) (*handler.Statement, error) {
|
|
|
|
e, err := assertEvent[*instance.SMTPConfigHTTPAddedEvent](event)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2022-02-16 15:49:17 +00:00
|
|
|
}
|
|
|
|
|
2024-09-12 04:27:29 +00:00
|
|
|
return handler.NewMultiStatement(
|
|
|
|
e,
|
|
|
|
handler.AddCreateStatement(
|
|
|
|
[]handler.Column{
|
|
|
|
handler.NewCol(SMTPConfigColumnCreationDate, e.CreationDate()),
|
|
|
|
handler.NewCol(SMTPConfigColumnChangeDate, e.CreationDate()),
|
|
|
|
handler.NewCol(SMTPConfigColumnInstanceID, e.Aggregate().InstanceID),
|
|
|
|
handler.NewCol(SMTPConfigColumnResourceOwner, e.Aggregate().ResourceOwner),
|
|
|
|
handler.NewCol(SMTPConfigColumnAggregateID, e.Aggregate().ID),
|
2024-10-04 09:34:44 +00:00
|
|
|
handler.NewCol(SMTPConfigColumnID, getSMTPConfigID(e.ID, e.Aggregate())),
|
2024-09-12 04:27:29 +00:00
|
|
|
handler.NewCol(SMTPConfigColumnSequence, e.Sequence()),
|
|
|
|
handler.NewCol(SMTPConfigColumnState, domain.SMTPConfigStateInactive),
|
|
|
|
handler.NewCol(SMTPConfigColumnDescription, e.Description),
|
|
|
|
},
|
|
|
|
),
|
|
|
|
handler.AddCreateStatement(
|
|
|
|
[]handler.Column{
|
2024-10-04 09:34:44 +00:00
|
|
|
handler.NewCol(SMTPConfigHTTPColumnInstanceID, e.Aggregate().InstanceID),
|
|
|
|
handler.NewCol(SMTPConfigHTTPColumnID, getSMTPConfigID(e.ID, e.Aggregate())),
|
2024-09-12 04:27:29 +00:00
|
|
|
handler.NewCol(SMTPConfigHTTPColumnEndpoint, e.Endpoint),
|
|
|
|
},
|
|
|
|
handler.WithTableSuffix(smtpConfigHTTPTableSuffix),
|
|
|
|
),
|
|
|
|
), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *smtpConfigProjection) reduceSMTPConfigHTTPChanged(event eventstore.Event) (*handler.Statement, error) {
|
|
|
|
e, err := assertEvent[*instance.SMTPConfigHTTPChangedEvent](event)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
stmts := make([]func(eventstore.Event) handler.Exec, 0, 3)
|
|
|
|
columns := []handler.Column{
|
|
|
|
handler.NewCol(SMTPConfigColumnChangeDate, e.CreationDate()),
|
|
|
|
handler.NewCol(SMTPConfigColumnSequence, e.Sequence()),
|
|
|
|
}
|
|
|
|
if e.Description != nil {
|
|
|
|
columns = append(columns, handler.NewCol(SMTPConfigColumnDescription, *e.Description))
|
|
|
|
}
|
|
|
|
if len(columns) > 0 {
|
|
|
|
stmts = append(stmts, handler.AddUpdateStatement(
|
|
|
|
columns,
|
|
|
|
[]handler.Condition{
|
2024-10-04 09:34:44 +00:00
|
|
|
handler.NewCond(SMTPConfigColumnID, getSMTPConfigID(e.ID, e.Aggregate())),
|
2024-09-12 04:27:29 +00:00
|
|
|
handler.NewCond(SMTPConfigColumnInstanceID, e.Aggregate().InstanceID),
|
|
|
|
},
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
smtpColumns := make([]handler.Column, 0, 1)
|
|
|
|
if e.Endpoint != nil {
|
|
|
|
smtpColumns = append(smtpColumns, handler.NewCol(SMTPConfigHTTPColumnEndpoint, *e.Endpoint))
|
|
|
|
}
|
|
|
|
if len(smtpColumns) > 0 {
|
|
|
|
stmts = append(stmts, handler.AddUpdateStatement(
|
|
|
|
smtpColumns,
|
|
|
|
[]handler.Condition{
|
2024-10-04 09:34:44 +00:00
|
|
|
handler.NewCond(SMTPConfigHTTPColumnID, getSMTPConfigID(e.ID, e.Aggregate())),
|
2024-09-12 04:27:29 +00:00
|
|
|
handler.NewCond(SMTPConfigHTTPColumnInstanceID, e.Aggregate().InstanceID),
|
|
|
|
},
|
|
|
|
handler.WithTableSuffix(smtpConfigHTTPTableSuffix),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
return handler.NewMultiStatement(e, stmts...), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *smtpConfigProjection) reduceSMTPConfigChanged(event eventstore.Event) (*handler.Statement, error) {
|
|
|
|
e, err := assertEvent[*instance.SMTPConfigChangedEvent](event)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-04-11 07:16:10 +00:00
|
|
|
|
2024-09-12 04:27:29 +00:00
|
|
|
stmts := make([]func(eventstore.Event) handler.Exec, 0, 3)
|
|
|
|
columns := []handler.Column{
|
|
|
|
handler.NewCol(SMTPConfigColumnChangeDate, e.CreationDate()),
|
|
|
|
handler.NewCol(SMTPConfigColumnSequence, e.Sequence()),
|
|
|
|
}
|
|
|
|
if e.Description != nil {
|
|
|
|
columns = append(columns, handler.NewCol(SMTPConfigColumnDescription, *e.Description))
|
|
|
|
}
|
|
|
|
if len(columns) > 0 {
|
|
|
|
stmts = append(stmts, handler.AddUpdateStatement(
|
|
|
|
columns,
|
|
|
|
[]handler.Condition{
|
2024-10-04 09:34:44 +00:00
|
|
|
handler.NewCond(SMTPConfigColumnID, getSMTPConfigID(e.ID, e.Aggregate())),
|
2024-09-12 04:27:29 +00:00
|
|
|
handler.NewCond(SMTPConfigColumnInstanceID, e.Aggregate().InstanceID),
|
|
|
|
},
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
2024-10-04 09:34:44 +00:00
|
|
|
smtpColumns := make([]handler.Column, 0, 7)
|
2022-02-16 15:49:17 +00:00
|
|
|
if e.TLS != nil {
|
2024-10-04 09:34:44 +00:00
|
|
|
smtpColumns = append(smtpColumns, handler.NewCol(SMTPConfigSMTPColumnTLS, *e.TLS))
|
2022-02-16 15:49:17 +00:00
|
|
|
}
|
|
|
|
if e.FromAddress != nil {
|
2024-10-04 09:34:44 +00:00
|
|
|
smtpColumns = append(smtpColumns, handler.NewCol(SMTPConfigSMTPColumnSenderAddress, *e.FromAddress))
|
2022-02-16 15:49:17 +00:00
|
|
|
}
|
|
|
|
if e.FromName != nil {
|
2024-10-04 09:34:44 +00:00
|
|
|
smtpColumns = append(smtpColumns, handler.NewCol(SMTPConfigSMTPColumnSenderName, *e.FromName))
|
2022-02-16 15:49:17 +00:00
|
|
|
}
|
2023-08-29 07:08:24 +00:00
|
|
|
if e.ReplyToAddress != nil {
|
2024-10-04 09:34:44 +00:00
|
|
|
smtpColumns = append(smtpColumns, handler.NewCol(SMTPConfigSMTPColumnReplyToAddress, *e.ReplyToAddress))
|
2023-08-29 07:08:24 +00:00
|
|
|
}
|
2022-02-16 15:49:17 +00:00
|
|
|
if e.Host != nil {
|
2024-10-04 09:34:44 +00:00
|
|
|
smtpColumns = append(smtpColumns, handler.NewCol(SMTPConfigSMTPColumnHost, *e.Host))
|
2022-02-16 15:49:17 +00:00
|
|
|
}
|
|
|
|
if e.User != nil {
|
2024-10-04 09:34:44 +00:00
|
|
|
smtpColumns = append(smtpColumns, handler.NewCol(SMTPConfigSMTPColumnUser, *e.User))
|
2022-02-16 15:49:17 +00:00
|
|
|
}
|
2024-04-11 07:16:10 +00:00
|
|
|
if e.Password != nil {
|
2024-10-04 09:34:44 +00:00
|
|
|
smtpColumns = append(smtpColumns, handler.NewCol(SMTPConfigSMTPColumnPassword, *e.Password))
|
2024-04-11 07:16:10 +00:00
|
|
|
}
|
2024-10-04 09:34:44 +00:00
|
|
|
if len(smtpColumns) > 0 {
|
2024-09-12 04:27:29 +00:00
|
|
|
stmts = append(stmts, handler.AddUpdateStatement(
|
2024-10-04 09:34:44 +00:00
|
|
|
smtpColumns,
|
2024-09-12 04:27:29 +00:00
|
|
|
[]handler.Condition{
|
2024-10-04 09:34:44 +00:00
|
|
|
handler.NewCond(SMTPConfigSMTPColumnID, getSMTPConfigID(e.ID, e.Aggregate())),
|
2024-09-12 04:27:29 +00:00
|
|
|
handler.NewCond(SMTPConfigSMTPColumnInstanceID, e.Aggregate().InstanceID),
|
|
|
|
},
|
|
|
|
handler.WithTableSuffix(smtpConfigSMTPTableSuffix),
|
|
|
|
))
|
2024-04-11 07:16:10 +00:00
|
|
|
}
|
2024-09-12 04:27:29 +00:00
|
|
|
|
|
|
|
return handler.NewMultiStatement(e, stmts...), nil
|
2022-02-16 15:49:17 +00:00
|
|
|
}
|
|
|
|
|
2022-06-14 05:51:00 +00:00
|
|
|
func (p *smtpConfigProjection) reduceSMTPConfigPasswordChanged(event eventstore.Event) (*handler.Statement, error) {
|
2024-09-12 04:27:29 +00:00
|
|
|
e, err := assertEvent[*instance.SMTPConfigPasswordChangedEvent](event)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2022-02-16 15:49:17 +00:00
|
|
|
}
|
|
|
|
|
2024-09-12 04:27:29 +00:00
|
|
|
return handler.NewMultiStatement(
|
2022-02-16 15:49:17 +00:00
|
|
|
e,
|
2024-09-12 04:27:29 +00:00
|
|
|
handler.AddUpdateStatement(
|
|
|
|
[]handler.Column{
|
|
|
|
handler.NewCol(SMTPConfigSMTPColumnPassword, e.Password),
|
|
|
|
},
|
|
|
|
[]handler.Condition{
|
2024-10-04 09:34:44 +00:00
|
|
|
handler.NewCond(SMTPConfigSMTPColumnID, getSMTPConfigID(e.ID, e.Aggregate())),
|
|
|
|
handler.NewCond(SMTPConfigSMTPColumnInstanceID, e.Aggregate().InstanceID),
|
2024-09-12 04:27:29 +00:00
|
|
|
},
|
|
|
|
handler.WithTableSuffix(smtpConfigSMTPTableSuffix),
|
|
|
|
),
|
|
|
|
handler.AddUpdateStatement(
|
|
|
|
[]handler.Column{
|
|
|
|
handler.NewCol(SMTPConfigColumnChangeDate, e.CreationDate()),
|
|
|
|
handler.NewCol(SMTPConfigColumnSequence, e.Sequence()),
|
|
|
|
},
|
|
|
|
[]handler.Condition{
|
2024-10-04 09:34:44 +00:00
|
|
|
handler.NewCond(SMTPConfigColumnID, getSMTPConfigID(e.ID, e.Aggregate())),
|
2024-09-12 04:27:29 +00:00
|
|
|
handler.NewCond(SMTPConfigColumnInstanceID, e.Aggregate().InstanceID),
|
|
|
|
},
|
|
|
|
),
|
2024-04-11 07:16:10 +00:00
|
|
|
), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *smtpConfigProjection) reduceSMTPConfigActivated(event eventstore.Event) (*handler.Statement, error) {
|
2024-09-12 04:27:29 +00:00
|
|
|
e, err := assertEvent[*instance.SMTPConfigActivatedEvent](event)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2024-04-11 07:16:10 +00:00
|
|
|
}
|
|
|
|
|
2024-09-12 04:27:29 +00:00
|
|
|
return handler.NewMultiStatement(
|
2024-04-11 07:16:10 +00:00
|
|
|
e,
|
2024-09-12 04:27:29 +00:00
|
|
|
handler.AddUpdateStatement(
|
|
|
|
[]handler.Column{
|
|
|
|
handler.NewCol(SMTPConfigColumnChangeDate, e.CreationDate()),
|
|
|
|
handler.NewCol(SMTPConfigColumnSequence, e.Sequence()),
|
|
|
|
handler.NewCol(SMTPConfigColumnState, domain.SMTPConfigStateInactive),
|
|
|
|
},
|
|
|
|
[]handler.Condition{
|
2024-10-04 09:34:44 +00:00
|
|
|
handler.Not(handler.NewCond(SMTPConfigColumnID, getSMTPConfigID(e.ID, e.Aggregate()))),
|
2024-09-12 04:27:29 +00:00
|
|
|
handler.NewCond(SMTPConfigColumnState, domain.SMTPConfigStateActive),
|
|
|
|
handler.NewCond(SMTPConfigColumnInstanceID, e.Aggregate().InstanceID),
|
|
|
|
},
|
|
|
|
),
|
|
|
|
handler.AddUpdateStatement(
|
|
|
|
[]handler.Column{
|
|
|
|
handler.NewCol(SMTPConfigColumnChangeDate, e.CreationDate()),
|
|
|
|
handler.NewCol(SMTPConfigColumnSequence, e.Sequence()),
|
|
|
|
handler.NewCol(SMTPConfigColumnState, domain.SMTPConfigStateActive),
|
|
|
|
},
|
|
|
|
[]handler.Condition{
|
2024-10-04 09:34:44 +00:00
|
|
|
handler.NewCond(SMTPConfigColumnID, getSMTPConfigID(e.ID, e.Aggregate())),
|
2024-09-12 04:27:29 +00:00
|
|
|
handler.NewCond(SMTPConfigColumnInstanceID, e.Aggregate().InstanceID),
|
|
|
|
},
|
|
|
|
),
|
2024-04-11 07:16:10 +00:00
|
|
|
), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *smtpConfigProjection) reduceSMTPConfigDeactivated(event eventstore.Event) (*handler.Statement, error) {
|
2024-09-12 04:27:29 +00:00
|
|
|
e, err := assertEvent[*instance.SMTPConfigDeactivatedEvent](event)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2024-04-11 07:16:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return handler.NewUpdateStatement(
|
|
|
|
e,
|
|
|
|
[]handler.Column{
|
|
|
|
handler.NewCol(SMTPConfigColumnChangeDate, e.CreationDate()),
|
|
|
|
handler.NewCol(SMTPConfigColumnSequence, e.Sequence()),
|
|
|
|
handler.NewCol(SMTPConfigColumnState, domain.SMTPConfigStateInactive),
|
|
|
|
},
|
|
|
|
[]handler.Condition{
|
2024-10-04 09:34:44 +00:00
|
|
|
handler.NewCond(SMTPConfigColumnID, getSMTPConfigID(e.ID, e.Aggregate())),
|
2022-05-25 12:15:13 +00:00
|
|
|
handler.NewCond(SMTPConfigColumnInstanceID, e.Aggregate().InstanceID),
|
2022-02-16 15:49:17 +00:00
|
|
|
},
|
|
|
|
), nil
|
|
|
|
}
|
2023-08-18 12:22:57 +00:00
|
|
|
|
|
|
|
func (p *smtpConfigProjection) reduceSMTPConfigRemoved(event eventstore.Event) (*handler.Statement, error) {
|
|
|
|
e, err := assertEvent[*instance.SMTPConfigRemovedEvent](event)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-04-11 07:16:10 +00:00
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
return handler.NewDeleteStatement(
|
2023-08-18 12:22:57 +00:00
|
|
|
e,
|
|
|
|
[]handler.Condition{
|
2024-10-04 09:34:44 +00:00
|
|
|
handler.NewCond(SMTPConfigColumnID, getSMTPConfigID(e.ID, e.Aggregate())),
|
2023-08-18 12:22:57 +00:00
|
|
|
handler.NewCond(SMTPConfigColumnInstanceID, e.Aggregate().InstanceID),
|
|
|
|
},
|
|
|
|
), nil
|
|
|
|
}
|
2024-10-04 09:34:44 +00:00
|
|
|
|
|
|
|
func getSMTPConfigID(id string, aggregate *eventstore.Aggregate) string {
|
|
|
|
if id != "" {
|
|
|
|
return id
|
|
|
|
}
|
|
|
|
// Deal with old and unique SMTP settings (empty ID)
|
|
|
|
return aggregate.ResourceOwner
|
|
|
|
}
|