Files
probo/pkg/probo/asset_service.go
Sacha Al Himdani 4c57d201a4 Make license declarations consistently MIT
The source headers, LICENSE files, and license metadata had drifted
apart. Align the entire project to MIT:

- Convert every source-file header to the MIT text across all comment
  styles (Go, TS, TSX, JS, MJS, SQL, CSS, GraphQL, shell), including
  SPDX-License-Identifier tags
- Set the root and cookie-banner LICENSE files to the MIT text with a
  "MIT License" title line
- Switch the package.json license fields, Docker image label, and
  cookie-banner README to MIT
- Update docs and the genmodels header generator accordingly
- Normalize copyright lines to a single format
  (Copyright (c) <year(s)> Probo Inc <hello@probo.com>.): unify the
  hello@getprobo.com and hello@probo.inc emails to hello@probo.com and
  the comma-separated years to a hyphenated range

Genuine third-party references are intentionally left untouched: the
Lucide icon attributions (Lucide is ISC) and the trivy dependency
license allowlist.

Signed-off-by: Sacha Al Himdani <sacha@probo.com>
2026-07-13 16:21:14 +02:00

317 lines
8.6 KiB
Go

// Copyright (c) 2025-2026 Probo Inc <hello@probo.com>.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
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 AssetService struct {
svc *Service
}
type CreateAssetRequest struct {
OrganizationID gid.GID
Name string
Amount int
OwnerID gid.GID
AssetType coredata.AssetType
DataTypesStored string
ThirdPartyIDs []gid.GID
}
type UpdateAssetRequest struct {
ID gid.GID
Name *string
Amount *int
OwnerID *gid.GID
AssetType *coredata.AssetType
DataTypesStored *string
ThirdPartyIDs []gid.GID
}
func (car *CreateAssetRequest) Validate() error {
v := validator.New()
v.Check(car.OrganizationID, "organization_id", validator.Required(), validator.GID(coredata.OrganizationEntityType))
v.Check(car.Name, "name", validator.Required(), validator.SafeTextNoNewLine(TitleMaxLength))
v.Check(car.Amount, "amount", validator.Required(), validator.Min(1))
v.Check(car.OwnerID, "owner_id", validator.Required(), validator.GID(coredata.MembershipProfileEntityType))
v.Check(car.AssetType, "asset_type", validator.Required(), validator.OneOfSlice(coredata.AssetTypes()))
v.Check(car.DataTypesStored, "data_types_stored", validator.Required(), validator.SafeText(ContentMaxLength))
v.CheckEach(car.ThirdPartyIDs, "third_party_ids", func(index int, item any) {
v.Check(item, fmt.Sprintf("third_party_ids[%d]", index), validator.Required(), validator.GID(coredata.ThirdPartyEntityType))
})
return v.Error()
}
func (uar *UpdateAssetRequest) Validate() error {
v := validator.New()
v.Check(uar.ID, "id", validator.Required(), validator.GID(coredata.AssetEntityType))
v.Check(uar.Name, "name", validator.SafeTextNoNewLine(NameMaxLength))
v.Check(uar.Amount, "amount", validator.Min(1))
v.Check(uar.OwnerID, "owner_id", validator.GID(coredata.MembershipProfileEntityType))
v.Check(uar.AssetType, "asset_type", validator.OneOfSlice(coredata.AssetTypes()))
v.Check(uar.DataTypesStored, "data_types_stored", validator.SafeText(ContentMaxLength))
v.CheckEach(uar.ThirdPartyIDs, "third_party_ids", func(index int, item any) {
v.Check(item, fmt.Sprintf("third_party_ids[%d]", index), validator.GID(coredata.ThirdPartyEntityType))
})
return v.Error()
}
func (s AssetService) Get(
ctx context.Context, scope coredata.Scoper,
assetID gid.GID,
) (*coredata.Asset, error) {
asset := &coredata.Asset{}
err := s.svc.pg.WithConn(
ctx,
func(ctx context.Context, conn pg.Querier) error {
return asset.LoadByID(ctx, conn, scope, assetID)
},
)
if err != nil {
return nil, err
}
return asset, nil
}
func (s AssetService) GetByOwnerID(
ctx context.Context, scope coredata.Scoper,
ownerID gid.GID,
) (*coredata.Asset, error) {
asset := &coredata.Asset{OwnerID: ownerID}
err := s.svc.pg.WithConn(
ctx,
func(ctx context.Context, conn pg.Querier) error {
return asset.LoadByOwnerID(ctx, conn, scope)
},
)
if err != nil {
return nil, err
}
return asset, nil
}
func (s AssetService) CountForOrganizationID(
ctx context.Context, scope coredata.Scoper,
organizationID gid.GID,
) (int, error) {
var count int
err := s.svc.pg.WithConn(
ctx,
func(ctx context.Context, conn pg.Querier) (err error) {
assets := coredata.Assets{}
count, err = assets.CountByOrganizationID(ctx, conn, scope, organizationID)
if err != nil {
return fmt.Errorf("cannot count assets: %w", err)
}
return nil
},
)
if err != nil {
return 0, err
}
return count, nil
}
func (s AssetService) ListForOrganizationID(
ctx context.Context, scope coredata.Scoper,
organizationID gid.GID,
cursor *page.Cursor[coredata.AssetOrderField],
) (*page.Page[*coredata.Asset, coredata.AssetOrderField], error) {
var assets coredata.Assets
err := s.svc.pg.WithConn(
ctx,
func(ctx context.Context, conn pg.Querier) error {
return assets.LoadByOrganizationID(
ctx,
conn,
scope,
organizationID,
cursor,
)
},
)
if err != nil {
return nil, err
}
return page.NewPage(assets, cursor), nil
}
func (s AssetService) Update(
ctx context.Context, scope coredata.Scoper,
req UpdateAssetRequest,
) (*coredata.Asset, error) {
if err := req.Validate(); err != nil {
return nil, fmt.Errorf("invalid request: %w", err)
}
now := time.Now()
asset := &coredata.Asset{ID: req.ID}
assetThirdParties := &coredata.AssetThirdParties{}
err := s.svc.pg.WithTx(ctx, func(ctx context.Context, conn pg.Tx) error {
if err := asset.LoadByID(ctx, conn, scope, req.ID); err != nil {
return fmt.Errorf("cannot load asset: %w", err)
}
asset.UpdatedAt = now
if req.Name != nil {
asset.Name = *req.Name
}
if req.Amount != nil {
asset.Amount = *req.Amount
}
if req.OwnerID != nil {
profile := &coredata.MembershipProfile{}
if err := profile.LoadByID(ctx, conn, scope, *req.OwnerID); err != nil {
return fmt.Errorf("cannot load owner profile: %w", err)
}
asset.OwnerID = *req.OwnerID
}
if req.AssetType != nil {
asset.AssetType = *req.AssetType
}
if req.DataTypesStored != nil {
asset.DataTypesStored = *req.DataTypesStored
}
if err := asset.Update(ctx, conn, scope); err != nil {
return fmt.Errorf("cannot update asset: %w", err)
}
if req.ThirdPartyIDs != nil {
if len(req.ThirdPartyIDs) > 0 {
thirdParties := &coredata.ThirdParties{}
if err := thirdParties.LoadByIDs(ctx, conn, scope, req.ThirdPartyIDs); err != nil {
return fmt.Errorf("cannot load thirdParties: %w", err)
}
}
if err := assetThirdParties.Merge(ctx, conn, scope, asset.ID, asset.OrganizationID, req.ThirdPartyIDs); err != nil {
return fmt.Errorf("cannot update asset thirdParties: %w", err)
}
}
return nil
})
if err != nil {
return nil, err
}
return asset, nil
}
func (s AssetService) Create(
ctx context.Context, scope coredata.Scoper,
req CreateAssetRequest,
) (*coredata.Asset, error) {
if err := req.Validate(); err != nil {
return nil, fmt.Errorf("invalid request: %w", err)
}
now := time.Now()
assetID := gid.New(scope.GetTenantID(), coredata.AssetEntityType)
assetThirdParties := &coredata.AssetThirdParties{}
asset := &coredata.Asset{
ID: assetID,
OrganizationID: req.OrganizationID,
Name: req.Name,
Amount: req.Amount,
OwnerID: req.OwnerID,
AssetType: req.AssetType,
DataTypesStored: req.DataTypesStored,
CreatedAt: now,
UpdatedAt: now,
}
err := s.svc.pg.WithTx(ctx, func(ctx context.Context, conn pg.Tx) error {
profile := &coredata.MembershipProfile{}
if err := profile.LoadByID(ctx, conn, scope, req.OwnerID); err != nil {
return fmt.Errorf("cannot load owner profile: %w", err)
}
if err := asset.Insert(ctx, conn, scope); err != nil {
return fmt.Errorf("cannot insert asset: %w", err)
}
if len(req.ThirdPartyIDs) > 0 {
thirdParties := &coredata.ThirdParties{}
if err := thirdParties.LoadByIDs(ctx, conn, scope, req.ThirdPartyIDs); err != nil {
return fmt.Errorf("cannot load thirdParties: %w", err)
}
if err := assetThirdParties.Insert(ctx, conn, scope, asset.ID, asset.OrganizationID, req.ThirdPartyIDs); err != nil {
return fmt.Errorf("cannot create asset thirdParties: %w", err)
}
}
return nil
})
if err != nil {
return nil, err
}
return asset, nil
}
func (s AssetService) Delete(
ctx context.Context, scope coredata.Scoper,
assetID gid.GID,
) error {
asset := &coredata.Asset{ID: assetID}
return s.svc.pg.WithTx(
ctx,
func(ctx context.Context, tx pg.Tx) error {
return asset.Delete(ctx, tx, scope)
},
)
}