Add controls crud

Signed-off-by: Sacha Al Himdani <sacha@getprobo.com>
This commit is contained in:
Sacha Al Himdani
2025-06-05 15:25:37 -07:00
parent c1c19ba9f2
commit 4b34157ba3
37 changed files with 2988 additions and 715 deletions

View File

@@ -28,22 +28,22 @@ import (
type (
Control struct {
ID gid.GID `db:"id"`
ReferenceID string `db:"reference_id"`
TenantID gid.TenantID `db:"tenant_id"`
FrameworkID gid.GID `db:"framework_id"`
Name string `db:"name"`
Description string `db:"description"`
CreatedAt time.Time `db:"created_at"`
UpdatedAt time.Time `db:"updated_at"`
ID gid.GID `db:"id"`
SectionTitle string `db:"section_title"`
TenantID gid.TenantID `db:"tenant_id"`
FrameworkID gid.GID `db:"framework_id"`
Name string `db:"name"`
Description string `db:"description"`
CreatedAt time.Time `db:"created_at"`
UpdatedAt time.Time `db:"updated_at"`
}
Controls []*Control
UpdateControlParams struct {
ExpectedVersion int
Name *string
Description *string
Name *string
Description *string
SectionTitle *string
}
)
@@ -51,6 +51,8 @@ func (c Control) CursorKey(orderBy ControlOrderField) page.CursorKey {
switch orderBy {
case ControlOrderFieldCreatedAt:
return page.CursorKey{ID: c.ID, Value: c.CreatedAt}
case ControlOrderFieldSectionTitle:
return page.CursorKey{ID: c.ID, Value: c.SectionTitle}
}
panic(fmt.Sprintf("unsupported order by: %s", orderBy))
@@ -67,7 +69,7 @@ func (c *Controls) LoadByDocumentID(
WITH ctrl AS (
SELECT
c.id,
c.reference_id,
c.section_title,
c.framework_id,
c.tenant_id,
c.name,
@@ -83,7 +85,7 @@ WITH ctrl AS (
)
SELECT
id,
reference_id,
section_title,
framework_id,
tenant_id,
name,
@@ -127,7 +129,7 @@ func (c *Controls) LoadByMeasureID(
WITH ctrl AS (
SELECT
c.id,
c.reference_id,
c.section_title,
c.framework_id,
c.tenant_id,
c.name,
@@ -143,7 +145,7 @@ WITH ctrl AS (
)
SELECT
id,
reference_id,
section_title,
framework_id,
tenant_id,
name,
@@ -186,7 +188,7 @@ func (c *Controls) LoadByRiskID(
WITH ctrl AS (
SELECT DISTINCT
c.id,
c.reference_id,
c.section_title,
c.framework_id,
c.tenant_id,
c.name,
@@ -208,7 +210,7 @@ WITH ctrl AS (
)
SELECT
id,
reference_id,
section_title,
framework_id,
tenant_id,
name,
@@ -251,7 +253,7 @@ func (c *Controls) LoadByFrameworkID(
q := `
SELECT
id,
reference_id,
section_title,
framework_id,
tenant_id,
name,
@@ -285,17 +287,17 @@ WHERE
return nil
}
func (c *Control) LoadByFrameworkIDAndReferenceID(
func (c *Control) LoadByFrameworkIDAndSectionTitle(
ctx context.Context,
conn pg.Conn,
scope Scoper,
frameworkID gid.GID,
referenceID string,
sectionTitle string,
) error {
q := `
SELECT
id,
reference_id,
section_title,
framework_id,
tenant_id,
name,
@@ -307,12 +309,12 @@ FROM
WHERE
%s
AND framework_id = @framework_id
AND reference_id = @reference_id
AND section_title = @section_title
LIMIT 1;
`
q = fmt.Sprintf(q, scope.SQLFragment())
args := pgx.StrictNamedArgs{"framework_id": frameworkID, "reference_id": referenceID}
args := pgx.StrictNamedArgs{"framework_id": frameworkID, "section_title": sectionTitle}
maps.Copy(args, scope.SQLArguments())
rows, err := conn.Query(ctx, q, args)
if err != nil {
@@ -338,7 +340,7 @@ func (c *Control) LoadByID(
q := `
SELECT
id,
reference_id,
section_title,
framework_id,
tenant_id,
name,
@@ -382,7 +384,7 @@ INSERT INTO
tenant_id,
id,
framework_id,
reference_id,
section_title,
name,
description,
created_at,
@@ -392,7 +394,7 @@ VALUES (
@tenant_id,
@control_id,
@framework_id,
@reference_id,
@section_title,
@name,
@description,
@created_at,
@@ -401,14 +403,14 @@ VALUES (
`
args := pgx.StrictNamedArgs{
"tenant_id": scope.GetTenantID(),
"control_id": c.ID,
"framework_id": c.FrameworkID,
"reference_id": c.ReferenceID,
"name": c.Name,
"description": c.Description,
"created_at": c.CreatedAt,
"updated_at": c.UpdatedAt,
"tenant_id": scope.GetTenantID(),
"control_id": c.ID,
"framework_id": c.FrameworkID,
"section_title": c.SectionTitle,
"name": c.Name,
"description": c.Description,
"created_at": c.CreatedAt,
"updated_at": c.UpdatedAt,
}
_, err := conn.Exec(ctx, q, args)
return err
@@ -446,6 +448,7 @@ func (c *Control) Update(
UPDATE controls SET
name = COALESCE(@name, name),
description = COALESCE(@description, description),
section_title = COALESCE(@section_title, section_title),
updated_at = @updated_at
WHERE %s
AND id = @control_id
@@ -455,15 +458,16 @@ RETURNING
tenant_id,
name,
description,
section_title,
created_at,
updated_at
`
q = fmt.Sprintf(q, scope.SQLFragment())
args := pgx.StrictNamedArgs{
"control_id": c.ID,
"expected_version": params.ExpectedVersion,
"updated_at": time.Now(),
"control_id": c.ID,
"section_title": params.SectionTitle,
"updated_at": time.Now(),
}
if params.Name != nil {

View File

@@ -19,11 +19,19 @@ type (
)
const (
ControlOrderFieldCreatedAt ControlOrderField = "CREATED_AT"
ControlOrderFieldCreatedAt ControlOrderField = "CREATED_AT"
ControlOrderFieldSectionTitle ControlOrderField = "SECTION_TITLE"
)
func (p ControlOrderField) Column() string {
return string(p)
switch p {
case ControlOrderFieldCreatedAt:
return "created_at"
case ControlOrderFieldSectionTitle:
return "section_title_sort_key(section_title)"
default:
return string(p)
}
}
func (p ControlOrderField) String() string {

View File

@@ -0,0 +1,34 @@
ALTER TABLE controls DROP COLUMN version;
ALTER TABLE controls RENAME COLUMN reference_id TO section_title;
CREATE OR REPLACE FUNCTION section_title_sort_key(text) RETURNS text AS $$
DECLARE
result text := '';
matches text[];
remainder text := $1;
BEGIN
WHILE remainder ~ '\d+' LOOP
-- Extract text before the number
result := result || substring(remainder FROM '^[^\d]*');
-- Extract and pad the next number
matches := regexp_matches(remainder, '(\d+)', ''); -- captures the first number
IF matches IS NOT NULL THEN
result := result || lpad(matches[1], 10, '0');
-- Remove processed part from remainder
remainder := substring(remainder FROM '\d+(.*)$');
ELSE
EXIT;
END IF;
END LOOP;
-- Append any remaining non-digit text
result := result || remainder;
RETURN result;
END;
$$ LANGUAGE plpgsql IMMUTABLE STRICT;
COMMENT ON FUNCTION section_title_sort_key(text) IS
'Converts numbers in strings to zero-padded format for natural sorting';

View File

@@ -31,17 +31,18 @@ type (
}
CreateControlRequest struct {
ID gid.GID
FrameworkID gid.GID
Name string
Description string
ID gid.GID
FrameworkID gid.GID
Name string
Description string
SectionTitle string
}
UpdateControlRequest struct {
ID gid.GID
ExpectedVersion int
Name *string
Description *string
ID gid.GID
Name *string
Description *string
SectionTitle *string
}
ConnectControlToMitigationRequest struct {
@@ -263,13 +264,14 @@ func (s ControlService) Create(
framework := &coredata.Framework{}
control := &coredata.Control{
ID: req.ID,
FrameworkID: req.FrameworkID,
TenantID: s.svc.scope.GetTenantID(),
Name: req.Name,
Description: req.Description,
CreatedAt: now,
UpdatedAt: now,
ID: gid.New(s.svc.scope.GetTenantID(), coredata.ControlEntityType),
FrameworkID: req.FrameworkID,
TenantID: s.svc.scope.GetTenantID(),
Name: req.Name,
Description: req.Description,
SectionTitle: req.SectionTitle,
CreatedAt: now,
UpdatedAt: now,
}
err := s.svc.pg.WithTx(
@@ -317,9 +319,9 @@ func (s ControlService) Update(
req UpdateControlRequest,
) (*coredata.Control, error) {
params := coredata.UpdateControlParams{
ExpectedVersion: req.ExpectedVersion,
Name: req.Name,
Description: req.Description,
Name: req.Name,
Description: req.Description,
SectionTitle: req.SectionTitle,
}
control := &coredata.Control{ID: req.ID}

View File

@@ -241,14 +241,14 @@ func (s FrameworkService) Import(
now := time.Now()
control := &coredata.Control{
ID: controlID,
TenantID: organizationID.TenantID(),
FrameworkID: frameworkID,
ReferenceID: control.ID,
Name: control.Name,
Description: control.Description,
CreatedAt: now,
UpdatedAt: now,
ID: controlID,
TenantID: organizationID.TenantID(),
FrameworkID: frameworkID,
SectionTitle: control.ID,
Name: control.Name,
Description: control.Description,
CreatedAt: now,
UpdatedAt: now,
}
if err := control.Insert(ctx, tx, s.svc.scope); err != nil {
@@ -307,7 +307,7 @@ func (s FrameworkService) ExportAudit(
}
for _, control := range controls {
controlDir := filepath.Join(exportDir, control.ReferenceID)
controlDir := filepath.Join(exportDir, filepath.Base(control.SectionTitle))
if err := os.MkdirAll(controlDir, 0755); err != nil {
return fmt.Errorf("cannot create control directory: %w", err)
}
@@ -344,7 +344,7 @@ func (s FrameworkService) ExportAudit(
}
for _, document := range documents {
documentDir := filepath.Join(controlDir, document.Title)
documentDir := filepath.Join(controlDir, filepath.Base(document.Title))
if err := os.MkdirAll(documentDir, 0755); err != nil {
return fmt.Errorf("cannot create document directory: %w", err)
}
@@ -361,13 +361,13 @@ func (s FrameworkService) ExportAudit(
}
for _, measure := range measures {
measureDir := filepath.Join(controlDir, measure.Name)
measureDir := filepath.Join(controlDir, filepath.Base(measure.Name))
if err := os.MkdirAll(measureDir, 0755); err != nil {
return fmt.Errorf("cannot create measure directory: %w", err)
}
evidences := coredata.Evidences{}
cursor := page.NewCursor(
evidenceCursor := page.NewCursor(
0,
nil,
page.Head,
@@ -377,12 +377,12 @@ func (s FrameworkService) ExportAudit(
},
)
if err := evidences.LoadByMeasureID(ctx, conn, s.svc.scope, measure.ID, cursor); err != nil {
if err := evidences.LoadByMeasureID(ctx, conn, s.svc.scope, measure.ID, evidenceCursor); err != nil {
return fmt.Errorf("cannot load evidences: %w", err)
}
for _, evidence := range evidences {
evidenceFile := filepath.Join(measureDir, evidence.Filename)
evidenceFile := filepath.Join(measureDir, filepath.Base(evidence.Filename))
if evidence.Type == coredata.EvidenceTypeFile && evidence.ObjectKey != "" {
output, err := s.svc.s3.GetObject(

View File

@@ -235,7 +235,7 @@ func (s MeasureService) Import(
}
control := &coredata.Control{}
if err := control.LoadByFrameworkIDAndReferenceID(ctx, tx, s.svc.scope, framework.ID, standard.Control); err != nil {
if err := control.LoadByFrameworkIDAndSectionTitle(ctx, tx, s.svc.scope, framework.ID, standard.Control); err != nil {
continue
}

View File

@@ -169,6 +169,10 @@ enum ControlOrderField
@goEnum(
value: "github.com/getprobo/probo/pkg/coredata.ControlOrderFieldCreatedAt"
)
SECTION_TITLE
@goEnum(
value: "github.com/getprobo/probo/pkg/coredata.ControlOrderFieldSectionTitle"
)
}
enum MeasureOrderField
@@ -683,7 +687,7 @@ type Framework implements Node {
type Control implements Node {
id: ID!
referenceId: String!
sectionTitle: String!
name: String!
description: String!
@@ -1087,6 +1091,11 @@ type Mutation {
importFramework(input: ImportFrameworkInput!): ImportFrameworkPayload!
deleteFramework(input: DeleteFrameworkInput!): DeleteFrameworkPayload!
# Control mutations
createControl(input: CreateControlInput!): CreateControlPayload!
updateControl(input: UpdateControlInput!): UpdateControlPayload!
deleteControl(input: DeleteControlInput!): DeleteControlPayload!
# Measure mutations
createMeasure(input: CreateMeasureInput!): CreateMeasurePayload!
updateMeasure(input: UpdateMeasureInput!): UpdateMeasurePayload!
@@ -1502,6 +1511,24 @@ input RemoveUserInput {
userId: ID!
}
input CreateControlInput {
frameworkId: ID!
sectionTitle: String!
name: String!
description: String!
}
input UpdateControlInput {
id: ID!
sectionTitle: String
name: String
description: String
}
input DeleteControlInput {
controlId: ID!
}
# Payload Types
type CreateOrganizationPayload {
organizationEdge: OrganizationEdge!
@@ -1515,6 +1542,18 @@ type DeleteOrganizationPayload {
deletedOrganizationId: ID!
}
type CreateControlPayload {
controlEdge: ControlEdge!
}
type UpdateControlPayload {
control: Control!
}
type DeleteControlPayload {
deletedControlId: ID!
}
type CreateVendorPayload {
vendorEdge: VendorEdge!
}

File diff suppressed because it is too large Load Diff

View File

@@ -45,11 +45,11 @@ func NewControlEdge(c *coredata.Control, orderBy coredata.ControlOrderField) *Co
func NewControl(c *coredata.Control) *Control {
return &Control{
ID: c.ID,
ReferenceID: c.ReferenceID,
Name: c.Name,
Description: c.Description,
CreatedAt: c.CreatedAt,
UpdatedAt: c.UpdatedAt,
ID: c.ID,
SectionTitle: c.SectionTitle,
Name: c.Name,
Description: c.Description,
CreatedAt: c.CreatedAt,
UpdatedAt: c.UpdatedAt,
}
}

View File

@@ -119,15 +119,15 @@ type ConnectorOrder struct {
}
type Control struct {
ID gid.GID `json:"id"`
ReferenceID string `json:"referenceId"`
Name string `json:"name"`
Description string `json:"description"`
Framework *Framework `json:"framework"`
Measures *MeasureConnection `json:"measures"`
Documents *DocumentConnection `json:"documents"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
ID gid.GID `json:"id"`
SectionTitle string `json:"sectionTitle"`
Name string `json:"name"`
Description string `json:"description"`
Framework *Framework `json:"framework"`
Measures *MeasureConnection `json:"measures"`
Documents *DocumentConnection `json:"documents"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
}
func (Control) IsNode() {}
@@ -168,6 +168,13 @@ type CreateControlDocumentMappingPayload struct {
DocumentEdge *DocumentEdge `json:"documentEdge"`
}
type CreateControlInput struct {
FrameworkID gid.GID `json:"frameworkId"`
SectionTitle string `json:"sectionTitle"`
Name string `json:"name"`
Description string `json:"description"`
}
type CreateControlMeasureMappingInput struct {
ControlID gid.GID `json:"controlId"`
MeasureID gid.GID `json:"measureId"`
@@ -178,6 +185,10 @@ type CreateControlMeasureMappingPayload struct {
MeasureEdge *MeasureEdge `json:"measureEdge"`
}
type CreateControlPayload struct {
ControlEdge *ControlEdge `json:"controlEdge"`
}
type CreateDatumInput struct {
OrganizationID gid.GID `json:"organizationId"`
Name string `json:"name"`
@@ -406,6 +417,10 @@ type DeleteControlDocumentMappingPayload struct {
DeletedDocumentID gid.GID `json:"deletedDocumentId"`
}
type DeleteControlInput struct {
ControlID gid.GID `json:"controlId"`
}
type DeleteControlMeasureMappingInput struct {
ControlID gid.GID `json:"controlId"`
MeasureID gid.GID `json:"measureId"`
@@ -416,6 +431,10 @@ type DeleteControlMeasureMappingPayload struct {
DeletedMeasureID gid.GID `json:"deletedMeasureId"`
}
type DeleteControlPayload struct {
DeletedControlID gid.GID `json:"deletedControlId"`
}
type DeleteDatumInput struct {
DatumID gid.GID `json:"datumId"`
}
@@ -980,6 +999,17 @@ type UpdateAssetPayload struct {
Asset *Asset `json:"asset"`
}
type UpdateControlInput struct {
ID gid.GID `json:"id"`
SectionTitle *string `json:"sectionTitle,omitempty"`
Name *string `json:"name,omitempty"`
Description *string `json:"description,omitempty"`
}
type UpdateControlPayload struct {
Control *Control `json:"control"`
}
type UpdateDatumInput struct {
ID gid.GID `json:"id"`
Name *string `json:"name,omitempty"`

View File

@@ -967,6 +967,59 @@ func (r *mutationResolver) DeleteFramework(ctx context.Context, input types.Dele
}, nil
}
// CreateControl is the resolver for the createControl field.
func (r *mutationResolver) CreateControl(ctx context.Context, input types.CreateControlInput) (*types.CreateControlPayload, error) {
svc := GetTenantService(ctx, r.proboSvc, input.FrameworkID.TenantID())
control, err := svc.Controls.Create(ctx, probo.CreateControlRequest{
FrameworkID: input.FrameworkID,
Name: input.Name,
Description: input.Description,
SectionTitle: input.SectionTitle,
})
if err != nil {
return nil, fmt.Errorf("cannot create control: %w", err)
}
return &types.CreateControlPayload{
ControlEdge: types.NewControlEdge(control, coredata.ControlOrderFieldCreatedAt),
}, nil
}
// UpdateControl is the resolver for the updateControl field.
func (r *mutationResolver) UpdateControl(ctx context.Context, input types.UpdateControlInput) (*types.UpdateControlPayload, error) {
svc := GetTenantService(ctx, r.proboSvc, input.ID.TenantID())
control, err := svc.Controls.Update(ctx, probo.UpdateControlRequest{
ID: input.ID,
Name: input.Name,
Description: input.Description,
SectionTitle: input.SectionTitle,
})
if err != nil {
return nil, fmt.Errorf("cannot update control: %w", err)
}
return &types.UpdateControlPayload{
Control: types.NewControl(control),
}, nil
}
// DeleteControl is the resolver for the deleteControl field.
func (r *mutationResolver) DeleteControl(ctx context.Context, input types.DeleteControlInput) (*types.DeleteControlPayload, error) {
svc := GetTenantService(ctx, r.proboSvc, input.ControlID.TenantID())
err := svc.Controls.Delete(ctx, input.ControlID)
if err != nil {
return nil, fmt.Errorf("cannot delete control: %w", err)
}
return &types.DeleteControlPayload{
DeletedControlID: input.ControlID,
}, nil
}
// // CreateMeasure is the resolver for the createMeasure field.
func (r *mutationResolver) CreateMeasure(ctx context.Context, input types.CreateMeasureInput) (*types.CreateMeasurePayload, error) {
svc := GetTenantService(ctx, r.proboSvc, input.OrganizationID.TenantID())