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,142 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module PortfolioPerformanceApi
|
|
4
|
+
# Canonical PTransaction.Type values from Portfolio Performance's client.proto.
|
|
5
|
+
# Java XML/UI uses different names (BUY, FEES, TAXES, DIVIDENDS, TRANSFER_IN, …).
|
|
6
|
+
# Writing those into protobuf makes PP throw UnsupportedOperationException on open.
|
|
7
|
+
# https://github.com/portfolio-performance/portfolio
|
|
8
|
+
module TransactionTypes
|
|
9
|
+
PROTO = %w[
|
|
10
|
+
PURCHASE SALE INBOUND_DELIVERY OUTBOUND_DELIVERY SECURITY_TRANSFER
|
|
11
|
+
CASH_TRANSFER DEPOSIT REMOVAL DIVIDEND INTEREST INTEREST_CHARGE
|
|
12
|
+
TAX TAX_REFUND FEE FEE_REFUND
|
|
13
|
+
].freeze
|
|
14
|
+
|
|
15
|
+
DEPOSIT_ACCOUNT = %w[
|
|
16
|
+
DEPOSIT REMOVAL DIVIDEND INTEREST INTEREST_CHARGE TAX TAX_REFUND FEE FEE_REFUND
|
|
17
|
+
CASH_TRANSFER PURCHASE SALE
|
|
18
|
+
].freeze
|
|
19
|
+
|
|
20
|
+
SECURITIES_ACCOUNT = %w[
|
|
21
|
+
PURCHASE SALE INBOUND_DELIVERY OUTBOUND_DELIVERY SECURITY_TRANSFER
|
|
22
|
+
].freeze
|
|
23
|
+
|
|
24
|
+
CROSS_ENTRY = %w[PURCHASE SALE CASH_TRANSFER SECURITY_TRANSFER].freeze
|
|
25
|
+
|
|
26
|
+
# Java XML / CSV / UI labels that mean a cash or securities transfer.
|
|
27
|
+
# Kind decides CASH_TRANSFER vs SECURITY_TRANSFER; never write TRANSFER_IN.
|
|
28
|
+
TRANSFER_ALIASES = %w[
|
|
29
|
+
transfer transfer_in transfer_out transfer_inbound transfer_outbound
|
|
30
|
+
trasferimento trasferimento_in_entrata trasferimento_in_uscita
|
|
31
|
+
].freeze
|
|
32
|
+
|
|
33
|
+
ALIASES = {
|
|
34
|
+
"buy" => "PURCHASE",
|
|
35
|
+
"sell" => "SALE",
|
|
36
|
+
"compra" => "PURCHASE",
|
|
37
|
+
"vendi" => "SALE",
|
|
38
|
+
"dividends" => "DIVIDEND",
|
|
39
|
+
"dividend" => "DIVIDEND",
|
|
40
|
+
"dividendo" => "DIVIDEND",
|
|
41
|
+
"fees" => "FEE",
|
|
42
|
+
"fee" => "FEE",
|
|
43
|
+
"commissioni" => "FEE",
|
|
44
|
+
"fees_refund" => "FEE_REFUND",
|
|
45
|
+
"fee_refund" => "FEE_REFUND",
|
|
46
|
+
"rimborso_commissioni" => "FEE_REFUND",
|
|
47
|
+
"taxes" => "TAX",
|
|
48
|
+
"tax" => "TAX",
|
|
49
|
+
"tasse" => "TAX",
|
|
50
|
+
"tax_refund" => "TAX_REFUND",
|
|
51
|
+
"rimborso_tasse" => "TAX_REFUND",
|
|
52
|
+
"delivery_inbound" => "INBOUND_DELIVERY",
|
|
53
|
+
"inbound_delivery" => "INBOUND_DELIVERY",
|
|
54
|
+
"delivery_outbound" => "OUTBOUND_DELIVERY",
|
|
55
|
+
"outbound_delivery" => "OUTBOUND_DELIVERY",
|
|
56
|
+
"trasferimento_titoli_in_entrata" => "INBOUND_DELIVERY",
|
|
57
|
+
"trasferimento_titoli_in_uscita" => "OUTBOUND_DELIVERY",
|
|
58
|
+
"deposit" => "DEPOSIT",
|
|
59
|
+
"deposito" => "DEPOSIT",
|
|
60
|
+
"versamento" => "DEPOSIT",
|
|
61
|
+
"entrate" => "DEPOSIT",
|
|
62
|
+
"removal" => "REMOVAL",
|
|
63
|
+
"withdrawal" => "REMOVAL",
|
|
64
|
+
"prelievo" => "REMOVAL",
|
|
65
|
+
"uscite" => "REMOVAL",
|
|
66
|
+
"interest" => "INTEREST",
|
|
67
|
+
"interessi" => "INTEREST",
|
|
68
|
+
"interest_charge" => "INTEREST_CHARGE",
|
|
69
|
+
"interessi_passivi" => "INTEREST_CHARGE"
|
|
70
|
+
}.freeze
|
|
71
|
+
|
|
72
|
+
module_function
|
|
73
|
+
|
|
74
|
+
def proto?(type)
|
|
75
|
+
name = type.to_s
|
|
76
|
+
return false if name.empty? || name == "UNRECOGNIZED"
|
|
77
|
+
|
|
78
|
+
PROTO.include?(name)
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def key(value)
|
|
82
|
+
value.to_s.strip.downcase.gsub(/[()]/, "").gsub(/[\s\-]+/, "_").gsub(/_+/, "_")
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def normalize(value, signed_cents = nil, kind: nil)
|
|
86
|
+
text = value.to_s.strip
|
|
87
|
+
keyed = key(text)
|
|
88
|
+
if TRANSFER_ALIASES.include?(keyed)
|
|
89
|
+
return kind.to_s == "securities" ? "SECURITY_TRANSFER" : "CASH_TRANSFER"
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
named = ALIASES[keyed]
|
|
93
|
+
named ||= text.upcase if proto?(text.upcase)
|
|
94
|
+
return named if named
|
|
95
|
+
return "REMOVAL" if signed_cents.to_i.negative?
|
|
96
|
+
return "DEPOSIT" if signed_cents.to_i.positive?
|
|
97
|
+
|
|
98
|
+
nil
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
def coerce(type, kind: :deposit)
|
|
102
|
+
named = proto?(type.to_s) ? type.to_s : normalize(type, kind: kind)
|
|
103
|
+
return unless named
|
|
104
|
+
|
|
105
|
+
allowed = kind.to_s == "securities" ? SECURITIES_ACCOUNT : DEPOSIT_ACCOUNT
|
|
106
|
+
return named if allowed.include?(named)
|
|
107
|
+
|
|
108
|
+
case [kind.to_s, named]
|
|
109
|
+
when %w[securities DEPOSIT] then "INBOUND_DELIVERY"
|
|
110
|
+
when %w[securities REMOVAL] then "OUTBOUND_DELIVERY"
|
|
111
|
+
when %w[deposit INBOUND_DELIVERY] then "DEPOSIT"
|
|
112
|
+
when %w[deposit OUTBOUND_DELIVERY] then "REMOVAL"
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
# Fields ProtobufWriter.loadTransactions requires before PP will open the file.
|
|
117
|
+
def loadable?(tx)
|
|
118
|
+
type = tx.type.to_s
|
|
119
|
+
return false unless proto?(type)
|
|
120
|
+
|
|
121
|
+
case type
|
|
122
|
+
when "PURCHASE", "SALE"
|
|
123
|
+
present?(tx, :account) && present?(tx, :portfolio) && present?(tx, :security) && present?(tx, :otherUuid)
|
|
124
|
+
when "CASH_TRANSFER"
|
|
125
|
+
present?(tx, :account) && present?(tx, :otherAccount) && present?(tx, :otherUuid)
|
|
126
|
+
when "SECURITY_TRANSFER"
|
|
127
|
+
present?(tx, :portfolio) && present?(tx, :otherPortfolio) && present?(tx, :security) && present?(tx, :otherUuid)
|
|
128
|
+
when "INBOUND_DELIVERY", "OUTBOUND_DELIVERY"
|
|
129
|
+
present?(tx, :portfolio) && present?(tx, :security)
|
|
130
|
+
else
|
|
131
|
+
present?(tx, :account)
|
|
132
|
+
end
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
def present?(tx, field)
|
|
136
|
+
has = "has_#{field}?"
|
|
137
|
+
return false if tx.respond_to?(has) && !tx.public_send(has)
|
|
138
|
+
|
|
139
|
+
tx.public_send(field).to_s != ""
|
|
140
|
+
end
|
|
141
|
+
end
|
|
142
|
+
end
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
require "time"
|
|
5
|
+
require "nokogiri"
|
|
6
|
+
|
|
7
|
+
require_relative "portfolio_performance_api/version"
|
|
8
|
+
require_relative "portfolio_performance_api/proto/client_pb"
|
|
9
|
+
require_relative "portfolio_performance_api/config"
|
|
10
|
+
require_relative "portfolio_performance_api/errors"
|
|
11
|
+
require_relative "portfolio_performance_api/decryptor"
|
|
12
|
+
require_relative "portfolio_performance_api/file_reader"
|
|
13
|
+
require_relative "portfolio_performance_api/parser"
|
|
14
|
+
require_relative "portfolio_performance_api/balances"
|
|
15
|
+
require_relative "portfolio_performance_api/drive_client"
|
|
16
|
+
require_relative "portfolio_performance_api/portfolio_source"
|
|
17
|
+
require_relative "portfolio_performance_api/cache"
|
|
18
|
+
require_relative "portfolio_performance_api/snapshot"
|
|
19
|
+
require_relative "portfolio_performance_api/accounts_csv"
|
|
20
|
+
require_relative "portfolio_performance_api/auth"
|
|
21
|
+
require_relative "portfolio_performance_api/app"
|
|
22
|
+
|
|
23
|
+
module PortfolioPerformanceApi
|
|
24
|
+
# Process-wide snapshot so tests and requests share the same cache.
|
|
25
|
+
@snapshot_mutex = Mutex.new
|
|
26
|
+
|
|
27
|
+
class << self
|
|
28
|
+
def snapshot
|
|
29
|
+
return @snapshot if @snapshot
|
|
30
|
+
|
|
31
|
+
@snapshot_mutex.synchronize do
|
|
32
|
+
@snapshot ||= Snapshot.new
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def snapshot=(value)
|
|
37
|
+
@snapshot_mutex.synchronize do
|
|
38
|
+
@snapshot = value
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
syntax = "proto3";
|
|
2
|
+
|
|
3
|
+
package name.abuchen.portfolio;
|
|
4
|
+
|
|
5
|
+
import "google/protobuf/timestamp.proto";
|
|
6
|
+
|
|
7
|
+
option ruby_package = "PortfolioPerformanceApi.Proto";
|
|
8
|
+
|
|
9
|
+
// Subset of Portfolio Performance's client.proto (field numbers match the
|
|
10
|
+
// official schema so unknown fields are ignored at parse time).
|
|
11
|
+
|
|
12
|
+
message PHistoricalPrice {
|
|
13
|
+
int64 date = 1;
|
|
14
|
+
int64 close = 2;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
message PFullHistoricalPrice {
|
|
18
|
+
int64 date = 1;
|
|
19
|
+
int64 close = 2;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
message PSecurity {
|
|
23
|
+
string uuid = 1;
|
|
24
|
+
string name = 3;
|
|
25
|
+
optional string currencyCode = 4;
|
|
26
|
+
optional string isin = 7;
|
|
27
|
+
optional string tickerSymbol = 8;
|
|
28
|
+
repeated PHistoricalPrice prices = 13;
|
|
29
|
+
optional PFullHistoricalPrice latest = 16;
|
|
30
|
+
bool isRetired = 20;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
message PAccount {
|
|
34
|
+
string uuid = 1;
|
|
35
|
+
string name = 2;
|
|
36
|
+
string currencyCode = 3;
|
|
37
|
+
optional string note = 4;
|
|
38
|
+
bool isRetired = 5;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
message PPortfolio {
|
|
42
|
+
string uuid = 1;
|
|
43
|
+
string name = 2;
|
|
44
|
+
optional string note = 3;
|
|
45
|
+
bool isRetired = 4;
|
|
46
|
+
optional string referenceAccount = 5;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
message PDecimal {
|
|
50
|
+
int32 scale = 1;
|
|
51
|
+
int32 precision = 2;
|
|
52
|
+
bytes value = 3;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
message PTransactionUnit {
|
|
56
|
+
enum Type {
|
|
57
|
+
GROSS_VALUE = 0;
|
|
58
|
+
TAX = 1;
|
|
59
|
+
FEE = 2;
|
|
60
|
+
}
|
|
61
|
+
Type type = 1;
|
|
62
|
+
int64 amount = 2;
|
|
63
|
+
string currencyCode = 3;
|
|
64
|
+
optional int64 fxAmount = 4;
|
|
65
|
+
optional string fxCurrencyCode = 5;
|
|
66
|
+
optional PDecimal fxRateToBase = 6;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
message PTransaction {
|
|
70
|
+
enum Type {
|
|
71
|
+
PURCHASE = 0;
|
|
72
|
+
SALE = 1;
|
|
73
|
+
INBOUND_DELIVERY = 2;
|
|
74
|
+
OUTBOUND_DELIVERY = 3;
|
|
75
|
+
SECURITY_TRANSFER = 4;
|
|
76
|
+
CASH_TRANSFER = 5;
|
|
77
|
+
DEPOSIT = 6;
|
|
78
|
+
REMOVAL = 7;
|
|
79
|
+
DIVIDEND = 8;
|
|
80
|
+
INTEREST = 9;
|
|
81
|
+
INTEREST_CHARGE = 10;
|
|
82
|
+
TAX = 11;
|
|
83
|
+
TAX_REFUND = 12;
|
|
84
|
+
FEE = 13;
|
|
85
|
+
FEE_REFUND = 14;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
string uuid = 1;
|
|
89
|
+
Type type = 2;
|
|
90
|
+
optional string account = 3;
|
|
91
|
+
optional string portfolio = 4;
|
|
92
|
+
optional string otherAccount = 5;
|
|
93
|
+
optional string otherPortfolio = 6;
|
|
94
|
+
optional string otherUuid = 7;
|
|
95
|
+
google.protobuf.Timestamp otherUpdatedAt = 8;
|
|
96
|
+
google.protobuf.Timestamp date = 9;
|
|
97
|
+
string currencyCode = 10;
|
|
98
|
+
int64 amount = 11;
|
|
99
|
+
optional int64 shares = 12;
|
|
100
|
+
optional string note = 13;
|
|
101
|
+
optional string security = 14;
|
|
102
|
+
repeated PTransactionUnit units = 15;
|
|
103
|
+
google.protobuf.Timestamp updatedAt = 16;
|
|
104
|
+
optional string source = 17;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
message PClient {
|
|
108
|
+
int32 version = 1;
|
|
109
|
+
repeated PSecurity securities = 2;
|
|
110
|
+
repeated PAccount accounts = 3;
|
|
111
|
+
repeated PPortfolio portfolios = 4;
|
|
112
|
+
repeated PTransaction transactions = 5;
|
|
113
|
+
string baseCurrency = 12;
|
|
114
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Bdgtr
|
|
4
|
+
module Psd2
|
|
5
|
+
# Bank account as a platform returns it. The same three reads the sync uses.
|
|
6
|
+
class Account
|
|
7
|
+
attr_reader :id, :name, :currency, :institution_id
|
|
8
|
+
|
|
9
|
+
def initialize(id:, name:, currency:, institution_id:, metadata:, details:, balances:)
|
|
10
|
+
@id = id
|
|
11
|
+
@name = name
|
|
12
|
+
@currency = currency
|
|
13
|
+
@institution_id = institution_id
|
|
14
|
+
@metadata = metadata
|
|
15
|
+
@details = details
|
|
16
|
+
@balances = balances
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def get_metadata = @metadata
|
|
20
|
+
def get_details = @details
|
|
21
|
+
def get_balances = @balances
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Bdgtr
|
|
4
|
+
module Psd2
|
|
5
|
+
# The account fields both providers can fill, in the Berlin Group shape the
|
|
6
|
+
# API already reads. Provider wrappers such as metadata stay on the sync.
|
|
7
|
+
module AccountSnapshot
|
|
8
|
+
BALANCE_TYPES = {
|
|
9
|
+
"ITAV" => "interimAvailable",
|
|
10
|
+
"ITBD" => "interimBooked",
|
|
11
|
+
"CLAV" => "closingAvailable",
|
|
12
|
+
"CLBD" => "closingBooked",
|
|
13
|
+
"OPBD" => "openingBooked",
|
|
14
|
+
"PRCD" => "previouslyClosedBooked",
|
|
15
|
+
"XPCD" => "expected",
|
|
16
|
+
"FWAV" => "forwardAvailable"
|
|
17
|
+
}.freeze
|
|
18
|
+
|
|
19
|
+
ACCOUNT_KEYS = %w[iban currency name cashAccountType bic product ownerName].freeze
|
|
20
|
+
|
|
21
|
+
def self.normalize(payload)
|
|
22
|
+
payload = stringify(payload)
|
|
23
|
+
return {} unless payload.is_a?(Hash)
|
|
24
|
+
|
|
25
|
+
account = payload.dig("details", "account")
|
|
26
|
+
account = {} unless account.is_a?(Hash)
|
|
27
|
+
{
|
|
28
|
+
"details" => { "account" => account.slice(*ACCOUNT_KEYS).compact },
|
|
29
|
+
"balances" => { "balances" => normalize_balances(payload["balances"]) }
|
|
30
|
+
}
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def self.normalize_balances(balances)
|
|
34
|
+
rows = balances.is_a?(Hash) ? balances["balances"] : balances
|
|
35
|
+
Array(rows).filter_map do |row|
|
|
36
|
+
next unless row.is_a?(Hash)
|
|
37
|
+
|
|
38
|
+
amount = row["balanceAmount"].is_a?(Hash) ? row["balanceAmount"] : {}
|
|
39
|
+
type = row["balanceType"].to_s
|
|
40
|
+
{
|
|
41
|
+
"balanceType" => BALANCE_TYPES.fetch(type, type),
|
|
42
|
+
"balanceAmount" => { "amount" => amount["amount"], "currency" => amount["currency"] }.compact
|
|
43
|
+
}
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def self.archive!(connection)
|
|
48
|
+
return unless connection.table_exists?(:accounts) && connection.table_exists?(:account_syncs)
|
|
49
|
+
return unless connection.column_exists?(:accounts, :sync_data)
|
|
50
|
+
return unless connection.column_exists?(:account_syncs, :data)
|
|
51
|
+
|
|
52
|
+
connection.select_all("SELECT id, sync_data, synced_at FROM accounts WHERE sync_data IS NOT NULL").each do |row|
|
|
53
|
+
payload = parse(row["sync_data"])
|
|
54
|
+
next unless payload.is_a?(Hash) && payload["metadata"].is_a?(Hash)
|
|
55
|
+
|
|
56
|
+
account_id = row["id"].to_i
|
|
57
|
+
latest = connection.select_value("SELECT id FROM account_syncs WHERE account_id = #{account_id} ORDER BY id DESC LIMIT 1")
|
|
58
|
+
literal = json_literal(connection, payload)
|
|
59
|
+
if latest
|
|
60
|
+
connection.execute("UPDATE account_syncs SET data = #{literal} WHERE id = #{latest.to_i} AND data IS NULL")
|
|
61
|
+
else
|
|
62
|
+
now = connection.quote(Time.current)
|
|
63
|
+
finished = row["synced_at"].present? ? connection.quote(row["synced_at"]) : now
|
|
64
|
+
connection.execute(<<~SQL)
|
|
65
|
+
INSERT INTO account_syncs (account_id, state, transactions_count, data, finished_at, created_at, updated_at)
|
|
66
|
+
VALUES (#{account_id}, 'synced', 0, #{literal}, #{finished}, #{now}, #{now})
|
|
67
|
+
SQL
|
|
68
|
+
end
|
|
69
|
+
connection.execute("UPDATE accounts SET sync_data = #{json_literal(connection, normalize(payload))} WHERE id = #{account_id}")
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def self.parse(value)
|
|
74
|
+
return value if value.is_a?(Hash)
|
|
75
|
+
return if value.blank?
|
|
76
|
+
|
|
77
|
+
JSON.parse(value)
|
|
78
|
+
rescue JSON::ParserError
|
|
79
|
+
nil
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def self.stringify(value)
|
|
83
|
+
case value
|
|
84
|
+
when Hash then value.to_h { |key, item| [ key.to_s, stringify(item) ] }
|
|
85
|
+
when Array then value.map { |item| stringify(item) }
|
|
86
|
+
else value
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def self.json_literal(connection, value)
|
|
91
|
+
quoted = connection.quote(JSON.generate(value))
|
|
92
|
+
connection.adapter_name.match?(/postg/i) ? "#{quoted}::json" : quoted
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
end
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "webrick"
|
|
4
|
+
|
|
5
|
+
module Bdgtr
|
|
6
|
+
module Psd2
|
|
7
|
+
# One local page that receives the bank redirect and lets the command continue.
|
|
8
|
+
class Callback
|
|
9
|
+
def self.listen(url = nil)
|
|
10
|
+
uri = url.present? ? URI(url) : nil
|
|
11
|
+
port = uri&.port || 0
|
|
12
|
+
path = uri&.path.presence || "/callback"
|
|
13
|
+
server = WEBrick::HTTPServer.new(
|
|
14
|
+
BindAddress: "127.0.0.1",
|
|
15
|
+
Port: port,
|
|
16
|
+
Logger: WEBrick::Log.new(File::NULL),
|
|
17
|
+
AccessLog: []
|
|
18
|
+
)
|
|
19
|
+
queue = Queue.new
|
|
20
|
+
server.mount_proc path do |request, response|
|
|
21
|
+
queue << request.query
|
|
22
|
+
response.status = 200
|
|
23
|
+
response["Content-Type"] = "text/html; charset=utf-8"
|
|
24
|
+
response.body = "<p>Authorization received. You can return to the terminal.</p>"
|
|
25
|
+
end
|
|
26
|
+
thread = Thread.new { server.start }
|
|
27
|
+
bound = server.listeners.first.addr[1]
|
|
28
|
+
redirect = uri&.to_s || "http://127.0.0.1:#{bound}#{path}"
|
|
29
|
+
new(server, thread, queue, redirect)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def initialize(server, thread, queue, redirect_url)
|
|
33
|
+
@server = server
|
|
34
|
+
@thread = thread
|
|
35
|
+
@queue = queue
|
|
36
|
+
@redirect_url = redirect_url
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
attr_reader :redirect_url
|
|
40
|
+
|
|
41
|
+
def wait
|
|
42
|
+
query = @queue.pop
|
|
43
|
+
@server.shutdown
|
|
44
|
+
@thread.join(2)
|
|
45
|
+
query || {}
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Bdgtr
|
|
4
|
+
module Psd2
|
|
5
|
+
# Saves a finished bank authorization. An account that already has the same
|
|
6
|
+
# external id keeps that link; the new consent is added beside it.
|
|
7
|
+
class Connect
|
|
8
|
+
def self.call(workspace:, user:, requisition:, service:)
|
|
9
|
+
saved = []
|
|
10
|
+
errors = []
|
|
11
|
+
record = nil
|
|
12
|
+
write(workspace) do
|
|
13
|
+
record = save_requisition!(requisition)
|
|
14
|
+
bank_accounts(requisition).each do |entry|
|
|
15
|
+
external_id = entry["id"]
|
|
16
|
+
scope = ::Account.where(workspace_id: workspace.id)
|
|
17
|
+
iban = ::Account.normalize_iban(entry["iban"])
|
|
18
|
+
currency = entry["currency"].to_s.strip.upcase.presence
|
|
19
|
+
if iban.blank? || currency.blank?
|
|
20
|
+
fetched_iban, fetched_currency = ::Account.bank_account_identity(service, external_id)
|
|
21
|
+
iban ||= fetched_iban
|
|
22
|
+
currency ||= fetched_currency
|
|
23
|
+
end
|
|
24
|
+
account = ::Account.locate_for_connection(scope, external_id: external_id, iban: iban, currency: currency)
|
|
25
|
+
account ||= ::Account.new(workspace_id: workspace.id)
|
|
26
|
+
account.user = user if user && account.user_id.blank?
|
|
27
|
+
account.iban = ::Account.normalize_iban(iban) if account.iban.blank?
|
|
28
|
+
account.save! if account.new_record? || account.iban_changed?
|
|
29
|
+
link = account.account_requisitions.find_or_initialize_by(requisition_id: record.id)
|
|
30
|
+
link.external_account_id = external_id
|
|
31
|
+
link.save!
|
|
32
|
+
account.sync_requisition = record
|
|
33
|
+
begin
|
|
34
|
+
account.sync_self!(service: service, save: false, force: true)
|
|
35
|
+
rescue ::Bdgtr::Psd2::Error => error
|
|
36
|
+
errors << [ account, error ]
|
|
37
|
+
end
|
|
38
|
+
account.save!
|
|
39
|
+
saved << account
|
|
40
|
+
yield account if block_given?
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
[ record, saved, errors ]
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def self.save_requisition!(requisition)
|
|
47
|
+
model = defined?(::Requisition) ? ::Requisition : Core::Models::Requisition
|
|
48
|
+
record = model.find_or_create_by!(requisition_id: requisition.id)
|
|
49
|
+
record.status = requisition.status if requisition.status.present?
|
|
50
|
+
record.platform = requisition.platform
|
|
51
|
+
record.max_historical_days = requisition.max_historical_days if requisition.max_historical_days.present?
|
|
52
|
+
record.access_valid_until = requisition.access_valid_until if requisition.access_valid_until.present?
|
|
53
|
+
record.access_valid_until ||= model::ACCESS_VALID_FOR_DAYS.days.from_now
|
|
54
|
+
record.save!
|
|
55
|
+
record
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def self.bank_accounts(requisition)
|
|
59
|
+
listed = requisition.respond_to?(:bank_accounts) ? Array(requisition.bank_accounts) : []
|
|
60
|
+
return listed if listed.any?
|
|
61
|
+
|
|
62
|
+
Array(requisition.account_ids).map { |external_id| { "id" => external_id } }
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def self.write(_workspace, &block)
|
|
66
|
+
path = Bdgtr::Cli::Config.active_ledger.presence || Bdgtr::Cli::Config.ledger
|
|
67
|
+
if path.present? && File.exist?(path)
|
|
68
|
+
Core::Session.open(path, &block)
|
|
69
|
+
else
|
|
70
|
+
yield
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
end
|