-
Notifications
You must be signed in to change notification settings - Fork 0
/
account.go
125 lines (107 loc) · 2.93 KB
/
account.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package omoney
import (
// "errors"
"fmt"
// "sort"
"time"
"github.com/google/uuid"
)
type AccountType string
const (
UnknownAccount = "unknown"
Checking = "checking"
Savings = "savings"
CreditCard = "creditCard"
Investment = "investment"
PersonalLoan = "personalLoan"
)
type Account struct {
// Unique identifier for account within this program.
// May be copied from Plaid or generated by this program.
// Required field.
Id string `bun:",pk"`
// English nickname for account within this program.
// Optional field that defaults to empty string.
Alias string `bun:",unique"`
// Plaid generated key for getting data on this account.
// Defaults to empty string if account was manually created.
PlaidToken string
Type AccountType
// Transactions []*Transaction
// The known value of this account at the time specified
// in `AnchorTime`. Optional field that defaults to 0
AnchorBalance float64 `json:"AnchorBalance"`
// The time specified for the known value `AnchorBalance`.
// Optional field that defaults to time.Now()
AnchorTime time.Time `json:"AnchorTime"`
// The calculated current balance of this account
// CurrentBalance float64
// The time at which `CurrentBalance` was last calculated
}
type AccountOption func(*Account)
func NewAccount(options ...AccountOption) *Account {
acc := &Account{
Id: uuid.New().String(),
Type: UnknownAccount,
AnchorTime: time.Now().Truncate(time.Second),
}
for _, op := range options {
op(acc)
}
return acc
}
func WithPlaidIds(itemId string, accessToken string) AccountOption {
return func(acc *Account) {
acc.Id = itemId
acc.PlaidToken = accessToken
}
}
func WithAlias(alias string) AccountOption {
return func(acc *Account) {
acc.Alias = alias
}
}
func WithAnchor(balance float64, time time.Time) AccountOption {
return func(acc *Account) {
acc.AnchorBalance = balance
acc.AnchorTime = time
}
}
func WithAccountType(accType AccountType) AccountOption {
return func(acc *Account) {
acc.Type = accType
}
}
func ParseAccountType(input string) (AccountType, error) {
switch input {
case "ch", "checking":
return Checking, nil
case "sa", "savings":
return Savings, nil
case "cc", "credit":
return CreditCard, nil
case "in", "investment":
return Investment, nil
case "pl", "personalLoan":
return PersonalLoan, nil
default:
return UnknownAccount, fmt.Errorf("account type %s not recognized", input)
}
}
func (acc *Account) GetAnchor() (float64, time.Time) {
return acc.AnchorBalance, acc.AnchorTime
}
func (acc *Account) GetAnchorBalance() float64 {
return acc.AnchorBalance
}
func (acc *Account) GetAnchorTime() time.Time {
return acc.AnchorTime
}
func (a *Account) LooseEquals(other *Account) bool {
return a.AnchorTime.Equal(other.AnchorTime) &&
a.Alias == other.Alias &&
a.AnchorBalance == other.AnchorBalance &&
a.Id == other.Id &&
a.PlaidToken == other.PlaidToken &&
a.Type == other.Type
}