2022-03-27 03:24:25 -04:00
|
|
|
package model
|
|
|
|
|
|
|
|
import (
|
2022-04-03 09:54:15 -04:00
|
|
|
"strconv"
|
|
|
|
|
2022-03-27 03:24:25 -04:00
|
|
|
"github.com/ananthakumaran/paisa/internal/ledger"
|
|
|
|
"github.com/ananthakumaran/paisa/internal/model/posting"
|
2022-04-03 09:54:15 -04:00
|
|
|
"github.com/ananthakumaran/paisa/internal/model/price"
|
|
|
|
"github.com/ananthakumaran/paisa/internal/scraper/mutualfund"
|
|
|
|
"github.com/logrusorgru/aurora"
|
2022-03-27 03:24:25 -04:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Sync(db *gorm.DB) {
|
|
|
|
db.AutoMigrate(&posting.Posting{})
|
2022-04-14 03:17:42 -04:00
|
|
|
log.Info("Syncing transactions from journal")
|
2022-03-27 03:24:25 -04:00
|
|
|
postings, _ := ledger.Parse(viper.GetString("journal_path"))
|
|
|
|
posting.UpsertAll(db, postings)
|
2022-04-03 09:54:15 -04:00
|
|
|
|
|
|
|
db.AutoMigrate(&price.Price{})
|
|
|
|
log.Info("Fetching mutual fund history")
|
2022-04-09 03:01:12 -04:00
|
|
|
type Commodity struct {
|
|
|
|
Name string
|
|
|
|
Type string
|
|
|
|
Code int
|
|
|
|
}
|
|
|
|
|
|
|
|
var commodities []Commodity
|
|
|
|
viper.UnmarshalKey("commodities", &commodities)
|
|
|
|
for _, commodity := range commodities {
|
|
|
|
name := commodity.Name
|
|
|
|
log.Info("Fetching commodity ", aurora.Bold(name))
|
|
|
|
schemeCode := strconv.Itoa(commodity.Code)
|
|
|
|
prices, err := mutualfund.GetNav(schemeCode, name)
|
2022-04-03 09:54:15 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
price.UpsertAll(db, price.MutualFund, schemeCode, prices)
|
|
|
|
}
|
2022-03-27 03:24:25 -04:00
|
|
|
}
|