From cd6a388672d97983dd16b2bbbba756e372bba4f4 Mon Sep 17 00:00:00 2001 From: gearnode Date: Fri, 31 Jan 2025 08:53:01 -0800 Subject: [PATCH] Add task node query support Signed-off-by: gearnode --- pkg/api/console/v1/v1_resolver.go | 7 +++ pkg/probo/coredata/task.go | 75 +++++++++++++++++++++++++++++++ pkg/probo/probo.go | 20 +++++++++ 3 files changed, 102 insertions(+) diff --git a/pkg/api/console/v1/v1_resolver.go b/pkg/api/console/v1/v1_resolver.go index 00d801c65..5d4bd6058 100644 --- a/pkg/api/console/v1/v1_resolver.go +++ b/pkg/api/console/v1/v1_resolver.go @@ -138,6 +138,13 @@ func (r *queryResolver) Node(ctx context.Context, id gid.GID) (types.Node, error } return types.NewControl(control), nil + case coredata.TaskEntityType: + task, err := r.svc.GetTask(ctx, id) + if err != nil { + return nil, err + } + + return types.NewTask(task), nil default: } diff --git a/pkg/probo/coredata/task.go b/pkg/probo/coredata/task.go index 2d1357811..ee011d882 100644 --- a/pkg/probo/coredata/task.go +++ b/pkg/probo/coredata/task.go @@ -60,6 +60,81 @@ func (t *Task) scan(r pgx.Row) error { ) } +func (t *Task) LoadByID( + ctx context.Context, + conn pg.Conn, + scope *Scope, + taskID gid.GID, +) error { + q := ` +WITH + control_tasks AS ( + SELECT + t.id, + ct.control_id AS control_id, + t.name, + t.description, + t.content_ref, + t.created_at, + t.updated_at + FROM + tasks t + INNER JOIN + controls_tasks ct ON + ct.task_id = t.id + WHERE + %s + AND id = @task_id + ), + task_states AS ( + SELECT + task_id, + to_state AS state, + reason, + RANK() OVER w + FROM + task_state_transitions + WHERE + task_id = @task_id + WINDOW + w AS (PARTITION BY task_id ORDER BY created_at DESC) + ) +SELECT + id, + control_id, + name, + description, + ts.state AS state, + content_ref, + created_at, + updated_at +FROM + control_tasks +INNER JOIN + task_states ts ON ts.task_id = control_tasks.id +WHERE + ts.rank = 1 + AND id = @task_id +LIMIT 1; +` + + q = fmt.Sprintf(q, scope.SQLFragment()) + + args := pgx.NamedArgs{"task_id": taskID} + maps.Copy(args, scope.SQLArguments()) + + r := conn.QueryRow(ctx, q, args) + + t2 := Task{} + if err := t2.scan(r); err != nil { + return err + } + + *t = t2 + + return nil +} + func (t *Tasks) LoadByControlID( ctx context.Context, conn pg.Conn, diff --git a/pkg/probo/probo.go b/pkg/probo/probo.go index 175c7ecd2..3ab562c09 100644 --- a/pkg/probo/probo.go +++ b/pkg/probo/probo.go @@ -149,6 +149,26 @@ func (s Service) GetControl( return control, nil } +func (s Service) GetTask( + ctx context.Context, + taskID gid.GID, +) (*coredata.Task, error) { + task := &coredata.Task{} + + err := s.pg.WithConn( + ctx, + func(conn pg.Conn) error { + return task.LoadByID(ctx, conn, s.scope, taskID) + }, + ) + + if err != nil { + return nil, err + } + + return task, nil +} + func (s Service) ListOrganizationFrameworks( ctx context.Context, organizationID gid.GID,