@@ -33,11 +33,17 @@ type (
|
||||
OrganizationID gid.GID `db:"organization_id"`
|
||||
OwnerID gid.GID `db:"owner_id"`
|
||||
DataClassification DataClassification `db:"data_classification"`
|
||||
SnapshotID *gid.GID `db:"snapshot_id"`
|
||||
SourceID *gid.GID `db:"source_id"`
|
||||
CreatedAt time.Time `db:"created_at"`
|
||||
UpdatedAt time.Time `db:"updated_at"`
|
||||
}
|
||||
|
||||
Data []*Datum
|
||||
|
||||
DataSnapshotter interface {
|
||||
InsertDataSnapshots(ctx context.Context, conn pg.Conn, scope Scoper, organizationID, snapshotID gid.GID) error
|
||||
}
|
||||
)
|
||||
|
||||
func (d *Datum) CursorKey(field DatumOrderField) page.CursorKey {
|
||||
@@ -66,6 +72,8 @@ SELECT
|
||||
owner_id,
|
||||
organization_id,
|
||||
data_classification,
|
||||
snapshot_id,
|
||||
source_id,
|
||||
created_at,
|
||||
updated_at
|
||||
FROM
|
||||
@@ -108,6 +116,8 @@ SELECT
|
||||
owner_id,
|
||||
organization_id,
|
||||
data_classification,
|
||||
snapshot_id,
|
||||
source_id,
|
||||
created_at,
|
||||
updated_at
|
||||
FROM
|
||||
@@ -176,6 +186,7 @@ func (d *Data) LoadByOrganizationID(
|
||||
scope Scoper,
|
||||
organizationID gid.GID,
|
||||
cursor *page.Cursor[DatumOrderField],
|
||||
filter *DatumFilter,
|
||||
) error {
|
||||
q := `
|
||||
SELECT
|
||||
@@ -184,6 +195,8 @@ SELECT
|
||||
organization_id,
|
||||
owner_id,
|
||||
data_classification,
|
||||
snapshot_id,
|
||||
source_id,
|
||||
created_at,
|
||||
updated_at
|
||||
FROM
|
||||
@@ -192,12 +205,14 @@ WHERE
|
||||
%s
|
||||
AND organization_id = @organization_id
|
||||
AND %s
|
||||
AND %s
|
||||
`
|
||||
|
||||
q = fmt.Sprintf(q, scope.SQLFragment(), cursor.SQLFragment())
|
||||
q = fmt.Sprintf(q, scope.SQLFragment(), filter.SQLFragment(), cursor.SQLFragment())
|
||||
|
||||
args := pgx.StrictNamedArgs{"organization_id": organizationID}
|
||||
maps.Copy(args, scope.SQLArguments())
|
||||
maps.Copy(args, filter.SQLArguments())
|
||||
maps.Copy(args, cursor.SQLArguments())
|
||||
|
||||
rows, err := conn.Query(ctx, q, args)
|
||||
@@ -228,6 +243,8 @@ INSERT INTO data (
|
||||
owner_id,
|
||||
organization_id,
|
||||
data_classification,
|
||||
snapshot_id,
|
||||
source_id,
|
||||
created_at,
|
||||
updated_at
|
||||
) VALUES (
|
||||
@@ -237,6 +254,8 @@ INSERT INTO data (
|
||||
@owner_id,
|
||||
@organization_id,
|
||||
@data_classification,
|
||||
@snapshot_id,
|
||||
@source_id,
|
||||
@created_at,
|
||||
@updated_at
|
||||
)
|
||||
@@ -249,6 +268,8 @@ INSERT INTO data (
|
||||
"owner_id": d.OwnerID,
|
||||
"organization_id": d.OrganizationID,
|
||||
"data_classification": d.DataClassification,
|
||||
"snapshot_id": d.SnapshotID,
|
||||
"source_id": d.SourceID,
|
||||
"created_at": d.CreatedAt,
|
||||
"updated_at": d.UpdatedAt,
|
||||
}
|
||||
@@ -276,12 +297,15 @@ SET
|
||||
WHERE
|
||||
%s
|
||||
AND id = @id
|
||||
AND snapshot_id IS NULL
|
||||
RETURNING
|
||||
id,
|
||||
name,
|
||||
owner_id,
|
||||
organization_id,
|
||||
data_classification,
|
||||
snapshot_id,
|
||||
source_id,
|
||||
created_at,
|
||||
updated_at
|
||||
`
|
||||
@@ -322,6 +346,7 @@ DELETE FROM data
|
||||
WHERE
|
||||
%s
|
||||
AND id = @id
|
||||
AND snapshot_id IS NULL
|
||||
`
|
||||
|
||||
q = fmt.Sprintf(q, scope.SQLFragment())
|
||||
@@ -336,3 +361,73 @@ WHERE
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d Data) Snapshot(ctx context.Context, conn pg.Conn, scope Scoper, organizationID, snapshotID gid.GID) error {
|
||||
snapshotters := []DataSnapshotter{Data{}, Vendors{}, DatumVendors{}}
|
||||
|
||||
for _, snapshotter := range snapshotters {
|
||||
if err := snapshotter.InsertDataSnapshots(ctx, conn, scope, organizationID, snapshotID); err != nil {
|
||||
return fmt.Errorf("cannot create data snapshots: (%T) %w", snapshotter, err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d Data) InsertDataSnapshots(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
scope Scoper,
|
||||
organizationID gid.GID,
|
||||
snapshotID gid.GID,
|
||||
) error {
|
||||
query := `
|
||||
WITH
|
||||
source_data AS (
|
||||
SELECT *
|
||||
FROM data
|
||||
WHERE %s AND organization_id = @organization_id AND snapshot_id IS NULL
|
||||
)
|
||||
INSERT INTO data (
|
||||
tenant_id,
|
||||
id,
|
||||
snapshot_id,
|
||||
source_id,
|
||||
name,
|
||||
organization_id,
|
||||
owner_id,
|
||||
data_classification,
|
||||
created_at,
|
||||
updated_at
|
||||
)
|
||||
SELECT
|
||||
@tenant_id,
|
||||
generate_gid(decode_base64_unpadded(@tenant_id), @datum_entity_type),
|
||||
@snapshot_id,
|
||||
d.id,
|
||||
d.name,
|
||||
d.organization_id,
|
||||
d.owner_id,
|
||||
d.data_classification,
|
||||
d.created_at,
|
||||
d.updated_at
|
||||
FROM source_data d
|
||||
`
|
||||
|
||||
query = fmt.Sprintf(query, scope.SQLFragment())
|
||||
|
||||
args := pgx.StrictNamedArgs{
|
||||
"tenant_id": scope.GetTenantID(),
|
||||
"snapshot_id": snapshotID,
|
||||
"organization_id": organizationID,
|
||||
"datum_entity_type": DatumEntityType,
|
||||
}
|
||||
maps.Copy(args, scope.SQLArguments())
|
||||
|
||||
_, err := conn.Exec(ctx, query, args)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot insert data snapshots: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
60
pkg/coredata/datum_filter.go
Normal file
60
pkg/coredata/datum_filter.go
Normal 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 (
|
||||
"github.com/getprobo/probo/pkg/gid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
)
|
||||
|
||||
type (
|
||||
DatumFilter struct {
|
||||
snapshotID **gid.GID
|
||||
}
|
||||
)
|
||||
|
||||
func NewDatumFilter() *DatumFilter {
|
||||
return &DatumFilter{
|
||||
snapshotID: nil,
|
||||
}
|
||||
}
|
||||
|
||||
func NewDatumFilterBySnapshotID(snapshotID **gid.GID) *DatumFilter {
|
||||
return &DatumFilter{
|
||||
snapshotID: snapshotID,
|
||||
}
|
||||
}
|
||||
|
||||
func (f *DatumFilter) SQLArguments() pgx.NamedArgs {
|
||||
args := pgx.NamedArgs{}
|
||||
|
||||
if f.snapshotID != nil && *f.snapshotID != nil {
|
||||
args["filter_snapshot_id"] = **f.snapshotID
|
||||
}
|
||||
|
||||
return args
|
||||
}
|
||||
|
||||
func (f *DatumFilter) SQLFragment() string {
|
||||
if f.snapshotID == nil {
|
||||
return "TRUE"
|
||||
}
|
||||
|
||||
if *f.snapshotID == nil {
|
||||
return "snapshot_id IS NULL"
|
||||
} else {
|
||||
return "snapshot_id = @filter_snapshot_id"
|
||||
}
|
||||
}
|
||||
@@ -17,6 +17,7 @@ package coredata
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"maps"
|
||||
"time"
|
||||
|
||||
"github.com/getprobo/probo/pkg/gid"
|
||||
@@ -26,10 +27,10 @@ import (
|
||||
|
||||
type (
|
||||
DatumVendor struct {
|
||||
DatumID gid.GID `db:"datum_id"`
|
||||
VendorID gid.GID `db:"vendor_id"`
|
||||
TenantID gid.TenantID `db:"tenant_id"`
|
||||
CreatedAt time.Time `db:"created_at"`
|
||||
DatumID gid.GID `db:"datum_id"`
|
||||
VendorID gid.GID `db:"vendor_id"`
|
||||
SnapshotID *gid.GID `db:"snapshot_id"`
|
||||
CreatedAt time.Time `db:"created_at"`
|
||||
}
|
||||
|
||||
DatumVendors []*DatumVendor
|
||||
@@ -112,3 +113,61 @@ FROM vendor_ids
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d DatumVendors) InsertDataSnapshots(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
scope Scoper,
|
||||
organizationID gid.GID,
|
||||
snapshotID gid.GID,
|
||||
) error {
|
||||
query := `
|
||||
WITH
|
||||
source_data AS (
|
||||
SELECT id
|
||||
FROM data
|
||||
WHERE organization_id = @organization_id AND snapshot_id IS NULL
|
||||
),
|
||||
snapshot_data AS (
|
||||
SELECT id, source_id
|
||||
FROM data
|
||||
WHERE organization_id = @organization_id AND snapshot_id = @snapshot_id
|
||||
),
|
||||
snapshot_vendors AS (
|
||||
SELECT id, source_id
|
||||
FROM vendors
|
||||
WHERE organization_id = @organization_id AND snapshot_id = @snapshot_id
|
||||
),
|
||||
source_data_vendors AS (
|
||||
SELECT datum_id, vendor_id, snapshot_id, created_at
|
||||
FROM data_vendors
|
||||
WHERE %s AND datum_id = ANY(SELECT id FROM source_data)
|
||||
)
|
||||
INSERT INTO data_vendors (tenant_id, datum_id, vendor_id, snapshot_id, created_at)
|
||||
SELECT
|
||||
@tenant_id,
|
||||
sd.id,
|
||||
sv.id,
|
||||
@snapshot_id,
|
||||
dv.created_at
|
||||
FROM source_data_vendors dv
|
||||
JOIN snapshot_data sd ON sd.source_id = dv.datum_id
|
||||
JOIN snapshot_vendors sv ON sv.source_id = dv.vendor_id
|
||||
`
|
||||
|
||||
query = fmt.Sprintf(query, scope.SQLFragment())
|
||||
|
||||
args := pgx.StrictNamedArgs{
|
||||
"tenant_id": scope.GetTenantID(),
|
||||
"snapshot_id": snapshotID,
|
||||
"organization_id": organizationID,
|
||||
}
|
||||
maps.Copy(args, scope.SQLArguments())
|
||||
|
||||
_, err := conn.Exec(ctx, query, args)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot insert datum vendor snapshots: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -46,4 +46,5 @@ const (
|
||||
NonconformityRegistryEntityType
|
||||
ComplianceRegistryEntityType
|
||||
VendorServiceEntityType
|
||||
SnapshotEntityType
|
||||
)
|
||||
|
||||
24
pkg/coredata/migrations/20250819T102905Z.sql
Normal file
24
pkg/coredata/migrations/20250819T102905Z.sql
Normal file
@@ -0,0 +1,24 @@
|
||||
CREATE TYPE snapshots_type AS ENUM (
|
||||
'RISKS',
|
||||
'VENDORS',
|
||||
'ASSETS',
|
||||
'DATA',
|
||||
'NON_CONFORMITY_REGISTRIES',
|
||||
'COMPLIANCE_REGISTRIES'
|
||||
);
|
||||
|
||||
CREATE TABLE snapshots (
|
||||
id TEXT PRIMARY KEY,
|
||||
tenant_id TEXT NOT NULL,
|
||||
organization_id TEXT NOT NULL,
|
||||
name TEXT NOT NULL,
|
||||
description TEXT,
|
||||
type snapshots_type NOT NULL,
|
||||
created_at TIMESTAMP WITH TIME ZONE NOT NULL,
|
||||
|
||||
CONSTRAINT snapshots_organization_id_fkey
|
||||
FOREIGN KEY (organization_id)
|
||||
REFERENCES organizations(id)
|
||||
ON UPDATE CASCADE
|
||||
ON DELETE CASCADE
|
||||
);
|
||||
31
pkg/coredata/migrations/20250822T123225Z.sql
Normal file
31
pkg/coredata/migrations/20250822T123225Z.sql
Normal file
@@ -0,0 +1,31 @@
|
||||
ALTER TABLE data ADD COLUMN snapshot_id TEXT;
|
||||
ALTER TABLE data ADD COLUMN source_id TEXT;
|
||||
|
||||
ALTER TABLE data ADD CONSTRAINT data_snapshot_id_fkey
|
||||
FOREIGN KEY (snapshot_id)
|
||||
REFERENCES snapshots(id)
|
||||
ON UPDATE CASCADE
|
||||
ON DELETE CASCADE;
|
||||
|
||||
ALTER TABLE data ADD CONSTRAINT data_source_id_snapshot_id_key
|
||||
UNIQUE (source_id, snapshot_id);
|
||||
|
||||
ALTER TABLE vendors ADD COLUMN snapshot_id TEXT;
|
||||
ALTER TABLE vendors ADD COLUMN source_id TEXT;
|
||||
|
||||
ALTER TABLE vendors ADD CONSTRAINT vendors_snapshot_id_fkey
|
||||
FOREIGN KEY (snapshot_id)
|
||||
REFERENCES snapshots(id)
|
||||
ON UPDATE CASCADE
|
||||
ON DELETE CASCADE;
|
||||
|
||||
ALTER TABLE vendors ADD CONSTRAINT vendors_source_id_snapshot_id_key
|
||||
UNIQUE (source_id, snapshot_id);
|
||||
|
||||
ALTER TABLE data_vendors ADD COLUMN snapshot_id TEXT;
|
||||
|
||||
ALTER TABLE data_vendors ADD CONSTRAINT data_vendors_snapshot_id_fkey
|
||||
FOREIGN KEY (snapshot_id)
|
||||
REFERENCES snapshots(id)
|
||||
ON UPDATE CASCADE
|
||||
ON DELETE CASCADE;
|
||||
239
pkg/coredata/snapshot.go
Normal file
239
pkg/coredata/snapshot.go
Normal file
@@ -0,0 +1,239 @@
|
||||
// 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 (
|
||||
Snapshot struct {
|
||||
ID gid.GID `db:"id"`
|
||||
OrganizationID gid.GID `db:"organization_id"`
|
||||
Name string `db:"name"`
|
||||
Description *string `db:"description"`
|
||||
Type SnapshotsType `db:"type"`
|
||||
CreatedAt time.Time `db:"created_at"`
|
||||
}
|
||||
|
||||
Snapshots []*Snapshot
|
||||
)
|
||||
|
||||
func (s *Snapshot) CursorKey(field SnapshotOrderField) page.CursorKey {
|
||||
switch field {
|
||||
case SnapshotOrderFieldCreatedAt:
|
||||
return page.NewCursorKey(s.ID, s.CreatedAt)
|
||||
case SnapshotOrderFieldName:
|
||||
return page.NewCursorKey(s.ID, s.Name)
|
||||
case SnapshotOrderFieldType:
|
||||
return page.NewCursorKey(s.ID, s.Type)
|
||||
}
|
||||
|
||||
panic(fmt.Sprintf("unsupported order by: %s", field))
|
||||
}
|
||||
|
||||
func (s *Snapshot) LoadByID(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
scope Scoper,
|
||||
snapshotID gid.GID,
|
||||
) error {
|
||||
q := `
|
||||
SELECT
|
||||
id,
|
||||
organization_id,
|
||||
name,
|
||||
description,
|
||||
type,
|
||||
created_at
|
||||
FROM
|
||||
snapshots
|
||||
WHERE
|
||||
%s
|
||||
AND id = @snapshot_id
|
||||
LIMIT 1;
|
||||
`
|
||||
|
||||
q = fmt.Sprintf(q, scope.SQLFragment())
|
||||
|
||||
args := pgx.StrictNamedArgs{"snapshot_id": snapshotID}
|
||||
maps.Copy(args, scope.SQLArguments())
|
||||
|
||||
rows, err := conn.Query(ctx, q, args)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot query snapshots: %w", err)
|
||||
}
|
||||
|
||||
snapshot, err := pgx.CollectExactlyOneRow(rows, pgx.RowToStructByName[Snapshot])
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot collect snapshot: %w", err)
|
||||
}
|
||||
|
||||
*s = snapshot
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Snapshots) CountByOrganizationID(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
scope Scoper,
|
||||
organizationID gid.GID,
|
||||
) (int, error) {
|
||||
q := `
|
||||
SELECT
|
||||
COUNT(id)
|
||||
FROM
|
||||
snapshots
|
||||
WHERE
|
||||
%s
|
||||
AND organization_id = @organization_id
|
||||
`
|
||||
|
||||
q = fmt.Sprintf(q, scope.SQLFragment())
|
||||
|
||||
args := pgx.StrictNamedArgs{"organization_id": organizationID}
|
||||
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 (s *Snapshots) LoadByOrganizationID(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
scope Scoper,
|
||||
organizationID gid.GID,
|
||||
cursor *page.Cursor[SnapshotOrderField],
|
||||
) error {
|
||||
q := `
|
||||
SELECT
|
||||
id,
|
||||
organization_id,
|
||||
name,
|
||||
description,
|
||||
type,
|
||||
created_at
|
||||
FROM
|
||||
snapshots
|
||||
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 snapshots: %w", err)
|
||||
}
|
||||
|
||||
snapshots, err := pgx.CollectRows(rows, pgx.RowToAddrOfStructByName[Snapshot])
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot collect snapshots: %w", err)
|
||||
}
|
||||
|
||||
*s = snapshots
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Snapshot) Insert(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
scope Scoper,
|
||||
) error {
|
||||
q := `
|
||||
INSERT INTO snapshots (
|
||||
id,
|
||||
tenant_id,
|
||||
organization_id,
|
||||
name,
|
||||
description,
|
||||
type,
|
||||
created_at
|
||||
) VALUES (
|
||||
@id,
|
||||
@tenant_id,
|
||||
@organization_id,
|
||||
@name,
|
||||
@description,
|
||||
@type,
|
||||
@created_at
|
||||
)
|
||||
`
|
||||
|
||||
args := pgx.StrictNamedArgs{
|
||||
"id": s.ID,
|
||||
"tenant_id": scope.GetTenantID(),
|
||||
"organization_id": s.OrganizationID,
|
||||
"name": s.Name,
|
||||
"description": s.Description,
|
||||
"type": s.Type,
|
||||
"created_at": s.CreatedAt,
|
||||
}
|
||||
|
||||
_, err := conn.Exec(ctx, q, args)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot insert snapshot: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Snapshot) Delete(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
scope Scoper,
|
||||
) error {
|
||||
q := `
|
||||
DELETE FROM snapshots
|
||||
WHERE
|
||||
%s
|
||||
AND organization_id = @organization_id
|
||||
AND id = @id
|
||||
`
|
||||
|
||||
q = fmt.Sprintf(q, scope.SQLFragment())
|
||||
|
||||
args := pgx.StrictNamedArgs{"id": s.ID, "organization_id": s.OrganizationID}
|
||||
maps.Copy(args, scope.SQLArguments())
|
||||
|
||||
_, err := conn.Exec(ctx, q, args)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot delete snapshot: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
51
pkg/coredata/snapshot_order_field.go
Normal file
51
pkg/coredata/snapshot_order_field.go
Normal 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
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type SnapshotOrderField string
|
||||
|
||||
const (
|
||||
SnapshotOrderFieldCreatedAt SnapshotOrderField = "CREATED_AT"
|
||||
SnapshotOrderFieldName SnapshotOrderField = "NAME"
|
||||
SnapshotOrderFieldType SnapshotOrderField = "TYPE"
|
||||
)
|
||||
|
||||
func (p SnapshotOrderField) Column() string {
|
||||
return string(p)
|
||||
}
|
||||
|
||||
func (p SnapshotOrderField) String() string {
|
||||
return string(p)
|
||||
}
|
||||
|
||||
func (p SnapshotOrderField) MarshalText() ([]byte, error) {
|
||||
return []byte(p.String()), nil
|
||||
}
|
||||
|
||||
func (p *SnapshotOrderField) UnmarshalText(text []byte) error {
|
||||
val := string(text)
|
||||
switch val {
|
||||
case string(SnapshotOrderFieldCreatedAt),
|
||||
string(SnapshotOrderFieldName),
|
||||
string(SnapshotOrderFieldType):
|
||||
*p = SnapshotOrderField(val)
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("invalid SnapshotOrderField value: %q", val)
|
||||
}
|
||||
71
pkg/coredata/snapshots_type.go
Normal file
71
pkg/coredata/snapshots_type.go
Normal file
@@ -0,0 +1,71 @@
|
||||
// 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 (
|
||||
SnapshotsType string
|
||||
)
|
||||
|
||||
const (
|
||||
SnapshotsTypeRisks SnapshotsType = "RISKS"
|
||||
SnapshotsTypeVendors SnapshotsType = "VENDORS"
|
||||
SnapshotsTypeAssets SnapshotsType = "ASSETS"
|
||||
SnapshotsTypeData SnapshotsType = "DATA"
|
||||
SnapshotsTypeNonConformityRegistries SnapshotsType = "NON_CONFORMITY_REGISTRIES"
|
||||
SnapshotsTypeComplianceRegistries SnapshotsType = "COMPLIANCE_REGISTRIES"
|
||||
)
|
||||
|
||||
func (st SnapshotsType) String() string {
|
||||
return string(st)
|
||||
}
|
||||
|
||||
func (st *SnapshotsType) 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 SnapshotsType: %T", value)
|
||||
}
|
||||
|
||||
switch s {
|
||||
case SnapshotsTypeRisks.String():
|
||||
*st = SnapshotsTypeRisks
|
||||
case SnapshotsTypeVendors.String():
|
||||
*st = SnapshotsTypeVendors
|
||||
case SnapshotsTypeAssets.String():
|
||||
*st = SnapshotsTypeAssets
|
||||
case SnapshotsTypeData.String():
|
||||
*st = SnapshotsTypeData
|
||||
case SnapshotsTypeNonConformityRegistries.String():
|
||||
*st = SnapshotsTypeNonConformityRegistries
|
||||
case SnapshotsTypeComplianceRegistries.String():
|
||||
*st = SnapshotsTypeComplianceRegistries
|
||||
default:
|
||||
return fmt.Errorf("invalid SnapshotsType value: %q", s)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (st SnapshotsType) Value() (driver.Value, error) {
|
||||
return st.String(), nil
|
||||
}
|
||||
36
pkg/coredata/snapshottable.go
Normal file
36
pkg/coredata/snapshottable.go
Normal file
@@ -0,0 +1,36 @@
|
||||
// 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"
|
||||
|
||||
"github.com/getprobo/probo/pkg/gid"
|
||||
"go.gearno.de/kit/pg"
|
||||
)
|
||||
|
||||
type Snapshottable interface {
|
||||
Snapshot(ctx context.Context, conn pg.Conn, scope Scoper, organizationID, snapshotID gid.GID) error
|
||||
}
|
||||
|
||||
func GetSnapshottable(snapshotType SnapshotsType) (Snapshottable, error) {
|
||||
switch snapshotType {
|
||||
case SnapshotsTypeData:
|
||||
return Data{}, nil
|
||||
default:
|
||||
return nil, fmt.Errorf("unsupported snapshot type: %s", snapshotType)
|
||||
}
|
||||
}
|
||||
@@ -50,6 +50,8 @@ type (
|
||||
SecurityPageURL *string `db:"security_page_url"`
|
||||
TrustPageURL *string `db:"trust_page_url"`
|
||||
ShowOnTrustCenter bool `db:"show_on_trust_center"`
|
||||
SnapshotID *gid.GID `db:"snapshot_id"`
|
||||
SourceID *gid.GID `db:"source_id"`
|
||||
CreatedAt time.Time `db:"created_at"`
|
||||
UpdatedAt time.Time `db:"updated_at"`
|
||||
}
|
||||
@@ -100,6 +102,8 @@ SELECT
|
||||
security_page_url,
|
||||
trust_page_url,
|
||||
show_on_trust_center,
|
||||
snapshot_id,
|
||||
source_id,
|
||||
created_at,
|
||||
updated_at
|
||||
FROM
|
||||
@@ -161,6 +165,8 @@ INSERT INTO
|
||||
security_page_url,
|
||||
trust_page_url,
|
||||
show_on_trust_center,
|
||||
snapshot_id,
|
||||
source_id,
|
||||
created_at,
|
||||
updated_at
|
||||
)
|
||||
@@ -187,6 +193,8 @@ VALUES (
|
||||
@security_page_url,
|
||||
@trust_page_url,
|
||||
@show_on_trust_center,
|
||||
@snapshot_id,
|
||||
@source_id,
|
||||
@created_at,
|
||||
@updated_at
|
||||
)
|
||||
@@ -215,6 +223,8 @@ VALUES (
|
||||
"security_page_url": v.SecurityPageURL,
|
||||
"trust_page_url": v.TrustPageURL,
|
||||
"show_on_trust_center": v.ShowOnTrustCenter,
|
||||
"snapshot_id": v.SnapshotID,
|
||||
"source_id": v.SourceID,
|
||||
"created_at": v.CreatedAt,
|
||||
"updated_at": v.UpdatedAt,
|
||||
}
|
||||
@@ -304,6 +314,8 @@ SELECT
|
||||
security_page_url,
|
||||
trust_page_url,
|
||||
show_on_trust_center,
|
||||
snapshot_id,
|
||||
source_id,
|
||||
created_at,
|
||||
updated_at
|
||||
FROM
|
||||
@@ -504,6 +516,8 @@ WITH vend AS (
|
||||
v.security_page_url,
|
||||
v.trust_page_url,
|
||||
v.show_on_trust_center,
|
||||
v.snapshot_id,
|
||||
v.source_id,
|
||||
v.created_at,
|
||||
v.updated_at
|
||||
FROM
|
||||
@@ -536,6 +550,8 @@ SELECT
|
||||
security_page_url,
|
||||
trust_page_url,
|
||||
show_on_trust_center,
|
||||
snapshot_id,
|
||||
source_id,
|
||||
created_at,
|
||||
updated_at
|
||||
FROM
|
||||
@@ -635,6 +651,8 @@ WITH vend AS (
|
||||
v.security_page_url,
|
||||
v.trust_page_url,
|
||||
v.show_on_trust_center,
|
||||
v.snapshot_id,
|
||||
v.source_id,
|
||||
v.created_at,
|
||||
v.updated_at
|
||||
FROM
|
||||
@@ -667,6 +685,8 @@ SELECT
|
||||
security_page_url,
|
||||
trust_page_url,
|
||||
show_on_trust_center,
|
||||
snapshot_id,
|
||||
source_id,
|
||||
created_at,
|
||||
updated_at
|
||||
FROM
|
||||
@@ -694,3 +714,103 @@ WHERE %s
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d Vendors) InsertDataSnapshots(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
scope Scoper,
|
||||
organizationID gid.GID,
|
||||
snapshotID gid.GID,
|
||||
) error {
|
||||
query := `
|
||||
WITH
|
||||
source_data AS (
|
||||
SELECT id
|
||||
FROM data
|
||||
WHERE organization_id = @organization_id AND snapshot_id IS NULL
|
||||
),
|
||||
source_data_vendors AS (
|
||||
SELECT datum_id, vendor_id, snapshot_id, created_at
|
||||
FROM data_vendors
|
||||
WHERE datum_id = ANY(SELECT id FROM source_data)
|
||||
),
|
||||
source_vendors AS (
|
||||
SELECT *
|
||||
FROM vendors
|
||||
WHERE %s AND id = ANY(SELECT vendor_id FROM source_data_vendors)
|
||||
)
|
||||
INSERT INTO vendors (
|
||||
tenant_id,
|
||||
id,
|
||||
snapshot_id,
|
||||
source_id,
|
||||
organization_id,
|
||||
name,
|
||||
description,
|
||||
category,
|
||||
headquarter_address,
|
||||
legal_name,
|
||||
website_url,
|
||||
privacy_policy_url,
|
||||
service_level_agreement_url,
|
||||
data_processing_agreement_url,
|
||||
business_associate_agreement_url,
|
||||
subprocessors_list_url,
|
||||
certifications,
|
||||
business_owner_id,
|
||||
security_owner_id,
|
||||
status_page_url,
|
||||
terms_of_service_url,
|
||||
security_page_url,
|
||||
trust_page_url,
|
||||
show_on_trust_center,
|
||||
created_at,
|
||||
updated_at
|
||||
)
|
||||
SELECT
|
||||
@tenant_id,
|
||||
generate_gid(decode_base64_unpadded(@tenant_id), @vendor_entity_type),
|
||||
@snapshot_id,
|
||||
v.id,
|
||||
v.organization_id,
|
||||
v.name,
|
||||
v.description,
|
||||
v.category,
|
||||
v.headquarter_address,
|
||||
v.legal_name,
|
||||
v.website_url,
|
||||
v.privacy_policy_url,
|
||||
v.service_level_agreement_url,
|
||||
v.data_processing_agreement_url,
|
||||
v.business_associate_agreement_url,
|
||||
v.subprocessors_list_url,
|
||||
v.certifications,
|
||||
v.business_owner_id,
|
||||
v.security_owner_id,
|
||||
v.status_page_url,
|
||||
v.terms_of_service_url,
|
||||
v.security_page_url,
|
||||
v.trust_page_url,
|
||||
v.show_on_trust_center,
|
||||
v.created_at,
|
||||
v.updated_at
|
||||
FROM source_vendors v
|
||||
`
|
||||
|
||||
query = fmt.Sprintf(query, scope.SQLFragment())
|
||||
|
||||
args := pgx.StrictNamedArgs{
|
||||
"tenant_id": scope.GetTenantID(),
|
||||
"snapshot_id": snapshotID,
|
||||
"organization_id": organizationID,
|
||||
"vendor_entity_type": VendorEntityType,
|
||||
}
|
||||
maps.Copy(args, scope.SQLArguments())
|
||||
|
||||
_, err := conn.Exec(ctx, query, args)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot insert vendor snapshots: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -15,12 +15,14 @@
|
||||
package coredata
|
||||
|
||||
import (
|
||||
"github.com/getprobo/probo/pkg/gid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
)
|
||||
|
||||
type (
|
||||
VendorFilter struct {
|
||||
showOnTrustCenter *bool
|
||||
snapshotID **gid.GID
|
||||
}
|
||||
)
|
||||
|
||||
@@ -35,20 +37,51 @@ func NewVendorTrustCenterFilter() *VendorFilter {
|
||||
}
|
||||
}
|
||||
|
||||
func (f *VendorFilter) SQLArguments() pgx.NamedArgs {
|
||||
args := pgx.NamedArgs{}
|
||||
func NewVendorFilterBySnapshotID(snapshotID **gid.GID) *VendorFilter {
|
||||
return &VendorFilter{
|
||||
snapshotID: snapshotID,
|
||||
}
|
||||
}
|
||||
|
||||
func (f *VendorFilter) SQLArguments() pgx.StrictNamedArgs {
|
||||
args := pgx.StrictNamedArgs{}
|
||||
|
||||
if f.showOnTrustCenter != nil {
|
||||
args["show_on_trust_center"] = *f.showOnTrustCenter
|
||||
} else {
|
||||
args["show_on_trust_center"] = nil
|
||||
}
|
||||
|
||||
if f.snapshotID == nil {
|
||||
args["has_snapshot_filter"] = false
|
||||
args["filter_snapshot_id"] = nil
|
||||
} else if *f.snapshotID == nil {
|
||||
args["has_snapshot_filter"] = true
|
||||
args["filter_snapshot_id"] = nil
|
||||
} else {
|
||||
args["has_snapshot_filter"] = true
|
||||
args["filter_snapshot_id"] = **f.snapshotID
|
||||
}
|
||||
|
||||
return args
|
||||
}
|
||||
|
||||
func (f *VendorFilter) SQLFragment() string {
|
||||
if f.showOnTrustCenter != nil {
|
||||
return "show_on_trust_center = @show_on_trust_center"
|
||||
}
|
||||
|
||||
return "TRUE"
|
||||
return `
|
||||
(
|
||||
CASE
|
||||
WHEN @show_on_trust_center::boolean IS NOT NULL THEN
|
||||
show_on_trust_center = @show_on_trust_center::boolean
|
||||
ELSE TRUE
|
||||
END
|
||||
AND
|
||||
CASE
|
||||
WHEN @has_snapshot_filter::boolean = false THEN TRUE
|
||||
WHEN @has_snapshot_filter::boolean = true AND @filter_snapshot_id::text IS NOT NULL THEN
|
||||
snapshot_id = @filter_snapshot_id::text
|
||||
WHEN @has_snapshot_filter::boolean = true AND @filter_snapshot_id::text IS NULL THEN
|
||||
snapshot_id IS NULL
|
||||
ELSE TRUE
|
||||
END
|
||||
)`
|
||||
}
|
||||
|
||||
@@ -115,6 +115,7 @@ func (s DatumService) ListForOrganizationID(
|
||||
ctx context.Context,
|
||||
organizationID gid.GID,
|
||||
cursor *page.Cursor[coredata.DatumOrderField],
|
||||
filter *coredata.DatumFilter,
|
||||
) (*page.Page[*coredata.Datum, coredata.DatumOrderField], error) {
|
||||
var data coredata.Data
|
||||
|
||||
@@ -127,6 +128,7 @@ func (s DatumService) ListForOrganizationID(
|
||||
s.svc.scope,
|
||||
organizationID,
|
||||
cursor,
|
||||
filter,
|
||||
)
|
||||
},
|
||||
)
|
||||
|
||||
@@ -84,6 +84,7 @@ type (
|
||||
TrustCenterAccesses *TrustCenterAccessService
|
||||
NonconformityRegistries *NonconformityRegistryService
|
||||
ComplianceRegistries *ComplianceRegistryService
|
||||
Snapshots *SnapshotService
|
||||
}
|
||||
)
|
||||
|
||||
@@ -179,5 +180,6 @@ func (s *Service) WithTenant(tenantID gid.TenantID) *TenantService {
|
||||
}
|
||||
tenantService.NonconformityRegistries = &NonconformityRegistryService{svc: tenantService}
|
||||
tenantService.ComplianceRegistries = &ComplianceRegistryService{svc: tenantService}
|
||||
tenantService.Snapshots = &SnapshotService{svc: tenantService}
|
||||
return tenantService
|
||||
}
|
||||
|
||||
186
pkg/probo/snapshot_service.go
Normal file
186
pkg/probo/snapshot_service.go
Normal file
@@ -0,0 +1,186 @@
|
||||
// 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 probo
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/getprobo/probo/pkg/coredata"
|
||||
"github.com/getprobo/probo/pkg/gid"
|
||||
"github.com/getprobo/probo/pkg/page"
|
||||
"go.gearno.de/kit/pg"
|
||||
)
|
||||
|
||||
type SnapshotService struct {
|
||||
svc *TenantService
|
||||
}
|
||||
|
||||
type (
|
||||
CreateSnapshotRequest struct {
|
||||
OrganizationID gid.GID
|
||||
Name string
|
||||
Description *string
|
||||
Type coredata.SnapshotsType
|
||||
}
|
||||
|
||||
UpdateSnapshotRequest struct {
|
||||
ID gid.GID
|
||||
Name *string
|
||||
Description **string
|
||||
Type *coredata.SnapshotsType
|
||||
}
|
||||
)
|
||||
|
||||
func (s *SnapshotService) Get(
|
||||
ctx context.Context,
|
||||
snapshotID gid.GID,
|
||||
) (*coredata.Snapshot, error) {
|
||||
snapshot := &coredata.Snapshot{}
|
||||
|
||||
err := s.svc.pg.WithConn(
|
||||
ctx,
|
||||
func(conn pg.Conn) error {
|
||||
return snapshot.LoadByID(ctx, conn, s.svc.scope, snapshotID)
|
||||
},
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return snapshot, nil
|
||||
}
|
||||
|
||||
func (s *SnapshotService) Create(
|
||||
ctx context.Context,
|
||||
req *CreateSnapshotRequest,
|
||||
) (*coredata.Snapshot, error) {
|
||||
now := time.Now()
|
||||
|
||||
snapshot := &coredata.Snapshot{
|
||||
ID: gid.New(s.svc.scope.GetTenantID(), coredata.SnapshotEntityType),
|
||||
OrganizationID: req.OrganizationID,
|
||||
Name: req.Name,
|
||||
Description: req.Description,
|
||||
Type: req.Type,
|
||||
CreatedAt: now,
|
||||
}
|
||||
|
||||
err := s.svc.pg.WithTx(
|
||||
ctx,
|
||||
func(conn pg.Conn) error {
|
||||
organization := &coredata.Organization{}
|
||||
if err := organization.LoadByID(ctx, conn, s.svc.scope, req.OrganizationID); err != nil {
|
||||
return fmt.Errorf("cannot load organization: %w", err)
|
||||
}
|
||||
|
||||
if err := snapshot.Insert(ctx, conn, s.svc.scope); err != nil {
|
||||
return fmt.Errorf("cannot insert snapshot: %w", err)
|
||||
}
|
||||
|
||||
snapshottable, err := coredata.GetSnapshottable(req.Type)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := snapshottable.Snapshot(ctx, conn, s.svc.scope, req.OrganizationID, snapshot.ID); err != nil {
|
||||
return fmt.Errorf("cannot create snapshot: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return snapshot, nil
|
||||
}
|
||||
|
||||
func (s *SnapshotService) Delete(
|
||||
ctx context.Context,
|
||||
snapshotID gid.GID,
|
||||
) error {
|
||||
err := s.svc.pg.WithConn(
|
||||
ctx,
|
||||
func(conn pg.Conn) error {
|
||||
snapshot := &coredata.Snapshot{}
|
||||
if err := snapshot.LoadByID(ctx, conn, s.svc.scope, snapshotID); err != nil {
|
||||
return fmt.Errorf("cannot load snapshot: %w", err)
|
||||
}
|
||||
|
||||
if err := snapshot.Delete(ctx, conn, s.svc.scope); err != nil {
|
||||
return fmt.Errorf("cannot delete snapshot: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *SnapshotService) ListForOrganizationID(
|
||||
ctx context.Context,
|
||||
organizationID gid.GID,
|
||||
cursor *page.Cursor[coredata.SnapshotOrderField],
|
||||
) (*page.Page[*coredata.Snapshot, coredata.SnapshotOrderField], error) {
|
||||
snapshots := coredata.Snapshots{}
|
||||
|
||||
err := s.svc.pg.WithConn(
|
||||
ctx,
|
||||
func(conn pg.Conn) error {
|
||||
if err := snapshots.LoadByOrganizationID(ctx, conn, s.svc.scope, organizationID, cursor); err != nil {
|
||||
return fmt.Errorf("cannot load snapshots: %w", err)
|
||||
}
|
||||
return nil
|
||||
},
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return page.NewPage(snapshots, cursor), nil
|
||||
}
|
||||
|
||||
func (s *SnapshotService) CountForOrganizationID(
|
||||
ctx context.Context,
|
||||
organizationID gid.GID,
|
||||
) (int, error) {
|
||||
var count int
|
||||
|
||||
err := s.svc.pg.WithConn(
|
||||
ctx,
|
||||
func(conn pg.Conn) (err error) {
|
||||
snapshots := coredata.Snapshots{}
|
||||
count, err = snapshots.CountByOrganizationID(ctx, conn, s.svc.scope, organizationID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot count snapshots: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return count, nil
|
||||
}
|
||||
@@ -120,6 +120,7 @@ func (s VendorService) ListForOrganizationID(
|
||||
ctx context.Context,
|
||||
organizationID gid.GID,
|
||||
cursor *page.Cursor[coredata.VendorOrderField],
|
||||
filter *coredata.VendorFilter,
|
||||
) (*page.Page[*coredata.Vendor, coredata.VendorOrderField], error) {
|
||||
var vendors coredata.Vendors
|
||||
organization := &coredata.Organization{}
|
||||
@@ -131,7 +132,6 @@ func (s VendorService) ListForOrganizationID(
|
||||
return fmt.Errorf("cannot load organization: %w", err)
|
||||
}
|
||||
|
||||
filter := coredata.NewVendorFilter()
|
||||
return vendors.LoadByOrganizationID(
|
||||
ctx,
|
||||
conn,
|
||||
|
||||
@@ -686,6 +686,50 @@ enum TrustCenterAccessOrderField
|
||||
)
|
||||
}
|
||||
|
||||
enum SnapshotsType
|
||||
@goModel(model: "github.com/getprobo/probo/pkg/coredata.SnapshotsType") {
|
||||
RISKS
|
||||
@goEnum(
|
||||
value: "github.com/getprobo/probo/pkg/coredata.SnapshotsTypeRisks"
|
||||
)
|
||||
VENDORS
|
||||
@goEnum(
|
||||
value: "github.com/getprobo/probo/pkg/coredata.SnapshotsTypeVendors"
|
||||
)
|
||||
ASSETS
|
||||
@goEnum(
|
||||
value: "github.com/getprobo/probo/pkg/coredata.SnapshotsTypeAssets"
|
||||
)
|
||||
DATA
|
||||
@goEnum(
|
||||
value: "github.com/getprobo/probo/pkg/coredata.SnapshotsTypeData"
|
||||
)
|
||||
NON_CONFORMITY_REGISTRIES
|
||||
@goEnum(
|
||||
value: "github.com/getprobo/probo/pkg/coredata.SnapshotsTypeNonConformityRegistries"
|
||||
)
|
||||
COMPLIANCE_REGISTRIES
|
||||
@goEnum(
|
||||
value: "github.com/getprobo/probo/pkg/coredata.SnapshotsTypeComplianceRegistries"
|
||||
)
|
||||
}
|
||||
|
||||
enum SnapshotOrderField
|
||||
@goModel(model: "github.com/getprobo/probo/pkg/coredata.SnapshotOrderField") {
|
||||
CREATED_AT
|
||||
@goEnum(
|
||||
value: "github.com/getprobo/probo/pkg/coredata.SnapshotOrderFieldCreatedAt"
|
||||
)
|
||||
NAME
|
||||
@goEnum(
|
||||
value: "github.com/getprobo/probo/pkg/coredata.SnapshotOrderFieldName"
|
||||
)
|
||||
TYPE
|
||||
@goEnum(
|
||||
value: "github.com/getprobo/probo/pkg/coredata.SnapshotOrderFieldType"
|
||||
)
|
||||
}
|
||||
|
||||
# Input Types
|
||||
input UserOrder
|
||||
@goModel(
|
||||
@@ -841,6 +885,14 @@ input DocumentVersionOrder
|
||||
field: DocumentVersionOrderField!
|
||||
}
|
||||
|
||||
input SnapshotOrder
|
||||
@goModel(
|
||||
model: "github.com/getprobo/probo/pkg/server/api/console/v1/types.SnapshotOrderBy"
|
||||
) {
|
||||
direction: OrderDirection!
|
||||
field: SnapshotOrderField!
|
||||
}
|
||||
|
||||
input DocumentVersionFilter {
|
||||
status: DocumentStatus
|
||||
}
|
||||
@@ -874,6 +926,10 @@ input TrustCenterFilter {
|
||||
slug: String
|
||||
}
|
||||
|
||||
input DatumFilter {
|
||||
snapshotId: ID
|
||||
}
|
||||
|
||||
# Core Types
|
||||
type TrustCenter implements Node {
|
||||
id: ID!
|
||||
@@ -996,6 +1052,7 @@ type Organization implements Node {
|
||||
last: Int
|
||||
before: CursorKey
|
||||
orderBy: DatumOrder
|
||||
filter: DatumFilter
|
||||
): DatumConnection! @goField(forceResolver: true)
|
||||
|
||||
audits(
|
||||
@@ -1022,6 +1079,14 @@ type Organization implements Node {
|
||||
orderBy: ComplianceRegistryOrder
|
||||
): ComplianceRegistryConnection! @goField(forceResolver: true)
|
||||
|
||||
snapshots(
|
||||
first: Int
|
||||
after: CursorKey
|
||||
last: Int
|
||||
before: CursorKey
|
||||
orderBy: SnapshotOrder
|
||||
): SnapshotConnection! @goField(forceResolver: true)
|
||||
|
||||
trustCenter: TrustCenter @goField(forceResolver: true)
|
||||
|
||||
createdAt: Datetime!
|
||||
@@ -1466,6 +1531,15 @@ type ComplianceRegistry implements Node {
|
||||
updatedAt: Datetime!
|
||||
}
|
||||
|
||||
type Snapshot implements Node {
|
||||
id: ID!
|
||||
organization: Organization! @goField(forceResolver: true)
|
||||
name: String!
|
||||
description: String
|
||||
type: SnapshotsType!
|
||||
createdAt: Datetime!
|
||||
}
|
||||
|
||||
type Report implements Node {
|
||||
id: ID!
|
||||
objectKey: String!
|
||||
@@ -1788,6 +1862,20 @@ type ComplianceRegistryEdge {
|
||||
node: ComplianceRegistry!
|
||||
}
|
||||
|
||||
type SnapshotConnection
|
||||
@goModel(
|
||||
model: "github.com/getprobo/probo/pkg/server/api/console/v1/types.SnapshotConnection"
|
||||
) {
|
||||
totalCount: Int! @goField(forceResolver: true)
|
||||
edges: [SnapshotEdge!]!
|
||||
pageInfo: PageInfo!
|
||||
}
|
||||
|
||||
type SnapshotEdge {
|
||||
cursor: CursorKey!
|
||||
node: Snapshot!
|
||||
}
|
||||
|
||||
# Root Types
|
||||
type Query {
|
||||
node(id: ID!): Node!
|
||||
@@ -2038,6 +2126,10 @@ type Mutation {
|
||||
deleteComplianceRegistry(
|
||||
input: DeleteComplianceRegistryInput!
|
||||
): DeleteComplianceRegistryPayload!
|
||||
|
||||
# Snapshot mutations
|
||||
createSnapshot(input: CreateSnapshotInput!): CreateSnapshotPayload!
|
||||
deleteSnapshot(input: DeleteSnapshotInput!): DeleteSnapshotPayload!
|
||||
}
|
||||
|
||||
# Input Types
|
||||
@@ -2593,6 +2685,17 @@ input DeleteComplianceRegistryInput {
|
||||
complianceRegistryId: ID!
|
||||
}
|
||||
|
||||
input CreateSnapshotInput {
|
||||
organizationId: ID!
|
||||
name: String!
|
||||
description: String
|
||||
type: SnapshotsType!
|
||||
}
|
||||
|
||||
input DeleteSnapshotInput {
|
||||
snapshotId: ID!
|
||||
}
|
||||
|
||||
# Payload Types
|
||||
type CreateOrganizationPayload {
|
||||
organizationEdge: OrganizationEdge!
|
||||
@@ -3302,3 +3405,11 @@ type UpdateComplianceRegistryPayload {
|
||||
type DeleteComplianceRegistryPayload {
|
||||
deletedComplianceRegistryId: ID!
|
||||
}
|
||||
|
||||
type CreateSnapshotPayload {
|
||||
snapshotEdge: SnapshotEdge!
|
||||
}
|
||||
|
||||
type DeleteSnapshotPayload {
|
||||
deletedSnapshotId: ID!
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
70
pkg/server/api/console/v1/types/snapshot.go
Normal file
70
pkg/server/api/console/v1/types/snapshot.go
Normal file
@@ -0,0 +1,70 @@
|
||||
// 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 types
|
||||
|
||||
import (
|
||||
"github.com/getprobo/probo/pkg/coredata"
|
||||
"github.com/getprobo/probo/pkg/gid"
|
||||
"github.com/getprobo/probo/pkg/page"
|
||||
)
|
||||
|
||||
type (
|
||||
SnapshotOrderBy OrderBy[coredata.SnapshotOrderField]
|
||||
|
||||
SnapshotConnection struct {
|
||||
TotalCount int
|
||||
Edges []*SnapshotEdge
|
||||
PageInfo PageInfo
|
||||
|
||||
Resolver any
|
||||
ParentID gid.GID
|
||||
}
|
||||
)
|
||||
|
||||
func NewSnapshotConnection(
|
||||
p *page.Page[*coredata.Snapshot, coredata.SnapshotOrderField],
|
||||
parentType any,
|
||||
parentID gid.GID,
|
||||
) *SnapshotConnection {
|
||||
edges := make([]*SnapshotEdge, len(p.Data))
|
||||
for i, snapshot := range p.Data {
|
||||
edges[i] = NewSnapshotEdge(snapshot, p.Cursor.OrderBy.Field)
|
||||
}
|
||||
|
||||
return &SnapshotConnection{
|
||||
Edges: edges,
|
||||
PageInfo: *NewPageInfo(p),
|
||||
|
||||
Resolver: parentType,
|
||||
ParentID: parentID,
|
||||
}
|
||||
}
|
||||
|
||||
func NewSnapshot(s *coredata.Snapshot) *Snapshot {
|
||||
return &Snapshot{
|
||||
ID: s.ID,
|
||||
Name: s.Name,
|
||||
Type: s.Type,
|
||||
Description: s.Description,
|
||||
CreatedAt: s.CreatedAt,
|
||||
}
|
||||
}
|
||||
|
||||
func NewSnapshotEdge(s *coredata.Snapshot, orderField coredata.SnapshotOrderField) *SnapshotEdge {
|
||||
return &SnapshotEdge{
|
||||
Node: NewSnapshot(s),
|
||||
Cursor: s.CursorKey(orderField),
|
||||
}
|
||||
}
|
||||
@@ -430,6 +430,17 @@ type CreateRiskPayload struct {
|
||||
RiskEdge *RiskEdge `json:"riskEdge"`
|
||||
}
|
||||
|
||||
type CreateSnapshotInput struct {
|
||||
OrganizationID gid.GID `json:"organizationId"`
|
||||
Name string `json:"name"`
|
||||
Description *string `json:"description,omitempty"`
|
||||
Type coredata.SnapshotsType `json:"type"`
|
||||
}
|
||||
|
||||
type CreateSnapshotPayload struct {
|
||||
SnapshotEdge *SnapshotEdge `json:"snapshotEdge"`
|
||||
}
|
||||
|
||||
type CreateTaskInput struct {
|
||||
OrganizationID gid.GID `json:"organizationId"`
|
||||
MeasureID *gid.GID `json:"measureId,omitempty"`
|
||||
@@ -537,6 +548,10 @@ type DatumEdge struct {
|
||||
Node *Datum `json:"node"`
|
||||
}
|
||||
|
||||
type DatumFilter struct {
|
||||
SnapshotID *gid.GID `json:"snapshotId,omitempty"`
|
||||
}
|
||||
|
||||
type DeleteAssetInput struct {
|
||||
AssetID gid.GID `json:"assetId"`
|
||||
}
|
||||
@@ -707,6 +722,14 @@ type DeleteRiskPayload struct {
|
||||
DeletedRiskID gid.GID `json:"deletedRiskId"`
|
||||
}
|
||||
|
||||
type DeleteSnapshotInput struct {
|
||||
SnapshotID gid.GID `json:"snapshotId"`
|
||||
}
|
||||
|
||||
type DeleteSnapshotPayload struct {
|
||||
DeletedSnapshotID gid.GID `json:"deletedSnapshotId"`
|
||||
}
|
||||
|
||||
type DeleteTaskInput struct {
|
||||
TaskID gid.GID `json:"taskId"`
|
||||
}
|
||||
@@ -1039,6 +1062,7 @@ type Organization struct {
|
||||
Audits *AuditConnection `json:"audits"`
|
||||
NonconformityRegistries *NonconformityRegistryConnection `json:"nonconformityRegistries"`
|
||||
ComplianceRegistries *ComplianceRegistryConnection `json:"complianceRegistries"`
|
||||
Snapshots *SnapshotConnection `json:"snapshots"`
|
||||
TrustCenter *TrustCenter `json:"trustCenter,omitempty"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
UpdatedAt time.Time `json:"updatedAt"`
|
||||
@@ -1201,6 +1225,23 @@ type Session struct {
|
||||
ExpiresAt time.Time `json:"expiresAt"`
|
||||
}
|
||||
|
||||
type Snapshot struct {
|
||||
ID gid.GID `json:"id"`
|
||||
Organization *Organization `json:"organization"`
|
||||
Name string `json:"name"`
|
||||
Description *string `json:"description,omitempty"`
|
||||
Type coredata.SnapshotsType `json:"type"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
}
|
||||
|
||||
func (Snapshot) IsNode() {}
|
||||
func (this Snapshot) GetID() gid.GID { return this.ID }
|
||||
|
||||
type SnapshotEdge struct {
|
||||
Cursor page.CursorKey `json:"cursor"`
|
||||
Node *Snapshot `json:"node"`
|
||||
}
|
||||
|
||||
type Task struct {
|
||||
ID gid.GID `json:"id"`
|
||||
Name string `json:"name"`
|
||||
|
||||
@@ -2995,6 +2995,38 @@ func (r *mutationResolver) DeleteComplianceRegistry(ctx context.Context, input t
|
||||
}, nil
|
||||
}
|
||||
|
||||
// CreateSnapshot is the resolver for the createSnapshot field.
|
||||
func (r *mutationResolver) CreateSnapshot(ctx context.Context, input types.CreateSnapshotInput) (*types.CreateSnapshotPayload, error) {
|
||||
prb := r.ProboService(ctx, input.OrganizationID.TenantID())
|
||||
|
||||
snapshot, err := prb.Snapshots.Create(ctx, &probo.CreateSnapshotRequest{
|
||||
OrganizationID: input.OrganizationID,
|
||||
Name: input.Name,
|
||||
Description: input.Description,
|
||||
Type: input.Type,
|
||||
})
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("cannot create snapshot: %w", err))
|
||||
}
|
||||
|
||||
return &types.CreateSnapshotPayload{
|
||||
SnapshotEdge: types.NewSnapshotEdge(snapshot, coredata.SnapshotOrderFieldCreatedAt),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// DeleteSnapshot is the resolver for the deleteSnapshot field.
|
||||
func (r *mutationResolver) DeleteSnapshot(ctx context.Context, input types.DeleteSnapshotInput) (*types.DeleteSnapshotPayload, error) {
|
||||
prb := r.ProboService(ctx, input.SnapshotID.TenantID())
|
||||
|
||||
if err := prb.Snapshots.Delete(ctx, input.SnapshotID); err != nil {
|
||||
panic(fmt.Errorf("cannot delete snapshot: %w", err))
|
||||
}
|
||||
|
||||
return &types.DeleteSnapshotPayload{
|
||||
DeletedSnapshotID: input.SnapshotID,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Organization is the resolver for the organization field.
|
||||
func (r *nonconformityRegistryResolver) Organization(ctx context.Context, obj *types.NonconformityRegistry) (*types.Organization, error) {
|
||||
prb := r.ProboService(ctx, obj.ID.TenantID())
|
||||
@@ -3188,8 +3220,10 @@ func (r *organizationResolver) Vendors(ctx context.Context, obj *types.Organizat
|
||||
}
|
||||
|
||||
cursor := types.NewCursor(first, after, last, before, pageOrderBy)
|
||||
var nilSnapshotID *gid.GID = nil
|
||||
vendorFilter := coredata.NewVendorFilterBySnapshotID(&nilSnapshotID)
|
||||
|
||||
page, err := prb.Vendors.ListForOrganizationID(ctx, obj.ID, cursor)
|
||||
page, err := prb.Vendors.ListForOrganizationID(ctx, obj.ID, cursor, vendorFilter)
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("cannot list organization vendors: %w", err))
|
||||
}
|
||||
@@ -3368,7 +3402,7 @@ func (r *organizationResolver) Assets(ctx context.Context, obj *types.Organizati
|
||||
}
|
||||
|
||||
// Assets is the resolver for the assets field.
|
||||
func (r *organizationResolver) Data(ctx context.Context, obj *types.Organization, first *int, after *page.CursorKey, last *int, before *page.CursorKey, orderBy *types.DatumOrderBy) (*types.DatumConnection, error) {
|
||||
func (r *organizationResolver) Data(ctx context.Context, obj *types.Organization, first *int, after *page.CursorKey, last *int, before *page.CursorKey, orderBy *types.DatumOrderBy, filter *types.DatumFilter) (*types.DatumConnection, error) {
|
||||
prb := r.ProboService(ctx, obj.ID.TenantID())
|
||||
|
||||
pageOrderBy := page.OrderBy[coredata.DatumOrderField]{
|
||||
@@ -3384,7 +3418,12 @@ func (r *organizationResolver) Data(ctx context.Context, obj *types.Organization
|
||||
|
||||
cursor := types.NewCursor(first, after, last, before, pageOrderBy)
|
||||
|
||||
page, err := prb.Data.ListForOrganizationID(ctx, obj.ID, cursor)
|
||||
datumFilter := coredata.NewDatumFilterBySnapshotID(nil)
|
||||
if filter != nil {
|
||||
datumFilter = coredata.NewDatumFilterBySnapshotID(&filter.SnapshotID)
|
||||
}
|
||||
|
||||
page, err := prb.Data.ListForOrganizationID(ctx, obj.ID, cursor, datumFilter)
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("cannot list organization data: %w", err))
|
||||
}
|
||||
@@ -3467,6 +3506,31 @@ func (r *organizationResolver) ComplianceRegistries(ctx context.Context, obj *ty
|
||||
return types.NewComplianceRegistryConnection(page, r, obj.ID), nil
|
||||
}
|
||||
|
||||
// Snapshots is the resolver for the snapshots field.
|
||||
func (r *organizationResolver) Snapshots(ctx context.Context, obj *types.Organization, first *int, after *page.CursorKey, last *int, before *page.CursorKey, orderBy *types.SnapshotOrderBy) (*types.SnapshotConnection, error) {
|
||||
prb := r.ProboService(ctx, obj.ID.TenantID())
|
||||
|
||||
pageOrderBy := page.OrderBy[coredata.SnapshotOrderField]{
|
||||
Field: coredata.SnapshotOrderFieldCreatedAt,
|
||||
Direction: page.OrderDirectionDesc,
|
||||
}
|
||||
if orderBy != nil {
|
||||
pageOrderBy = page.OrderBy[coredata.SnapshotOrderField]{
|
||||
Field: orderBy.Field,
|
||||
Direction: orderBy.Direction,
|
||||
}
|
||||
}
|
||||
|
||||
cursor := types.NewCursor(first, after, last, before, pageOrderBy)
|
||||
|
||||
page, err := prb.Snapshots.ListForOrganizationID(ctx, obj.ID, cursor)
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("cannot list organization snapshots: %w", err))
|
||||
}
|
||||
|
||||
return types.NewSnapshotConnection(page, r, obj.ID), nil
|
||||
}
|
||||
|
||||
// TrustCenter is the resolver for the trustCenter field.
|
||||
func (r *organizationResolver) TrustCenter(ctx context.Context, obj *types.Organization) (*types.TrustCenter, error) {
|
||||
prb := r.ProboService(ctx, obj.ID.TenantID())
|
||||
@@ -3634,6 +3698,12 @@ func (r *queryResolver) Node(ctx context.Context, id gid.GID) (types.Node, error
|
||||
panic(fmt.Errorf("cannot get report: %w", err))
|
||||
}
|
||||
return types.NewReport(report), nil
|
||||
case coredata.SnapshotEntityType:
|
||||
snapshot, err := prb.Snapshots.Get(ctx, id)
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("cannot get snapshot: %w", err))
|
||||
}
|
||||
return types.NewSnapshot(snapshot), nil
|
||||
case coredata.TrustCenterEntityType:
|
||||
trustCenter, err := prb.TrustCenters.Get(ctx, id)
|
||||
if err != nil {
|
||||
@@ -3823,6 +3893,39 @@ func (r *riskConnectionResolver) TotalCount(ctx context.Context, obj *types.Risk
|
||||
panic(fmt.Errorf("unsupported resolver: %T", obj.Resolver))
|
||||
}
|
||||
|
||||
// Organization is the resolver for the organization field.
|
||||
func (r *snapshotResolver) Organization(ctx context.Context, obj *types.Snapshot) (*types.Organization, error) {
|
||||
prb := r.ProboService(ctx, obj.ID.TenantID())
|
||||
|
||||
snapshot, err := prb.Snapshots.Get(ctx, obj.ID)
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("cannot get snapshot: %w", err))
|
||||
}
|
||||
|
||||
organization, err := prb.Organizations.Get(ctx, snapshot.OrganizationID)
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("cannot get organization: %w", err))
|
||||
}
|
||||
|
||||
return types.NewOrganization(organization), nil
|
||||
}
|
||||
|
||||
// TotalCount is the resolver for the totalCount field.
|
||||
func (r *snapshotConnectionResolver) TotalCount(ctx context.Context, obj *types.SnapshotConnection) (int, error) {
|
||||
prb := r.ProboService(ctx, obj.ParentID.TenantID())
|
||||
|
||||
switch obj.Resolver.(type) {
|
||||
case *organizationResolver:
|
||||
count, err := prb.Snapshots.CountForOrganizationID(ctx, obj.ParentID)
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("cannot count snapshots: %w", err))
|
||||
}
|
||||
return count, nil
|
||||
}
|
||||
|
||||
panic(fmt.Errorf("unsupported resolver: %T", obj.Resolver))
|
||||
}
|
||||
|
||||
// AssignedTo is the resolver for the assignedTo field.
|
||||
func (r *taskResolver) AssignedTo(ctx context.Context, obj *types.Task) (*types.People, error) {
|
||||
prb := r.ProboService(ctx, obj.ID.TenantID())
|
||||
@@ -4470,6 +4573,14 @@ func (r *Resolver) Risk() schema.RiskResolver { return &riskResolver{r} }
|
||||
// RiskConnection returns schema.RiskConnectionResolver implementation.
|
||||
func (r *Resolver) RiskConnection() schema.RiskConnectionResolver { return &riskConnectionResolver{r} }
|
||||
|
||||
// Snapshot returns schema.SnapshotResolver implementation.
|
||||
func (r *Resolver) Snapshot() schema.SnapshotResolver { return &snapshotResolver{r} }
|
||||
|
||||
// SnapshotConnection returns schema.SnapshotConnectionResolver implementation.
|
||||
func (r *Resolver) SnapshotConnection() schema.SnapshotConnectionResolver {
|
||||
return &snapshotConnectionResolver{r}
|
||||
}
|
||||
|
||||
// Task returns schema.TaskResolver implementation.
|
||||
func (r *Resolver) Task() schema.TaskResolver { return &taskResolver{r} }
|
||||
|
||||
@@ -4548,6 +4659,8 @@ type queryResolver struct{ *Resolver }
|
||||
type reportResolver struct{ *Resolver }
|
||||
type riskResolver struct{ *Resolver }
|
||||
type riskConnectionResolver struct{ *Resolver }
|
||||
type snapshotResolver struct{ *Resolver }
|
||||
type snapshotConnectionResolver struct{ *Resolver }
|
||||
type taskResolver struct{ *Resolver }
|
||||
type taskConnectionResolver struct{ *Resolver }
|
||||
type trustCenterResolver struct{ *Resolver }
|
||||
|
||||
Reference in New Issue
Block a user