2023-07-06 06:38:13 +00:00
|
|
|
package projection
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"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"
|
2023-07-06 06:38:13 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/repository/milestone"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2024-10-28 08:29:34 +00:00
|
|
|
MilestonesProjectionTable = "projections.milestones2"
|
2023-07-06 06:38:13 +00:00
|
|
|
|
2024-10-28 08:29:34 +00:00
|
|
|
MilestoneColumnInstanceID = "instance_id"
|
|
|
|
MilestoneColumnType = "type"
|
|
|
|
MilestoneColumnReachedDate = "reached_date"
|
|
|
|
MilestoneColumnPushedDate = "last_pushed_date"
|
2023-07-06 06:38:13 +00:00
|
|
|
)
|
|
|
|
|
2024-10-28 08:29:34 +00:00
|
|
|
type milestoneProjection struct{}
|
2023-07-06 06:38:13 +00:00
|
|
|
|
2024-10-28 08:29:34 +00:00
|
|
|
func newMilestoneProjection(ctx context.Context, config handler.Config) *handler.Handler {
|
|
|
|
return handler.NewHandler(ctx, &config, &milestoneProjection{})
|
2023-10-19 10:19:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (*milestoneProjection) Name() string {
|
|
|
|
return MilestonesProjectionTable
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*milestoneProjection) Init() *old_handler.Check {
|
|
|
|
return handler.NewMultiTableCheck(
|
|
|
|
handler.NewTable([]*handler.InitColumn{
|
|
|
|
handler.NewColumn(MilestoneColumnInstanceID, handler.ColumnTypeText),
|
|
|
|
handler.NewColumn(MilestoneColumnType, handler.ColumnTypeEnum),
|
|
|
|
handler.NewColumn(MilestoneColumnReachedDate, handler.ColumnTypeTimestamp, handler.Nullable()),
|
|
|
|
handler.NewColumn(MilestoneColumnPushedDate, handler.ColumnTypeTimestamp, handler.Nullable()),
|
2023-07-06 06:38:13 +00:00
|
|
|
},
|
2023-10-19 10:19:10 +00:00
|
|
|
handler.NewPrimaryKey(MilestoneColumnInstanceID, MilestoneColumnType),
|
2023-07-06 06:38:13 +00:00
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
// Reducers implements handler.Projection.
|
|
|
|
func (p *milestoneProjection) Reducers() []handler.AggregateReducer {
|
2023-07-06 06:38:13 +00:00
|
|
|
return []handler.AggregateReducer{
|
|
|
|
{
|
2024-10-28 08:29:34 +00:00
|
|
|
Aggregate: milestone.AggregateType,
|
2024-06-12 04:49:14 +00:00
|
|
|
EventReducers: []handler.EventReducer{
|
|
|
|
{
|
2024-10-28 08:29:34 +00:00
|
|
|
Event: milestone.ReachedEventType,
|
|
|
|
Reduce: p.reduceReached,
|
2024-06-12 04:49:14 +00:00
|
|
|
},
|
2023-07-06 06:38:13 +00:00
|
|
|
{
|
|
|
|
Event: milestone.PushedEventType,
|
2024-10-28 08:29:34 +00:00
|
|
|
Reduce: p.reducePushed,
|
2023-07-06 06:38:13 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-28 08:29:34 +00:00
|
|
|
func (p *milestoneProjection) reduceReached(event eventstore.Event) (*handler.Statement, error) {
|
|
|
|
e, err := assertEvent[*milestone.ReachedEvent](event)
|
2023-07-06 06:38:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-10-28 08:29:34 +00:00
|
|
|
return handler.NewCreateStatement(event, []handler.Column{
|
|
|
|
handler.NewCol(MilestoneColumnInstanceID, e.Agg.InstanceID),
|
|
|
|
handler.NewCol(MilestoneColumnType, e.MilestoneType),
|
|
|
|
handler.NewCol(MilestoneColumnReachedDate, e.GetReachedDate()),
|
|
|
|
}), nil
|
2023-07-06 06:38:13 +00:00
|
|
|
}
|
|
|
|
|
2024-10-28 08:29:34 +00:00
|
|
|
func (p *milestoneProjection) reducePushed(event eventstore.Event) (*handler.Statement, error) {
|
2023-07-06 06:38:13 +00:00
|
|
|
e, err := assertEvent[*milestone.PushedEvent](event)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-10-28 08:29:34 +00:00
|
|
|
if e.Agg.Version != milestone.AggregateVersion {
|
|
|
|
return handler.NewNoOpStatement(event), nil // Skip v1 events.
|
|
|
|
}
|
2023-07-06 06:38:13 +00:00
|
|
|
if e.MilestoneType != milestone.InstanceDeleted {
|
2023-10-19 10:19:10 +00:00
|
|
|
return handler.NewUpdateStatement(
|
2023-07-06 06:38:13 +00:00
|
|
|
event,
|
|
|
|
[]handler.Column{
|
2024-10-28 08:29:34 +00:00
|
|
|
handler.NewCol(MilestoneColumnPushedDate, e.GetPushedDate()),
|
2023-07-06 06:38:13 +00:00
|
|
|
},
|
|
|
|
[]handler.Condition{
|
|
|
|
handler.NewCond(MilestoneColumnInstanceID, event.Aggregate().InstanceID),
|
|
|
|
handler.NewCond(MilestoneColumnType, e.MilestoneType),
|
|
|
|
},
|
|
|
|
), nil
|
|
|
|
}
|
2023-10-19 10:19:10 +00:00
|
|
|
return handler.NewDeleteStatement(
|
2023-07-06 06:38:13 +00:00
|
|
|
event,
|
|
|
|
[]handler.Condition{
|
|
|
|
handler.NewCond(MilestoneColumnInstanceID, event.Aggregate().InstanceID),
|
|
|
|
},
|
|
|
|
), nil
|
|
|
|
}
|