plaid_rails 0.9.0 → 0.10.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/app/assets/javascripts/plaid_rails/link.js +2 -1
- data/app/controllers/plaid_rails/link_controller.rb +20 -8
- data/app/models/plaid_rails/account.rb +35 -8
- data/app/views/plaid_rails/link/authenticate.js.erb +0 -3
- data/app/views/plaid_rails/link/update.js.erb +0 -3
- data/lib/plaid_rails/version.rb +1 -1
- data/spec/controllers/plaid_rails/link_controller_spec.rb +1 -1
- data/spec/dummy/log/test.log +1874 -12001
- data/spec/factories/account.rb +1 -0
- data/spec/models/plaid_rails/account_spec.rb +51 -28
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 61f6da3ef8adaecdfc317658e67984c85e41523b
|
4
|
+
data.tar.gz: 78da897a2aa4f7d77bd1fdb7e5aa7b10e9dabdd6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 193ce2a12853307505f295dbfa264baca9e95c8d1a04392e2c89546ed7d55d0173ea409f663bbce241c108a4a86abaa961163e68a120c74123ca03f167b90124
|
7
|
+
data.tar.gz: d4e424a09d944f832db08a3b664021ad25de8de0c690c22629ee3aef064b70ef9b6b752c146cc772f61badcd8b8eae2f7082034dba4e5346a67d8ae8ddf34e47
|
@@ -35,7 +35,8 @@ function getPlaid(plaidData) {
|
|
35
35
|
name: metadata.institution.name,
|
36
36
|
type: metadata.institution.type,
|
37
37
|
owner_type: plaidData.data('owner-type'),
|
38
|
-
owner_id: plaidData.data('owner-id')
|
38
|
+
owner_id: plaidData.data('owner-id'),
|
39
|
+
number: plaidData.data('number')
|
39
40
|
}
|
40
41
|
});
|
41
42
|
},
|
@@ -16,27 +16,39 @@ module PlaidRails
|
|
16
16
|
end
|
17
17
|
end
|
18
18
|
|
19
|
+
# updates the access token after changing password with institution
|
19
20
|
def update
|
20
21
|
begin
|
21
22
|
exchange_token = Plaid::User.exchange_token(link_params[:public_token])
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
23
|
+
|
24
|
+
# find old access_token
|
25
|
+
old_access_token = PlaidRails::Account.find_by(owner_type: link_params[:owner_type],
|
26
|
+
owner_id: link_params[:owner_id],number: link_params[:number]).access_token
|
27
|
+
|
28
|
+
# find all plaid_accounts with old access_token
|
29
|
+
accounts = PlaidRails::Account.where(owner_type: link_params[:owner_type],
|
30
|
+
owner_id: link_params[:owner_id], access_token: old_access_token)
|
31
|
+
|
32
|
+
# update found accounts with new token.
|
33
|
+
accounts.update_all(access_token: exchange_token.access_token,
|
27
34
|
transactions_start_date: Date.today)
|
28
|
-
|
35
|
+
|
36
|
+
# get all accounts to display back to user.
|
37
|
+
@plaid_accounts = PlaidRails::Account.where(owner_type: link_params[:owner_type],
|
38
|
+
owner_id: link_params[:owner_id])
|
39
|
+
|
29
40
|
flash[:success]="You have successfully updated your account(s)"
|
30
41
|
rescue => e
|
31
42
|
Rails.logger.error "Error: #{e}"
|
32
|
-
Rails.logger.error e.backtrace.join("\n")
|
33
43
|
render text: e.message, status: 500
|
34
44
|
end
|
35
45
|
end
|
46
|
+
|
36
47
|
private
|
37
48
|
# Never trust parameters from the scary internet, only allow the white list through.
|
38
49
|
def link_params
|
39
|
-
params.permit(:access_token, :public_token, :type,:name,:owner_id
|
50
|
+
params.permit(:access_token, :public_token, :type,:name,:owner_id,
|
51
|
+
:owner_type,:number)
|
40
52
|
end
|
41
53
|
end
|
42
54
|
end
|
@@ -2,8 +2,8 @@ module PlaidRails
|
|
2
2
|
class Account < ActiveRecord::Base
|
3
3
|
belongs_to :owner, polymorphic: true, foreign_key: :owner_id
|
4
4
|
|
5
|
-
before_destroy :delete_connect
|
6
|
-
|
5
|
+
before_destroy :delete_connect, :unless=> :accounts_with_same_token?
|
6
|
+
before_update :delete_updated_token, :if=> :access_token_changed?
|
7
7
|
|
8
8
|
validates :plaid_id, presence: true
|
9
9
|
validates :name, presence: true
|
@@ -13,18 +13,45 @@ module PlaidRails
|
|
13
13
|
private
|
14
14
|
|
15
15
|
# delete token from Plaid if there are no more accounts for this token
|
16
|
+
def delete_updated_token
|
17
|
+
# change all matching tokens on update
|
18
|
+
accounts = PlaidRails::Account.where(access_token: my_token)
|
19
|
+
if accounts.size > 0
|
20
|
+
delete_connect
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
# delete Plaid user
|
16
25
|
def delete_connect
|
17
|
-
token = self.access_token
|
18
26
|
begin
|
19
|
-
|
20
|
-
|
21
|
-
|
27
|
+
Rails.logger.info "Deleting Plaid User with token #{token_last_8}"
|
28
|
+
user = Plaid::User.load(:connect, my_token)
|
29
|
+
# skip delete if there are no transactions
|
30
|
+
if user.transactions.any?
|
31
|
+
user.delete
|
32
|
+
Rails.logger.info "Deleted Plaid User with token #{token_last_8}"
|
22
33
|
end
|
23
34
|
rescue => e
|
24
|
-
message = "Unable to delete token #{
|
35
|
+
message = "Unable to delete user with token #{token_last_8}"
|
25
36
|
Rails.logger.error "#{message}: #{e.message}"
|
26
|
-
Rails.logger.error e.backtrace.join("\n")
|
27
37
|
end
|
28
38
|
end
|
39
|
+
|
40
|
+
# check if access token changed and use that token
|
41
|
+
# means the bank password was changed
|
42
|
+
# return token changed token if it was changed
|
43
|
+
def my_token
|
44
|
+
self.access_token_changed? ? self.access_token_was : self.access_token
|
45
|
+
end
|
46
|
+
|
47
|
+
# hide full token from logs
|
48
|
+
def token_last_8
|
49
|
+
my_token[-8..-1]
|
50
|
+
end
|
51
|
+
|
52
|
+
# are there more accounts that use the same token
|
53
|
+
def accounts_with_same_token?
|
54
|
+
PlaidRails::Account.where(access_token: my_token).size > 1
|
55
|
+
end
|
29
56
|
end
|
30
57
|
end
|
data/lib/plaid_rails/version.rb
CHANGED
@@ -22,7 +22,7 @@ module PlaidRails
|
|
22
22
|
it "update with public token" do
|
23
23
|
account = create(:account, transactions_start_date: Date.today - 3)
|
24
24
|
xhr :post, :update, public_token: 'test,wells,connected', name:'Wells Fargo', type: 'wells',
|
25
|
-
owner_id: "1", owner_type: "User"
|
25
|
+
owner_id: "1", owner_type: "User", number: 1234
|
26
26
|
expect(response).to be_success
|
27
27
|
expect(assigns(:plaid_accounts)).to_not be_nil
|
28
28
|
expect(assigns(:plaid_accounts).first.transactions_start_date).to eq Date.today
|