Add control node query support
Signed-off-by: gearnode <bryan@frimin.fr>
This commit is contained in:
@@ -131,6 +131,13 @@ func (r *queryResolver) Node(ctx context.Context, id gid.GID) (types.Node, error
|
|||||||
}
|
}
|
||||||
|
|
||||||
return types.NewFramework(framework), nil
|
return types.NewFramework(framework), nil
|
||||||
|
case coredata.ControlEntityType:
|
||||||
|
control, err := r.svc.GetControl(ctx, id)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return types.NewControl(control), nil
|
||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -60,6 +60,63 @@ func (c *Control) scan(r pgx.Row) error {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (v *Control) LoadByID(
|
||||||
|
ctx context.Context,
|
||||||
|
conn pg.Conn,
|
||||||
|
scope *Scope,
|
||||||
|
controlID gid.GID,
|
||||||
|
) error {
|
||||||
|
q := `
|
||||||
|
WITH control_states AS (
|
||||||
|
SELECT
|
||||||
|
control_id,
|
||||||
|
to_state,
|
||||||
|
reason,
|
||||||
|
RANK() OVER w
|
||||||
|
FROM
|
||||||
|
control_state_transitions
|
||||||
|
WHERE
|
||||||
|
control_id = @control_id
|
||||||
|
WINDOW
|
||||||
|
w AS (PARTITION BY control_id ORDER BY created_at DESC)
|
||||||
|
)
|
||||||
|
SELECT
|
||||||
|
id,
|
||||||
|
framework_id,
|
||||||
|
name,
|
||||||
|
description,
|
||||||
|
cs.to_state AS state,
|
||||||
|
content_ref,
|
||||||
|
created_at,
|
||||||
|
updated_at
|
||||||
|
FROM
|
||||||
|
controls
|
||||||
|
INNER JOIN
|
||||||
|
control_states cs ON cs.control_id = controls.id
|
||||||
|
WHERE
|
||||||
|
%s
|
||||||
|
AND id = @control_id
|
||||||
|
AND cs.rank = 1
|
||||||
|
LIMIT 1;
|
||||||
|
`
|
||||||
|
|
||||||
|
q = fmt.Sprintf(q, scope.SQLFragment())
|
||||||
|
|
||||||
|
args := pgx.NamedArgs{"control_id": controlID}
|
||||||
|
maps.Copy(args, scope.SQLArguments())
|
||||||
|
|
||||||
|
r := conn.QueryRow(ctx, q, args)
|
||||||
|
|
||||||
|
c2 := Control{}
|
||||||
|
if err := c2.scan(r); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
*v = c2
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (c *Controls) LoadByFrameworkID(
|
func (c *Controls) LoadByFrameworkID(
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
conn pg.Conn,
|
conn pg.Conn,
|
||||||
|
|||||||
@@ -129,6 +129,26 @@ func (s Service) GetFramework(
|
|||||||
return framework, nil
|
return framework, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s Service) GetControl(
|
||||||
|
ctx context.Context,
|
||||||
|
controlID gid.GID,
|
||||||
|
) (*coredata.Control, error) {
|
||||||
|
control := &coredata.Control{}
|
||||||
|
|
||||||
|
err := s.pg.WithConn(
|
||||||
|
ctx,
|
||||||
|
func(conn pg.Conn) error {
|
||||||
|
return control.LoadByID(ctx, conn, s.scope, controlID)
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return control, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (s Service) ListOrganizationFrameworks(
|
func (s Service) ListOrganizationFrameworks(
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
organizationID gid.GID,
|
organizationID gid.GID,
|
||||||
|
|||||||
Reference in New Issue
Block a user