Add task node query support

Signed-off-by: gearnode <bryan@frimin.fr>
This commit is contained in:
gearnode
2025-01-31 08:53:01 -08:00
parent ddfd3928ac
commit cd6a388672
3 changed files with 102 additions and 0 deletions

View File

@@ -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:
}

View File

@@ -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,

View File

@@ -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,