bursary

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2023 License: Apache-2.0 Imports: 4 Imported by: 0

README

bursary

This package is used to manage agency relationship and calculate benefits between members for multiple levels.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrMemberRequired   = errors.New("bursary: require member")
	ErrMemberNotFound   = errors.New("bursary: member not found")
	ErrUpstreamNotFound = errors.New("bursary: upstream not found")
)
View Source
var DefaultRule = Rule{
	Commission:    0.0,
	Share:         0.0,
	ReturnedShare: 0.0,
}
View Source
var (
	ErrLedgerNotFound = errors.New("bursary: ledger not found")
)

Functions

This section is empty.

Types

type Bursary

type Bursary interface {
	RelationManager() RelationManager
	LedgerManager() LedgerManager
	GeneralLedger() Ledger
	GetLevels(memberId string) ([]*Member, error)
	CalculateRewards(t *Ticket) ([]*LedgerEntry, error)
	WriteTicket(t *Ticket) error
	WriteEntry(le *LedgerEntry) error
	WriteEntries(ledgerName string, entries []*LedgerEntry) error
	Close() error
}

func NewBursary

func NewBursary(opts ...Opt) Bursary

type Condition

type Condition struct {
	Page      int          `json:"page"`
	Limit     int          `json:"limit"`
	TimeRange *TimeRange   `json:"timeRange,omitempty"`
	Sort      []*SortField `json:"sort,omitempty"`
}

func NewCondition

func NewCondition() *Condition

type Ledger

type Ledger interface {
	WriteRecords(entries []*LedgerEntry) error
	ReadRecordsByMemberID(memberID string, cond *Condition) ([]*LedgerEntry, error)
}

func NewLedgerMemory

func NewLedgerMemory() Ledger

type LedgerEntry

type LedgerEntry struct {
	ID              string                 `json:"id"`
	Channel         string                 `json:"channel"`
	Upstream        string                 `json:"upstream"`
	MemberID        string                 `json:"member_id"`
	Contributor     string                 `json:"contributor"`
	Expense         int64                  `json:"expense"`
	Income          int64                  `json:"income"`
	Amount          int64                  `json:"amount"` // income - expense
	Fee             int64                  `json:"fee"`    // original fee
	Share           float64                `json:"share"`
	ReturnedShare   float64                `json:"returned_share"`
	CommissionShare float64                `json:"commission_share"`
	Gain            int64                  `json:"gain"`        // amount * (share + returned share by downstream)
	Commissions     int64                  `json:"commissions"` // fee * commission share
	Contributions   int64                  `json:"contributions"`
	Total           int64                  `json:"total"` // profit + commissions
	Desc            string                 `json:"desc"`
	Info            map[string]interface{} `json:"info"`
	PrimaryID       string                 `json:"primary_id"`
	IsPrimary       bool                   `json:"is_primary"`
	CreatedAt       time.Time              `json:"created_at"`
}

type LedgerManager

type LedgerManager interface {
	Add(name string, l Ledger) error
	Get(name string) (Ledger, error)
	Delete(name string) error
}

func NewLedgerManager

func NewLedgerManager() LedgerManager

type Member

type Member struct {
	ID           string           `json:"id"`
	ChannelRules map[string]*Rule `json:"channel_rules"`
	RelationPath []string         `json:"relation_path"`
	Upstream     string           `json:"upstream"`
}

func (*Member) GetChannelRule

func (m *Member) GetChannelRule(channel string) *Rule

type MemberEntry

type MemberEntry struct {
	ID           string           `json:"id"`
	ChannelRules map[string]*Rule `json:"channel_rules"`
}

func NewMemberEntry

func NewMemberEntry() *MemberEntry

type Opt

type Opt func(*bursary)

func WithGeneralLedger

func WithGeneralLedger(l Ledger) Opt

func WithLedgerManager

func WithLedgerManager(lm LedgerManager) Opt

func WithRelationManager

func WithRelationManager(rm RelationManager) Opt

type RelationManager

type RelationManager interface {
	AddMembers(members []*MemberEntry, upstream string) error
	ChangePath(mid string, newPath []string) error
	DeleteMembers(mids []string) error
	GetPath(mid string) ([]string, error)
	GetMember(mid string) (*Member, error)
	GetUpstreams(mid string) ([]*Member, error)
	MoveMembers(mids []string, upstream string) error
	ListMembers(upstream string, cond *Condition) ([]*Member, error)
	UpdateChannelRule(mid string, channel string, rule *Rule) error
	RemoveChannelRule(mid string, channel string) error
	RemoveChannel(channel string) error
	Close() error
}

func NewRelationManagerMemory

func NewRelationManagerMemory() RelationManager

type Rule

type Rule struct {
	Commission    float64 `json:"commission"`
	Share         float64 `json:"share"`
	ReturnedShare float64 `json:"returned_share"`
}

type SortField

type SortField struct {
	Field     string `json:"field"`
	Ascending bool   `json:"asc"`
}

type Ticket

type Ticket struct {
	ID        string                 `json:"id"`
	Channel   string                 `json:"channel"`
	MemberID  string                 `json:"member_id"`
	Expense   int64                  `json:"expense"`
	Income    int64                  `json:"income"`
	Amount    int64                  `json:"amount"` // Amount = Income - Expense
	Fee       int64                  `json:"fee"`
	Total     int64                  `json:"total"` // Amount + Fee
	Desc      string                 `json:"desc"`
	Info      map[string]interface{} `json:"info"`
	CreatedAt time.Time              `json:"created_at"`
}

func NewTicket

func NewTicket() *Ticket

type TimeRange

type TimeRange struct {
	StartTime time.Time `json:"startTime"`
	EndTime   time.Time `json:"endTime"`
}

Directories

Path Synopsis
relation_manager

Jump to

Keyboard shortcuts

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