diff --git a/cli/internal/cmd/load.plaid-data.go b/cli/internal/cmd/load.plaid-data.go index 3e450cf..0e32529 100644 --- a/cli/internal/cmd/load.plaid-data.go +++ b/cli/internal/cmd/load.plaid-data.go @@ -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") } diff --git a/cli/internal/plaid/accounts.go b/cli/internal/plaid/accounts.go index c106ca8..c2206d0 100644 --- a/cli/internal/plaid/accounts.go +++ b/cli/internal/plaid/accounts.go @@ -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{}) @@ -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 } diff --git a/cli/internal/plaid/cache.go b/cli/internal/plaid/cache.go index fcfb56b..febf7a8 100644 --- a/cli/internal/plaid/cache.go +++ b/cli/internal/plaid/cache.go @@ -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 { @@ -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 { diff --git a/cli/internal/plaid/transactions.go b/cli/internal/plaid/transactions.go index 9ff70f3..095455f 100644 --- a/cli/internal/plaid/transactions.go +++ b/cli/internal/plaid/transactions.go @@ -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 } @@ -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 } }