· 9 years ago · Sep 16, 2016, 05:34 PM
1shkarlat@shkarlat:~/rails_projects/accountdock-rails$ git diff --staged
2diff --git a/app/controllers/braintree_controller.rb b/app/controllers/braintree_controller.rb
3index 14f4adf..17ca5a7 100644
4--- a/app/controllers/braintree_controller.rb
5+++ b/app/controllers/braintree_controller.rb
6@@ -31,11 +31,12 @@ class BraintreeController < ApplicationController
7 account_id: account.id})
8 end
9
10- account.add_connection({:scope => token_params.credentials.instance_variable_get(:@scope),
11- :client_id => params[:merchantId],
12- :refresh_token => token_params.credentials.refresh_token,
13- :secret_key => token_params.credentials.access_token,
14- :publishable_key => ''})
15+ account.add_connection({ scope: token_params.credentials.instance_variable_get(:@scope),
16+ client_id: params[:merchantId],
17+ refresh_token: token_params.credentials.refresh_token,
18+ secret_key: token_params.credentials.access_token,
19+ publishable_key: '',
20+ type: 'BraintreeConnection' })
21
22 AccountSyncJob.perform_later(account)
23 redirect_to team_switch_path(public_key: account.public_key, mode: params[:mode], path: 'dashboard')
24diff --git a/app/controllers/stripe_controller.rb b/app/controllers/stripe_controller.rb
25index 3c59b30..930484d 100644
26--- a/app/controllers/stripe_controller.rb
27+++ b/app/controllers/stripe_controller.rb
28@@ -48,13 +48,12 @@ class StripeController < ApplicationController
29 end
30
31 # Add connections
32- account.add_connection({
33- scope: token_params[:scope],
34- client_id: token_params[:stripe_client_id],
35- refresh_token: token_params[:refresh_token],
36- secret_key: token_params[:access_token],
37- publishable_key: token_params[:stripe_publishable_key]
38- })
39+ account.add_connection({ scope: token_params[:scope],
40+ client_id: token_params[:stripe_client_id],
41+ refresh_token: token_params[:refresh_token],
42+ secret_key: token_params[:access_token],
43+ publishable_key: token_params[:stripe_publishable_key],
44+ type: 'StripeConnection' })
45
46 account.update({stripe_account_raw: stripe_account.to_json})
47
48diff --git a/app/models/account.rb b/app/models/account.rb
49index 7803ba8..36e7216 100644
50--- a/app/models/account.rb
51+++ b/app/models/account.rb
52@@ -7,7 +7,6 @@
53 # handle :string(255) not null
54 # installed :boolean default(FALSE), not null
55 # cnames :text
56-# stripe_account_id :string
57 # stripe_account_raw :text
58 # is_pro :boolean default(FALSE), not null
59 # transactions_viewed :integer default(0), not null
60@@ -36,14 +35,12 @@
61 # slack_bot_access_token :string
62 # synced :boolean default(FALSE)
63 # subscriptions_count :integer default(0), not null
64-# braintree_account_id :string
65 #
66 # Indexes
67 #
68-# index_accounts_on_handle (handle)
69-# index_accounts_on_id (id) UNIQUE
70-# index_accounts_on_public_key (public_key) UNIQUE
71-# index_accounts_on_stripe_account_id (stripe_account_id)
72+# index_accounts_on_handle (handle)
73+# index_accounts_on_id (id) UNIQUE
74+# index_accounts_on_public_key (public_key) UNIQUE
75 #
76
77 # I don't believe handle is used at the moment
78@@ -62,7 +59,7 @@ class Account < ActiveRecord::Base
79 has_many :cards, through: :customers # Don't know if this is necessary
80 has_many :charges, dependent: :destroy
81 has_many :refunds, through: :charges
82- has_one :connection, dependent: :destroy
83+ has_many :connections, dependent: :destroy
84 has_many :customers, dependent: :destroy
85 has_many :invoices, dependent: :destroy
86 has_many :disputes, dependent: :destroy
87@@ -430,31 +427,7 @@ class Account < ActiveRecord::Base
88 # Pull historic Stripe data into local DB (this is usually called by ActiveSyncJob.perform_later(self))
89 def sync
90 if self.connection.present? && !self.disconnected_at
91- if self.stripe?
92- begin
93- puts "Syncing Customers for account #{self.id}"
94- sync_customers
95- puts "Syncing Invoices for account #{self.id}"
96- sync_invoices
97- puts "Syncing Charges for account #{self.id}"
98- sync_charges
99- puts "Syncing Disputes for account #{self.id}"
100- sync_disputes
101- puts "Syncing Subscriptions for account #{self.id}"
102- sync_subscriptions
103- self.update_attribute(:synced, true)
104- # # rescue Exception => ex
105- # # self.update_attribute(:disconnected_reason, ex.to_s)
106- # self.touch(:disconnected_at)
107- # # raise ex
108- end
109- else
110- begin
111- puts "Syncing Customers for account #{self.id}"
112- BraintreeClient.sync_customers(self)
113- self.update_attribute(:synced, true)
114- end
115- end
116+ self.connections.map(&:sync)
117 end
118 end
119 # These are helpers to see sync progress...
120@@ -549,22 +522,6 @@ class Account < ActiveRecord::Base
121 self.add_or_update_charge(stripe_charge)
122 end
123
124- def provider
125- if self.braintree_account_id.present?
126- 'braintree'
127- elsif self.stripe_account_id.present?
128- 'stripe'
129- end
130- end
131-
132- def stripe?
133- provider == 'stripe'
134- end
135-
136- def braintree?
137- provider == 'braintree'
138- end
139-
140 protected
141 def slack
142 @slack ||= Slack.new(self.slack_bot_access_token)
143diff --git a/app/models/braintree_connection.rb b/app/models/braintree_connection.rb
144new file mode 100644
145index 0000000..af05be1
146--- /dev/null
147+++ b/app/models/braintree_connection.rb
148@@ -0,0 +1,39 @@
149+# == Schema Information
150+#
151+# Table name: connections
152+#
153+# id :integer not null, primary key
154+# status :string(8) not null
155+# public_key :string(255) not null
156+# scope :string(255) not null
157+# account_id :integer not null
158+# client_id :string(255) not null
159+# refresh_token :string(255) not null
160+# secret_key :string(255) not null
161+# publishable_key :string(255) not null
162+# created_at :datetime not null
163+# updated_at :datetime not null
164+# type :string
165+# active :string
166+# synced :string
167+# provider_account_id :string
168+#
169+# Indexes
170+#
171+# index_connections_on_account_id (account_id)
172+# index_connections_on_id_and_status (id,status)
173+# index_connections_on_status_and_account_id (status,account_id)
174+# index_connections_on_status_and_public_key (status,public_key)
175+#
176+# Foreign Keys
177+#
178+# fk_rails_b202e9a6e9 (account_id => accounts.id)
179+#
180+
181+class BraintreeConnection < Connection
182+ def sync
183+ puts "Syncing Customers for account #{current_account.id}"
184+ BraintreeClient.sync_customers(current_account)
185+ self.update_attribute(:synced, true)
186+ end
187+end
188diff --git a/app/models/connection.rb b/app/models/connection.rb
189index db6f97d..7ffccac 100644
190--- a/app/models/connection.rb
191+++ b/app/models/connection.rb
192@@ -2,17 +2,21 @@
193 #
194 # Table name: connections
195 #
196-# id :integer not null, primary key
197-# status :string(8) not null
198-# public_key :string(255) not null
199-# scope :string(255) not null
200-# account_id :integer not null
201-# client_id :string(255) not null
202-# refresh_token :string(255) not null
203-# secret_key :string(255) not null
204-# publishable_key :string(255) not null
205-# created_at :datetime not null
206-# updated_at :datetime not null
207+# id :integer not null, primary key
208+# status :string(8) not null
209+# public_key :string(255) not null
210+# scope :string(255) not null
211+# account_id :integer not null
212+# client_id :string(255) not null
213+# refresh_token :string(255) not null
214+# secret_key :string(255) not null
215+# publishable_key :string(255) not null
216+# created_at :datetime not null
217+# updated_at :datetime not null
218+# type :string
219+# active :string
220+# synced :string
221+# provider_account_id :string
222 #
223 # Indexes
224 #
225@@ -49,4 +53,12 @@ class Connection < ActiveRecord::Base
226 "Read/Write"
227 end
228 end
229+
230+ def provider
231+ if current_account.connection.type == 'BraintreeConnection'
232+ 'braintree'
233+ elsif current_account.connection.type == 'StripeConnection'
234+ 'stripe'
235+ end
236+ end
237 end
238diff --git a/app/models/customer.rb b/app/models/customer.rb
239index 48ce53d..2bf0651 100644
240--- a/app/models/customer.rb
241+++ b/app/models/customer.rb
242@@ -4,7 +4,6 @@
243 #
244 # id :integer not null, primary key
245 # public_key :string(255) not null
246-# stripe_customer_id :string
247 # email :string default("")
248 # account_id :integer not null
249 # deleted :boolean default(FALSE), not null
250@@ -22,14 +21,13 @@
251 # additional_billing_info :string
252 # receive_email_notifications :boolean default(FALSE)
253 # timezone :string
254-# braintree_customer_id :string
255+# type :string
256+# vendor_customer_id :string
257 #
258 # Indexes
259 #
260-# index_customers_on_account_id (account_id)
261-# index_customers_on_account_id_and_stripe_customer_id (account_id,stripe_customer_id) UNIQUE
262-# index_customers_on_public_key (public_key) UNIQUE
263-# index_customers_on_stripe_customer_id (stripe_customer_id)
264+# index_customers_on_account_id (account_id)
265+# index_customers_on_public_key (public_key) UNIQUE
266 #
267 # Foreign Keys
268 #
269@@ -48,6 +46,9 @@ class Customer < ActiveRecord::Base
270 dependent: :destroy
271 has_many :activities, dependent: :destroy
272
273+ # Uniqueness is still a requirement, but we enforce in the db.
274+ validates :vendor_customer_id, presence: true #, uniqueness: true
275+
276 scope :deleted, -> { where(deleted: true) }
277 scope :active, -> { where.not(deleted: true) }
278
279diff --git a/app/models/stripe_connection.rb b/app/models/stripe_connection.rb
280new file mode 100644
281index 0000000..932649d
282--- /dev/null
283+++ b/app/models/stripe_connection.rb
284@@ -0,0 +1,47 @@
285+# == Schema Information
286+#
287+# Table name: connections
288+#
289+# id :integer not null, primary key
290+# status :string(8) not null
291+# public_key :string(255) not null
292+# scope :string(255) not null
293+# account_id :integer not null
294+# client_id :string(255) not null
295+# refresh_token :string(255) not null
296+# secret_key :string(255) not null
297+# publishable_key :string(255) not null
298+# created_at :datetime not null
299+# updated_at :datetime not null
300+# type :string
301+# active :string
302+# synced :string
303+# provider_account_id :string
304+#
305+# Indexes
306+#
307+# index_connections_on_account_id (account_id)
308+# index_connections_on_id_and_status (id,status)
309+# index_connections_on_status_and_account_id (status,account_id)
310+# index_connections_on_status_and_public_key (status,public_key)
311+#
312+# Foreign Keys
313+#
314+# fk_rails_b202e9a6e9 (account_id => accounts.id)
315+#
316+
317+class StripeConnection < Connection
318+ def sync
319+ puts "Syncing Customers for account #{current_account.id}"
320+ sync_customers
321+ puts "Syncing Invoices for account #{current_account.id}"
322+ sync_invoices
323+ puts "Syncing Charges for account #{current_account.id}"
324+ sync_charges
325+ puts "Syncing Disputes for account #{current_account.id}"
326+ sync_disputes
327+ puts "Syncing Subscriptions for account #{current_account.id}"
328+ sync_subscriptions
329+ self.update_attribute(:synced, true)
330+ end
331+end
332diff --git a/db/migrate/20160914103835_add_braintree_customer_id_to_customers.rb b/db/migrate/20160914103835_add_braintree_customer_id_to_customers.rb
333deleted file mode 100644
334index 1bccc48..0000000
335--- a/db/migrate/20160914103835_add_braintree_customer_id_to_customers.rb
336+++ /dev/null
337@@ -1,5 +0,0 @@
338-class AddBraintreeCustomerIdToCustomers < ActiveRecord::Migration
339- def change
340- add_column :customers, :braintree_customer_id, :string
341- end
342-end
343diff --git a/db/migrate/20160914130511_remove_null_validation_from_customers.rb b/db/migrate/20160914130511_remove_null_validation_from_customers.rb
344deleted file mode 100644
345index 000823b..0000000
346--- a/db/migrate/20160914130511_remove_null_validation_from_customers.rb
347+++ /dev/null
348@@ -1,5 +0,0 @@
349-class RemoveNullValidationFromCustomers < ActiveRecord::Migration
350- def change
351- change_column :customers, :stripe_customer_id, :string, null: true
352- end
353-end
354diff --git a/db/migrate/20160916094926_add_vendor_customer_id_to_customers.rb b/db/migrate/20160916094926_add_vendor_customer_id_to_customers.rb
355new file mode 100644
356index 0000000..ba92c85
357--- /dev/null
358+++ b/db/migrate/20160916094926_add_vendor_customer_id_to_customers.rb
359@@ -0,0 +1,6 @@
360+class AddVendorCustomerIdToCustomers < ActiveRecord::Migration
361+ def change
362+ add_column :customers, :vendor_customer_id, :string
363+ remove_column :customers, :stripe_customer_id, :string
364+ end
365+end
366diff --git a/db/migrate/20160916103818_add_type_to_customers.rb b/db/migrate/20160916103818_add_type_to_customers.rb
367new file mode 100644
368index 0000000..0defb16
369--- /dev/null
370+++ b/db/migrate/20160916103818_add_type_to_customers.rb
371@@ -0,0 +1,5 @@
372+class AddTypeToCustomers < ActiveRecord::Migration
373+ def change
374+ add_column :customers, :type, :string
375+ end
376+end
377diff --git a/db/migrate/20160916105308_remove_account_ids_from_accounts.rb b/db/migrate/20160916105308_remove_account_ids_from_accounts.rb
378new file mode 100644
379index 0000000..4c27356
380--- /dev/null
381+++ b/db/migrate/20160916105308_remove_account_ids_from_accounts.rb
382@@ -0,0 +1,6 @@
383+class RemoveAccountIdsFromAccounts < ActiveRecord::Migration
384+ def change
385+ remove_column :accounts, :stripe_account_id, :string
386+ remove_column :accounts, :braintree_account_id, :string
387+ end
388+end
389diff --git a/db/migrate/20160916105450_add_provider_account_id_to_connections.rb b/db/migrate/20160916105450_add_provider_account_id_to_connections.rb
390new file mode 100644
391index 0000000..921b98e
392--- /dev/null
393+++ b/db/migrate/20160916105450_add_provider_account_id_to_connections.rb
394@@ -0,0 +1,5 @@
395+class AddProviderAccountIdToConnections < ActiveRecord::Migration
396+ def change
397+ add_column :connections, :provider_account_id, :string
398+ end
399+end
400diff --git a/db/migrate/20160916122225_add_type_to_connections.rb b/db/migrate/20160916122225_add_type_to_connections.rb
401new file mode 100644
402index 0000000..7ce6b9a
403--- /dev/null
404+++ b/db/migrate/20160916122225_add_type_to_connections.rb
405@@ -0,0 +1,7 @@
406+class AddTypeToConnections < ActiveRecord::Migration
407+ def change
408+ add_column :connections, :type, :string
409+ add_column :connections, :active, :string
410+ add_column :connections, :synced, :string
411+ end
412+end
413diff --git a/db/schema.rb b/db/schema.rb
414index f04a328..23a06ee 100644
415--- a/db/schema.rb
416+++ b/db/schema.rb
417@@ -11,7 +11,7 @@
418 #
419 # It's strongly recommended that you check this file into your version control system.
420
421-ActiveRecord::Schema.define(version: 20160914133617) do
422+ActiveRecord::Schema.define(version: 20160916122225) do
423
424 # These are extensions that must be enabled in order to support this database
425 enable_extension "plpgsql"
426@@ -34,7 +34,6 @@ ActiveRecord::Schema.define(version: 20160914133617) do
427 t.string "handle", :limit=>255, :null=>false, :index=>{:name=>"index_accounts_on_handle"}
428 t.boolean "installed", :default=>false, :null=>false
429 t.text "cnames"
430- t.string "stripe_account_id", :index=>{:name=>"index_accounts_on_stripe_account_id"}
431 t.text "stripe_account_raw"
432 t.boolean "is_pro", :default=>false, :null=>false
433 t.integer "transactions_viewed", :default=>0, :null=>false
434@@ -63,7 +62,6 @@ ActiveRecord::Schema.define(version: 20160914133617) do
435 t.string "slack_bot_access_token"
436 t.boolean "synced", :default=>false
437 t.integer "subscriptions_count", :default=>0, :null=>false
438- t.string "braintree_account_id"
439 end
440 add_index "accounts", ["id"], :name=>"index_accounts_on_id", :unique=>true
441
442@@ -128,23 +126,26 @@ ActiveRecord::Schema.define(version: 20160914133617) do
443 end
444
445 create_table "connections", force: :cascade do |t|
446- t.string "status", :limit=>8, :null=>false, :index=>{:name=>"index_connections_on_status_and_account_id", :with=>["account_id"]}
447- t.string "public_key", :limit=>255, :null=>false
448- t.string "scope", :limit=>255, :null=>false
449- t.integer "account_id", :null=>false, :index=>{:name=>"index_connections_on_account_id"}
450- t.string "client_id", :limit=>255, :null=>false
451- t.string "refresh_token", :limit=>255, :null=>false
452- t.string "secret_key", :limit=>255, :null=>false
453- t.string "publishable_key", :limit=>255, :null=>false
454- t.datetime "created_at", :null=>false
455- t.datetime "updated_at", :null=>false
456+ t.string "status", :limit=>8, :null=>false, :index=>{:name=>"index_connections_on_status_and_account_id", :with=>["account_id"]}
457+ t.string "public_key", :limit=>255, :null=>false
458+ t.string "scope", :limit=>255, :null=>false
459+ t.integer "account_id", :null=>false, :index=>{:name=>"index_connections_on_account_id"}
460+ t.string "client_id", :limit=>255, :null=>false
461+ t.string "refresh_token", :limit=>255, :null=>false
462+ t.string "secret_key", :limit=>255, :null=>false
463+ t.string "publishable_key", :limit=>255, :null=>false
464+ t.datetime "created_at", :null=>false
465+ t.datetime "updated_at", :null=>false
466+ t.string "type"
467+ t.string "active"
468+ t.string "synced"
469+ t.string "provider_account_id"
470 end
471 add_index "connections", ["id", "status"], :name=>"index_connections_on_id_and_status"
472 add_index "connections", ["status", "public_key"], :name=>"index_connections_on_status_and_public_key"
473
474 create_table "customers", force: :cascade do |t|
475 t.string "public_key", :limit=>255, :null=>false, :index=>{:name=>"index_customers_on_public_key", :unique=>true}
476- t.string "stripe_customer_id", :index=>{:name=>"index_customers_on_stripe_customer_id"}
477 t.string "email", :default=>""
478 t.integer "account_id", :null=>false, :index=>{:name=>"index_customers_on_account_id"}
479 t.boolean "deleted", :default=>false, :null=>false
480@@ -162,9 +163,9 @@ ActiveRecord::Schema.define(version: 20160914133617) do
481 t.string "additional_billing_info"
482 t.boolean "receive_email_notifications", :default=>false
483 t.string "timezone"
484- t.string "braintree_customer_id"
485+ t.string "type"
486+ t.string "vendor_customer_id"
487 end
488- add_index "customers", ["account_id", "stripe_customer_id"], :name=>"index_customers_on_account_id_and_stripe_customer_id", :unique=>true
489
490 create_table "disputes", force: :cascade do |t|
491 t.string "status"