-
Notifications
You must be signed in to change notification settings - Fork 10
/
Rakefile
135 lines (111 loc) · 3.9 KB
/
Rakefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# frozen_string_literal: true
require "sequel"
# Load application
require "./config/configuration"
namespace :generate do
desc "Generate a timestamped, empty Sequel migration."
task :migration, :name do |_, args|
if args[:name].nil?
puts "You must specify a migration name (e.g. rake generate:migration[create_events])!"
exit false
end
content = "Sequel.migration do\n up do\n \n end\n\n down do\n \n end\nend\n"
timestamp = Time.now.strftime("%Y%m%d%H%M%S")
filename = File.join(File.dirname(__FILE__), "migrations", "#{timestamp}_#{args[:name]}.rb")
File.open(filename, "w") do |f|
f.puts content
end
puts "Created the migration #{filename}"
end
end
namespace :migration_tasks do
desc "calculate SHAs of bank_statements"
task :calculate_bank_statements_sha do
env = ENV.fetch("RACK_ENV", :development)
if env.to_s != "production"
# Load environment from file
require "dotenv"
Dotenv.load
end
require "./config/bootstrap"
require "./box/models/bank_statement"
require "./lib/checksum_generator"
i = 0
statements = Box::BankStatement.where(sha: nil)
p "Found #{statements.count} Bank Statements without a SHA."
next if statements.count.zero?
p "Recalculating Bank Statement SHAs."
statements.each do |bs|
payload = [
bs.account_id,
bs.year,
bs.content
]
bs.update(sha: ChecksumGenerator.from_payload(payload))
i += 1
end
p "Updated #{i} Bank Statement SHAs."
end
# this should ONLY be run via migration migrations/20191217114900_recalculate_statement_sha.rb
desc "calculate new SHA"
task :calculate_new_sha do
env = ENV.fetch("RACK_ENV", :development)
if env.to_s != "production"
# Load environment from file
require "dotenv"
Dotenv.load
end
require "./config/bootstrap"
require "./box/models/account"
require "./box/models/statement"
require "./box/models/bank_statement"
require "./lib/checksum_updater"
# safe guard to only run this task when temp checksum field is available
next unless Box::Statement.columns.include?(:sha2)
account_ids = Box::Account.all_active_ids
account_ids.each.with_index(1) do |account_id, idx|
pp "Processing Account #{idx} / #{account_ids.count}"
bank_statements = Box::BankStatement.where(account_id: account_id).all
bank_statements.each do |bank_statement|
parser = bank_statement.content.starts_with?(":") ? Cmxl : CamtParser::Format053::Statement
begin
result = parser.parse(bank_statement.content)
transactions = result.is_a?(Array) ? result.first.transactions : result.transactions
transactions.each do |transaction|
ChecksumUpdater.new(transaction, bank_statement.remote_account).call
end
rescue => e
p "--- ERROR ---"
p bank_statement.id
p e
p "--- !ERROR ---"
end
end
end
Box::Statement.where(sha2: nil).each do |statement|
remote_account = statement&.bank_statement&.remote_account
payload = ::ChecksumUpdater.new(statement, remote_account).send(:new_checksum_payload)
sha = ChecksumGenerator.from_payload(payload)
if Box::Statement.find(sha2: sha).present?
# prevent duplicates
pp "Statement #{statement.id} has duplicate sha: #{sha}"
next
end
statement.update(sha2: sha)
end; nil
end
desc "copies partner value to ebics_users"
task :copy_partners do
env = ENV.fetch("RACK_ENV", :development)
if env.to_s != "production"
# Load environment from file
require "dotenv"
Dotenv.load
end
require "./config/bootstrap"
require "./box/models/ebics_user"
Box::EbicsUser.where(partner: nil).each do |ebics_user|
ebics_user.update(partner: ebics_user.accounts.first&.partner)
end
end
end