@@ -53,6 +53,12 @@ const (
|
||||
ActionComplianceFrameworkDelete = "core:compliance-framework:delete"
|
||||
ActionComplianceFrameworkUpdateRank = "core:compliance-framework:update-rank"
|
||||
|
||||
// ComplianceExternalURL actions
|
||||
ActionComplianceExternalURLList = "core:compliance-external-url:list"
|
||||
ActionComplianceExternalURLCreate = "core:compliance-external-url:create"
|
||||
ActionComplianceExternalURLUpdate = "core:compliance-external-url:update"
|
||||
ActionComplianceExternalURLDelete = "core:compliance-external-url:delete"
|
||||
|
||||
// TrustCenterFile actions
|
||||
ActionTrustCenterFileGet = "core:trust-center-file:get"
|
||||
ActionTrustCenterFileList = "core:trust-center-file:list"
|
||||
|
||||
195
pkg/probo/compliance_external_url_service.go
Normal file
195
pkg/probo/compliance_external_url_service.go
Normal file
@@ -0,0 +1,195 @@
|
||||
package probo
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"go.gearno.de/kit/pg"
|
||||
"go.probo.inc/probo/pkg/coredata"
|
||||
"go.probo.inc/probo/pkg/gid"
|
||||
"go.probo.inc/probo/pkg/page"
|
||||
"go.probo.inc/probo/pkg/validator"
|
||||
)
|
||||
|
||||
type (
|
||||
ComplianceExternalURLService struct {
|
||||
svc *TenantService
|
||||
}
|
||||
|
||||
CreateComplianceExternalURLRequest struct {
|
||||
TrustCenterID gid.GID
|
||||
Name string
|
||||
URL string
|
||||
}
|
||||
|
||||
UpdateComplianceExternalURLRequest struct {
|
||||
ID gid.GID
|
||||
Name string
|
||||
URL string
|
||||
Rank *int
|
||||
}
|
||||
|
||||
DeleteComplianceExternalURLRequest struct {
|
||||
ID gid.GID
|
||||
}
|
||||
)
|
||||
|
||||
func (r *CreateComplianceExternalURLRequest) Validate() error {
|
||||
v := validator.New()
|
||||
v.Check(r.TrustCenterID, "trust_center_id", validator.Required(), validator.GID(coredata.TrustCenterEntityType))
|
||||
v.Check(r.URL, "url", validator.Required(), validator.URL())
|
||||
return v.Error()
|
||||
}
|
||||
|
||||
func (r *UpdateComplianceExternalURLRequest) Validate() error {
|
||||
v := validator.New()
|
||||
v.Check(r.ID, "id", validator.Required(), validator.GID(coredata.ComplianceExternalURLEntityType))
|
||||
v.Check(r.URL, "url", validator.Required(), validator.URL())
|
||||
v.Check(r.Rank, "rank", validator.Min(1))
|
||||
return v.Error()
|
||||
}
|
||||
|
||||
func (r *DeleteComplianceExternalURLRequest) Validate() error {
|
||||
v := validator.New()
|
||||
v.Check(r.ID, "id", validator.Required(), validator.GID(coredata.ComplianceExternalURLEntityType))
|
||||
return v.Error()
|
||||
}
|
||||
|
||||
func (s ComplianceExternalURLService) List(
|
||||
ctx context.Context,
|
||||
trustCenterID gid.GID,
|
||||
cursor *page.Cursor[coredata.ComplianceExternalURLOrderField],
|
||||
) (*page.Page[*coredata.ComplianceExternalURL, coredata.ComplianceExternalURLOrderField], error) {
|
||||
var items coredata.ComplianceExternalURLs
|
||||
|
||||
err := s.svc.pg.WithConn(
|
||||
ctx,
|
||||
func(conn pg.Conn) error {
|
||||
if err := items.LoadByTrustCenterID(ctx, conn, s.svc.scope, trustCenterID, cursor); err != nil {
|
||||
return fmt.Errorf("cannot load compliance external URLs: %w", err)
|
||||
}
|
||||
return nil
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return page.NewPage(items, cursor), nil
|
||||
}
|
||||
|
||||
func (s ComplianceExternalURLService) Create(
|
||||
ctx context.Context,
|
||||
req *CreateComplianceExternalURLRequest,
|
||||
) (*coredata.ComplianceExternalURL, error) {
|
||||
if err := req.Validate(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
now := time.Now()
|
||||
id := gid.New(s.svc.scope.GetTenantID(), coredata.ComplianceExternalURLEntityType)
|
||||
|
||||
var item *coredata.ComplianceExternalURL
|
||||
|
||||
err := s.svc.pg.WithTx(
|
||||
ctx,
|
||||
func(tx pg.Conn) error {
|
||||
trustCenter := &coredata.TrustCenter{}
|
||||
if err := trustCenter.LoadByID(ctx, tx, s.svc.scope, req.TrustCenterID); err != nil {
|
||||
return fmt.Errorf("cannot load trust center: %w", err)
|
||||
}
|
||||
|
||||
item = &coredata.ComplianceExternalURL{
|
||||
ID: id,
|
||||
OrganizationID: trustCenter.OrganizationID,
|
||||
TrustCenterID: req.TrustCenterID,
|
||||
Name: req.Name,
|
||||
URL: req.URL,
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
}
|
||||
|
||||
if err := item.Insert(ctx, tx, s.svc.scope); err != nil {
|
||||
return fmt.Errorf("cannot insert compliance external URL: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return item, nil
|
||||
}
|
||||
|
||||
func (s ComplianceExternalURLService) Update(
|
||||
ctx context.Context,
|
||||
req *UpdateComplianceExternalURLRequest,
|
||||
) (*coredata.ComplianceExternalURL, error) {
|
||||
if err := req.Validate(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var item *coredata.ComplianceExternalURL
|
||||
|
||||
err := s.svc.pg.WithTx(
|
||||
ctx,
|
||||
func(tx pg.Conn) error {
|
||||
item = &coredata.ComplianceExternalURL{}
|
||||
|
||||
if err := item.LoadByID(ctx, tx, s.svc.scope, req.ID); err != nil {
|
||||
return fmt.Errorf("cannot load compliance external URL: %w", err)
|
||||
}
|
||||
|
||||
item.Name = req.Name
|
||||
item.URL = req.URL
|
||||
item.UpdatedAt = time.Now()
|
||||
|
||||
if req.Rank != nil {
|
||||
item.Rank = *req.Rank
|
||||
if err := item.UpdateRank(ctx, tx, s.svc.scope); err != nil {
|
||||
return fmt.Errorf("cannot update compliance external URL rank: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
if err := item.Update(ctx, tx, s.svc.scope); err != nil {
|
||||
return fmt.Errorf("cannot update compliance external URL: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return item, nil
|
||||
}
|
||||
|
||||
func (s ComplianceExternalURLService) Delete(
|
||||
ctx context.Context,
|
||||
req *DeleteComplianceExternalURLRequest,
|
||||
) error {
|
||||
if err := req.Validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return s.svc.pg.WithTx(
|
||||
ctx,
|
||||
func(tx pg.Conn) error {
|
||||
item := &coredata.ComplianceExternalURL{}
|
||||
|
||||
if err := item.LoadByID(ctx, tx, s.svc.scope, req.ID); err != nil {
|
||||
return fmt.Errorf("cannot load compliance external URL: %w", err)
|
||||
}
|
||||
|
||||
if err := item.Delete(ctx, tx, s.svc.scope); err != nil {
|
||||
return fmt.Errorf("cannot delete compliance external URL: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
)
|
||||
}
|
||||
@@ -101,6 +101,7 @@ type (
|
||||
TrustCenterReferences *TrustCenterReferenceService
|
||||
TrustCenterFiles *TrustCenterFileService
|
||||
ComplianceFrameworks *ComplianceFrameworkService
|
||||
ComplianceExternalURLs *ComplianceExternalURLService
|
||||
Nonconformities *NonconformityService
|
||||
Obligations *ObligationService
|
||||
Snapshots *SnapshotService
|
||||
@@ -226,6 +227,7 @@ func (s *Service) WithTenant(tenantID gid.TenantID) *TenantService {
|
||||
tenantService.TrustCenterAccesses = &TrustCenterAccessService{svc: tenantService}
|
||||
tenantService.TrustCenterReferences = &TrustCenterReferenceService{svc: tenantService}
|
||||
tenantService.ComplianceFrameworks = &ComplianceFrameworkService{svc: tenantService}
|
||||
tenantService.ComplianceExternalURLs = &ComplianceExternalURLService{svc: tenantService}
|
||||
tenantService.TrustCenterFiles = &TrustCenterFileService{
|
||||
svc: tenantService,
|
||||
fileValidator: filevalidation.NewValidator(
|
||||
|
||||
@@ -178,7 +178,6 @@ func (s TrustCenterService) Update(
|
||||
if req.Slug != nil {
|
||||
trustCenter.Slug = *req.Slug
|
||||
}
|
||||
|
||||
trustCenter.UpdatedAt = time.Now()
|
||||
|
||||
if err := trustCenter.Update(ctx, conn, s.svc.scope); err != nil {
|
||||
|
||||
Reference in New Issue
Block a user