Add trust center files

Signed-off-by: Sacha Al Himdani <sacha@getprobo.com>
This commit is contained in:
Sacha Al Himdani
2025-10-24 10:04:54 +02:00
parent e53b1239db
commit 1bd6f7c1c9
55 changed files with 9074 additions and 394 deletions

View File

@@ -62,4 +62,5 @@ const (
InvitationEntityType
MembershipEntityType
SlackMessageEntityType
TrustCenterFileEntityType
)

View File

@@ -0,0 +1,21 @@
CREATE TABLE trust_center_files (
id TEXT PRIMARY KEY,
organization_id TEXT NOT NULL REFERENCES organizations(id) ON DELETE CASCADE,
tenant_id TEXT NOT NULL,
name TEXT NOT NULL,
category TEXT NOT NULL,
file_id TEXT NOT NULL REFERENCES files(id) ON UPDATE CASCADE ON DELETE RESTRICT,
trust_center_visibility trust_center_visibility NOT NULL,
created_at TIMESTAMP WITH TIME ZONE NOT NULL,
updated_at TIMESTAMP WITH TIME ZONE NOT NULL
);
ALTER TABLE trust_center_document_accesses ADD COLUMN trust_center_file_id TEXT REFERENCES trust_center_files(id) ON UPDATE CASCADE ON DELETE CASCADE;
ALTER TABLE trust_center_document_accesses DROP CONSTRAINT trust_center_document_accesses_check;
ALTER TABLE trust_center_document_accesses ADD CONSTRAINT trust_center_document_accesses_check CHECK (
(document_id IS NOT NULL)::int + (report_id IS NOT NULL)::int + (trust_center_file_id IS NOT NULL)::int = 1
);
ALTER TABLE trust_center_document_accesses ADD CONSTRAINT trust_center_document_accesses_trust_center_file_id_key UNIQUE (trust_center_access_id, trust_center_file_id);

View File

@@ -32,6 +32,7 @@ type (
TrustCenterAccessID gid.GID `db:"trust_center_access_id"`
DocumentID *gid.GID `db:"document_id"`
ReportID *gid.GID `db:"report_id"`
TrustCenterFileID *gid.GID `db:"trust_center_file_id"`
Active bool `db:"active"`
CreatedAt time.Time `db:"created_at"`
UpdatedAt time.Time `db:"updated_at"`
@@ -61,6 +62,7 @@ SELECT
trust_center_access_id,
document_id,
report_id,
trust_center_file_id,
active,
created_at,
updated_at
@@ -105,6 +107,7 @@ SELECT
trust_center_access_id,
document_id,
report_id,
trust_center_file_id,
active,
created_at,
updated_at
@@ -153,6 +156,7 @@ SELECT
trust_center_access_id,
document_id,
report_id,
trust_center_file_id,
active,
created_at,
updated_at
@@ -200,6 +204,7 @@ INSERT INTO trust_center_document_accesses (
trust_center_access_id,
document_id,
report_id,
trust_center_file_id,
active,
created_at,
updated_at
@@ -209,6 +214,7 @@ INSERT INTO trust_center_document_accesses (
@trust_center_access_id,
@document_id,
@report_id,
@trust_center_file_id,
@active,
@created_at,
@updated_at
@@ -221,6 +227,7 @@ INSERT INTO trust_center_document_accesses (
"trust_center_access_id": tcda.TrustCenterAccessID,
"document_id": tcda.DocumentID,
"report_id": tcda.ReportID,
"trust_center_file_id": tcda.TrustCenterFileID,
"active": tcda.Active,
"created_at": tcda.CreatedAt,
"updated_at": tcda.UpdatedAt,
@@ -338,6 +345,7 @@ SELECT
trust_center_access_id,
document_id,
report_id,
trust_center_file_id,
active,
created_at,
updated_at
@@ -384,6 +392,7 @@ SELECT
trust_center_access_id,
document_id,
report_id,
trust_center_file_id,
active,
created_at,
updated_at
@@ -543,12 +552,13 @@ WITH document_access_data AS (
@trust_center_access_id AS trust_center_access_id,
unnest(@document_ids::text[]) AS document_id,
null::text AS report_id,
null::text AS trust_center_file_id,
false AS active,
@created_at::timestamptz AS created_at,
@updated_at::timestamptz AS updated_at
)
INSERT INTO trust_center_document_accesses (
id, tenant_id, trust_center_access_id, document_id, report_id, active, created_at, updated_at
id, tenant_id, trust_center_access_id, document_id, report_id, trust_center_file_id, active, created_at, updated_at
)
SELECT * FROM document_access_data
`
@@ -589,12 +599,13 @@ WITH report_access_data AS (
@trust_center_access_id AS trust_center_access_id,
null::text AS document_id,
unnest(@report_ids::text[]) AS report_id,
null::text AS trust_center_file_id,
false AS active,
@created_at::timestamptz AS created_at,
@updated_at::timestamptz AS updated_at
)
INSERT INTO trust_center_document_accesses (
id, tenant_id, trust_center_access_id, document_id, report_id, active, created_at, updated_at
id, tenant_id, trust_center_access_id, document_id, report_id, trust_center_file_id, active, created_at, updated_at
)
SELECT * FROM report_access_data
`
@@ -614,3 +625,129 @@ SELECT * FROM report_access_data
return nil
}
func (tcda *TrustCenterDocumentAccess) LoadByTrustCenterAccessIDAndTrustCenterFileID(
ctx context.Context,
conn pg.Conn,
scope Scoper,
trustCenterAccessID gid.GID,
trustCenterFileID gid.GID,
) error {
q := `
SELECT
id,
trust_center_access_id,
document_id,
report_id,
trust_center_file_id,
active,
created_at,
updated_at
FROM
trust_center_document_accesses
WHERE
%s
AND trust_center_access_id = @trust_center_access_id
AND trust_center_file_id = @trust_center_file_id
LIMIT 1;
`
q = fmt.Sprintf(q, scope.SQLFragment())
args := pgx.StrictNamedArgs{
"trust_center_access_id": trustCenterAccessID,
"trust_center_file_id": trustCenterFileID,
}
maps.Copy(args, scope.SQLArguments())
rows, err := conn.Query(ctx, q, args)
if err != nil {
return fmt.Errorf("cannot query trust center document access: %w", err)
}
access, err := pgx.CollectExactlyOneRow(rows, pgx.RowToStructByName[TrustCenterDocumentAccess])
if err != nil {
return fmt.Errorf("cannot collect trust center document access: %w", err)
}
*tcda = access
return nil
}
func ActivateByTrustCenterFileIDs(
ctx context.Context,
conn pg.Conn,
scope Scoper,
trustCenterAccessID gid.GID,
trustCenterFileIDs []gid.GID,
updatedAt time.Time,
) error {
q := `
UPDATE trust_center_document_accesses
SET active = true, updated_at = @updated_at
WHERE
%s
AND trust_center_access_id = @trust_center_access_id
AND trust_center_file_id = ANY(@trust_center_file_ids)
`
q = fmt.Sprintf(q, scope.SQLFragment())
args := pgx.StrictNamedArgs{
"trust_center_access_id": trustCenterAccessID,
"trust_center_file_ids": trustCenterFileIDs,
"updated_at": updatedAt,
}
maps.Copy(args, scope.SQLArguments())
_, err := conn.Exec(ctx, q, args)
if err != nil {
return fmt.Errorf("cannot activate trust center document accesses by trust center file IDs: %w", err)
}
return nil
}
func (tcdas TrustCenterDocumentAccesses) BulkInsertTrustCenterFileAccesses(
ctx context.Context,
conn pg.Conn,
scope Scoper,
trustCenterAccessID gid.GID,
trustCenterFileIDs []gid.GID,
createdAt time.Time,
) error {
q := `
WITH trust_center_file_access_data AS (
SELECT
generate_gid(decode_base64_unpadded(@tenant_id), @trust_center_document_access_entity_type) AS id,
@tenant_id AS tenant_id,
@trust_center_access_id AS trust_center_access_id,
null::text AS document_id,
null::text AS report_id,
unnest(@trust_center_file_ids::text[]) AS trust_center_file_id,
false AS active,
@created_at::timestamptz AS created_at,
@updated_at::timestamptz AS updated_at
)
INSERT INTO trust_center_document_accesses (
id, tenant_id, trust_center_access_id, document_id, report_id, trust_center_file_id, active, created_at, updated_at
)
SELECT * FROM trust_center_file_access_data
`
args := pgx.StrictNamedArgs{
"tenant_id": scope.GetTenantID(),
"trust_center_document_access_entity_type": TrustCenterDocumentAccessEntityType,
"trust_center_access_id": trustCenterAccessID,
"trust_center_file_ids": trustCenterFileIDs,
"created_at": createdAt,
"updated_at": createdAt,
}
if _, err := conn.Exec(ctx, q, args); err != nil {
return fmt.Errorf("cannot bulk insert trust center file accesses: %w", err)
}
return nil
}

View File

@@ -0,0 +1,347 @@
// 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/getprobo/probo/pkg/page"
"github.com/jackc/pgx/v5"
"go.gearno.de/kit/pg"
)
type (
TrustCenterFile struct {
ID gid.GID `db:"id"`
OrganizationID gid.GID `db:"organization_id"`
Name string `db:"name"`
Category string `db:"category"`
FileID gid.GID `db:"file_id"`
TrustCenterVisibility TrustCenterVisibility `db:"trust_center_visibility"`
CreatedAt time.Time `db:"created_at"`
UpdatedAt time.Time `db:"updated_at"`
}
TrustCenterFiles []*TrustCenterFile
)
func (t TrustCenterFile) CursorKey(orderBy TrustCenterFileOrderField) page.CursorKey {
switch orderBy {
case TrustCenterFileOrderFieldName:
return page.NewCursorKey(t.ID, t.Name)
case TrustCenterFileOrderFieldCreatedAt:
return page.NewCursorKey(t.ID, t.CreatedAt)
case TrustCenterFileOrderFieldUpdatedAt:
return page.NewCursorKey(t.ID, t.UpdatedAt)
}
panic(fmt.Sprintf("unsupported order by: %s", orderBy))
}
func (t *TrustCenterFile) LoadByID(
ctx context.Context,
conn pg.Conn,
scope Scoper,
trustCenterFileID gid.GID,
) error {
q := `
SELECT
id,
organization_id,
name,
category,
file_id,
trust_center_visibility,
created_at,
updated_at
FROM
trust_center_files
WHERE
%s
AND id = @trust_center_file_id
LIMIT 1;
`
q = fmt.Sprintf(q, scope.SQLFragment())
args := pgx.StrictNamedArgs{"trust_center_file_id": trustCenterFileID}
maps.Copy(args, scope.SQLArguments())
rows, err := conn.Query(ctx, q, args)
if err != nil {
return fmt.Errorf("cannot query trust_center_files: %w", err)
}
file, err := pgx.CollectExactlyOneRow(rows, pgx.RowToStructByName[TrustCenterFile])
if err != nil {
return fmt.Errorf("cannot collect trust center file: %w", err)
}
*t = file
return nil
}
func (t TrustCenterFile) Insert(
ctx context.Context,
conn pg.Conn,
scope Scoper,
) error {
q := `
INSERT INTO
trust_center_files (
tenant_id,
id,
organization_id,
name,
category,
file_id,
trust_center_visibility,
created_at,
updated_at
)
VALUES (
@tenant_id,
@id,
@organization_id,
@name,
@category,
@file_id,
@trust_center_visibility,
@created_at,
@updated_at
);
`
args := pgx.StrictNamedArgs{
"tenant_id": scope.GetTenantID(),
"id": t.ID,
"organization_id": t.OrganizationID,
"name": t.Name,
"category": t.Category,
"file_id": t.FileID,
"trust_center_visibility": t.TrustCenterVisibility,
"created_at": t.CreatedAt,
"updated_at": t.UpdatedAt,
}
_, err := conn.Exec(ctx, q, args)
if err != nil {
return fmt.Errorf("cannot insert trust center file: %w", err)
}
return nil
}
func (t *TrustCenterFile) Update(
ctx context.Context,
conn pg.Conn,
scope Scoper,
) error {
q := `
UPDATE trust_center_files
SET
name = @name,
category = @category,
trust_center_visibility = @trust_center_visibility,
updated_at = @updated_at
WHERE
%s
AND id = @id
RETURNING
id,
organization_id,
name,
category,
file_id,
trust_center_visibility,
created_at,
updated_at
`
q = fmt.Sprintf(q, scope.SQLFragment())
args := pgx.StrictNamedArgs{
"id": t.ID,
"name": t.Name,
"category": t.Category,
"trust_center_visibility": t.TrustCenterVisibility,
"updated_at": t.UpdatedAt,
}
maps.Copy(args, scope.SQLArguments())
rows, err := conn.Query(ctx, q, args)
if err != nil {
return fmt.Errorf("cannot update trust center file: %w", err)
}
file, err := pgx.CollectExactlyOneRow(rows, pgx.RowToStructByName[TrustCenterFile])
if err != nil {
return fmt.Errorf("cannot collect updated trust center file: %w", err)
}
*t = file
return nil
}
func (t *TrustCenterFile) Delete(
ctx context.Context,
conn pg.Conn,
scope Scoper,
) error {
q := `
DELETE FROM
trust_center_files
WHERE
%s
AND id = @id
`
q = fmt.Sprintf(q, scope.SQLFragment())
args := pgx.StrictNamedArgs{"id": t.ID}
maps.Copy(args, scope.SQLArguments())
_, err := conn.Exec(ctx, q, args)
if err != nil {
return fmt.Errorf("cannot delete trust center file: %w", err)
}
return nil
}
func (t *TrustCenterFiles) LoadByOrganizationID(
ctx context.Context,
conn pg.Conn,
scope Scoper,
organizationID gid.GID,
cursor *page.Cursor[TrustCenterFileOrderField],
) error {
q := `
SELECT
id,
organization_id,
name,
category,
file_id,
trust_center_visibility,
created_at,
updated_at
FROM
trust_center_files
WHERE
%s
AND organization_id = @organization_id
AND %s
`
q = fmt.Sprintf(q, scope.SQLFragment(), cursor.SQLFragment())
args := pgx.StrictNamedArgs{"organization_id": organizationID}
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 trust_center_files: %w", err)
}
files, err := pgx.CollectRows(rows, pgx.RowToAddrOfStructByName[TrustCenterFile])
if err != nil {
return fmt.Errorf("cannot collect trust center files: %w", err)
}
*t = files
return nil
}
func (t *TrustCenterFiles) CountByOrganizationID(
ctx context.Context,
conn pg.Conn,
scope Scoper,
organizationID gid.GID,
) (int, error) {
q := `
SELECT
COUNT(*)
FROM
trust_center_files
WHERE
%s
AND organization_id = @organization_id
`
q = fmt.Sprintf(q, scope.SQLFragment())
args := pgx.StrictNamedArgs{"organization_id": organizationID}
maps.Copy(args, scope.SQLArguments())
var count int
err := conn.QueryRow(ctx, q, args).Scan(&count)
if err != nil {
return 0, fmt.Errorf("cannot count trust center files: %w", err)
}
return count, nil
}
func (t *TrustCenterFiles) LoadAllByOrganizationID(
ctx context.Context,
conn pg.Conn,
scope Scoper,
organizationID gid.GID,
) error {
q := `
SELECT
id,
organization_id,
name,
category,
file_id,
trust_center_visibility,
created_at,
updated_at
FROM
trust_center_files
WHERE
%s
AND organization_id = @organization_id
ORDER BY
created_at DESC
`
q = fmt.Sprintf(q, scope.SQLFragment())
args := pgx.StrictNamedArgs{"organization_id": organizationID}
maps.Copy(args, scope.SQLArguments())
rows, err := conn.Query(ctx, q, args)
if err != nil {
return fmt.Errorf("cannot query trust center files: %w", err)
}
files, err := pgx.CollectRows(rows, pgx.RowToAddrOfStructByName[TrustCenterFile])
if err != nil {
return fmt.Errorf("cannot collect trust center files: %w", err)
}
*t = files
return nil
}

View File

@@ -0,0 +1,51 @@
// 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
type (
TrustCenterFileOrderField string
)
const (
TrustCenterFileOrderFieldName TrustCenterFileOrderField = "NAME"
TrustCenterFileOrderFieldCreatedAt TrustCenterFileOrderField = "CREATED_AT"
TrustCenterFileOrderFieldUpdatedAt TrustCenterFileOrderField = "UPDATED_AT"
)
func (p TrustCenterFileOrderField) Column() string {
switch p {
case TrustCenterFileOrderFieldName:
return "name"
case TrustCenterFileOrderFieldCreatedAt:
return "created_at"
case TrustCenterFileOrderFieldUpdatedAt:
return "updated_at"
default:
return string(p)
}
}
func (p TrustCenterFileOrderField) String() string {
return string(p)
}
func (p TrustCenterFileOrderField) MarshalText() ([]byte, error) {
return []byte(p.String()), nil
}
func (p *TrustCenterFileOrderField) UnmarshalText(text []byte) error {
*p = TrustCenterFileOrderField(text)
return nil
}