paisa/internal/model/model.go

44 lines
1.1 KiB
Go
Raw Normal View History

package model
import (
2022-04-03 09:54:15 -04:00
"strconv"
"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"
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")
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)
}
}