Skip to content

Commit

Permalink
fix(sync): remove hardcoded account name and use passed in value
Browse files Browse the repository at this point in the history
  • Loading branch information
will7200 committed Jun 2, 2021
1 parent fce0562 commit 7557eb1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
8 changes: 5 additions & 3 deletions cmd/crypto-sync/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,15 @@ func (s *SyncCmd) Run(ctx *Context) error {
raw := personCapitalValues.Execute(ctx.Tree)
email := raw.Values()[0].(*toml.Tree).Get("email").(string)
password := raw.Values()[0].(*toml.Tree).Get("password").(string)
accountName := raw.Values()[0].(*toml.Tree).Get("accountName").(string)
cfg := personalcapital.NewConfiguration()
cfg.Debug = ctx.Debug
cfg.Logger = ctx.Logger
pc.Sync(pc.SyncParams{
Email: email,
Password: password,
Operations: s.holdingOperations(),
Email: email,
Password: password,
Operations: s.holdingOperations(),
AccountName: accountName,
}, cfg, allHoldings, pricingData)
}
return nil
Expand Down
15 changes: 11 additions & 4 deletions internal/pc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"net/http/cookiejar"
"net/url"
"os"
"reflect"
"strings"
"time"

Expand Down Expand Up @@ -64,6 +65,7 @@ func askFor2FA() (string, error) {

type SyncParams struct {
Email, Password string
AccountName string
Operations common.HoldingOperation
}

Expand All @@ -75,7 +77,7 @@ func Sync(params SyncParams, cfg *personalcapital.Configuration, holds providers
var cookies []*http.Cookie
err := personalcapital.LoadSession(&cookies, "cookies.json")
if err != nil {
log.Error(err)
log.Warn(err)
}

// Setup a cookie jar so the session can be saved
Expand Down Expand Up @@ -146,11 +148,11 @@ func Sync(params SyncParams, cfg *personalcapital.Configuration, holds providers
} else {
err := personalcapital.SaveSession(client, cfg.Host, "cookies.json")
if err != nil {
log.Error(err)
log.Warn(err)
}
}
apiClient.CSRF = &resp.CRSF
pcHoldings, err := apiClient.Holdings.GetHoldings(context.Background(), &personalcapital.GetHoldingsParams{FilterUserCreated: true, FilterAccountName: "CryptoSync managed automatically"})
pcHoldings, err := apiClient.Holdings.GetHoldings(context.Background(), &personalcapital.GetHoldingsParams{FilterUserCreated: true, FilterAccountName: params.AccountName})
if err != nil {
log.Fatal(err)
}
Expand All @@ -160,10 +162,15 @@ func Sync(params SyncParams, cfg *personalcapital.Configuration, holds providers
}
var account personalcapital.AccountType
for index, account1 := range accounts.SpData.Accounts {
if account1.Name == "CryptoSync managed automatically" && account1.AccountTypeNew == "CRYPTO_CURRENCY" {
if account1.Name == params.AccountName {
account = accounts.SpData.Accounts[index]
break
}
}
if reflect.DeepEqual(personalcapital.AccountType{}, account) {
log.Fatalf("Was not able to find account with the name of %s in Personal Capital", params.AccountName)
}
log.Debugf("Using account number %d from Personal Capital to sync holdings", account.UserAccountID)
mappedResults := holds.HasCurrencyMap(func(l providers.IHolding) string {
return strings.ToLower(l.CurrencySymbolName())
}, func(r providers.IHolding) string {
Expand Down

0 comments on commit 7557eb1

Please sign in to comment.