Add contols audits

Signed-off-by: Sacha Al Himdani <sacha@getprobo.com>
This commit is contained in:
Sacha Al Himdani
2025-08-14 13:42:47 +02:00
parent 215e97d9f5
commit 55d488edac
22 changed files with 3762 additions and 21 deletions

View File

@@ -314,3 +314,109 @@ WHERE
return nil
}
func (a *Audits) CountByControlID(
ctx context.Context,
conn pg.Conn,
scope Scoper,
controlID gid.GID,
) (int, error) {
q := `
WITH audits_by_control AS (
SELECT
a.id,
a.tenant_id
FROM
audits a
INNER JOIN
controls_audits ca ON a.id = ca.audit_id
WHERE
ca.control_id = @control_id
)
SELECT
COUNT(id)
FROM
audits_by_control
WHERE %s
`
q = fmt.Sprintf(q, scope.SQLFragment())
args := pgx.StrictNamedArgs{"control_id": controlID}
maps.Copy(args, scope.SQLArguments())
row := conn.QueryRow(ctx, q, args)
var count int
if err := row.Scan(&count); err != nil {
return 0, fmt.Errorf("cannot scan count: %w", err)
}
return count, nil
}
func (a *Audits) LoadByControlID(
ctx context.Context,
conn pg.Conn,
scope Scoper,
controlID gid.GID,
cursor *page.Cursor[AuditOrderField],
) error {
q := `
WITH audits_by_control AS (
SELECT
a.id,
a.tenant_id,
a.name,
a.organization_id,
a.framework_id,
a.report_id,
a.valid_from,
a.valid_until,
a.state,
a.show_on_trust_center,
a.created_at,
a.updated_at
FROM
audits a
INNER JOIN
controls_audits ca ON a.id = ca.audit_id
WHERE
ca.control_id = @control_id
)
SELECT
id,
name,
organization_id,
framework_id,
report_id,
valid_from,
valid_until,
state,
show_on_trust_center,
created_at,
updated_at
FROM
audits_by_control
WHERE %s
AND %s
`
q = fmt.Sprintf(q, scope.SQLFragment(), cursor.SQLFragment())
args := pgx.NamedArgs{"control_id": controlID}
maps.Copy(args, scope.SQLArguments())
maps.Copy(args, cursor.SQLArguments())
rows, err := conn.Query(ctx, q, args)
if err != nil {
return fmt.Errorf("cannot query audits: %w", err)
}
audits, err := pgx.CollectRows(rows, pgx.RowToAddrOfStructByName[Audit])
if err != nil {
return fmt.Errorf("cannot collect audits: %w", err)
}
*a = audits
return nil
}

View File

@@ -826,3 +826,114 @@ RETURNING
return nil
}
func (c *Controls) CountByAuditID(
ctx context.Context,
conn pg.Conn,
scope Scoper,
auditID gid.GID,
filter *ControlFilter,
) (int, error) {
q := `
WITH ctrl AS (
SELECT
c.id,
c.tenant_id,
c.search_vector
FROM
controls c
INNER JOIN
controls_audits ca ON c.id = ca.control_id
WHERE
ca.audit_id = @audit_id
)
SELECT
COUNT(id)
FROM
ctrl
WHERE %s
AND %s
`
q = fmt.Sprintf(q, scope.SQLFragment(), filter.SQLFragment())
args := pgx.NamedArgs{"audit_id": auditID}
maps.Copy(args, scope.SQLArguments())
maps.Copy(args, filter.SQLArguments())
row := conn.QueryRow(ctx, q, args)
var count int
if err := row.Scan(&count); err != nil {
return 0, fmt.Errorf("cannot scan count: %w", err)
}
return count, nil
}
func (c *Controls) LoadByAuditID(
ctx context.Context,
conn pg.Conn,
scope Scoper,
auditID gid.GID,
cursor *page.Cursor[ControlOrderField],
filter *ControlFilter,
) error {
q := `
WITH ctrl AS (
SELECT
c.id,
c.section_title,
c.framework_id,
c.tenant_id,
c.name,
c.description,
c.status,
c.exclusion_justification,
c.created_at,
c.updated_at,
c.search_vector
FROM
controls c
INNER JOIN
controls_audits ca ON c.id = ca.control_id
WHERE
ca.audit_id = @audit_id
)
SELECT
id,
section_title,
framework_id,
tenant_id,
name,
description,
status,
exclusion_justification,
created_at,
updated_at
FROM
ctrl
WHERE %s
AND %s
AND %s
`
q = fmt.Sprintf(q, scope.SQLFragment(), filter.SQLFragment(), cursor.SQLFragment())
args := pgx.NamedArgs{"audit_id": auditID}
maps.Copy(args, scope.SQLArguments())
maps.Copy(args, filter.SQLArguments())
maps.Copy(args, cursor.SQLArguments())
rows, err := conn.Query(ctx, q, args)
if err != nil {
return fmt.Errorf("cannot query controls: %w", err)
}
controls, err := pgx.CollectRows(rows, pgx.RowToAddrOfStructByName[Control])
if err != nil {
return fmt.Errorf("cannot collect controls: %w", err)
}
*c = controls
return nil
}

View File

@@ -0,0 +1,168 @@
// 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/gid"
"github.com/jackc/pgx/v5"
"go.gearno.de/kit/pg"
)
type (
ControlAudit struct {
ControlID gid.GID `db:"control_id"`
AuditID gid.GID `db:"audit_id"`
CreatedAt time.Time `db:"created_at"`
}
ControlAudits []*ControlAudit
)
func (ca ControlAudit) Upsert(
ctx context.Context,
conn pg.Conn,
scope Scoper,
) error {
q := `
INSERT INTO
controls_audits (
control_id,
audit_id,
tenant_id,
created_at
)
VALUES (
@control_id,
@audit_id,
@tenant_id,
@created_at
)
ON CONFLICT (control_id, audit_id) DO NOTHING;
`
args := pgx.StrictNamedArgs{
"control_id": ca.ControlID,
"audit_id": ca.AuditID,
"tenant_id": scope.GetTenantID(),
"created_at": ca.CreatedAt,
}
_, err := conn.Exec(ctx, q, args)
return err
}
func (ca ControlAudit) Delete(
ctx context.Context,
conn pg.Conn,
scope Scoper,
controlID gid.GID,
auditID gid.GID,
) error {
q := `
DELETE
FROM
controls_audits
WHERE
%s
AND control_id = @control_id
AND audit_id = @audit_id;
`
args := pgx.StrictNamedArgs{
"control_id": controlID,
"audit_id": auditID,
}
maps.Copy(args, scope.SQLArguments())
q = fmt.Sprintf(q, scope.SQLFragment())
_, err := conn.Exec(ctx, q, args)
return err
}
func (cas *ControlAudits) LoadByControlID(
ctx context.Context,
conn pg.Conn,
scope Scoper,
controlID gid.GID,
) error {
q := `
SELECT
control_id,
audit_id,
created_at
FROM
controls_audits
WHERE
%s
AND control_id = @control_id
`
q = fmt.Sprintf(q, scope.SQLFragment())
args := pgx.StrictNamedArgs{"control_id": controlID}
maps.Copy(args, scope.SQLArguments())
rows, err := conn.Query(ctx, q, args)
if err != nil {
return fmt.Errorf("cannot query control_audits: %w", err)
}
controlAudits, err := pgx.CollectRows(rows, pgx.RowToAddrOfStructByName[ControlAudit])
if err != nil {
return fmt.Errorf("cannot collect control_audits: %w", err)
}
*cas = controlAudits
return nil
}
func (cas *ControlAudits) LoadByAuditID(
ctx context.Context,
conn pg.Conn,
scope Scoper,
auditID gid.GID,
) error {
q := `
SELECT
control_id,
audit_id,
created_at
FROM
controls_audits
WHERE
%s
AND audit_id = @audit_id
`
q = fmt.Sprintf(q, scope.SQLFragment())
args := pgx.StrictNamedArgs{"audit_id": auditID}
maps.Copy(args, scope.SQLArguments())
rows, err := conn.Query(ctx, q, args)
if err != nil {
return fmt.Errorf("cannot query control_audits: %w", err)
}
controlAudits, err := pgx.CollectRows(rows, pgx.RowToAddrOfStructByName[ControlAudit])
if err != nil {
return fmt.Errorf("cannot collect control_audits: %w", err)
}
*cas = controlAudits
return nil
}

View File

@@ -0,0 +1,7 @@
CREATE TABLE controls_audits (
control_id TEXT NOT NULL REFERENCES controls(id),
audit_id TEXT NOT NULL REFERENCES audits(id),
tenant_id TEXT NOT NULL,
created_at TIMESTAMP WITH TIME ZONE NOT NULL,
PRIMARY KEY (control_id, audit_id)
);