Skip to content
This repository has been archived by the owner on Dec 3, 2024. It is now read-only.

Commit

Permalink
feat: Add Institutions
Browse files Browse the repository at this point in the history
  • Loading branch information
erezrokah committed Jan 25, 2023
1 parent 97f78bd commit 7f6c8de
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 2 deletions.
1 change: 1 addition & 0 deletions plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func Plugin() *source.Plugin {
"plaid",
Version,
schema.Tables{
resources.Institutions(),
resources.Wallets(),
},
client.New,
Expand Down
64 changes: 64 additions & 0 deletions resources/institutions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package resources

import (
"context"

"github.com/cloudquery/cq-source-plaid/client"
"github.com/cloudquery/plugin-sdk/schema"
"github.com/cloudquery/plugin-sdk/transformers"
"github.com/plaid/plaid-go/v10/plaid"
)

const count = 500

func Institutions() *schema.Table {
return &schema.Table{
Name: "plaid_institutions",
Resolver: fetchInstitutions,
Transform: transformers.TransformWithStruct(plaid.Institution{}, transformers.WithPrimaryKeys("InstitutionId")),
}
}

func fetchInstitutions(ctx context.Context, meta schema.ClientMeta, parent *schema.Resource, res chan<- interface{}) error {
client := meta.(*client.Client)

var total int32
for {
request := plaid.NewInstitutionsGetRequest(
count,
total,
[]plaid.CountryCode{
plaid.COUNTRYCODE_US,
plaid.COUNTRYCODE_GB,
plaid.COUNTRYCODE_ES,
plaid.COUNTRYCODE_NL,
plaid.COUNTRYCODE_FR,
plaid.COUNTRYCODE_IE,
plaid.COUNTRYCODE_CA,
plaid.COUNTRYCODE_DE,
plaid.COUNTRYCODE_IT,
plaid.COUNTRYCODE_PL,
plaid.COUNTRYCODE_DK,
plaid.COUNTRYCODE_NO,
plaid.COUNTRYCODE_SE,
plaid.COUNTRYCODE_EE,
plaid.COUNTRYCODE_LT,
plaid.COUNTRYCODE_LV,
},
)
resp, _, err := client.Services.PlaidApi.InstitutionsGet(ctx).InstitutionsGetRequest(*request).Execute()
if err != nil {
return err
}

institutions := resp.GetInstitutions()
total += int32(len(institutions))
res <- institutions

if total >= resp.GetTotal() || len(institutions) == 0 {
break
}
}

return nil
}
34 changes: 34 additions & 0 deletions resources/institutions_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package resources

import (
"testing"

_ "embed"

"github.com/cloudquery/cq-source-plaid/client"
"github.com/cloudquery/plugin-sdk/faker"
"github.com/plaid/plaid-go/v10/plaid"
)

type institutionsResponse struct {
Institutions []plaid.Institution `json:"institutions"`
}

func TestInstitutions(t *testing.T) {
var res institutionsResponse
if err := faker.FakeObject(&res); err != nil {
t.Fatal(err)
}
testString := "test"
res.Institutions[0].Url.Set(&testString)
res.Institutions[0].Logo.Set(&testString)
res.Institutions[0].PrimaryColor.Set(&testString)
res.Institutions[0].Status.Set(&plaid.InstitutionStatus{})
res.Institutions[0].PaymentInitiationMetadata.Set(&plaid.PaymentInitiationMetadata{})
res.Institutions[0].AuthMetadata.Set(&plaid.AuthMetadata{})

ts := testServer(t, res)

defer ts.Close()
client.TestHelper(t, Institutions(), ts)
}
4 changes: 2 additions & 2 deletions resources/wallets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import (
"github.com/plaid/plaid-go/v10/plaid"
)

type response struct {
type walletsResponse struct {
Wallets []plaid.Wallet `json:"wallets"`
}

func TestWallets(t *testing.T) {
var res response
var res walletsResponse
if err := faker.FakeObject(&res); err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit 7f6c8de

Please sign in to comment.