bdgtr-cli 0.2 → 0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/bin/bdgtr +26 -5
- data/lib/bdgtr/cli/config.rb +188 -0
- data/lib/bdgtr/cli/config.sample.yml +15 -0
- data/lib/bdgtr/cli/help.rb +168 -0
- data/lib/bdgtr/cli/ledger_sync.rb +102 -0
- data/lib/bdgtr/cli/prompt.rb +47 -0
- data/lib/bdgtr/cli/psd2.rb +530 -0
- data/lib/bdgtr/cli/rows.rb +38 -0
- data/lib/bdgtr/cli/runtime.rb +88 -0
- data/lib/bdgtr/cli/table.rb +458 -0
- data/lib/bdgtr/cli.rb +126 -0
- data/lib/bdgtr/core/binder.rb +79 -0
- data/lib/bdgtr/core/exchange.rb +66 -0
- data/lib/bdgtr/core/models/account.rb +177 -0
- data/lib/bdgtr/core/models/account_requisition.rb +12 -0
- data/lib/bdgtr/core/models/account_sync.rb +12 -0
- data/lib/bdgtr/core/models/import.rb +12 -0
- data/lib/bdgtr/core/models/ledger.rb +10 -0
- data/lib/bdgtr/core/models/requisition.rb +27 -0
- data/lib/bdgtr/core/models/transaction.rb +98 -0
- data/lib/bdgtr/core/models/transaction_category.rb +12 -0
- data/lib/bdgtr/core/record.rb +11 -0
- data/lib/bdgtr/core/schema.rb +290 -0
- data/lib/bdgtr/core/session.rb +76 -0
- data/lib/bdgtr/core/syncable.rb +272 -0
- data/lib/bdgtr/core/transfer.rb +90 -0
- data/lib/bdgtr/core.rb +18 -0
- data/lib/bdgtr/portfolio/Rakefile +20 -0
- data/lib/bdgtr/portfolio/cli/LICENSE +21 -0
- data/lib/bdgtr/portfolio/cli/lib/portfolio_performance_api/cli/app.rb +77 -0
- data/lib/bdgtr/portfolio/cli/lib/portfolio_performance_api/cli/import.rb +30 -0
- data/lib/bdgtr/portfolio/cli/lib/portfolio_performance_api/cli/spreadsheet.rb +25 -0
- data/lib/bdgtr/portfolio/cli/lib/portfolio_performance_api/cli.rb +52 -0
- data/lib/bdgtr/portfolio/exe/bdgtr +7 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/accounts_csv.rb +67 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/app.rb +104 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/auth.rb +52 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/balances.rb +157 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/cache.rb +46 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/checkbox_menu.rb +90 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/checkbox_selection.rb +68 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/config.rb +138 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/decryptor.rb +70 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/drive_client.rb +182 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/errors.rb +10 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/file_reader.rb +80 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/fineco_import.rb +609 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/fineco_xls.rb +235 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/import/fineco.rb +243 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/import.rb +85 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/parser.rb +254 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/portfolio_source.rb +30 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/portfolio_store.rb +124 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/preview_window.rb +98 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/proto/client_pb.rb +29 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/row_preview.rb +174 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/sheets_client.rb +272 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/snapshot.rb +27 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/sync.rb +247 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/sync_preview.rb +63 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/transaction_identity.rb +49 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/transaction_sync.rb +948 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/transaction_types.rb +142 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/version.rb +5 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api.rb +42 -0
- data/lib/bdgtr/portfolio/proto/client.proto +114 -0
- data/lib/bdgtr/psd2/account.rb +24 -0
- data/lib/bdgtr/psd2/account_snapshot.rb +96 -0
- data/lib/bdgtr/psd2/callback.rb +49 -0
- data/lib/bdgtr/psd2/connect.rb +75 -0
- data/lib/bdgtr/psd2/enable_banking.rb +250 -0
- data/lib/bdgtr/psd2/go_cardless.rb +133 -0
- data/lib/bdgtr/psd2/institution.rb +17 -0
- data/lib/bdgtr/psd2/platform.rb +37 -0
- data/lib/bdgtr/psd2/requisition.rb +21 -0
- data/lib/bdgtr/psd2/transaction.rb +29 -0
- data/lib/bdgtr/psd2.rb +25 -0
- data/lib/bdgtr/sync/importer.rb +125 -0
- data/lib/bdgtr/sync/library.rb +18 -0
- data/lib/bdgtr/sync/portfolio.rb +14 -0
- data/lib/bdgtr/sync/projection.rb +38 -0
- data/lib/bdgtr/sync/spreadsheet.rb +23 -0
- data/lib/bdgtr/sync.rb +25 -0
- data/lib/bdgtr/version.rb +1 -1
- metadata +353 -30
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Bdgtr
|
|
4
|
+
module Core
|
|
5
|
+
# Ledger tables. Column names match db/schema.rb. Workspaces and users stay
|
|
6
|
+
# on the app database. A bank consent (requisitions) travels with the file.
|
|
7
|
+
module Schema
|
|
8
|
+
def self.apply!(connection)
|
|
9
|
+
if connection.table_exists?(:accounts)
|
|
10
|
+
add_do_sync!(connection)
|
|
11
|
+
rename_sync_transaction_id!(connection)
|
|
12
|
+
add_account_sync_provider!(connection)
|
|
13
|
+
move_account_data_to_sync_data!(connection)
|
|
14
|
+
link_account_requisitions!(connection)
|
|
15
|
+
add_account_iban!(connection)
|
|
16
|
+
add_account_sync_data!(connection)
|
|
17
|
+
add_requisitions!(connection)
|
|
18
|
+
return
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
connection.create_table :transaction_categories do |t|
|
|
22
|
+
t.integer :workspace_id, null: false
|
|
23
|
+
t.string :name, null: false
|
|
24
|
+
t.string :slug, null: false
|
|
25
|
+
t.string :color, null: false
|
|
26
|
+
t.string :icon, null: false
|
|
27
|
+
t.integer :position, null: false
|
|
28
|
+
t.timestamps null: false
|
|
29
|
+
t.index [ :workspace_id, :name ], unique: true
|
|
30
|
+
t.index [ :workspace_id, :slug ], unique: true
|
|
31
|
+
t.index [ :workspace_id ]
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
connection.create_table :accounts do |t|
|
|
35
|
+
t.string :name
|
|
36
|
+
t.integer :user_id
|
|
37
|
+
t.integer :sync_requisition_id
|
|
38
|
+
t.json :activejobbable_data
|
|
39
|
+
t.datetime :synced_at
|
|
40
|
+
t.integer :sync_every_minutes
|
|
41
|
+
t.json :sync_data
|
|
42
|
+
t.integer :workspace_id
|
|
43
|
+
t.boolean :visible, default: true, null: false
|
|
44
|
+
t.boolean :do_sync, default: true, null: false
|
|
45
|
+
t.string :nickname
|
|
46
|
+
t.string :iban
|
|
47
|
+
t.integer :ord, default: 0, null: false
|
|
48
|
+
t.timestamps null: false
|
|
49
|
+
t.index [ :sync_requisition_id ]
|
|
50
|
+
t.index [ :user_id ]
|
|
51
|
+
t.index [ :workspace_id ]
|
|
52
|
+
t.index [ :workspace_id, :iban ], name: "index_accounts_on_workspace_id_and_iban"
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
connection.create_table :requisitions do |t|
|
|
56
|
+
t.string :requisition_id
|
|
57
|
+
t.string :status
|
|
58
|
+
t.integer :max_historical_days
|
|
59
|
+
t.datetime :access_valid_until
|
|
60
|
+
t.datetime :renewal_notified_at
|
|
61
|
+
t.string :platform, default: "gocardless", null: false
|
|
62
|
+
t.timestamps null: false
|
|
63
|
+
t.index [ :requisition_id ]
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
connection.create_table :account_requisitions do |t|
|
|
67
|
+
t.integer :account_id, null: false
|
|
68
|
+
t.integer :requisition_id, null: false
|
|
69
|
+
t.string :external_account_id, null: false
|
|
70
|
+
t.timestamps null: false
|
|
71
|
+
t.index [ :account_id, :requisition_id ], unique: true, name: "index_account_requisitions_on_account_and_requisition"
|
|
72
|
+
t.index [ :requisition_id ]
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
connection.create_table :imports do |t|
|
|
76
|
+
t.integer :workspace_id, null: false
|
|
77
|
+
t.integer :account_id, null: false
|
|
78
|
+
t.string :state, default: "pre_created", null: false
|
|
79
|
+
t.text :notes
|
|
80
|
+
t.json :preview
|
|
81
|
+
t.text :error_message
|
|
82
|
+
t.json :source_parsed
|
|
83
|
+
t.integer :imported_rows, default: 0, null: false
|
|
84
|
+
t.timestamps null: false
|
|
85
|
+
t.index [ :account_id ]
|
|
86
|
+
t.index [ :workspace_id ]
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
connection.create_table :transactions do |t|
|
|
90
|
+
t.integer :account_id
|
|
91
|
+
t.string :sync_transaction_id
|
|
92
|
+
t.date :date_booking
|
|
93
|
+
t.date :date_value
|
|
94
|
+
t.decimal :amount, precision: 8, scale: 2
|
|
95
|
+
t.string :currency
|
|
96
|
+
t.boolean :reconciled, default: false
|
|
97
|
+
t.datetime :reconciled_at
|
|
98
|
+
t.text :notes
|
|
99
|
+
t.json :data
|
|
100
|
+
t.text :description
|
|
101
|
+
t.datetime :synced_at
|
|
102
|
+
t.json :sync_data
|
|
103
|
+
t.boolean :user_edited, default: false, null: false
|
|
104
|
+
t.string :source
|
|
105
|
+
t.string :entry_hash
|
|
106
|
+
t.integer :import_id
|
|
107
|
+
t.integer :transaction_category_id
|
|
108
|
+
t.timestamps null: false
|
|
109
|
+
t.index [ :account_id, :entry_hash ]
|
|
110
|
+
t.index [ :account_id, :sync_transaction_id ], unique: true, where: "sync_transaction_id IS NOT NULL AND sync_transaction_id != ''"
|
|
111
|
+
t.index [ :account_id ]
|
|
112
|
+
t.index [ :date_value ]
|
|
113
|
+
t.index [ :import_id ]
|
|
114
|
+
t.index [ :transaction_category_id ]
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
connection.create_table :account_syncs do |t|
|
|
118
|
+
t.integer :account_id, null: false
|
|
119
|
+
t.string :state, default: "created", null: false
|
|
120
|
+
t.text :error_message
|
|
121
|
+
t.integer :transactions_count, default: 0, null: false
|
|
122
|
+
t.json :sync_output
|
|
123
|
+
t.json :data
|
|
124
|
+
t.datetime :scheduled_at
|
|
125
|
+
t.datetime :started_at
|
|
126
|
+
t.datetime :finished_at
|
|
127
|
+
t.integer :provider_id
|
|
128
|
+
t.timestamps null: false
|
|
129
|
+
t.index [ :account_id ]
|
|
130
|
+
t.index [ :provider_id ]
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
connection.create_table :tags do |t|
|
|
134
|
+
t.string :name
|
|
135
|
+
t.integer :taggings_count, default: 0
|
|
136
|
+
t.timestamps null: false
|
|
137
|
+
t.index [ :name ], unique: true
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
connection.create_table :taggings do |t|
|
|
141
|
+
t.integer :tag_id
|
|
142
|
+
t.string :taggable_type
|
|
143
|
+
t.bigint :taggable_id
|
|
144
|
+
t.string :tagger_type
|
|
145
|
+
t.bigint :tagger_id
|
|
146
|
+
t.string :context, limit: 128
|
|
147
|
+
t.string :tenant, limit: 128
|
|
148
|
+
t.datetime :created_at
|
|
149
|
+
t.index [ :tag_id, :taggable_id, :taggable_type, :context, :tagger_id, :tagger_type ], unique: true, name: "taggings_idx"
|
|
150
|
+
t.index [ :tag_id ]
|
|
151
|
+
t.index [ :taggable_id, :taggable_type, :context ], name: "taggings_taggable_context_idx"
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
connection.add_foreign_key :imports, :accounts
|
|
155
|
+
connection.add_foreign_key :account_syncs, :accounts
|
|
156
|
+
connection.add_foreign_key :transactions, :imports
|
|
157
|
+
connection.add_foreign_key :transactions, :transaction_categories
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
def self.add_do_sync!(connection)
|
|
161
|
+
return if connection.column_exists?(:accounts, :do_sync)
|
|
162
|
+
|
|
163
|
+
connection.add_column :accounts, :do_sync, :boolean, default: true, null: false
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
def self.rename_sync_transaction_id!(connection)
|
|
167
|
+
return unless connection.table_exists?(:transactions)
|
|
168
|
+
return if connection.column_exists?(:transactions, :sync_transaction_id)
|
|
169
|
+
return unless connection.column_exists?(:transactions, :transaction_id)
|
|
170
|
+
|
|
171
|
+
connection.indexes(:transactions).each do |index|
|
|
172
|
+
next unless index.columns == [ "account_id", "transaction_id" ]
|
|
173
|
+
|
|
174
|
+
connection.remove_index(:transactions, name: index.name)
|
|
175
|
+
end
|
|
176
|
+
connection.rename_column :transactions, :transaction_id, :sync_transaction_id
|
|
177
|
+
connection.add_index :transactions, [ :account_id, :sync_transaction_id ],
|
|
178
|
+
unique: true,
|
|
179
|
+
where: "sync_transaction_id IS NOT NULL AND sync_transaction_id != ''",
|
|
180
|
+
name: "index_transactions_on_account_id_and_sync_transaction_id"
|
|
181
|
+
end
|
|
182
|
+
|
|
183
|
+
def self.move_account_data_to_sync_data!(connection)
|
|
184
|
+
return unless connection.table_exists?(:accounts)
|
|
185
|
+
return unless connection.column_exists?(:accounts, :data)
|
|
186
|
+
|
|
187
|
+
unless connection.column_exists?(:accounts, :sync_data)
|
|
188
|
+
connection.add_column :accounts, :sync_data, :json
|
|
189
|
+
end
|
|
190
|
+
connection.execute("UPDATE accounts SET sync_data = data WHERE data IS NOT NULL")
|
|
191
|
+
connection.remove_column :accounts, :data
|
|
192
|
+
end
|
|
193
|
+
|
|
194
|
+
def self.link_account_requisitions!(connection)
|
|
195
|
+
return unless connection.table_exists?(:accounts)
|
|
196
|
+
|
|
197
|
+
unless connection.table_exists?(:account_requisitions)
|
|
198
|
+
connection.create_table :account_requisitions do |t|
|
|
199
|
+
t.integer :account_id, null: false
|
|
200
|
+
t.integer :requisition_id, null: false
|
|
201
|
+
t.string :external_account_id, null: false
|
|
202
|
+
t.timestamps null: false
|
|
203
|
+
t.index [ :account_id, :requisition_id ], unique: true, name: "index_account_requisitions_on_account_and_requisition"
|
|
204
|
+
t.index [ :requisition_id ]
|
|
205
|
+
end
|
|
206
|
+
end
|
|
207
|
+
|
|
208
|
+
unless connection.column_exists?(:accounts, :sync_requisition_id)
|
|
209
|
+
connection.add_column :accounts, :sync_requisition_id, :integer
|
|
210
|
+
connection.add_index :accounts, :sync_requisition_id
|
|
211
|
+
end
|
|
212
|
+
|
|
213
|
+
if connection.column_exists?(:accounts, :requisition_id)
|
|
214
|
+
connection.execute(<<~SQL)
|
|
215
|
+
INSERT INTO account_requisitions (account_id, requisition_id, external_account_id, created_at, updated_at)
|
|
216
|
+
SELECT id, requisition_id, requisition_account_id, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP
|
|
217
|
+
FROM accounts
|
|
218
|
+
WHERE requisition_id IS NOT NULL
|
|
219
|
+
AND requisition_account_id IS NOT NULL
|
|
220
|
+
AND requisition_account_id != ''
|
|
221
|
+
AND NOT EXISTS (
|
|
222
|
+
SELECT 1 FROM account_requisitions
|
|
223
|
+
WHERE account_requisitions.account_id = accounts.id
|
|
224
|
+
AND account_requisitions.requisition_id = accounts.requisition_id
|
|
225
|
+
)
|
|
226
|
+
SQL
|
|
227
|
+
connection.execute(<<~SQL)
|
|
228
|
+
UPDATE accounts
|
|
229
|
+
SET sync_requisition_id = requisition_id
|
|
230
|
+
WHERE sync_requisition_id IS NULL AND requisition_id IS NOT NULL
|
|
231
|
+
SQL
|
|
232
|
+
if connection.index_exists?(:accounts, :requisition_id)
|
|
233
|
+
connection.remove_index :accounts, :requisition_id
|
|
234
|
+
end
|
|
235
|
+
connection.remove_column :accounts, :requisition_id
|
|
236
|
+
end
|
|
237
|
+
|
|
238
|
+
if connection.column_exists?(:accounts, :requisition_account_id)
|
|
239
|
+
connection.remove_column :accounts, :requisition_account_id
|
|
240
|
+
end
|
|
241
|
+
end
|
|
242
|
+
|
|
243
|
+
def self.add_account_iban!(connection)
|
|
244
|
+
return unless connection.table_exists?(:accounts)
|
|
245
|
+
return if connection.column_exists?(:accounts, :iban)
|
|
246
|
+
|
|
247
|
+
connection.add_column :accounts, :iban, :string
|
|
248
|
+
if connection.column_exists?(:accounts, :sync_data)
|
|
249
|
+
connection.execute(<<~SQL)
|
|
250
|
+
UPDATE accounts
|
|
251
|
+
SET iban = upper(replace(json_extract(sync_data, '$.details.account.iban'), ' ', ''))
|
|
252
|
+
WHERE json_extract(sync_data, '$.details.account.iban') IS NOT NULL
|
|
253
|
+
AND trim(json_extract(sync_data, '$.details.account.iban')) != ''
|
|
254
|
+
SQL
|
|
255
|
+
end
|
|
256
|
+
connection.add_index :accounts, [ :workspace_id, :iban ], name: "index_accounts_on_workspace_id_and_iban"
|
|
257
|
+
end
|
|
258
|
+
|
|
259
|
+
def self.add_account_sync_data!(connection)
|
|
260
|
+
return unless connection.table_exists?(:account_syncs)
|
|
261
|
+
|
|
262
|
+
connection.add_column :account_syncs, :data, :json unless connection.column_exists?(:account_syncs, :data)
|
|
263
|
+
Bdgtr::Psd2::AccountSnapshot.archive!(connection)
|
|
264
|
+
end
|
|
265
|
+
|
|
266
|
+
def self.add_requisitions!(connection)
|
|
267
|
+
return if connection.table_exists?(:requisitions)
|
|
268
|
+
|
|
269
|
+
connection.create_table :requisitions do |t|
|
|
270
|
+
t.string :requisition_id
|
|
271
|
+
t.string :status
|
|
272
|
+
t.integer :max_historical_days
|
|
273
|
+
t.datetime :access_valid_until
|
|
274
|
+
t.datetime :renewal_notified_at
|
|
275
|
+
t.string :platform, default: "gocardless", null: false
|
|
276
|
+
t.timestamps null: false
|
|
277
|
+
t.index [ :requisition_id ]
|
|
278
|
+
end
|
|
279
|
+
end
|
|
280
|
+
|
|
281
|
+
def self.add_account_sync_provider!(connection)
|
|
282
|
+
return unless connection.table_exists?(:account_syncs)
|
|
283
|
+
return if connection.column_exists?(:account_syncs, :provider_id)
|
|
284
|
+
|
|
285
|
+
connection.add_column :account_syncs, :provider_id, :integer
|
|
286
|
+
connection.add_index :account_syncs, :provider_id
|
|
287
|
+
end
|
|
288
|
+
end
|
|
289
|
+
end
|
|
290
|
+
end
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "fileutils"
|
|
4
|
+
|
|
5
|
+
module Bdgtr
|
|
6
|
+
module Core
|
|
7
|
+
# Points the ledger models at one SQLite file for the block, then returns
|
|
8
|
+
# them to the primary database. One open at a time in this process.
|
|
9
|
+
class Session
|
|
10
|
+
LOCK = Mutex.new
|
|
11
|
+
|
|
12
|
+
def self.open(path)
|
|
13
|
+
path = File.expand_path(path.to_s)
|
|
14
|
+
return yield if Thread.current[:bdgtr_ledger_path] == path
|
|
15
|
+
|
|
16
|
+
LOCK.synchronize do
|
|
17
|
+
switched = false
|
|
18
|
+
committed = false
|
|
19
|
+
previous_path = Thread.current[:bdgtr_ledger_path]
|
|
20
|
+
owned = owns_connection?(Record)
|
|
21
|
+
tag_owned = tag_models.to_h { |model| [ model, owns_connection?(model) ] }
|
|
22
|
+
saved_record = owned ? Record.connection_db_config : nil
|
|
23
|
+
saved_tags = tag_models.to_h { |model| [ model, tag_owned[model] ? model.connection_db_config : nil ] }
|
|
24
|
+
Thread.current[:bdgtr_ledger_path] = path
|
|
25
|
+
FileUtils.mkdir_p(File.dirname(path))
|
|
26
|
+
connect(adapter: "sqlite3", database: path, pool: 5, timeout: 5_000)
|
|
27
|
+
switched = true
|
|
28
|
+
Schema.apply!(Record.connection)
|
|
29
|
+
result = yield
|
|
30
|
+
committed = true
|
|
31
|
+
result
|
|
32
|
+
ensure
|
|
33
|
+
Thread.current[:bdgtr_ledger_path] = previous_path
|
|
34
|
+
persist_ledger! if switched && committed
|
|
35
|
+
if switched
|
|
36
|
+
if owned
|
|
37
|
+
Record.establish_connection(saved_record)
|
|
38
|
+
else
|
|
39
|
+
Record.remove_connection
|
|
40
|
+
end
|
|
41
|
+
tag_models.each do |model|
|
|
42
|
+
if tag_owned[model]
|
|
43
|
+
model.establish_connection(saved_tags[model])
|
|
44
|
+
else
|
|
45
|
+
model.remove_connection
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def self.persist_ledger!
|
|
53
|
+
connection = Record.lease_connection
|
|
54
|
+
connection.commit_transaction if connection.transaction_open?
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def self.connect(config)
|
|
58
|
+
Record.establish_connection(config)
|
|
59
|
+
shared = Record.connection_db_config
|
|
60
|
+
tag_models.each { |model| model.establish_connection(shared) }
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def self.owns_connection?(model)
|
|
64
|
+
return false unless model.instance_variable_defined?(:@connection_specification_name)
|
|
65
|
+
|
|
66
|
+
model.instance_variable_get(:@connection_specification_name).present?
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def self.tag_models
|
|
70
|
+
return [] unless defined?(ActsAsTaggableOn::Tag)
|
|
71
|
+
|
|
72
|
+
[ ActsAsTaggableOn::Tag, ActsAsTaggableOn::Tagging ]
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
end
|
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Bdgtr
|
|
4
|
+
module Core
|
|
5
|
+
# Bank sync used by the ledger account. The web app adds the job, the
|
|
6
|
+
# cache, and the renewal email around the same steps.
|
|
7
|
+
module Syncable
|
|
8
|
+
extend ActiveSupport::Concern
|
|
9
|
+
|
|
10
|
+
included do
|
|
11
|
+
const_set(:BALANCE_TYPE_DEFAULT, "interimAvailable") unless const_defined?(:BALANCE_TYPE_DEFAULT, false)
|
|
12
|
+
|
|
13
|
+
after_save do
|
|
14
|
+
Rails.cache.delete(cache_key.to_s) if defined?(Rails::Application)
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
class_methods do
|
|
19
|
+
def schedule_due_syncs!
|
|
20
|
+
return unless defined?(::Workspace) && defined?(::AccountSyncJob)
|
|
21
|
+
|
|
22
|
+
due = where(id: link_model(:AccountRequisition).where.not(external_account_id: [ nil, "" ]).select(:account_id))
|
|
23
|
+
ledger_workspace_ids = ::Workspace.where.not(ledger_path: [ nil, "" ]).pluck(:id)
|
|
24
|
+
primary = ledger_workspace_ids.any? ? due.where.not(workspace_id: ledger_workspace_ids) : due
|
|
25
|
+
primary.find_each { |account| enqueue_due_sync(account) }
|
|
26
|
+
|
|
27
|
+
::Workspace.where.not(ledger_path: [ nil, "" ]).find_each do |workspace|
|
|
28
|
+
Bdgtr::Core::Session.open(workspace.ledger_path) do
|
|
29
|
+
due.find_each { |account| enqueue_due_sync(account) }
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def enqueue_due_sync(account)
|
|
35
|
+
return unless defined?(::AccountSyncJob)
|
|
36
|
+
return unless account.do_sync?
|
|
37
|
+
return if account.account_syncs.open.exists?
|
|
38
|
+
|
|
39
|
+
record = account.account_syncs.create!(scheduled_at: Time.current)
|
|
40
|
+
::AccountSyncJob.perform_later(record.id)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def link_model(name)
|
|
44
|
+
return Object.const_get(name) if Object.const_defined?(name, false)
|
|
45
|
+
|
|
46
|
+
Bdgtr::Core::Models.const_get(name)
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def sync_min_seconds_between_requests
|
|
51
|
+
return ::Settings.gocardless.min_seconds_between_requests if defined?(::Settings)
|
|
52
|
+
|
|
53
|
+
8640
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def sync_next_time
|
|
57
|
+
synced_at ? synced_at.advance(seconds: sync_min_seconds_between_requests) : Time.now
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def sync_in_seconds
|
|
61
|
+
(sync_next_time - Time.now).to_i
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def sync_next_time_in_words
|
|
65
|
+
ActiveSupport::Duration.build(sync_in_seconds).inspect
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def sync_every_minutes
|
|
69
|
+
value = read_attribute(:sync_every_minutes)
|
|
70
|
+
default = defined?(::Settings) ? ::Settings.account.default_sync_every_minutes : 144
|
|
71
|
+
return default unless value
|
|
72
|
+
return default if value < default
|
|
73
|
+
|
|
74
|
+
value
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def sync_json
|
|
78
|
+
return "{}" unless defined?(::AccountSerializer)
|
|
79
|
+
|
|
80
|
+
if defined?(Rails::Application)
|
|
81
|
+
Rails.cache.fetch(cache_key, expires_in: sync_min_seconds_between_requests.seconds) do
|
|
82
|
+
::AccountSerializer.new(self).to_json
|
|
83
|
+
end
|
|
84
|
+
else
|
|
85
|
+
::AccountSerializer.new(self).to_json
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def sync_self!(options = {})
|
|
90
|
+
options = { force: false }.merge(options.compact)
|
|
91
|
+
return sync_json if Time.now < sync_next_time && !options[:force]
|
|
92
|
+
|
|
93
|
+
service = psd2_service(options[:service], provider: options[:provider])
|
|
94
|
+
account = service.get_account(external_account_id)
|
|
95
|
+
metadata = account.get_metadata
|
|
96
|
+
details = account.get_details
|
|
97
|
+
balances = account.get_balances
|
|
98
|
+
payload = {
|
|
99
|
+
"metadata" => metadata,
|
|
100
|
+
"details" => details,
|
|
101
|
+
"balances" => balances
|
|
102
|
+
}
|
|
103
|
+
status_code = details.dig("status_code") || balances.dig("status_code") || 200
|
|
104
|
+
if status_code == 200
|
|
105
|
+
self.name = [
|
|
106
|
+
metadata.dig("institution_id"),
|
|
107
|
+
details.dig("account", "name"),
|
|
108
|
+
details.dig("account", "currency")
|
|
109
|
+
].compact.join(" - ")
|
|
110
|
+
self.provider_payload = payload
|
|
111
|
+
self.sync_data = Bdgtr::Psd2::AccountSnapshot.normalize(payload)
|
|
112
|
+
self.synced_at = Time.now
|
|
113
|
+
remember_iban!(details)
|
|
114
|
+
save! unless options[:save] == false
|
|
115
|
+
end
|
|
116
|
+
Rails.cache.delete(cache_key.to_s) if defined?(Rails::Application) && (status_code == 200 || options[:force])
|
|
117
|
+
sync_json
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
def sync_transactions!(options = {})
|
|
121
|
+
options = { destroy_existing: false, force: false }.merge(options.compact)
|
|
122
|
+
return { "skipped" => "interval" } if !options[:force] && !options[:destroy_existing] && !transactions_sync_due?
|
|
123
|
+
|
|
124
|
+
transactions.destroy_all if options[:destroy_existing]
|
|
125
|
+
service = psd2_service(options[:service], provider: options[:provider])
|
|
126
|
+
date_from = options[:date_from] || (Date.current - historical_days)
|
|
127
|
+
touched = []
|
|
128
|
+
payload = service.get_transactions(
|
|
129
|
+
requisition_account_id: external_account_id,
|
|
130
|
+
date_from: date_from,
|
|
131
|
+
date_to: options[:date_to]
|
|
132
|
+
)
|
|
133
|
+
transactions_model = self.class.link_model(:Transaction)
|
|
134
|
+
transactions_model.without_categorization do
|
|
135
|
+
payload.dig("transactions", "booked")&.each do |transaction_data|
|
|
136
|
+
bank_id = transaction_data["transactionId"].presence
|
|
137
|
+
next if bank_id.blank?
|
|
138
|
+
|
|
139
|
+
transaction = transactions.find_or_initialize_by(sync_transaction_id: bank_id)
|
|
140
|
+
transaction.sync_data = transaction_data
|
|
141
|
+
transaction.synced_at = Time.current
|
|
142
|
+
unless transaction.user_edited?
|
|
143
|
+
transaction.sync_transaction_id = bank_id
|
|
144
|
+
transaction.amount = transaction_data.dig("transactionAmount", "amount") || 0
|
|
145
|
+
transaction.date_booking = transaction_data["bookingDate"]
|
|
146
|
+
transaction.date_value = transaction_data["valueDate"]
|
|
147
|
+
transaction.currency = transaction_data.dig("transactionAmount", "currency")
|
|
148
|
+
transaction.description = transaction_data["remittanceInformationUnstructured"]
|
|
149
|
+
transaction.data = transaction_data
|
|
150
|
+
end
|
|
151
|
+
transaction.assign_source("sync")
|
|
152
|
+
transaction.save!
|
|
153
|
+
transaction.reconcile! unless transaction.user_edited?
|
|
154
|
+
touched << transaction
|
|
155
|
+
end
|
|
156
|
+
end
|
|
157
|
+
transactions_model.categorize_transactions(touched)
|
|
158
|
+
payload
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
def sync!(options = {})
|
|
162
|
+
options = {
|
|
163
|
+
skip_now: false,
|
|
164
|
+
schedule?: true,
|
|
165
|
+
job_args: {},
|
|
166
|
+
options_self: {},
|
|
167
|
+
options_transactions: {}
|
|
168
|
+
}.merge(options.compact)
|
|
169
|
+
|
|
170
|
+
output = nil
|
|
171
|
+
unless options[:skip_now]
|
|
172
|
+
link = latest_account_requisition
|
|
173
|
+
remember_sync_requisition!(link)
|
|
174
|
+
service = options.dig(:options_self, :service) || options.dig(:options_transactions, :service)
|
|
175
|
+
provider = options.dig(:options_self, :provider) || options.dig(:options_transactions, :provider)
|
|
176
|
+
refresh_requisition!(service, provider: provider)
|
|
177
|
+
output = if linked_requisition&.needs_renewal?
|
|
178
|
+
notify_requisition_renewal!
|
|
179
|
+
{ "skipped" => "renewal" }
|
|
180
|
+
else
|
|
181
|
+
sync_self!(options[:options_self] || {})
|
|
182
|
+
sync_transactions!(options[:options_transactions] || {})
|
|
183
|
+
end
|
|
184
|
+
end
|
|
185
|
+
|
|
186
|
+
schedule_next_sync!(options[:job_args] || {}) if options[:schedule?]
|
|
187
|
+
output
|
|
188
|
+
end
|
|
189
|
+
|
|
190
|
+
def refresh_requisition!(service = nil, provider: nil)
|
|
191
|
+
record = linked_requisition
|
|
192
|
+
return unless record&.requisition_id.present?
|
|
193
|
+
|
|
194
|
+
service ||= psd2_service(provider: provider)
|
|
195
|
+
data = service.get_requisition_by_id(record.requisition_id)
|
|
196
|
+
return data unless data.is_a?(Hash)
|
|
197
|
+
|
|
198
|
+
record.status = data["status"] if data["status"].present?
|
|
199
|
+
record.save! if record.changed?
|
|
200
|
+
data
|
|
201
|
+
rescue StandardError => error
|
|
202
|
+
message = "[sync] requisition refresh account=#{id}: #{error.message}"
|
|
203
|
+
defined?(Rails::Application) ? Rails.logger.warn(message) : warn(message)
|
|
204
|
+
nil
|
|
205
|
+
end
|
|
206
|
+
|
|
207
|
+
def notify_requisition_renewal!
|
|
208
|
+
record = linked_requisition
|
|
209
|
+
return unless record&.needs_renewal?
|
|
210
|
+
return if record.renewal_recently_notified?
|
|
211
|
+
return unless defined?(::UserMailer)
|
|
212
|
+
return if user&.email.blank?
|
|
213
|
+
|
|
214
|
+
::UserMailer.requisition_renewal(user, self).deliver_later
|
|
215
|
+
record.update!(renewal_notified_at: Time.current)
|
|
216
|
+
end
|
|
217
|
+
|
|
218
|
+
def historical_days
|
|
219
|
+
days = linked_requisition&.max_historical_days.to_i
|
|
220
|
+
days.positive? ? days : 90
|
|
221
|
+
end
|
|
222
|
+
|
|
223
|
+
def linked_requisition
|
|
224
|
+
requisition
|
|
225
|
+
end
|
|
226
|
+
|
|
227
|
+
def remember_sync_requisition!(link)
|
|
228
|
+
return if link.nil? || sync_requisition_id == link.requisition_id
|
|
229
|
+
|
|
230
|
+
if persisted?
|
|
231
|
+
update_column(:sync_requisition_id, link.requisition_id)
|
|
232
|
+
else
|
|
233
|
+
self.sync_requisition_id = link.requisition_id
|
|
234
|
+
end
|
|
235
|
+
end
|
|
236
|
+
|
|
237
|
+
def psd2_service(explicit = nil, provider: nil)
|
|
238
|
+
return explicit if explicit
|
|
239
|
+
|
|
240
|
+
platform = linked_requisition&.platform.to_s
|
|
241
|
+
platform = provider.to_s if platform.blank?
|
|
242
|
+
platform = "enablebanking" if platform == "enable_banking"
|
|
243
|
+
if platform.blank? || platform == "gocardless"
|
|
244
|
+
return defined?(::NordigenService) ? ::NordigenService.new : Bdgtr::Psd2::GoCardless.new
|
|
245
|
+
end
|
|
246
|
+
|
|
247
|
+
Bdgtr::Psd2.client(platform)
|
|
248
|
+
end
|
|
249
|
+
|
|
250
|
+
def transactions_sync_due?
|
|
251
|
+
last = transactions.maximum(:synced_at)
|
|
252
|
+
last.nil? || Time.current >= last + sync_min_seconds_between_requests
|
|
253
|
+
end
|
|
254
|
+
|
|
255
|
+
def schedule_next_sync!(_job_args = {})
|
|
256
|
+
return unless defined?(::AccountSyncJob)
|
|
257
|
+
return unless persisted?
|
|
258
|
+
return unless do_sync?
|
|
259
|
+
return if account_syncs.open.exists?
|
|
260
|
+
|
|
261
|
+
code = linked_requisition&.platform.to_s
|
|
262
|
+
code = "enablebanking" if code == "enable_banking"
|
|
263
|
+
provider = ::Provider.find_by(code: code) if defined?(::Provider) && ::Provider.connection.table_exists?(:providers)
|
|
264
|
+
record = account_syncs.create!(
|
|
265
|
+
scheduled_at: sync_every_minutes.minutes.from_now,
|
|
266
|
+
provider: provider
|
|
267
|
+
)
|
|
268
|
+
::AccountSyncJob.set(wait: sync_every_minutes.minutes).perform_later(record.id)
|
|
269
|
+
end
|
|
270
|
+
end
|
|
271
|
+
end
|
|
272
|
+
end
|