Add intial state when create control
Signed-off-by: gearnode <bryan@frimin.fr>
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -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
|
||||
},
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user