kmm

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 31, 2022 License: Apache-2.0 Imports: 6 Imported by: 0

README

Kids money manager

KMM provides a way for kids to learn how to manage money. Money can be (logically) deposited to the account by parents/guardians, and kids can withdraw up to a certain amount per day/week/month that their parent(s) defines.

This is a demo application for the series Implementing an event store on NATS. It utilizes rita which was developed as part of this series and provides an event store implementation on NATS and various event-centric patterns.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrUnknownCommand     = errors.New("unknown command")
	ErrNonZeroAmount      = errors.New("kmm: amount must be greater than zero")
	ErrInvalidPeriod      = errors.New("kmm: period must be minutely, daily, weekly, monthly")
	ErrInsufficientFunds  = errors.New("kmm: insufficient funds")
	ErrExceedWithinPeriod = errors.New("kmm: withdrawal would exceed max amount allowed in current period")
)
View Source
var (
	Types = map[string]*types.Type{

		"deposit-funds":   {Init: func() any { return &DepositFunds{} }},
		"funds-deposited": {Init: func() any { return &FundsDeposited{} }},
		"withdraw-funds":  {Init: func() any { return &WithdrawFunds{} }},
		"funds-withdrawn": {Init: func() any { return &FundsWithdrawn{} }},
		"set-budget":      {Init: func() any { return &SetBudget{} }},
		"budget-set":      {Init: func() any { return &BudgetSet{} }},
		"remove-budget":   {Init: func() any { return &RemoveBudget{} }},
		"budget-removed":  {Init: func() any { return &BudgetRemoved{} }},

		"account": {Init: func() any { return NewAccount() }},

		"current-funds": {Init: func() any { return &CurrentFunds{} }},
		"budget-period": {Init: func() any { return &BudgetPeriod{} }},
	}
)

Functions

This section is empty.

Types

type Account

type Account struct {
	CurrentFunds decimal.Decimal

	// Policy related.
	MaxWithdrawAmount      decimal.Decimal
	PolicyPeriod           Period
	PeriodStartTime        time.Time
	NextPeriodStartTime    time.Time
	FundsWithdrawnInPeriod decimal.Decimal
	// contains filtered or unexported fields
}

Account aggregate which primarily decides on whether a withdrawal is allowed given the current funds and if a policy is set.

The DepositFunds command doesn't even need an aggregate since there is no validation other than command validation that the amount is a positive value (which is done prior to the command being received here).

The Set/RemoveWithdrawPolicy are in the same category and does not really need any aggregated state for them to be accepted.

func NewAccount

func NewAccount() *Account

func (*Account) Decide

func (a *Account) Decide(command *rita.Command) ([]*rita.Event, error)

func (*Account) Evolve

func (a *Account) Evolve(event *rita.Event) error

type BudgetPeriod

type BudgetPeriod struct {
	PolicyPeriod            Period
	PolicyStartTime         time.Time
	PolicyMaxWithdrawAmount decimal.Decimal
	WithdrawalsInPeriod     int
	FundsWithdrawnInPeriod  decimal.Decimal
	PeriodStartTime         time.Time
	NextPeriodStartTime     time.Time
}

func (*BudgetPeriod) Evolve

func (p *BudgetPeriod) Evolve(event *rita.Event) error

type BudgetRemoved

type BudgetRemoved struct {
	PolicyRemoveTime time.Time
}

type BudgetSet

type BudgetSet struct {
	MaxWithdrawAmount   decimal.Decimal
	Period              Period
	PolicyStartTime     time.Time
	PeriodStartTime     time.Time
	NextPeriodStartTime time.Time
}

type CurrentFunds

type CurrentFunds struct {
	Amount decimal.Decimal
}

func (*CurrentFunds) Evolve

func (c *CurrentFunds) Evolve(event *rita.Event) error

type DeciderEvolver

type DeciderEvolver interface {
	rita.Decider
	rita.Evolver
}

type DepositFunds

type DepositFunds struct {
	Amount      decimal.Decimal
	Description string
}

func (*DepositFunds) Validate

func (c *DepositFunds) Validate() error

type FundsDeposited

type FundsDeposited struct {
	Amount      decimal.Decimal
	Description string
	Time        time.Time
}

type FundsWithdrawn

type FundsWithdrawn struct {
	Amount        decimal.Decimal
	Description   string
	Time          time.Time
	PeriodChanged bool
}

type Period

type Period string
const (
	Minutely Period = "minutely" // For demo...
	Daily    Period = "daily"
	Weekly   Period = "weekly"
	Monthly  Period = "monthly"
)

type RemoveBudget

type RemoveBudget struct{}

type SetBudget

type SetBudget struct {
	MaxAmount decimal.Decimal
	Period    Period
}

func (*SetBudget) Validate

func (c *SetBudget) Validate() error

type WithdrawFunds

type WithdrawFunds struct {
	Amount      decimal.Decimal
	Description string
}

func (*WithdrawFunds) Validate

func (c *WithdrawFunds) Validate() error

Directories

Path Synopsis
cmd
kmm

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL