-
Notifications
You must be signed in to change notification settings - Fork 115
/
model.go
120 lines (101 loc) · 3.15 KB
/
model.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
package model
import (
"strings"
"github.com/ananthakumaran/paisa/internal/config"
"github.com/ananthakumaran/paisa/internal/ledger"
"github.com/ananthakumaran/paisa/internal/model/cii"
"github.com/ananthakumaran/paisa/internal/model/commodity"
mutualfundModel "github.com/ananthakumaran/paisa/internal/model/mutualfund/scheme"
npsModel "github.com/ananthakumaran/paisa/internal/model/nps/scheme"
"github.com/ananthakumaran/paisa/internal/model/portfolio"
"github.com/ananthakumaran/paisa/internal/model/posting"
"github.com/ananthakumaran/paisa/internal/model/price"
"github.com/ananthakumaran/paisa/internal/scraper/india"
"github.com/ananthakumaran/paisa/internal/scraper/mutualfund"
"github.com/ananthakumaran/paisa/internal/scraper/nps"
"github.com/ananthakumaran/paisa/internal/scraper/stock"
log "github.com/sirupsen/logrus"
"gorm.io/gorm"
)
func AutoMigrate(db *gorm.DB) {
db.AutoMigrate(&npsModel.Scheme{})
db.AutoMigrate(&mutualfundModel.Scheme{})
db.AutoMigrate(&posting.Posting{})
db.AutoMigrate(&price.Price{})
db.AutoMigrate(&portfolio.Portfolio{})
db.AutoMigrate(&price.Price{})
db.AutoMigrate(&cii.CII{})
}
func SyncJournal(db *gorm.DB) (string, error) {
AutoMigrate(db)
log.Info("Syncing transactions from journal")
errors, _, err := ledger.Cli().ValidateFile(config.GetJournalPath())
if err != nil {
if len(errors) == 0 {
return err.Error(), err
}
var message string
for _, error := range errors {
message += error.Message + "\n\n"
}
return strings.TrimRight(message, "\n"), err
}
prices, err := ledger.Cli().Prices(config.GetJournalPath())
if err != nil {
return err.Error(), err
}
price.UpsertAllByType(db, config.Unknown, prices)
postings, err := ledger.Cli().Parse(config.GetJournalPath(), prices)
if err != nil {
return err.Error(), err
}
posting.UpsertAll(db, postings)
return "", nil
}
func SyncCommodities(db *gorm.DB) {
AutoMigrate(db)
log.Info("Fetching commodities price history")
commodities := commodity.All()
for _, commodity := range commodities {
name := commodity.Name
log.Info("Fetching commodity ", name)
code := commodity.Price.Code
var prices []*price.Price
var err error
switch commodity.Type {
case config.MutualFund:
prices, err = mutualfund.GetNav(code, name)
case config.NPS:
prices, err = nps.GetNav(code, name)
case config.Stock:
prices, err = stock.GetHistory(code, name)
}
if err != nil {
log.Fatal(err)
}
price.UpsertAllByTypeAndID(db, commodity.Type, code, prices)
}
}
func SyncCII(db *gorm.DB) {
AutoMigrate(db)
log.Info("Fetching taxation related info")
ciis, err := india.GetCostInflationIndex()
if err != nil {
log.Fatal(err)
}
cii.UpsertAll(db, ciis)
}
func SyncPortfolios(db *gorm.DB) {
db.AutoMigrate(&portfolio.Portfolio{})
log.Info("Fetching commodities portfolio")
commodities := commodity.FindByType(config.MutualFund)
for _, commodity := range commodities {
name := commodity.Name
log.Info("Fetching portfolio for ", name)
portfolios, err := mutualfund.GetPortfolio(commodity.Price.Code, commodity.Name)
if err != nil {
log.Fatal(err)
}
portfolio.UpsertAll(db, commodity.Type, commodity.Price.Code, portfolios)
}
}