Migrate audit reports to the files table
Signed-off-by: Ludovic Vielle <ludovic@probo.com>
This commit is contained in:
@@ -34,7 +34,7 @@ type (
|
||||
Name *string `db:"name"`
|
||||
OrganizationID gid.GID `db:"organization_id"`
|
||||
FrameworkID gid.GID `db:"framework_id"`
|
||||
ReportID *gid.GID `db:"report_id"`
|
||||
ReportFileID *gid.GID `db:"report_file_id"`
|
||||
ValidFrom *time.Time `db:"valid_from"`
|
||||
ValidUntil *time.Time `db:"valid_until"`
|
||||
State AuditState `db:"state"`
|
||||
@@ -113,7 +113,7 @@ SELECT
|
||||
name,
|
||||
organization_id,
|
||||
framework_id,
|
||||
report_id,
|
||||
report_file_id,
|
||||
valid_from,
|
||||
valid_until,
|
||||
state,
|
||||
@@ -199,7 +199,7 @@ SELECT
|
||||
name,
|
||||
organization_id,
|
||||
framework_id,
|
||||
report_id,
|
||||
report_file_id,
|
||||
valid_from,
|
||||
valid_until,
|
||||
state,
|
||||
@@ -250,7 +250,7 @@ SELECT
|
||||
name,
|
||||
organization_id,
|
||||
framework_id,
|
||||
report_id,
|
||||
report_file_id,
|
||||
valid_from,
|
||||
valid_until,
|
||||
state,
|
||||
@@ -299,7 +299,7 @@ INSERT INTO audits (
|
||||
tenant_id,
|
||||
organization_id,
|
||||
framework_id,
|
||||
report_id,
|
||||
report_file_id,
|
||||
valid_from,
|
||||
valid_until,
|
||||
state,
|
||||
@@ -312,7 +312,7 @@ INSERT INTO audits (
|
||||
@tenant_id,
|
||||
@organization_id,
|
||||
@framework_id,
|
||||
@report_id,
|
||||
@report_file_id,
|
||||
@valid_from,
|
||||
@valid_until,
|
||||
@state,
|
||||
@@ -328,7 +328,7 @@ INSERT INTO audits (
|
||||
"tenant_id": scope.GetTenantID(),
|
||||
"organization_id": a.OrganizationID,
|
||||
"framework_id": a.FrameworkID,
|
||||
"report_id": a.ReportID,
|
||||
"report_file_id": a.ReportFileID,
|
||||
"valid_from": a.ValidFrom,
|
||||
"valid_until": a.ValidUntil,
|
||||
"state": a.State,
|
||||
@@ -354,7 +354,7 @@ func (a *Audit) Update(
|
||||
UPDATE audits
|
||||
SET
|
||||
name = @name,
|
||||
report_id = @report_id,
|
||||
report_file_id = @report_file_id,
|
||||
valid_from = @valid_from,
|
||||
valid_until = @valid_until,
|
||||
state = @state,
|
||||
@@ -370,7 +370,7 @@ WHERE
|
||||
args := pgx.StrictNamedArgs{
|
||||
"id": a.ID,
|
||||
"name": a.Name,
|
||||
"report_id": a.ReportID,
|
||||
"report_file_id": a.ReportFileID,
|
||||
"valid_from": a.ValidFrom,
|
||||
"valid_until": a.ValidUntil,
|
||||
"state": a.State,
|
||||
@@ -427,7 +427,7 @@ WITH audits_by_control AS (
|
||||
a.name,
|
||||
a.organization_id,
|
||||
a.framework_id,
|
||||
a.report_id,
|
||||
a.report_file_id,
|
||||
a.valid_from,
|
||||
a.valid_until,
|
||||
a.state,
|
||||
@@ -446,7 +446,7 @@ SELECT
|
||||
name,
|
||||
organization_id,
|
||||
framework_id,
|
||||
report_id,
|
||||
report_file_id,
|
||||
valid_from,
|
||||
valid_until,
|
||||
state,
|
||||
@@ -494,7 +494,7 @@ WITH audits_by_finding AS (
|
||||
a.name,
|
||||
a.organization_id,
|
||||
a.framework_id,
|
||||
a.report_id,
|
||||
a.report_file_id,
|
||||
a.valid_from,
|
||||
a.valid_until,
|
||||
a.state,
|
||||
@@ -513,7 +513,7 @@ SELECT
|
||||
name,
|
||||
organization_id,
|
||||
framework_id,
|
||||
report_id,
|
||||
report_file_id,
|
||||
valid_from,
|
||||
valid_until,
|
||||
state,
|
||||
@@ -632,11 +632,11 @@ WHERE
|
||||
return count, nil
|
||||
}
|
||||
|
||||
func (a *Audit) LoadByReportID(
|
||||
func (a *Audit) LoadByReportFileID(
|
||||
ctx context.Context,
|
||||
conn pg.Querier,
|
||||
scope Scoper,
|
||||
reportID gid.GID,
|
||||
fileID gid.GID,
|
||||
) error {
|
||||
q := `
|
||||
SELECT
|
||||
@@ -644,7 +644,7 @@ SELECT
|
||||
name,
|
||||
organization_id,
|
||||
framework_id,
|
||||
report_id,
|
||||
report_file_id,
|
||||
valid_from,
|
||||
valid_until,
|
||||
state,
|
||||
@@ -654,12 +654,12 @@ SELECT
|
||||
FROM
|
||||
audits
|
||||
WHERE %s
|
||||
AND report_id = @report_id
|
||||
AND report_file_id = @report_file_id
|
||||
LIMIT 1;
|
||||
`
|
||||
q = fmt.Sprintf(q, scope.SQLFragment())
|
||||
|
||||
args := pgx.StrictNamedArgs{"report_id": reportID}
|
||||
args := pgx.StrictNamedArgs{"report_file_id": fileID}
|
||||
maps.Copy(args, scope.SQLArguments())
|
||||
|
||||
rows, err := conn.Query(ctx, q, args)
|
||||
@@ -681,6 +681,52 @@ LIMIT 1;
|
||||
return nil
|
||||
}
|
||||
|
||||
func (as *Audits) LoadByReportFileIDs(
|
||||
ctx context.Context,
|
||||
conn pg.Querier,
|
||||
scope Scoper,
|
||||
fileIDs []gid.GID,
|
||||
) error {
|
||||
q := `
|
||||
SELECT
|
||||
id,
|
||||
name,
|
||||
organization_id,
|
||||
framework_id,
|
||||
report_file_id,
|
||||
valid_from,
|
||||
valid_until,
|
||||
state,
|
||||
trust_center_visibility,
|
||||
created_at,
|
||||
updated_at
|
||||
FROM
|
||||
audits
|
||||
WHERE
|
||||
%s
|
||||
AND report_file_id = ANY(@file_ids)
|
||||
`
|
||||
|
||||
q = fmt.Sprintf(q, scope.SQLFragment())
|
||||
|
||||
args := pgx.StrictNamedArgs{"file_ids": fileIDs}
|
||||
maps.Copy(args, scope.SQLArguments())
|
||||
|
||||
rows, err := conn.Query(ctx, q, args)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot query audits by report file IDs: %w", err)
|
||||
}
|
||||
|
||||
audits, err := pgx.CollectRows(rows, pgx.RowToAddrOfStructByName[Audit])
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot collect audits by report file IDs: %w", err)
|
||||
}
|
||||
|
||||
*as = audits
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (as *Audits) LoadByReportIDs(
|
||||
ctx context.Context,
|
||||
conn pg.Querier,
|
||||
|
||||
@@ -53,8 +53,6 @@ func ResourceTypeName(entityType uint16) string {
|
||||
return "Datum"
|
||||
case AuditEntityType:
|
||||
return "Audit"
|
||||
case ReportEntityType:
|
||||
return "Report"
|
||||
case TrustCenterEntityType:
|
||||
return "TrustCenter"
|
||||
case TrustCenterAccessEntityType:
|
||||
|
||||
@@ -44,7 +44,7 @@ const (
|
||||
AssetEntityType uint16 = 18
|
||||
DatumEntityType uint16 = 19
|
||||
AuditEntityType uint16 = 20
|
||||
ReportEntityType uint16 = 21
|
||||
_ uint16 = 21 // ReportEntityType - removed
|
||||
TrustCenterEntityType uint16 = 22
|
||||
TrustCenterAccessEntityType uint16 = 23
|
||||
ThirdPartyBusinessAssociateAgreementEntityType uint16 = 24
|
||||
@@ -168,8 +168,6 @@ func NewEntityFromID(id gid.GID) (any, bool) {
|
||||
return &Datum{ID: id}, true
|
||||
case AuditEntityType:
|
||||
return &Audit{ID: id}, true
|
||||
case ReportEntityType:
|
||||
return &Report{ID: id}, true
|
||||
case TrustCenterEntityType:
|
||||
return &TrustCenter{ID: id}, true
|
||||
case TrustCenterAccessEntityType:
|
||||
|
||||
@@ -157,6 +157,10 @@ LIMIT 1;
|
||||
return nil
|
||||
}
|
||||
|
||||
// LoadByIDs Loads every given files, whether they are active or not. See
|
||||
// Files.LoadActiveByIDs for a safer option.
|
||||
//
|
||||
// DISCLAIMER: use with caution on user-facing features.
|
||||
func (f *Files) LoadByIDs(
|
||||
ctx context.Context,
|
||||
conn pg.Querier,
|
||||
@@ -269,6 +273,59 @@ VALUES (
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *File) LoadActiveByID(
|
||||
ctx context.Context,
|
||||
conn pg.Querier,
|
||||
scope Scoper,
|
||||
fileID gid.GID,
|
||||
) error {
|
||||
q := `
|
||||
SELECT
|
||||
id,
|
||||
organization_id,
|
||||
bucket_name,
|
||||
mime_type,
|
||||
file_name,
|
||||
file_key,
|
||||
file_size,
|
||||
visibility,
|
||||
created_at,
|
||||
updated_at,
|
||||
deleted_at
|
||||
FROM
|
||||
files
|
||||
WHERE
|
||||
%s
|
||||
AND id = @file_id
|
||||
AND deleted_at IS NULL
|
||||
LIMIT 1;
|
||||
`
|
||||
|
||||
q = fmt.Sprintf(q, scope.SQLFragment())
|
||||
|
||||
args := pgx.StrictNamedArgs{"file_id": fileID}
|
||||
maps.Copy(args, scope.SQLArguments())
|
||||
|
||||
rows, err := conn.Query(ctx, q, args)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot query file: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
file, err := pgx.CollectExactlyOneRow(rows, pgx.RowToStructByName[File])
|
||||
if err != nil {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return ErrResourceNotFound
|
||||
}
|
||||
|
||||
return fmt.Errorf("cannot collect file: %w", err)
|
||||
}
|
||||
|
||||
*f = file
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *File) LoadPublicByID(
|
||||
ctx context.Context,
|
||||
conn pg.Querier,
|
||||
|
||||
@@ -1,295 +0,0 @@
|
||||
// Copyright (c) 2025-2026 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"
|
||||
"errors"
|
||||
"fmt"
|
||||
"maps"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"go.gearno.de/kit/pg"
|
||||
"go.probo.inc/probo/pkg/gid"
|
||||
"go.probo.inc/probo/pkg/iam/policy"
|
||||
"go.probo.inc/probo/pkg/page"
|
||||
)
|
||||
|
||||
type (
|
||||
Report struct {
|
||||
ID gid.GID `db:"id"`
|
||||
OrganizationID gid.GID `db:"organization_id"`
|
||||
ObjectKey string `db:"object_key"`
|
||||
MimeType string `db:"mime_type"`
|
||||
Filename string `db:"filename"`
|
||||
Size int64 `db:"size"`
|
||||
CreatedAt time.Time `db:"created_at"`
|
||||
UpdatedAt time.Time `db:"updated_at"`
|
||||
}
|
||||
|
||||
Reports []*Report
|
||||
)
|
||||
|
||||
func (r *Report) AuthorizationAttributes(
|
||||
ctx context.Context,
|
||||
conn pg.Querier,
|
||||
resourceIDs []gid.GID,
|
||||
) (policy.AttributesByID, error) {
|
||||
q := `SELECT id, organization_id FROM reports WHERE id = ANY(@resource_ids::text[])`
|
||||
|
||||
args := pgx.StrictNamedArgs{
|
||||
"resource_ids": resourceIDs,
|
||||
}
|
||||
|
||||
rows, err := conn.Query(ctx, q, args)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot query authorization attributes: %w", err)
|
||||
}
|
||||
|
||||
defer rows.Close()
|
||||
|
||||
attrsByID := make(policy.AttributesByID)
|
||||
|
||||
for rows.Next() {
|
||||
var id, organizationID gid.GID
|
||||
|
||||
if err := rows.Scan(&id, &organizationID); err != nil {
|
||||
return nil, fmt.Errorf("cannot scan authorization attributes: %w", err)
|
||||
}
|
||||
|
||||
attrsByID[id] = policy.Attributes{
|
||||
"organization_id": organizationID.String(),
|
||||
}
|
||||
}
|
||||
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, fmt.Errorf("cannot iterate authorization attributes: %w", err)
|
||||
}
|
||||
|
||||
return attrsByID, nil
|
||||
}
|
||||
|
||||
func (r *Report) LoadByID(
|
||||
ctx context.Context,
|
||||
conn pg.Querier,
|
||||
scope Scoper,
|
||||
reportID gid.GID,
|
||||
) error {
|
||||
q := `
|
||||
SELECT
|
||||
id,
|
||||
organization_id,
|
||||
object_key,
|
||||
mime_type,
|
||||
filename,
|
||||
size,
|
||||
created_at,
|
||||
updated_at
|
||||
FROM
|
||||
reports
|
||||
WHERE
|
||||
%s
|
||||
AND id = @report_id
|
||||
LIMIT 1;
|
||||
`
|
||||
|
||||
q = fmt.Sprintf(q, scope.SQLFragment())
|
||||
|
||||
args := pgx.StrictNamedArgs{"report_id": reportID}
|
||||
maps.Copy(args, scope.SQLArguments())
|
||||
|
||||
rows, err := conn.Query(ctx, q, args)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot query report: %w", err)
|
||||
}
|
||||
|
||||
report, err := pgx.CollectExactlyOneRow(rows, pgx.RowToStructByName[Report])
|
||||
if err != nil {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return ErrResourceNotFound
|
||||
}
|
||||
|
||||
return fmt.Errorf("cannot collect report: %w", err)
|
||||
}
|
||||
|
||||
*r = report
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Reports) LoadByIDs(
|
||||
ctx context.Context,
|
||||
conn pg.Querier,
|
||||
scope Scoper,
|
||||
reportIDs []gid.GID,
|
||||
) error {
|
||||
q := `
|
||||
SELECT
|
||||
id,
|
||||
organization_id,
|
||||
object_key,
|
||||
mime_type,
|
||||
filename,
|
||||
size,
|
||||
created_at,
|
||||
updated_at
|
||||
FROM
|
||||
reports
|
||||
WHERE
|
||||
%s
|
||||
AND id = ANY(@report_ids);
|
||||
`
|
||||
|
||||
q = fmt.Sprintf(q, scope.SQLFragment())
|
||||
|
||||
args := pgx.StrictNamedArgs{"report_ids": reportIDs}
|
||||
maps.Copy(args, scope.SQLArguments())
|
||||
|
||||
rows, err := conn.Query(ctx, q, args)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot query report: %w", err)
|
||||
}
|
||||
|
||||
reports, err := pgx.CollectRows(rows, pgx.RowToAddrOfStructByName[Report])
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot collect reports: %w", err)
|
||||
}
|
||||
|
||||
*r = reports
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Report) Insert(
|
||||
ctx context.Context,
|
||||
conn pg.Tx,
|
||||
scope Scoper,
|
||||
) error {
|
||||
q := `
|
||||
INSERT INTO reports (
|
||||
id,
|
||||
tenant_id,
|
||||
organization_id,
|
||||
object_key,
|
||||
mime_type,
|
||||
filename,
|
||||
size,
|
||||
created_at,
|
||||
updated_at
|
||||
) VALUES (
|
||||
@id,
|
||||
@tenant_id,
|
||||
@organization_id,
|
||||
@object_key,
|
||||
@mime_type,
|
||||
@filename,
|
||||
@size,
|
||||
@created_at,
|
||||
@updated_at
|
||||
)
|
||||
`
|
||||
|
||||
args := pgx.StrictNamedArgs{
|
||||
"id": r.ID,
|
||||
"tenant_id": scope.GetTenantID(),
|
||||
"organization_id": r.OrganizationID,
|
||||
"object_key": r.ObjectKey,
|
||||
"mime_type": r.MimeType,
|
||||
"filename": r.Filename,
|
||||
"size": r.Size,
|
||||
"created_at": r.CreatedAt,
|
||||
"updated_at": r.UpdatedAt,
|
||||
}
|
||||
|
||||
_, err := conn.Exec(ctx, q, args)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot insert report: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Report) Update(
|
||||
ctx context.Context,
|
||||
conn pg.Tx,
|
||||
scope Scoper,
|
||||
) error {
|
||||
q := `
|
||||
UPDATE reports
|
||||
SET
|
||||
object_key = @object_key,
|
||||
mime_type = @mime_type,
|
||||
filename = @filename,
|
||||
size = @size,
|
||||
updated_at = @updated_at
|
||||
WHERE
|
||||
%s
|
||||
AND id = @id
|
||||
`
|
||||
|
||||
q = fmt.Sprintf(q, scope.SQLFragment())
|
||||
|
||||
args := pgx.StrictNamedArgs{
|
||||
"id": r.ID,
|
||||
"object_key": r.ObjectKey,
|
||||
"mime_type": r.MimeType,
|
||||
"filename": r.Filename,
|
||||
"size": r.Size,
|
||||
"updated_at": r.UpdatedAt,
|
||||
}
|
||||
maps.Copy(args, scope.SQLArguments())
|
||||
|
||||
_, err := conn.Exec(ctx, q, args)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot update report: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Report) Delete(
|
||||
ctx context.Context,
|
||||
conn pg.Tx,
|
||||
scope Scoper,
|
||||
) error {
|
||||
q := `
|
||||
DELETE FROM reports
|
||||
WHERE
|
||||
%s
|
||||
AND id = @id
|
||||
`
|
||||
|
||||
q = fmt.Sprintf(q, scope.SQLFragment())
|
||||
|
||||
args := pgx.StrictNamedArgs{"id": r.ID}
|
||||
maps.Copy(args, scope.SQLArguments())
|
||||
|
||||
_, err := conn.Exec(ctx, q, args)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot delete report: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Report) CursorKey(orderBy ReportOrderField) page.CursorKey {
|
||||
switch orderBy {
|
||||
case ReportOrderFieldID:
|
||||
return page.NewCursorKey(r.ID, r.ID)
|
||||
}
|
||||
|
||||
panic(fmt.Sprintf("unsupported order by: %s", orderBy))
|
||||
}
|
||||
@@ -35,7 +35,7 @@ type (
|
||||
OrganizationID gid.GID `db:"organization_id"`
|
||||
TrustCenterAccessID gid.GID `db:"trust_center_access_id"`
|
||||
DocumentID *gid.GID `db:"document_id"`
|
||||
ReportID *gid.GID `db:"report_id"`
|
||||
ReportFileID *gid.GID `db:"report_file_id"`
|
||||
TrustCenterFileID *gid.GID `db:"trust_center_file_id"`
|
||||
Status TrustCenterDocumentAccessStatus `db:"status"`
|
||||
CreatedAt time.Time `db:"created_at"`
|
||||
@@ -105,7 +105,7 @@ SELECT
|
||||
organization_id,
|
||||
trust_center_access_id,
|
||||
document_id,
|
||||
report_id,
|
||||
report_file_id,
|
||||
trust_center_file_id,
|
||||
status,
|
||||
created_at,
|
||||
@@ -155,7 +155,7 @@ SELECT
|
||||
organization_id,
|
||||
trust_center_access_id,
|
||||
document_id,
|
||||
report_id,
|
||||
report_file_id,
|
||||
trust_center_file_id,
|
||||
status,
|
||||
created_at,
|
||||
@@ -196,12 +196,12 @@ LIMIT 1;
|
||||
return nil
|
||||
}
|
||||
|
||||
func (tcda *TrustCenterDocumentAccess) LoadByTrustCenterAccessIDAndReportID(
|
||||
func (tcda *TrustCenterDocumentAccess) LoadByTrustCenterAccessIDAndReportFileID(
|
||||
ctx context.Context,
|
||||
conn pg.Querier,
|
||||
scope Scoper,
|
||||
trustCenterAccessID gid.GID,
|
||||
reportID gid.GID,
|
||||
reportFileID gid.GID,
|
||||
) error {
|
||||
q := `
|
||||
SELECT
|
||||
@@ -209,7 +209,7 @@ SELECT
|
||||
organization_id,
|
||||
trust_center_access_id,
|
||||
document_id,
|
||||
report_id,
|
||||
report_file_id,
|
||||
trust_center_file_id,
|
||||
status,
|
||||
created_at,
|
||||
@@ -219,7 +219,7 @@ FROM
|
||||
WHERE
|
||||
%s
|
||||
AND trust_center_access_id = @trust_center_access_id
|
||||
AND report_id = @report_id
|
||||
AND report_file_id = @report_file_id
|
||||
LIMIT 1;
|
||||
`
|
||||
|
||||
@@ -227,7 +227,7 @@ LIMIT 1;
|
||||
|
||||
args := pgx.StrictNamedArgs{
|
||||
"trust_center_access_id": trustCenterAccessID,
|
||||
"report_id": reportID,
|
||||
"report_file_id": reportFileID,
|
||||
}
|
||||
maps.Copy(args, scope.SQLArguments())
|
||||
|
||||
@@ -262,7 +262,7 @@ INSERT INTO trust_center_document_accesses (
|
||||
organization_id,
|
||||
trust_center_access_id,
|
||||
document_id,
|
||||
report_id,
|
||||
report_file_id,
|
||||
trust_center_file_id,
|
||||
status,
|
||||
created_at,
|
||||
@@ -273,7 +273,7 @@ INSERT INTO trust_center_document_accesses (
|
||||
@organization_id,
|
||||
@trust_center_access_id,
|
||||
@document_id,
|
||||
@report_id,
|
||||
@report_file_id,
|
||||
@trust_center_file_id,
|
||||
@status::trust_center_document_access_status,
|
||||
@created_at,
|
||||
@@ -287,7 +287,7 @@ INSERT INTO trust_center_document_accesses (
|
||||
"organization_id": tcda.OrganizationID,
|
||||
"trust_center_access_id": tcda.TrustCenterAccessID,
|
||||
"document_id": tcda.DocumentID,
|
||||
"report_id": tcda.ReportID,
|
||||
"report_file_id": tcda.ReportFileID,
|
||||
"trust_center_file_id": tcda.TrustCenterFileID,
|
||||
"status": tcda.Status,
|
||||
"created_at": tcda.CreatedAt,
|
||||
@@ -300,7 +300,7 @@ INSERT INTO trust_center_document_accesses (
|
||||
if pgErr.Code == "23505" {
|
||||
switch pgErr.ConstraintName {
|
||||
case "trust_center_document_accesse_trust_center_access_id_docume_key",
|
||||
"trust_center_document_accesse_trust_center_access_id_report_key",
|
||||
"trust_center_document_accesses_trust_center_access_id_report_file_key",
|
||||
"trust_center_document_accesses_trust_center_file_id_key":
|
||||
return ErrResourceAlreadyExists
|
||||
}
|
||||
@@ -496,7 +496,7 @@ all_items AS (
|
||||
SELECT
|
||||
d.id AS item_id,
|
||||
d.id AS document_id,
|
||||
NULL::text AS report_id,
|
||||
NULL::text AS report_file_id,
|
||||
NULL::text AS trust_center_file_id,
|
||||
d.created_at AS item_created_at,
|
||||
d.updated_at AS item_updated_at
|
||||
@@ -508,23 +508,23 @@ all_items AS (
|
||||
UNION ALL
|
||||
|
||||
SELECT
|
||||
r.report_id AS item_id,
|
||||
r.report_file_id AS item_id,
|
||||
NULL::text AS document_id,
|
||||
r.report_id AS report_id,
|
||||
r.report_file_id AS report_file_id,
|
||||
NULL::text AS trust_center_file_id,
|
||||
r.created_at AS item_created_at,
|
||||
r.updated_at AS item_updated_at
|
||||
FROM audits r, tenant_organization o
|
||||
WHERE r.organization_id = o.organization_id
|
||||
AND r.trust_center_visibility = 'PRIVATE'::trust_center_visibility
|
||||
AND r.report_id IS NOT NULL
|
||||
AND r.report_file_id IS NOT NULL
|
||||
|
||||
UNION ALL
|
||||
|
||||
SELECT
|
||||
tcf.id AS item_id,
|
||||
NULL::text AS document_id,
|
||||
NULL::text AS report_id,
|
||||
NULL::text AS report_file_id,
|
||||
tcf.id AS trust_center_file_id,
|
||||
tcf.created_at AS item_created_at,
|
||||
tcf.updated_at AS item_updated_at
|
||||
@@ -542,7 +542,7 @@ final_items AS (
|
||||
(SELECT organization_id FROM organization) AS organization_id,
|
||||
@trust_center_access_id AS trust_center_access_id,
|
||||
ai.document_id,
|
||||
ai.report_id,
|
||||
ai.report_file_id,
|
||||
ai.trust_center_file_id,
|
||||
COALESCE(tcda.status, 'REQUESTED'::trust_center_document_access_status) AS status,
|
||||
COALESCE(tcda.created_at, ai.item_created_at) AS created_at,
|
||||
@@ -552,7 +552,7 @@ final_items AS (
|
||||
tcda.trust_center_access_id = @trust_center_access_id
|
||||
AND (
|
||||
(tcda.document_id = ai.document_id AND ai.document_id IS NOT NULL)
|
||||
OR (tcda.report_id = ai.report_id AND ai.report_id IS NOT NULL)
|
||||
OR (tcda.report_file_id = ai.report_file_id AND ai.report_file_id IS NOT NULL)
|
||||
OR (tcda.trust_center_file_id = ai.trust_center_file_id AND ai.trust_center_file_id IS NOT NULL)
|
||||
)
|
||||
)
|
||||
@@ -562,7 +562,7 @@ SELECT
|
||||
organization_id,
|
||||
trust_center_access_id,
|
||||
document_id,
|
||||
report_id,
|
||||
report_file_id,
|
||||
trust_center_file_id,
|
||||
status,
|
||||
created_at,
|
||||
@@ -606,7 +606,7 @@ SELECT
|
||||
organization_id,
|
||||
trust_center_access_id,
|
||||
document_id,
|
||||
report_id,
|
||||
report_file_id,
|
||||
trust_center_file_id,
|
||||
status,
|
||||
created_at,
|
||||
@@ -714,12 +714,12 @@ WHERE
|
||||
return nil
|
||||
}
|
||||
|
||||
func GrantByReportIDs(
|
||||
func GrantByReportFileIDs(
|
||||
ctx context.Context,
|
||||
conn pg.Querier,
|
||||
scope Scoper,
|
||||
trustCenterAccessID gid.GID,
|
||||
reportIDs []gid.GID,
|
||||
reportFileIDs []gid.GID,
|
||||
updatedAt time.Time,
|
||||
) error {
|
||||
q := `
|
||||
@@ -728,32 +728,32 @@ SET status = 'GRANTED'::trust_center_document_access_status, updated_at = @updat
|
||||
WHERE
|
||||
%s
|
||||
AND trust_center_access_id = @trust_center_access_id
|
||||
AND report_id = ANY(@report_ids)
|
||||
AND report_file_id = ANY(@report_file_ids)
|
||||
`
|
||||
|
||||
q = fmt.Sprintf(q, scope.SQLFragment())
|
||||
|
||||
args := pgx.StrictNamedArgs{
|
||||
"trust_center_access_id": trustCenterAccessID,
|
||||
"report_ids": reportIDs,
|
||||
"report_file_ids": reportFileIDs,
|
||||
"updated_at": updatedAt,
|
||||
}
|
||||
maps.Copy(args, scope.SQLArguments())
|
||||
|
||||
_, err := conn.Exec(ctx, q, args)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot grant trust center document accesses by report IDs: %w", err)
|
||||
return fmt.Errorf("cannot grant trust center document accesses by report file IDs: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func RejectOrRevokeByReportIDs(
|
||||
func RejectOrRevokeByReportFileIDs(
|
||||
ctx context.Context,
|
||||
conn pg.Querier,
|
||||
scope Scoper,
|
||||
trustCenterAccessID gid.GID,
|
||||
reportIDs []gid.GID,
|
||||
reportFileIDs []gid.GID,
|
||||
updatedAt time.Time,
|
||||
) error {
|
||||
q := `
|
||||
@@ -767,21 +767,21 @@ SET
|
||||
WHERE
|
||||
%s
|
||||
AND trust_center_access_id = @trust_center_access_id
|
||||
AND report_id = ANY(@report_ids)
|
||||
AND report_file_id = ANY(@report_file_ids)
|
||||
`
|
||||
|
||||
q = fmt.Sprintf(q, scope.SQLFragment())
|
||||
|
||||
args := pgx.StrictNamedArgs{
|
||||
"trust_center_access_id": trustCenterAccessID,
|
||||
"report_ids": reportIDs,
|
||||
"report_file_ids": reportFileIDs,
|
||||
"updated_at": updatedAt,
|
||||
}
|
||||
maps.Copy(args, scope.SQLArguments())
|
||||
|
||||
_, err := conn.Exec(ctx, q, args)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot reject trust center document accesses by report IDs: %w", err)
|
||||
return fmt.Errorf("cannot reject trust center document accesses by report file IDs: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
@@ -829,7 +829,7 @@ WHEN NOT MATCHED
|
||||
organization_id,
|
||||
trust_center_access_id,
|
||||
document_id,
|
||||
report_id,
|
||||
report_file_id,
|
||||
trust_center_file_id,
|
||||
status,
|
||||
created_at,
|
||||
@@ -887,7 +887,7 @@ WITH document_access_data AS (
|
||||
@organization_id AS organization_id,
|
||||
@trust_center_access_id AS trust_center_access_id,
|
||||
unnest(@document_ids::text[]) AS document_id,
|
||||
null::text AS report_id,
|
||||
null::text AS report_file_id,
|
||||
null::text AS trust_center_file_id,
|
||||
@status::trust_center_document_access_status AS status,
|
||||
@created_at::timestamptz AS created_at,
|
||||
@@ -899,7 +899,7 @@ INSERT INTO trust_center_document_accesses (
|
||||
organization_id,
|
||||
trust_center_access_id,
|
||||
document_id,
|
||||
report_id,
|
||||
report_file_id,
|
||||
trust_center_file_id,
|
||||
status,
|
||||
created_at,
|
||||
@@ -927,7 +927,7 @@ ON CONFLICT DO NOTHING
|
||||
return nil
|
||||
}
|
||||
|
||||
func (tcdas TrustCenterDocumentAccesses) MergeReportAccesses(
|
||||
func (tcdas TrustCenterDocumentAccesses) MergeReportFileAccesses(
|
||||
ctx context.Context,
|
||||
conn pg.Querier,
|
||||
scope Scoper,
|
||||
@@ -947,7 +947,7 @@ WITH data AS (
|
||||
)
|
||||
MERGE INTO trust_center_document_accesses AS tcda
|
||||
USING data
|
||||
ON data.id = tcda.report_id
|
||||
ON data.id = tcda.report_file_id
|
||||
AND tcda.tenant_id = @tenant_id
|
||||
AND tcda.trust_center_access_id = @trust_center_access_id
|
||||
WHEN MATCHED
|
||||
@@ -955,7 +955,7 @@ WHEN MATCHED
|
||||
WHEN NOT MATCHED BY SOURCE
|
||||
AND tcda.tenant_id = @tenant_id
|
||||
AND tcda.trust_center_access_id = @trust_center_access_id
|
||||
AND tcda.report_id IS NOT NULL
|
||||
AND tcda.report_file_id IS NOT NULL
|
||||
THEN DELETE
|
||||
WHEN NOT MATCHED
|
||||
THEN INSERT (
|
||||
@@ -964,7 +964,7 @@ WHEN NOT MATCHED
|
||||
organization_id,
|
||||
trust_center_access_id,
|
||||
document_id,
|
||||
report_id,
|
||||
report_file_id,
|
||||
trust_center_file_id,
|
||||
status,
|
||||
created_at,
|
||||
@@ -1000,29 +1000,29 @@ WHEN NOT MATCHED
|
||||
return nil
|
||||
}
|
||||
|
||||
func (tcdas TrustCenterDocumentAccesses) BulkInsertReportAccesses(
|
||||
func (tcdas TrustCenterDocumentAccesses) BulkInsertReportFileAccesses(
|
||||
ctx context.Context,
|
||||
conn pg.Querier,
|
||||
scope Scoper,
|
||||
trustCenterAccessID gid.GID,
|
||||
organizationID gid.GID,
|
||||
reportIDs []gid.GID,
|
||||
reportFileIDs []gid.GID,
|
||||
status TrustCenterDocumentAccessStatus,
|
||||
createdAt time.Time,
|
||||
) error {
|
||||
if len(reportIDs) == 0 {
|
||||
if len(reportFileIDs) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
q := `
|
||||
WITH report_access_data AS (
|
||||
WITH report_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,
|
||||
@organization_id AS organization_id,
|
||||
@trust_center_access_id AS trust_center_access_id,
|
||||
null::text AS document_id,
|
||||
unnest(@report_ids::text[]) AS report_id,
|
||||
unnest(@report_file_ids::text[]) AS report_file_id,
|
||||
null::text AS trust_center_file_id,
|
||||
@status::trust_center_document_access_status AS status,
|
||||
@created_at::timestamptz AS created_at,
|
||||
@@ -1034,13 +1034,13 @@ INSERT INTO trust_center_document_accesses (
|
||||
organization_id,
|
||||
trust_center_access_id,
|
||||
document_id,
|
||||
report_id,
|
||||
report_file_id,
|
||||
trust_center_file_id,
|
||||
status,
|
||||
created_at,
|
||||
updated_at
|
||||
)
|
||||
SELECT * FROM report_access_data
|
||||
SELECT * FROM report_file_access_data
|
||||
ON CONFLICT DO NOTHING
|
||||
`
|
||||
|
||||
@@ -1049,14 +1049,14 @@ ON CONFLICT DO NOTHING
|
||||
"organization_id": organizationID,
|
||||
"trust_center_document_access_entity_type": TrustCenterDocumentAccessEntityType,
|
||||
"trust_center_access_id": trustCenterAccessID,
|
||||
"report_ids": reportIDs,
|
||||
"report_file_ids": reportFileIDs,
|
||||
"status": status,
|
||||
"created_at": createdAt,
|
||||
"updated_at": createdAt,
|
||||
}
|
||||
|
||||
if _, err := conn.Exec(ctx, q, args); err != nil {
|
||||
return fmt.Errorf("cannot bulk insert trust center report accesses: %w", err)
|
||||
return fmt.Errorf("cannot bulk insert trust center report file accesses: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
@@ -1075,7 +1075,7 @@ SELECT
|
||||
organization_id,
|
||||
trust_center_access_id,
|
||||
document_id,
|
||||
report_id,
|
||||
report_file_id,
|
||||
trust_center_file_id,
|
||||
status,
|
||||
created_at,
|
||||
@@ -1226,7 +1226,7 @@ WHEN NOT MATCHED
|
||||
organization_id,
|
||||
trust_center_access_id,
|
||||
document_id,
|
||||
report_id,
|
||||
report_file_id,
|
||||
trust_center_file_id,
|
||||
status,
|
||||
created_at,
|
||||
@@ -1280,7 +1280,7 @@ WITH trust_center_file_access_data AS (
|
||||
@organization_id AS organization_id,
|
||||
@trust_center_access_id AS trust_center_access_id,
|
||||
null::text AS document_id,
|
||||
null::text AS report_id,
|
||||
null::text AS report_file_id,
|
||||
unnest(@trust_center_file_ids::text[]) AS trust_center_file_id,
|
||||
@status::trust_center_document_access_status AS status,
|
||||
@created_at::timestamptz AS created_at,
|
||||
@@ -1292,7 +1292,7 @@ INSERT INTO trust_center_document_accesses (
|
||||
organization_id,
|
||||
trust_center_access_id,
|
||||
document_id,
|
||||
report_id,
|
||||
report_file_id,
|
||||
trust_center_file_id,
|
||||
status,
|
||||
created_at,
|
||||
|
||||
Reference in New Issue
Block a user