Skip to content

Commit

Permalink
separate by account
Browse files Browse the repository at this point in the history
  • Loading branch information
quinn committed Feb 2, 2024
1 parent ec7fd5b commit c9c0e16
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 11 deletions.
4 changes: 2 additions & 2 deletions cli/internal/cmd/load.plaid-data.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ func LoadPlaidDataCmd(ctx context.Context) *cobra.Command {
}

for _, institution := range institutions {
err = pc.LoadTransactions(ctx, institution.PlaidAccessToken)
err = pc.LoadTransactions(ctx, institution.PlaidId, institution.PlaidAccessToken)

if err != nil {
return errors.Wrap(err, "failed to load transactions")
}

err = pc.LoadAccounts(ctx, institution.PlaidAccessToken)
err = pc.LoadAccounts(ctx, institution.PlaidId, institution.PlaidAccessToken)
if err != nil {
return errors.Wrap(err, "failed to load accounts")
}
Expand Down
5 changes: 3 additions & 2 deletions cli/internal/plaid/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import (
"context"
"errors"
"io"
"path/filepath"

"github.com/plaid/plaid-go/v20/plaid"
)

func (pc *APIClient) LoadAccounts(ctx context.Context, accessToken string) error {
func (pc *APIClient) LoadAccounts(ctx context.Context, institutionId string, accessToken string) error {
accountsGetRequest := plaid.NewAccountsGetRequest(accessToken)
accountsGetRequest.SetOptions(plaid.AccountsGetRequestOptions{})

Expand All @@ -32,7 +33,7 @@ func (pc *APIClient) LoadAccounts(ctx context.Context, accessToken string) error
return err
}

if err = pc.SetCache(ctx, "accounts", "", body); err != nil {
if err = pc.SetCache(ctx, filepath.Join("transactions", institutionId), "", body); err != nil {
return err
}

Expand Down
18 changes: 14 additions & 4 deletions cli/internal/plaid/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,13 @@ func (pc *APIClient) GetNextCursor(ctx context.Context, prefix string) (string,
return syncResponse.GetNextCursor(), nil
}

func (pc *APIClient) GetCache(ctx context.Context, path string) ([]byte, error) {
func (pc *APIClient) GetCache(ctx context.Context, prefix string) ([]byte, error) {
var lastEntry os.DirEntry
cachePath := pc.cacheDir + "/" + path
cachePath := filepath.Join(pc.cacheDir, prefix)
err := os.MkdirAll(cachePath, 0755)
if err != nil {
return nil, err
}

entries, err := os.ReadDir(cachePath)
if err != nil {
Expand All @@ -58,9 +62,15 @@ func (pc *APIClient) GetCache(ctx context.Context, path string) ([]byte, error)
return nil, nil
}

func (pc *APIClient) SetCache(ctx context.Context, path string, cursor string, bytes []byte) error {
func (pc *APIClient) SetCache(ctx context.Context, prefix string, cursor string, bytes []byte) error {
cachePath := filepath.Join(pc.cacheDir, prefix)
err := os.MkdirAll(cachePath, 0755)
if err != nil {
return err
}

timestamp := strings.Replace(time.Now().Format(time.RFC3339Nano), ":", "X", -1)
fileName := filepath.Join(pc.cacheDir, path, fmt.Sprintf("%s_%s.json", timestamp, strings.Replace(cursor, "/", "_", -1)))
fileName := filepath.Join(cachePath, fmt.Sprintf("%s_%s.json", timestamp, strings.Replace(cursor, "/", "_", -1)))

log.Println("writing", fileName)
if err := os.WriteFile(fileName, bytes, 0644); err != nil {
Expand Down
8 changes: 5 additions & 3 deletions cli/internal/plaid/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@ import (
"errors"
"io"
"log"
"path/filepath"

"github.com/plaid/plaid-go/v20/plaid"
)

func (pc *APIClient) LoadTransactions(ctx context.Context, accessToken string) error {
func (pc *APIClient) LoadTransactions(ctx context.Context, institutionId string, accessToken string) error {
var hasMore bool = true
prefix := filepath.Join("transactions", institutionId)

// Get previous cursor from the latest cached response
cursor, err := pc.GetNextCursor(ctx, "transactions")
cursor, err := pc.GetNextCursor(ctx, prefix)
if err != nil {
return err
}
Expand Down Expand Up @@ -64,7 +66,7 @@ func (pc *APIClient) LoadTransactions(ctx context.Context, accessToken string) e
nextCursor := resp.GetNextCursor()
cursor = nextCursor

if err := pc.SetCache(ctx, "transactions", cursor, body); err != nil {
if err := pc.SetCache(ctx, prefix, cursor, body); err != nil {
return err
}
}
Expand Down

0 comments on commit c9c0e16

Please sign in to comment.