50 lines
1.7 KiB
Go
50 lines
1.7 KiB
Go
package domain
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
// ErrInvalidHolding reports a holding that was not built with a founder
|
|
// account and a creation time.
|
|
var ErrInvalidHolding = errors.New("invalid holding")
|
|
|
|
// Holding groups the organizations that one founder account created, as the
|
|
// divisions of one company belong to one parent. It carries no profile and no
|
|
// right: its only effect is the E0 quota pool that its organizations share.
|
|
// Organizations of one account are grouped; cross-owner merges are not
|
|
// modelled.
|
|
type Holding struct {
|
|
id uuid.UUID
|
|
founderAccountID uuid.UUID
|
|
createdAt time.Time
|
|
}
|
|
|
|
// NewHolding creates the holding of a founder account with a time-ordered
|
|
// UUIDv7 identifier. The storage keeps one holding per founder and returns the
|
|
// stored one when the account already has it, so the identifier of this value
|
|
// is only used when the holding is new.
|
|
func NewHolding(founderAccountID uuid.UUID, createdAt time.Time) (Holding, error) {
|
|
const location = "domain.NewHolding"
|
|
if founderAccountID == uuid.Nil {
|
|
return Holding{}, fmt.Errorf("%s - founder account must be set: %w", location, ErrInvalidHolding)
|
|
}
|
|
if createdAt.IsZero() {
|
|
return Holding{}, fmt.Errorf("%s - creation time must be set: %w", location, ErrInvalidHolding)
|
|
}
|
|
id, err := uuid.NewV7()
|
|
if err != nil {
|
|
return Holding{}, fmt.Errorf("%s - generate holding ID: %w", location, err)
|
|
}
|
|
return Holding{id: id, founderAccountID: founderAccountID, createdAt: createdAt.UTC()}, nil
|
|
}
|
|
|
|
func (holding Holding) ID() uuid.UUID { return holding.id }
|
|
|
|
func (holding Holding) FounderAccountID() uuid.UUID { return holding.founderAccountID }
|
|
|
|
func (holding Holding) CreatedAt() time.Time { return holding.createdAt }
|