bdgtr-cli 0.1.0 → 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 +3 -3
- data/bin/bdgtr +26 -10
- data/data/bdgtr.sqlite +0 -0
- data/lib/bdgtr/cli/config.rb +188 -0
- data/lib/bdgtr/cli/config.sample.yml +15 -0
- data/lib/bdgtr/cli/gem_home.rb +0 -25
- 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,530 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "rbconfig"
|
|
4
|
+
require "tty-prompt"
|
|
5
|
+
require "uri"
|
|
6
|
+
|
|
7
|
+
module Bdgtr
|
|
8
|
+
class Cli
|
|
9
|
+
class Psd2
|
|
10
|
+
def self.start(argv)
|
|
11
|
+
command = Array(argv)[0].to_s
|
|
12
|
+
case command
|
|
13
|
+
when "account:list" then list_accounts(argv[1])
|
|
14
|
+
when "account:connect" then connect_account(argv.drop(1))
|
|
15
|
+
when "account:sync" then sync_account(argv[1])
|
|
16
|
+
when "account:sync:enable" then set_account_sync(argv[1], true)
|
|
17
|
+
when "account:sync:disable" then set_account_sync(argv[1], false)
|
|
18
|
+
when "account:delete" then delete_account(argv[1])
|
|
19
|
+
when "transaction:search" then search_transactions(argv[1])
|
|
20
|
+
else
|
|
21
|
+
warn "unknown command #{command}"
|
|
22
|
+
exit 1
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def self.list_accounts(workspace_id)
|
|
27
|
+
workspace = workspace!(workspace_id)
|
|
28
|
+
records = read(workspace) do
|
|
29
|
+
::Account.where(workspace_id: workspace.id).order(:id).map do |account|
|
|
30
|
+
{ row: account_row(account), detail: account_detail(account) }
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
if records.empty?
|
|
34
|
+
puts "No accounts."
|
|
35
|
+
return
|
|
36
|
+
end
|
|
37
|
+
Cli::Table.show(
|
|
38
|
+
%w[id name currency],
|
|
39
|
+
records.map { |record| record[:row] },
|
|
40
|
+
on_select: ->(index) { records[index][:detail] }
|
|
41
|
+
)
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def self.sync_account(account_id)
|
|
45
|
+
if account_id.blank?
|
|
46
|
+
warn "Usage: bdgtr account:sync ACCOUNT_ID"
|
|
47
|
+
exit 1
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
workspace = workspace!
|
|
51
|
+
summary = read(workspace) do
|
|
52
|
+
account = ::Account.find_by(id: account_id, workspace_id: workspace.id)
|
|
53
|
+
next if account.nil?
|
|
54
|
+
|
|
55
|
+
run_sync(account)
|
|
56
|
+
end
|
|
57
|
+
if summary.nil?
|
|
58
|
+
warn "account #{account_id} not found"
|
|
59
|
+
exit 1
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
report_sync(summary)
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def self.set_account_sync(account_id, enabled)
|
|
66
|
+
if account_id.blank?
|
|
67
|
+
warn "Usage: bdgtr account:sync:#{enabled ? "enable" : "disable"} ACCOUNT_ID"
|
|
68
|
+
exit 1
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
workspace = workspace!
|
|
72
|
+
account = read(workspace) do
|
|
73
|
+
record = ::Account.find_by(id: account_id, workspace_id: workspace.id)
|
|
74
|
+
next if record.nil?
|
|
75
|
+
|
|
76
|
+
record.update!(do_sync: enabled)
|
|
77
|
+
record
|
|
78
|
+
end
|
|
79
|
+
if account.nil?
|
|
80
|
+
warn "account #{account_id} not found"
|
|
81
|
+
exit 1
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
puts "account #{account.id} sync #{enabled ? "enabled" : "disabled"}"
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def self.delete_account(account_id)
|
|
88
|
+
if account_id.blank?
|
|
89
|
+
warn "Usage: bdgtr account:delete ACCOUNT_ID"
|
|
90
|
+
exit 1
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
workspace = workspace!
|
|
94
|
+
found = read(workspace) do
|
|
95
|
+
account = ::Account.find_by(id: account_id, workspace_id: workspace.id)
|
|
96
|
+
next if account.nil?
|
|
97
|
+
|
|
98
|
+
{
|
|
99
|
+
id: account.id,
|
|
100
|
+
name: account.read_attribute("name").presence || account.name,
|
|
101
|
+
transactions: account.transactions.count
|
|
102
|
+
}
|
|
103
|
+
end
|
|
104
|
+
if found.nil?
|
|
105
|
+
warn "account #{account_id} not found"
|
|
106
|
+
exit 1
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
question = "Delete account #{found[:id]} #{found[:name]} and #{found[:transactions]} transactions?"
|
|
110
|
+
unless Cli::Prompt.confirm(question) == :yes
|
|
111
|
+
warn "Aborted."
|
|
112
|
+
exit 1
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
read(workspace) do
|
|
116
|
+
account = ::Account.find(account_id)
|
|
117
|
+
count = account.transactions.count
|
|
118
|
+
account.destroy!
|
|
119
|
+
puts "Deleted account #{account.id} and #{count} transactions."
|
|
120
|
+
end
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
def self.search_transactions(account_id)
|
|
124
|
+
if account_id.blank?
|
|
125
|
+
warn "Usage: bdgtr transaction:search ACCOUNT_ID"
|
|
126
|
+
exit 1
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
workspace = workspace!
|
|
130
|
+
records = read(workspace) do
|
|
131
|
+
account = ::Account.find_by(id: account_id, workspace_id: workspace.id)
|
|
132
|
+
next if account.nil?
|
|
133
|
+
|
|
134
|
+
account.transactions.includes(:transaction_category, :tags).order(Arel.sql("COALESCE(date_value, date_booking) DESC"), :id).map do |transaction|
|
|
135
|
+
{ row: transaction_row(transaction), detail: transaction_detail(transaction) }
|
|
136
|
+
end
|
|
137
|
+
end
|
|
138
|
+
if records.nil?
|
|
139
|
+
warn "account #{account_id} not found"
|
|
140
|
+
exit 1
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
if records.empty?
|
|
144
|
+
puts "No transactions."
|
|
145
|
+
else
|
|
146
|
+
Cli::Table.show(
|
|
147
|
+
%w[id date amount currency description categories tags],
|
|
148
|
+
records.map { |record| record[:row] },
|
|
149
|
+
on_select: ->(index) { records[index][:detail] },
|
|
150
|
+
search: true,
|
|
151
|
+
summary: "amount"
|
|
152
|
+
)
|
|
153
|
+
end
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
def self.connect_account(args)
|
|
157
|
+
args = Array(args).dup
|
|
158
|
+
provider = take_option(args, "--provider")
|
|
159
|
+
platform = chosen_platform(provider)
|
|
160
|
+
workspace = workspace!(args.find { |arg| !arg.start_with?("-") })
|
|
161
|
+
require "tty-prompt"
|
|
162
|
+
prompt = TTY::Prompt.new
|
|
163
|
+
choices = country_choices
|
|
164
|
+
country = prompt.select("Country", choices, **country_prompt_options(choices))
|
|
165
|
+
service = ::Bdgtr::Psd2.client(platform)
|
|
166
|
+
banks = service.institutions(country)
|
|
167
|
+
if banks.empty?
|
|
168
|
+
warn "No banks for #{country}."
|
|
169
|
+
exit 1
|
|
170
|
+
end
|
|
171
|
+
|
|
172
|
+
bank = prompt.select("Bank", banks.map { |item| { name: item.name, value: item } }, filter: true, per_page: 15)
|
|
173
|
+
listener, redirect_url = authorization_redirect(platform)
|
|
174
|
+
started = service.start_link(
|
|
175
|
+
institution_id: bank.id,
|
|
176
|
+
redirect_url: redirect_url,
|
|
177
|
+
country: country,
|
|
178
|
+
max_historical_days: 90
|
|
179
|
+
)
|
|
180
|
+
puts started.link
|
|
181
|
+
open_browser(started.link)
|
|
182
|
+
callback = if listener
|
|
183
|
+
puts "Waiting for the bank page to return..."
|
|
184
|
+
$stdout.flush
|
|
185
|
+
query = listener.wait
|
|
186
|
+
puts "Authorization received."
|
|
187
|
+
$stdout.flush
|
|
188
|
+
query
|
|
189
|
+
else
|
|
190
|
+
pasted = prompt.ask("Paste the final browser URL:")
|
|
191
|
+
parse_url(pasted)
|
|
192
|
+
end
|
|
193
|
+
finished = service.finish(callback.merge(requisition_id: started.id, max_historical_days: started.max_historical_days))
|
|
194
|
+
previews = account_previews(service, finished)
|
|
195
|
+
if previews.empty?
|
|
196
|
+
puts "No accounts were returned."
|
|
197
|
+
return
|
|
198
|
+
end
|
|
199
|
+
chosen = choose_bank_accounts(prompt, previews)
|
|
200
|
+
if chosen.empty?
|
|
201
|
+
warn "No accounts selected."
|
|
202
|
+
exit 1
|
|
203
|
+
end
|
|
204
|
+
finished = requisition_with_accounts(finished, chosen)
|
|
205
|
+
puts "Saving #{chosen.size} accounts..."
|
|
206
|
+
sheet = Cli::Rows.new(%w[n id name currency])
|
|
207
|
+
sheet.header
|
|
208
|
+
number = 0
|
|
209
|
+
record, _saved, errors = ::Bdgtr::Psd2::Connect.call(workspace: workspace, user: workspace.user, requisition: finished, service: service) do |account|
|
|
210
|
+
number += 1
|
|
211
|
+
sheet.row([ number, account.id, account.read_attribute("name").presence || "-", account.currency || "-" ])
|
|
212
|
+
end
|
|
213
|
+
Array(errors).each do |account, error|
|
|
214
|
+
warn "account #{account.id}: #{error.message}"
|
|
215
|
+
end
|
|
216
|
+
puts "requisition #{record.requisition_id} (#{record.platform})"
|
|
217
|
+
end
|
|
218
|
+
|
|
219
|
+
def self.run_sync(account)
|
|
220
|
+
if account.external_account_id.blank?
|
|
221
|
+
return { error: "account #{account.id} has no bank connection" }
|
|
222
|
+
end
|
|
223
|
+
|
|
224
|
+
started_at = Time.current
|
|
225
|
+
provider = sync_provider(account)
|
|
226
|
+
output = account.sync!(
|
|
227
|
+
schedule?: false,
|
|
228
|
+
options_self: { force: true, provider: provider },
|
|
229
|
+
options_transactions: { force: true, provider: provider }
|
|
230
|
+
)
|
|
231
|
+
failure = bank_failure(output)
|
|
232
|
+
record = account.account_syncs.create!(sync_row(account, provider, failure ? "error" : "synced", started_at, output, failure))
|
|
233
|
+
sync_summary(account, record)
|
|
234
|
+
rescue StandardError => exception
|
|
235
|
+
record = account.account_syncs.create!(
|
|
236
|
+
sync_row(account, provider, "error", Time.current, nil, nil).merge(
|
|
237
|
+
error_message: exception.message.to_s.truncate(5_000),
|
|
238
|
+
transactions_count: 0
|
|
239
|
+
)
|
|
240
|
+
)
|
|
241
|
+
sync_summary(account, record)
|
|
242
|
+
end
|
|
243
|
+
|
|
244
|
+
def self.sync_row(account, provider, state, started_at, output, failure)
|
|
245
|
+
row = {
|
|
246
|
+
state: state,
|
|
247
|
+
error_message: failure ? failure["summary"].presence : nil,
|
|
248
|
+
scheduled_at: started_at,
|
|
249
|
+
started_at: started_at,
|
|
250
|
+
finished_at: Time.current,
|
|
251
|
+
sync_output: output,
|
|
252
|
+
data: account.provider_payload,
|
|
253
|
+
transactions_count: failure ? 0 : account.transactions.where(synced_at: started_at..).count
|
|
254
|
+
}
|
|
255
|
+
record = sync_provider_record(provider)
|
|
256
|
+
row[:provider] = record if record
|
|
257
|
+
row
|
|
258
|
+
end
|
|
259
|
+
|
|
260
|
+
def self.sync_summary(account, record)
|
|
261
|
+
{
|
|
262
|
+
id: account.id,
|
|
263
|
+
state: record.state,
|
|
264
|
+
transactions_count: record.transactions_count,
|
|
265
|
+
skipped: record.output_skipped,
|
|
266
|
+
error_message: record.error_message,
|
|
267
|
+
output: bank_failure(record.sync_output)
|
|
268
|
+
}
|
|
269
|
+
end
|
|
270
|
+
|
|
271
|
+
def self.bank_failure(output)
|
|
272
|
+
return unless output.is_a?(Hash)
|
|
273
|
+
|
|
274
|
+
code = output["status_code"]
|
|
275
|
+
return if code.nil? || code.to_i.between?(200, 299)
|
|
276
|
+
|
|
277
|
+
output
|
|
278
|
+
end
|
|
279
|
+
|
|
280
|
+
def self.report_sync(summary)
|
|
281
|
+
if summary[:error]
|
|
282
|
+
warn summary[:error]
|
|
283
|
+
exit 1
|
|
284
|
+
end
|
|
285
|
+
|
|
286
|
+
puts "account #{summary[:id]} #{summary[:state]}"
|
|
287
|
+
if summary[:output]
|
|
288
|
+
puts JSON.pretty_generate(summary[:output])
|
|
289
|
+
exit 1
|
|
290
|
+
end
|
|
291
|
+
if summary[:error_message].present?
|
|
292
|
+
warn summary[:error_message]
|
|
293
|
+
exit 1
|
|
294
|
+
end
|
|
295
|
+
puts(summary[:skipped].present? ? "skipped #{summary[:skipped]}" : "transactions #{summary[:transactions_count].to_i}")
|
|
296
|
+
end
|
|
297
|
+
|
|
298
|
+
def self.workspace!(id = nil)
|
|
299
|
+
chosen = id.presence || Cli::Config.workspace_id
|
|
300
|
+
if chosen.blank?
|
|
301
|
+
warn "Set a workspace first: bdgtr workspace:set WORKSPACE_ID"
|
|
302
|
+
exit 1
|
|
303
|
+
end
|
|
304
|
+
|
|
305
|
+
Core::Models::Ledger.new(id: chosen.to_i)
|
|
306
|
+
end
|
|
307
|
+
|
|
308
|
+
def self.account_row(account)
|
|
309
|
+
[
|
|
310
|
+
account.id,
|
|
311
|
+
account.name.presence || "-",
|
|
312
|
+
account.currency || "-"
|
|
313
|
+
]
|
|
314
|
+
end
|
|
315
|
+
|
|
316
|
+
def self.account_detail(account)
|
|
317
|
+
lines = account.attribute_names.flat_map do |name|
|
|
318
|
+
value = name == "name" ? account.read_attribute("name") : account.public_send(name)
|
|
319
|
+
detail_lines(name, detail_value(value))
|
|
320
|
+
end
|
|
321
|
+
lines + detail_lines("currency", account.currency) + detail_lines("external_account_id", account.external_account_id)
|
|
322
|
+
end
|
|
323
|
+
|
|
324
|
+
def self.transaction_row(transaction)
|
|
325
|
+
date = transaction.date_value || transaction.date_booking
|
|
326
|
+
[
|
|
327
|
+
transaction.id,
|
|
328
|
+
date&.iso8601,
|
|
329
|
+
transaction.amount,
|
|
330
|
+
transaction.currency,
|
|
331
|
+
Cli::Table.ellipsize(transaction.description, 30),
|
|
332
|
+
category_names(transaction),
|
|
333
|
+
tag_names(transaction)
|
|
334
|
+
]
|
|
335
|
+
end
|
|
336
|
+
|
|
337
|
+
def self.transaction_detail(transaction)
|
|
338
|
+
lines = transaction.attribute_names.flat_map do |name|
|
|
339
|
+
detail_lines(name, detail_value(transaction.public_send(name)))
|
|
340
|
+
end
|
|
341
|
+
lines + detail_lines("categories", category_names(transaction)) + detail_lines("tags", tag_names(transaction))
|
|
342
|
+
end
|
|
343
|
+
|
|
344
|
+
def self.category_names(transaction)
|
|
345
|
+
transaction.category_name
|
|
346
|
+
end
|
|
347
|
+
|
|
348
|
+
def self.tag_names(transaction)
|
|
349
|
+
Array(transaction.tag_names).join(", ")
|
|
350
|
+
end
|
|
351
|
+
|
|
352
|
+
def self.sync_provider_record(code)
|
|
353
|
+
return unless defined?(::Provider)
|
|
354
|
+
return unless ::Provider.connection.table_exists?(:providers)
|
|
355
|
+
|
|
356
|
+
::Provider.find_by(code: code)
|
|
357
|
+
end
|
|
358
|
+
|
|
359
|
+
def self.detail_value(value)
|
|
360
|
+
case value
|
|
361
|
+
when nil then ""
|
|
362
|
+
when BigDecimal then value.to_s("F")
|
|
363
|
+
when Time, DateTime, ActiveSupport::TimeWithZone then value.iso8601
|
|
364
|
+
when Date then value.iso8601
|
|
365
|
+
when Hash, Array then JSON.pretty_generate(value)
|
|
366
|
+
else value.to_s
|
|
367
|
+
end
|
|
368
|
+
end
|
|
369
|
+
|
|
370
|
+
def self.detail_lines(name, value)
|
|
371
|
+
rows = value.to_s.split("\n", -1)
|
|
372
|
+
rows = [ "" ] if rows.empty?
|
|
373
|
+
rows.map.with_index { |row, index| index.zero? ? "#{name}: #{row}" : row }
|
|
374
|
+
end
|
|
375
|
+
|
|
376
|
+
def self.read(_workspace, &block)
|
|
377
|
+
path = Cli::Config.active_ledger.presence || Cli::Config.ledger
|
|
378
|
+
if path.present? && File.exist?(path)
|
|
379
|
+
Core::Session.open(path, &block)
|
|
380
|
+
else
|
|
381
|
+
yield
|
|
382
|
+
end
|
|
383
|
+
end
|
|
384
|
+
|
|
385
|
+
def self.country_choices
|
|
386
|
+
TZInfo::Country.all.map { |country| { name: "#{country.code} #{country.name}", value: country.code } }
|
|
387
|
+
.sort_by { |choice| choice[:name] }
|
|
388
|
+
end
|
|
389
|
+
|
|
390
|
+
def self.default_country_name(choices)
|
|
391
|
+
code = Settings.nordigen.default_country_code
|
|
392
|
+
choices.find { |choice| choice[:value] == code }&.fetch(:name)
|
|
393
|
+
end
|
|
394
|
+
|
|
395
|
+
def self.country_prompt_options(choices)
|
|
396
|
+
options = { filter: true, per_page: 12 }
|
|
397
|
+
name = default_country_name(choices)
|
|
398
|
+
options[:default] = name if name
|
|
399
|
+
options
|
|
400
|
+
end
|
|
401
|
+
|
|
402
|
+
def self.take_option(args, name)
|
|
403
|
+
index = args.index(name)
|
|
404
|
+
return if index.nil?
|
|
405
|
+
|
|
406
|
+
value = args[index + 1]
|
|
407
|
+
if value.nil? || value.start_with?("-")
|
|
408
|
+
warn "Usage: bdgtr account:connect --provider gocardless|enablebanking"
|
|
409
|
+
exit 1
|
|
410
|
+
end
|
|
411
|
+
|
|
412
|
+
args.slice!(index, 2)
|
|
413
|
+
value
|
|
414
|
+
end
|
|
415
|
+
|
|
416
|
+
def self.chosen_platform(provider)
|
|
417
|
+
codes = ::Bdgtr::Psd2.platforms
|
|
418
|
+
if provider.present?
|
|
419
|
+
unless codes.include?(provider)
|
|
420
|
+
warn "Unknown provider #{provider}. Use #{codes.join(" or ")}."
|
|
421
|
+
exit 1
|
|
422
|
+
end
|
|
423
|
+
return provider
|
|
424
|
+
end
|
|
425
|
+
|
|
426
|
+
require "tty-prompt"
|
|
427
|
+
TTY::Prompt.new.select("Provider", codes)
|
|
428
|
+
end
|
|
429
|
+
|
|
430
|
+
def self.account_previews(service, requisition)
|
|
431
|
+
listed = requisition.respond_to?(:bank_accounts) ? Array(requisition.bank_accounts) : []
|
|
432
|
+
return listed if listed.any?
|
|
433
|
+
|
|
434
|
+
Array(requisition.account_ids).filter_map do |external_id|
|
|
435
|
+
details = service.get_account(external_id).get_details
|
|
436
|
+
account = details.is_a?(Hash) ? details["account"] : nil
|
|
437
|
+
account = {} unless account.is_a?(Hash)
|
|
438
|
+
{ "id" => external_id, "name" => account["name"], "currency" => account["currency"], "iban" => account["iban"] }
|
|
439
|
+
rescue ::Bdgtr::Psd2::Error
|
|
440
|
+
{ "id" => external_id }
|
|
441
|
+
end
|
|
442
|
+
end
|
|
443
|
+
|
|
444
|
+
def self.choose_bank_accounts(prompt, accounts)
|
|
445
|
+
return accounts unless $stdin.tty?
|
|
446
|
+
|
|
447
|
+
puts "↑/↓ move, space select, A all, N none, enter confirm\n\n"
|
|
448
|
+
choices = accounts.map { |account| { name: account_choice_name(account), value: account } }
|
|
449
|
+
list = AccountChoiceList.new(prompt, per_page: 100, echo: false, symbols: { cross: " " })
|
|
450
|
+
Array(list.("Select accounts", choices))
|
|
451
|
+
end
|
|
452
|
+
|
|
453
|
+
def self.account_choice_name(account)
|
|
454
|
+
name = account["name"].to_s.strip
|
|
455
|
+
name = account["id"].to_s if name.empty?
|
|
456
|
+
currency = account["currency"].to_s.strip
|
|
457
|
+
currency = "-" if currency.empty?
|
|
458
|
+
iban = account["iban"].to_s.gsub(/\s+/, "").upcase
|
|
459
|
+
iban = "-" if iban.empty?
|
|
460
|
+
"#{name} #{currency} #{iban}"
|
|
461
|
+
end
|
|
462
|
+
|
|
463
|
+
def self.requisition_with_accounts(requisition, accounts)
|
|
464
|
+
::Bdgtr::Psd2::Requisition.new(
|
|
465
|
+
id: requisition.id,
|
|
466
|
+
platform: requisition.platform,
|
|
467
|
+
status: requisition.status,
|
|
468
|
+
bank_accounts: accounts,
|
|
469
|
+
link: requisition.link,
|
|
470
|
+
access_valid_until: requisition.access_valid_until,
|
|
471
|
+
max_historical_days: requisition.max_historical_days,
|
|
472
|
+
raw: requisition.raw
|
|
473
|
+
)
|
|
474
|
+
end
|
|
475
|
+
|
|
476
|
+
def self.sync_provider(account)
|
|
477
|
+
current = account.requisition&.platform.to_s
|
|
478
|
+
current = "enablebanking" if current == "enable_banking"
|
|
479
|
+
current.presence || "gocardless"
|
|
480
|
+
end
|
|
481
|
+
|
|
482
|
+
def self.authorization_redirect(platform)
|
|
483
|
+
configured = Cli::Config.api("enablebanking", "redirect_url") || Cli::Config.api("enable_banking", "redirect_url") || ENV["ENABLEBANKING_REDIRECT_URL"].presence
|
|
484
|
+
if platform == "enablebanking" && configured.present? && !local_url?(configured)
|
|
485
|
+
return [ nil, configured ]
|
|
486
|
+
end
|
|
487
|
+
|
|
488
|
+
listener = if platform == "enablebanking" && configured.present?
|
|
489
|
+
::Bdgtr::Psd2::Callback.listen(configured)
|
|
490
|
+
else
|
|
491
|
+
::Bdgtr::Psd2::Callback.listen
|
|
492
|
+
end
|
|
493
|
+
[ listener, listener.redirect_url ]
|
|
494
|
+
end
|
|
495
|
+
|
|
496
|
+
def self.local_url?(url)
|
|
497
|
+
%w[127.0.0.1 localhost].include?(URI(url).host)
|
|
498
|
+
rescue URI::InvalidURIError
|
|
499
|
+
false
|
|
500
|
+
end
|
|
501
|
+
|
|
502
|
+
def self.parse_url(url)
|
|
503
|
+
URI.decode_www_form(URI(url).query.to_s).to_h
|
|
504
|
+
rescue URI::InvalidURIError
|
|
505
|
+
{}
|
|
506
|
+
end
|
|
507
|
+
|
|
508
|
+
def self.open_browser(url)
|
|
509
|
+
command = RbConfig::CONFIG["host_os"].match?(/darwin/) ? "open" : "xdg-open"
|
|
510
|
+
system(command, url)
|
|
511
|
+
end
|
|
512
|
+
class AccountChoiceList < TTY::Prompt::MultiList
|
|
513
|
+
def keyalpha(event)
|
|
514
|
+
case event.value.to_s.downcase
|
|
515
|
+
when "a" then select_every
|
|
516
|
+
when "n" then @selected.clear
|
|
517
|
+
end
|
|
518
|
+
end
|
|
519
|
+
|
|
520
|
+
def select_every
|
|
521
|
+
return if @max && @max < choices.size
|
|
522
|
+
|
|
523
|
+
@selected = TTY::Prompt::SelectedChoices.new(choices.enabled, choices.enabled_indexes)
|
|
524
|
+
end
|
|
525
|
+
end
|
|
526
|
+
|
|
527
|
+
private_class_method :run_sync, :sync_row, :sync_summary, :bank_failure, :report_sync, :workspace!, :account_row, :account_detail, :transaction_row, :transaction_detail, :category_names, :tag_names, :sync_provider_record, :detail_value, :detail_lines, :read, :country_choices, :default_country_name, :country_prompt_options, :authorization_redirect, :local_url?, :parse_url, :open_browser
|
|
528
|
+
end
|
|
529
|
+
end
|
|
530
|
+
end
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "unicode/display_width/string_ext"
|
|
4
|
+
|
|
5
|
+
module Bdgtr
|
|
6
|
+
class Cli
|
|
7
|
+
# Rows printed as they arrive. The header goes out first. A column is at
|
|
8
|
+
# least as wide as its header, with two spaces between columns.
|
|
9
|
+
class Rows
|
|
10
|
+
GAP = " "
|
|
11
|
+
|
|
12
|
+
def initialize(headers)
|
|
13
|
+
@headers = headers.map(&:to_s)
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def header
|
|
17
|
+
write(@headers)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def row(values)
|
|
21
|
+
write(@headers.each_index.map { |index| values[index] })
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
private
|
|
25
|
+
|
|
26
|
+
def write(values)
|
|
27
|
+
$stdout.puts values.each_with_index.map { |value, index| cell(value, @headers[index]) }.join(GAP)
|
|
28
|
+
$stdout.flush
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def cell(value, header)
|
|
32
|
+
text = value.to_s
|
|
33
|
+
gap = header.display_width - text.display_width
|
|
34
|
+
gap.positive? ? text + (" " * gap) : text
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Bdgtr
|
|
4
|
+
class Cli
|
|
5
|
+
# Loads ActiveRecord and the ledger models. It does not boot the Rails app.
|
|
6
|
+
module Runtime
|
|
7
|
+
LEDGER_MODELS = %w[Account Transaction AccountRequisition AccountSync TransactionCategory Import Requisition].freeze
|
|
8
|
+
|
|
9
|
+
def self.boot!(models: false)
|
|
10
|
+
require "active_support"
|
|
11
|
+
require "active_support/core_ext"
|
|
12
|
+
load_commands
|
|
13
|
+
return unless models
|
|
14
|
+
|
|
15
|
+
require "active_record"
|
|
16
|
+
require "sqlite3"
|
|
17
|
+
load_models
|
|
18
|
+
concretize!
|
|
19
|
+
attach_associations!
|
|
20
|
+
include_sync!
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def self.load_commands
|
|
24
|
+
root = File.expand_path("../..", __dir__)
|
|
25
|
+
require File.join(root, "bdgtr/cli")
|
|
26
|
+
%w[config prompt help table rows psd2 ledger_sync].each do |name|
|
|
27
|
+
require File.join(root, "bdgtr/cli/#{name}")
|
|
28
|
+
end
|
|
29
|
+
require File.join(root, "bdgtr/sync/library")
|
|
30
|
+
require File.join(root, "bdgtr/core/exchange")
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def self.load_models
|
|
34
|
+
root = File.expand_path("../..", __dir__)
|
|
35
|
+
require File.join(root, "bdgtr/core/record")
|
|
36
|
+
require File.join(root, "bdgtr/core/schema")
|
|
37
|
+
require File.join(root, "bdgtr/core/session")
|
|
38
|
+
%w[ledger account transaction account_requisition account_sync transaction_category import requisition].each do |name|
|
|
39
|
+
require File.join(root, "bdgtr/core/models/#{name}")
|
|
40
|
+
end
|
|
41
|
+
require File.join(root, "bdgtr/core/syncable")
|
|
42
|
+
require File.join(root, "bdgtr/psd2")
|
|
43
|
+
%w[account transaction institution requisition account_snapshot platform callback go_cardless enable_banking connect].each do |name|
|
|
44
|
+
require File.join(root, "bdgtr/psd2/#{name}")
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def self.concretize!
|
|
49
|
+
LEDGER_MODELS.each do |name|
|
|
50
|
+
model = Bdgtr::Core::Models.const_get(name)
|
|
51
|
+
model.abstract_class = false
|
|
52
|
+
Object.const_set(name, model) unless Object.const_defined?(name, false)
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def self.attach_associations!
|
|
57
|
+
link = Bdgtr::Core::Models::AccountRequisition
|
|
58
|
+
unless link.reflect_on_association(:account)
|
|
59
|
+
link.belongs_to :account, class_name: "Bdgtr::Core::Models::Account"
|
|
60
|
+
link.belongs_to :requisition, class_name: "Bdgtr::Core::Models::Requisition"
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
account = Bdgtr::Core::Models::Account
|
|
64
|
+
return if account.reflect_on_association(:account_requisitions)
|
|
65
|
+
|
|
66
|
+
account.belongs_to :sync_requisition, class_name: "Bdgtr::Core::Models::Requisition", optional: true
|
|
67
|
+
account.has_many :account_requisitions, class_name: "Bdgtr::Core::Models::AccountRequisition", dependent: :destroy
|
|
68
|
+
account.has_many :requisitions, through: :account_requisitions, class_name: "Bdgtr::Core::Models::Requisition"
|
|
69
|
+
account.has_many :transactions, class_name: "Bdgtr::Core::Models::Transaction", dependent: :destroy
|
|
70
|
+
account.has_many :imports, class_name: "Bdgtr::Core::Models::Import", dependent: :destroy
|
|
71
|
+
account.has_many :account_syncs, class_name: "Bdgtr::Core::Models::AccountSync", dependent: :destroy
|
|
72
|
+
|
|
73
|
+
transaction = Bdgtr::Core::Models::Transaction
|
|
74
|
+
return if transaction.reflect_on_association(:account)
|
|
75
|
+
|
|
76
|
+
transaction.belongs_to :account, class_name: "Bdgtr::Core::Models::Account"
|
|
77
|
+
transaction.belongs_to :import, class_name: "Bdgtr::Core::Models::Import", optional: true
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def self.include_sync!
|
|
81
|
+
account = Bdgtr::Core::Models::Account
|
|
82
|
+
return if account.included_modules.include?(Bdgtr::Core::Syncable)
|
|
83
|
+
|
|
84
|
+
account.include Bdgtr::Core::Syncable
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
end
|