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
}