From ace78da341b489506cea01158e4a099483a7c8ad Mon Sep 17 00:00:00 2001 From: gearnode Date: Wed, 5 Feb 2025 10:40:29 -0800 Subject: [PATCH] Add intial state when create control Signed-off-by: gearnode --- .../coredata/control_state_transition.go | 39 +++++++++++++++++++ pkg/probo/create_control.go | 23 ++++++++++- 2 files changed, 61 insertions(+), 1 deletion(-) diff --git a/pkg/probo/coredata/control_state_transition.go b/pkg/probo/coredata/control_state_transition.go index 2ba54b019..d4f0263fa 100644 --- a/pkg/probo/coredata/control_state_transition.go +++ b/pkg/probo/coredata/control_state_transition.go @@ -52,6 +52,45 @@ func (cst *ControlStateTransition) scan(r pgx.Row) error { ) } +func (cst ControlStateTransition) Insert( + ctx context.Context, + conn pg.Conn, +) error { + q := ` +INSERT INTO + control_state_transitions ( + id, + control_id, + from_state + to_state, + reason, + created_at, + updated_at + ) +VALUES ( + @control_state_transition_id, + @control_id, + @from_state, + @to_state, + @reason, + @created_at, + @updated_at +); +` + + args := pgx.NamedArgs{ + "control_state_transition_id": cst.ID, + "control_id": cst.ControlID, + "from_state": cst.FromState, + "to_state": cst.ToState, + "reason": cst.Reason, + "created_at": cst.CreatedAt, + "updated_at": cst.UpdatedAt, + } + _, err := conn.Exec(ctx, q, args) + return err +} + func (cst *ControlStateTransitions) LoadByControlID( ctx context.Context, conn pg.Conn, diff --git a/pkg/probo/create_control.go b/pkg/probo/create_control.go index bc93ae233..d4b1406d7 100644 --- a/pkg/probo/create_control.go +++ b/pkg/probo/create_control.go @@ -19,6 +19,7 @@ import ( "fmt" "time" + "gearno.de/ref" "github.com/getprobo/probo/pkg/gid" "github.com/getprobo/probo/pkg/probo/coredata" "go.gearno.de/kit/pg" @@ -40,7 +41,11 @@ func (s Service) CreateControl( now := time.Now() controlID, err := gid.NewGID(coredata.ControlEntityType) if err != nil { - return nil, fmt.Errorf("cannot create global id: %w", err) + return nil, fmt.Errorf("cannot create control global id: %w", err) + } + controlStateTransitionID, err := gid.NewGID(coredata.ControlStateTransitionEntityType) + if err != nil { + return nil, fmt.Errorf("cannot create control state transition global id: %w", err) } framework := &coredata.Framework{} @@ -54,6 +59,18 @@ func (s Service) CreateControl( UpdatedAt: now, } + controlStateTransition := coredata.ControlStateTransition{ + StateTransition: coredata.StateTransition[coredata.ControlState]{ + ID: controlStateTransitionID, + FromState: nil, + ToState: coredata.ControlStateNotStarted, + Reason: ref.Ref("Initial state"), + CreatedAt: now, + UpdatedAt: now, + }, + ControlID: control.ID, + } + err = s.pg.WithTx( ctx, func(conn pg.Conn) error { @@ -65,6 +82,10 @@ func (s Service) CreateControl( return fmt.Errorf("cannot insert control: %w", err) } + if err := controlStateTransition.Insert(ctx, conn); err != nil { + return fmt.Errorf("cannot insert control state transition: %w", err) + } + return nil }, )