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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 72d8bf7cf5be435eb044cfbd8c92c163a52be1aa
4
- data.tar.gz: e799208fe98d47f8cda6af3d5ae7796715066b27
3
+ metadata.gz: 61f6da3ef8adaecdfc317658e67984c85e41523b
4
+ data.tar.gz: 78da897a2aa4f7d77bd1fdb7e5aa7b10e9dabdd6
5
5
  SHA512:
6
- metadata.gz: 6a8f287bef770e7555d0b5f9bacf1f64298fa6b5a5aaf0215fcc1b8361779d132c0a3b74c3381b2ff65b734b5b0efd7a06418e51d60cfdc187528cdf9107153a
7
- data.tar.gz: ca780efbb78ca457855d6faa32e9c406feade50a3e067cc5f79b52af1e93d143c3dfce72f643ff16472debf4841a72cee2582c277f6250e4e70b73861ef4296b
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
- @plaid_accounts =PlaidRails::Account.where(owner_type: link_params[:owner_type],
24
- owner_id: link_params[:owner_id])
25
- @plaid_accounts.each do |account|
26
- account.update(access_token: exchange_token.access_token,
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
- end
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,:owner_type)
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
- before_save :delete_connect, if: Proc.new{|a| a.access_token_changed? and !a.new_record?}
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
- if PlaidRails::Account.where(access_token: token).size == 1
20
- user = Plaid::User.load(:connect, token)
21
- user.delete
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 #{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
@@ -1,5 +1,2 @@
1
1
  // override this js file with your own in your the own view in
2
2
  // app/views/plaid_rails/link/authenticate.js.erb
3
-
4
- console.log('success')
5
- alert('success')
@@ -1,5 +1,2 @@
1
1
  // override this js file with your own in your the own view in
2
2
  // app/views/plaid_rails/link/authenticate.js.erb
3
-
4
- console.log('success')
5
- alert('success')
@@ -1,3 +1,3 @@
1
1
  module PlaidRails
2
- VERSION = "0.9.0"
2
+ VERSION = "0.10.0"
3
3
  end
@@ -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