Add public trust center documents

Signed-off-by: Sacha Al Himdani <sacha@getprobo.com>
This commit is contained in:
Sacha Al Himdani
2025-10-02 16:02:57 +02:00
parent ba91e46cf7
commit 8504830c4b
39 changed files with 890 additions and 599 deletions

View File

@@ -28,17 +28,17 @@ import (
type (
Audit struct {
ID gid.GID `db:"id"`
Name *string `db:"name"`
OrganizationID gid.GID `db:"organization_id"`
FrameworkID gid.GID `db:"framework_id"`
ReportID *gid.GID `db:"report_id"`
ValidFrom *time.Time `db:"valid_from"`
ValidUntil *time.Time `db:"valid_until"`
State AuditState `db:"state"`
ShowOnTrustCenter bool `db:"show_on_trust_center"`
CreatedAt time.Time `db:"created_at"`
UpdatedAt time.Time `db:"updated_at"`
ID gid.GID `db:"id"`
Name *string `db:"name"`
OrganizationID gid.GID `db:"organization_id"`
FrameworkID gid.GID `db:"framework_id"`
ReportID *gid.GID `db:"report_id"`
ValidFrom *time.Time `db:"valid_from"`
ValidUntil *time.Time `db:"valid_until"`
State AuditState `db:"state"`
TrustCenterVisibility TrustCenterVisibility `db:"trust_center_visibility"`
CreatedAt time.Time `db:"created_at"`
UpdatedAt time.Time `db:"updated_at"`
}
Audits []*Audit
@@ -75,7 +75,7 @@ SELECT
valid_from,
valid_until,
state,
show_on_trust_center,
trust_center_visibility,
created_at,
updated_at
FROM
@@ -156,7 +156,7 @@ SELECT
valid_from,
valid_until,
state,
show_on_trust_center,
trust_center_visibility,
created_at,
updated_at
FROM
@@ -207,7 +207,7 @@ SELECT
valid_from,
valid_until,
state,
show_on_trust_center,
trust_center_visibility,
created_at,
updated_at
FROM
@@ -256,7 +256,7 @@ INSERT INTO audits (
valid_from,
valid_until,
state,
show_on_trust_center,
trust_center_visibility,
created_at,
updated_at
) VALUES (
@@ -269,25 +269,25 @@ INSERT INTO audits (
@valid_from,
@valid_until,
@state,
@show_on_trust_center,
@trust_center_visibility,
@created_at,
@updated_at
)
`
args := pgx.StrictNamedArgs{
"id": a.ID,
"name": a.Name,
"tenant_id": scope.GetTenantID(),
"organization_id": a.OrganizationID,
"framework_id": a.FrameworkID,
"report_id": a.ReportID,
"valid_from": a.ValidFrom,
"valid_until": a.ValidUntil,
"state": a.State,
"show_on_trust_center": a.ShowOnTrustCenter,
"created_at": a.CreatedAt,
"updated_at": a.UpdatedAt,
"id": a.ID,
"name": a.Name,
"tenant_id": scope.GetTenantID(),
"organization_id": a.OrganizationID,
"framework_id": a.FrameworkID,
"report_id": a.ReportID,
"valid_from": a.ValidFrom,
"valid_until": a.ValidUntil,
"state": a.State,
"trust_center_visibility": a.TrustCenterVisibility,
"created_at": a.CreatedAt,
"updated_at": a.UpdatedAt,
}
_, err := conn.Exec(ctx, q, args)
@@ -311,7 +311,7 @@ SET
valid_from = @valid_from,
valid_until = @valid_until,
state = @state,
show_on_trust_center = @show_on_trust_center,
trust_center_visibility = @trust_center_visibility,
updated_at = @updated_at
WHERE
%s
@@ -321,14 +321,14 @@ WHERE
q = fmt.Sprintf(q, scope.SQLFragment())
args := pgx.StrictNamedArgs{
"id": a.ID,
"name": a.Name,
"report_id": a.ReportID,
"valid_from": a.ValidFrom,
"valid_until": a.ValidUntil,
"state": a.State,
"show_on_trust_center": a.ShowOnTrustCenter,
"updated_at": a.UpdatedAt,
"id": a.ID,
"name": a.Name,
"report_id": a.ReportID,
"valid_from": a.ValidFrom,
"valid_until": a.ValidUntil,
"state": a.State,
"trust_center_visibility": a.TrustCenterVisibility,
"updated_at": a.UpdatedAt,
}
maps.Copy(args, scope.SQLArguments())
@@ -423,7 +423,7 @@ WITH audits_by_control AS (
a.valid_from,
a.valid_until,
a.state,
a.show_on_trust_center,
a.trust_center_visibility,
a.created_at,
a.updated_at
FROM
@@ -442,7 +442,7 @@ SELECT
valid_from,
valid_until,
state,
show_on_trust_center,
trust_center_visibility,
created_at,
updated_at
FROM
@@ -487,7 +487,7 @@ SELECT
valid_from,
valid_until,
state,
show_on_trust_center,
trust_center_visibility,
created_at,
updated_at
FROM

View File

@@ -20,7 +20,7 @@ import (
type (
AuditFilter struct {
showOnTrustCenter *bool
trustCenterVisibilities []TrustCenterVisibility
}
)
@@ -29,25 +29,31 @@ func NewAuditFilter() *AuditFilter {
}
func NewAuditTrustCenterFilter() *AuditFilter {
showOnTrustCenter := true
return &AuditFilter{
showOnTrustCenter: &showOnTrustCenter,
trustCenterVisibilities: []TrustCenterVisibility{
TrustCenterVisibilityPrivate,
TrustCenterVisibilityPublic,
},
}
}
func (f *AuditFilter) SQLArguments() pgx.NamedArgs {
args := pgx.NamedArgs{}
if f.showOnTrustCenter != nil {
args["show_on_trust_center"] = *f.showOnTrustCenter
if f.trustCenterVisibilities != nil {
visibilities := make([]string, len(f.trustCenterVisibilities))
for i, v := range f.trustCenterVisibilities {
visibilities[i] = v.String()
}
args["trust_center_visibilities"] = visibilities
}
return args
}
func (f *AuditFilter) SQLFragment() string {
if f.showOnTrustCenter != nil {
return "show_on_trust_center = @show_on_trust_center"
if f.trustCenterVisibilities != nil {
return "trust_center_visibility = ANY(@trust_center_visibilities::trust_center_visibility[])"
}
return "TRUE"

View File

@@ -28,15 +28,15 @@ import (
type (
Document struct {
ID gid.GID `db:"id"`
OrganizationID gid.GID `db:"organization_id"`
OwnerID gid.GID `db:"owner_id"`
Title string `db:"title"`
DocumentType DocumentType `db:"document_type"`
CurrentPublishedVersion *int `db:"current_published_version"`
ShowOnTrustCenter bool `db:"show_on_trust_center"`
CreatedAt time.Time `db:"created_at"`
UpdatedAt time.Time `db:"updated_at"`
ID gid.GID `db:"id"`
OrganizationID gid.GID `db:"organization_id"`
OwnerID gid.GID `db:"owner_id"`
Title string `db:"title"`
DocumentType DocumentType `db:"document_type"`
CurrentPublishedVersion *int `db:"current_published_version"`
TrustCenterVisibility TrustCenterVisibility `db:"trust_center_visibility"`
CreatedAt time.Time `db:"created_at"`
UpdatedAt time.Time `db:"updated_at"`
}
Documents []*Document
@@ -69,7 +69,7 @@ SELECT
title,
document_type,
current_published_version,
show_on_trust_center,
trust_center_visibility,
created_at,
updated_at
FROM
@@ -151,7 +151,7 @@ SELECT
title,
document_type,
current_published_version,
show_on_trust_center,
trust_center_visibility,
created_at,
updated_at
FROM
@@ -201,7 +201,7 @@ SELECT
title,
document_type,
current_published_version,
show_on_trust_center,
trust_center_visibility,
created_at,
updated_at
FROM
@@ -250,7 +250,7 @@ INSERT INTO
title,
document_type,
current_published_version,
show_on_trust_center,
trust_center_visibility,
created_at,
updated_at
)
@@ -262,7 +262,7 @@ VALUES (
@title,
@document_type,
@current_published_version,
@show_on_trust_center,
@trust_center_visibility,
@created_at,
@updated_at
);
@@ -276,7 +276,7 @@ VALUES (
"title": p.Title,
"document_type": p.DocumentType,
"current_published_version": p.CurrentPublishedVersion,
"show_on_trust_center": p.ShowOnTrustCenter,
"trust_center_visibility": p.TrustCenterVisibility,
"created_at": p.CreatedAt,
"updated_at": p.UpdatedAt,
}
@@ -334,7 +334,7 @@ SET
current_published_version = @current_published_version,
owner_id = @owner_id,
document_type = @document_type,
show_on_trust_center = @show_on_trust_center,
trust_center_visibility = @trust_center_visibility,
updated_at = @updated_at
WHERE
%s
@@ -350,7 +350,7 @@ WHERE
"current_published_version": p.CurrentPublishedVersion,
"owner_id": p.OwnerID,
"document_type": p.DocumentType,
"show_on_trust_center": p.ShowOnTrustCenter,
"trust_center_visibility": p.TrustCenterVisibility,
}
maps.Copy(args, scope.SQLArguments())
@@ -375,7 +375,7 @@ WITH plcs AS (
p.id,
p.tenant_id,
p.search_vector,
p.show_on_trust_center,
p.trust_center_visibility,
p.deleted_at
FROM
documents p
@@ -428,7 +428,7 @@ WITH plcs AS (
p.title,
p.document_type,
p.current_published_version,
p.show_on_trust_center,
p.trust_center_visibility,
p.created_at,
p.updated_at,
p.deleted_at
@@ -446,7 +446,7 @@ SELECT
title,
document_type,
current_published_version,
show_on_trust_center,
trust_center_visibility,
created_at,
updated_at
FROM
@@ -492,7 +492,7 @@ WITH plcs AS (
p.id,
p.tenant_id,
p.search_vector,
p.show_on_trust_center,
p.trust_center_visibility,
p.deleted_at
FROM
documents p
@@ -544,7 +544,7 @@ WITH plcs AS (
p.title,
p.document_type,
p.current_published_version,
p.show_on_trust_center,
p.trust_center_visibility,
p.created_at,
p.updated_at,
p.search_vector,
@@ -563,7 +563,7 @@ SELECT
title,
document_type,
current_published_version,
show_on_trust_center,
trust_center_visibility,
created_at,
updated_at
FROM

View File

@@ -20,8 +20,8 @@ import (
type (
DocumentFilter struct {
query *string
showOnTrustCenter *bool
query *string
trustCenterVisibilities []TrustCenterVisibility
}
)
@@ -32,16 +32,25 @@ func NewDocumentFilter(query *string) *DocumentFilter {
}
func NewDocumentTrustCenterFilter() *DocumentFilter {
showOnTrustCenter := true
return &DocumentFilter{
showOnTrustCenter: &showOnTrustCenter,
trustCenterVisibilities: []TrustCenterVisibility{
TrustCenterVisibilityPrivate,
TrustCenterVisibilityPublic,
},
}
}
func (f *DocumentFilter) SQLArguments() pgx.StrictNamedArgs {
var visibilities []string
if f.trustCenterVisibilities != nil {
visibilities = make([]string, len(f.trustCenterVisibilities))
for i, v := range f.trustCenterVisibilities {
visibilities[i] = v.String()
}
}
return pgx.StrictNamedArgs{
"query": f.query,
"show_on_trust_center": f.showOnTrustCenter,
"query": f.query,
"trust_center_visibilities": visibilities,
}
}
@@ -58,8 +67,8 @@ func (f *DocumentFilter) SQLFragment() string {
END
AND
CASE
WHEN @show_on_trust_center::boolean IS NOT NULL THEN
show_on_trust_center = @show_on_trust_center::boolean
WHEN @trust_center_visibilities::trust_center_visibility[] IS NOT NULL THEN
trust_center_visibility = ANY(@trust_center_visibilities::trust_center_visibility[])
ELSE TRUE
END
)`

View File

@@ -0,0 +1,25 @@
CREATE TYPE trust_center_visibility AS ENUM (
'NONE',
'PRIVATE',
'PUBLIC'
);
ALTER TABLE documents ADD COLUMN trust_center_visibility trust_center_visibility NOT NULL DEFAULT 'NONE';
UPDATE documents SET trust_center_visibility = CASE
WHEN show_on_trust_center = true THEN 'PRIVATE'::trust_center_visibility
ELSE 'NONE'::trust_center_visibility
END;
ALTER TABLE documents ALTER COLUMN trust_center_visibility DROP DEFAULT;
ALTER TABLE documents DROP COLUMN show_on_trust_center;
ALTER TABLE audits ADD COLUMN trust_center_visibility trust_center_visibility NOT NULL DEFAULT 'NONE';
UPDATE audits SET trust_center_visibility = CASE
WHEN show_on_trust_center = true THEN 'PRIVATE'::trust_center_visibility
ELSE 'NONE'::trust_center_visibility
END;
ALTER TABLE audits ALTER COLUMN trust_center_visibility DROP DEFAULT;
ALTER TABLE audits DROP COLUMN show_on_trust_center;

View File

@@ -0,0 +1,60 @@
// 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 (
"database/sql/driver"
"fmt"
)
type TrustCenterVisibility string
const (
TrustCenterVisibilityNone TrustCenterVisibility = "NONE"
TrustCenterVisibilityPrivate TrustCenterVisibility = "PRIVATE"
TrustCenterVisibilityPublic TrustCenterVisibility = "PUBLIC"
)
func (tcv TrustCenterVisibility) String() string {
return string(tcv)
}
func (tcv *TrustCenterVisibility) Scan(value any) error {
var s string
switch v := value.(type) {
case string:
s = v
case []byte:
s = string(v)
default:
return fmt.Errorf("unsupported type for TrustCenterVisibility: %T", value)
}
switch s {
case "NONE":
*tcv = TrustCenterVisibilityNone
case "PRIVATE":
*tcv = TrustCenterVisibilityPrivate
case "PUBLIC":
*tcv = TrustCenterVisibilityPublic
default:
return fmt.Errorf("invalid TrustCenterVisibility value: %q", s)
}
return nil
}
func (tcv TrustCenterVisibility) Value() (driver.Value, error) {
return tcv.String(), nil
}