Add control state transitions graphql resolver
Signed-off-by: gearnode <bryan@frimin.fr>
This commit is contained in:
@@ -14,10 +14,10 @@ interface Node {
|
||||
}
|
||||
|
||||
enum ControlState {
|
||||
NotStarted
|
||||
InProgress
|
||||
NotApplicable
|
||||
Implemented
|
||||
NOT_STARTED
|
||||
IN_PROGRESS
|
||||
NOT_APPLICABLE
|
||||
IMPLEMENTED
|
||||
}
|
||||
|
||||
|
||||
@@ -85,6 +85,13 @@ type Control implements Node {
|
||||
description: String!
|
||||
state: ControlState!
|
||||
|
||||
stateTransisions(
|
||||
first: Int
|
||||
after: CursorKey
|
||||
last: Int
|
||||
before: CursorKey
|
||||
): ControlStateTransitionConnection! @goField(forceResolver: true)
|
||||
|
||||
tasks(
|
||||
first: Int
|
||||
after: CursorKey
|
||||
@@ -96,6 +103,25 @@ type Control implements Node {
|
||||
updatedAt: Datetime!
|
||||
}
|
||||
|
||||
type ControlStateTransitionConnection {
|
||||
edges: [ControlStateTransitionEdge!]!
|
||||
pageInfo: PageInfo!
|
||||
}
|
||||
|
||||
type ControlStateTransitionEdge {
|
||||
cursor: CursorKey!
|
||||
node: ControlStateTransition!
|
||||
}
|
||||
|
||||
type ControlStateTransition {
|
||||
id: ID!
|
||||
fromState: ControlState
|
||||
toState: ControlState!
|
||||
reason: String
|
||||
createdAt: Datetime!
|
||||
updatedAt: Datetime!
|
||||
}
|
||||
|
||||
type TaskConnection {
|
||||
edges: [TaskEdge!]!
|
||||
pageInfo: PageInfo!
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
59
pkg/api/console/v1/types/control_state_transition.go
Normal file
59
pkg/api/console/v1/types/control_state_transition.go
Normal file
@@ -0,0 +1,59 @@
|
||||
// Copyright (c) 2025 Probo Inc <hello@getprobo.com>.
|
||||
//
|
||||
// Permission to use, copy, modify, and/or distribute this software for any
|
||||
// purpose with or without fee is hereby granted, provided that the above
|
||||
// copyright notice and this permission notice appear in all copies.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
||||
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
||||
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
||||
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
||||
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
||||
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
||||
// PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
package types
|
||||
|
||||
import (
|
||||
"github.com/getprobo/probo/pkg/probo/coredata"
|
||||
"github.com/getprobo/probo/pkg/probo/coredata/page"
|
||||
)
|
||||
|
||||
func NewControlStateTransitionConnection(
|
||||
p *page.Page[*coredata.ControlStateTransition],
|
||||
) *ControlStateTransitionConnection {
|
||||
var edges = make([]*ControlStateTransitionEdge, len(p.Data))
|
||||
|
||||
for i := range edges {
|
||||
edges[i] = NewControlStateTransitionEdge(p.Data[i])
|
||||
}
|
||||
|
||||
return &ControlStateTransitionConnection{
|
||||
Edges: edges,
|
||||
PageInfo: NewPageInfo(p),
|
||||
}
|
||||
}
|
||||
|
||||
func NewControlStateTransitionEdge(cst *coredata.ControlStateTransition) *ControlStateTransitionEdge {
|
||||
return &ControlStateTransitionEdge{
|
||||
Cursor: cst.CursorKey(),
|
||||
Node: NewControlStateTransition(cst),
|
||||
}
|
||||
}
|
||||
|
||||
func NewControlStateTransition(cst *coredata.ControlStateTransition) *ControlStateTransition {
|
||||
var fromState *ControlState
|
||||
if cst.FromState != nil {
|
||||
val := ControlState((*cst.FromState).String())
|
||||
fromState = &val
|
||||
}
|
||||
|
||||
return &ControlStateTransition{
|
||||
ID: cst.ID,
|
||||
FromState: fromState,
|
||||
ToState: ControlState(cst.ToState.String()),
|
||||
Reason: cst.Reason,
|
||||
CreatedAt: cst.CreatedAt,
|
||||
UpdatedAt: cst.UpdatedAt,
|
||||
}
|
||||
}
|
||||
@@ -18,13 +18,14 @@ type Node interface {
|
||||
}
|
||||
|
||||
type Control struct {
|
||||
ID gid.GID `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
State ControlState `json:"state"`
|
||||
Tasks *TaskConnection `json:"tasks"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
UpdatedAt time.Time `json:"updatedAt"`
|
||||
ID gid.GID `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
State ControlState `json:"state"`
|
||||
StateTransisions *ControlStateTransitionConnection `json:"stateTransisions"`
|
||||
Tasks *TaskConnection `json:"tasks"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
UpdatedAt time.Time `json:"updatedAt"`
|
||||
}
|
||||
|
||||
func (Control) IsNode() {}
|
||||
@@ -40,6 +41,25 @@ type ControlEdge struct {
|
||||
Node *Control `json:"node"`
|
||||
}
|
||||
|
||||
type ControlStateTransition struct {
|
||||
ID gid.GID `json:"id"`
|
||||
FromState *ControlState `json:"fromState,omitempty"`
|
||||
ToState ControlState `json:"toState"`
|
||||
Reason *string `json:"reason,omitempty"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
UpdatedAt time.Time `json:"updatedAt"`
|
||||
}
|
||||
|
||||
type ControlStateTransitionConnection struct {
|
||||
Edges []*ControlStateTransitionEdge `json:"edges"`
|
||||
PageInfo *PageInfo `json:"pageInfo"`
|
||||
}
|
||||
|
||||
type ControlStateTransitionEdge struct {
|
||||
Cursor page.CursorKey `json:"cursor"`
|
||||
Node *ControlStateTransition `json:"node"`
|
||||
}
|
||||
|
||||
type Framework struct {
|
||||
ID gid.GID `json:"id"`
|
||||
Name string `json:"name"`
|
||||
@@ -106,10 +126,10 @@ type TaskEdge struct {
|
||||
type ControlState string
|
||||
|
||||
const (
|
||||
ControlStateNotStarted ControlState = "NotStarted"
|
||||
ControlStateInProgress ControlState = "InProgress"
|
||||
ControlStateNotApplicable ControlState = "NotApplicable"
|
||||
ControlStateImplemented ControlState = "Implemented"
|
||||
ControlStateNotStarted ControlState = "NOT_STARTED"
|
||||
ControlStateInProgress ControlState = "IN_PROGRESS"
|
||||
ControlStateNotApplicable ControlState = "NOT_APPLICABLE"
|
||||
ControlStateImplemented ControlState = "IMPLEMENTED"
|
||||
)
|
||||
|
||||
var AllControlState = []ControlState{
|
||||
|
||||
@@ -16,6 +16,18 @@ import (
|
||||
"github.com/vektah/gqlparser/v2/gqlerror"
|
||||
)
|
||||
|
||||
// StateTransisions is the resolver for the stateTransisions field.
|
||||
func (r *controlResolver) StateTransisions(ctx context.Context, obj *types.Control, first *int, after *page.CursorKey, last *int, before *page.CursorKey) (*types.ControlStateTransitionConnection, error) {
|
||||
cursor := types.NewCursor(first, after, last, before)
|
||||
|
||||
page, err := r.svc.ListControlStateTransitions(ctx, obj.ID, cursor)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot list control tasks: %w", err)
|
||||
}
|
||||
|
||||
return types.NewControlStateTransitionConnection(page), nil
|
||||
}
|
||||
|
||||
// Tasks is the resolver for the tasks field.
|
||||
func (r *controlResolver) Tasks(ctx context.Context, obj *types.Control, first *int, after *page.CursorKey, last *int, before *page.CursorKey) (*types.TaskConnection, error) {
|
||||
cursor := types.NewCursor(first, after, last, before)
|
||||
|
||||
@@ -17,8 +17,6 @@ package coredata
|
||||
import (
|
||||
"database/sql/driver"
|
||||
"fmt"
|
||||
|
||||
"go.gearno.de/x/panicf"
|
||||
)
|
||||
|
||||
type (
|
||||
@@ -67,14 +65,12 @@ func (cs ControlState) String() string {
|
||||
val = "NOT_APPLICABLE"
|
||||
case ControlStateImplemented:
|
||||
val = "IMPLEMENTED"
|
||||
default:
|
||||
panicf.Panic("invalid control state value: %v", cs)
|
||||
}
|
||||
|
||||
return val
|
||||
}
|
||||
|
||||
func (cs ControlState) Scan(value any) error {
|
||||
func (cs *ControlState) Scan(value any) error {
|
||||
val, ok := value.(string)
|
||||
if !ok {
|
||||
return fmt.Errorf("invalid scan source for ControlState, expected string got %T", value)
|
||||
|
||||
112
pkg/probo/coredata/control_state_transition.go
Normal file
112
pkg/probo/coredata/control_state_transition.go
Normal file
@@ -0,0 +1,112 @@
|
||||
// Copyright (c) 2025 Probo Inc <hello@getprobo.com>.
|
||||
//
|
||||
// Permission to use, copy, modify, and/or distribute this software for any
|
||||
// purpose with or without fee is hereby granted, provided that the above
|
||||
// copyright notice and this permission notice appear in all copies.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
||||
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
||||
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
||||
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
||||
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
||||
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
||||
// PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
package coredata
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"maps"
|
||||
"time"
|
||||
|
||||
"github.com/getprobo/probo/pkg/probo/coredata/gid"
|
||||
"github.com/getprobo/probo/pkg/probo/coredata/page"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"go.gearno.de/crypto/uuid"
|
||||
"go.gearno.de/kit/pg"
|
||||
)
|
||||
|
||||
type (
|
||||
ControlStateTransition struct {
|
||||
ID gid.GID
|
||||
ControlID gid.GID
|
||||
FromState *ControlState
|
||||
ToState ControlState
|
||||
Reason *string
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
}
|
||||
|
||||
ControlStateTransitions []*ControlStateTransition
|
||||
)
|
||||
|
||||
func (cst ControlStateTransition) CursorKey() page.CursorKey {
|
||||
return page.NewCursorKey(uuid.UUID(cst.ID), cst.CreatedAt)
|
||||
}
|
||||
|
||||
func (cst *ControlStateTransition) scan(r pgx.Row) error {
|
||||
return r.Scan(
|
||||
&cst.ID,
|
||||
&cst.ControlID,
|
||||
&cst.FromState,
|
||||
&cst.ToState,
|
||||
&cst.Reason,
|
||||
&cst.CreatedAt,
|
||||
&cst.UpdatedAt,
|
||||
)
|
||||
}
|
||||
|
||||
func (cst *ControlStateTransitions) LoadByControlID(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
scope *Scope,
|
||||
controlID gid.GID,
|
||||
cursor *page.Cursor,
|
||||
) error {
|
||||
q := `
|
||||
SELECT
|
||||
id,
|
||||
control_id,
|
||||
from_state,
|
||||
to_state,
|
||||
reason,
|
||||
created_at,
|
||||
updated_at
|
||||
FROM
|
||||
control_state_transitions
|
||||
WHERE
|
||||
%s
|
||||
AND control_id = @control_id
|
||||
AND %s
|
||||
`
|
||||
|
||||
q = fmt.Sprintf(q, scope.SQLFragment(), cursor.SQLFragment())
|
||||
|
||||
args := pgx.NamedArgs{"control_id": controlID}
|
||||
maps.Copy(args, scope.SQLArguments())
|
||||
|
||||
r, err := conn.Query(ctx, q, args)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer r.Close()
|
||||
|
||||
controlStateTransitions := ControlStateTransitions{}
|
||||
for r.Next() {
|
||||
controlStateTransition := &ControlStateTransition{}
|
||||
if err := controlStateTransition.scan(r); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
controlStateTransitions = append(controlStateTransitions, controlStateTransition)
|
||||
}
|
||||
|
||||
if err := r.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*cst = controlStateTransitions
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -20,4 +20,5 @@ const (
|
||||
ControlEntityType
|
||||
TaskEntityType
|
||||
EvidenceEntityType
|
||||
ControlStateTransitionEntityType
|
||||
)
|
||||
|
||||
@@ -149,3 +149,31 @@ func (s *Service) ListControlTasks(
|
||||
|
||||
return page.NewPage(tasks, cursor), nil
|
||||
}
|
||||
|
||||
func (s *Service) ListControlStateTransitions(
|
||||
ctx context.Context,
|
||||
controlID gid.GID,
|
||||
cursor *page.Cursor,
|
||||
) (*page.Page[*coredata.ControlStateTransition], error) {
|
||||
var controlStateTransitions coredata.ControlStateTransitions
|
||||
|
||||
err := s.pg.WithConn(
|
||||
ctx,
|
||||
func(conn pg.Conn) error {
|
||||
return controlStateTransitions.LoadByControlID(
|
||||
ctx,
|
||||
conn,
|
||||
s.scope,
|
||||
controlID,
|
||||
cursor,
|
||||
)
|
||||
},
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return page.NewPage(controlStateTransitions, cursor), nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user