-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtind_batch.rb
More file actions
104 lines (86 loc) · 3.5 KB
/
Copy pathtind_batch.rb
File metadata and controls
104 lines (86 loc) · 3.5 KB
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
require 'roo'
require_relative 'tind_validation'
require_relative 'make_batch'
require_relative 'spread_tool'
module TindSpread
class TindBatch
def initialize(args, xlsx, extension, email)
@email = email
@form_info = args
@xlsx_path = xlsx
@extension = extension
end
def format_errors
error_message = ''
@all_errors.each do |line_num, errors|
error_message << "Errors for Line #{line_num}\n"
errors.each do |error|
error_message << "#{error}\n"
end
error_message << "\n"
end
# File.write('errors.txt', error_message)
error_message
end
def attachments(attachment_name)
return { "ERRORREPORT_#{attachment_name}.txt" => format_errors } if @all_errors.key?(1)
return { "#{attachment_name}.csv" => @csv.to_s } if @all_errors.empty?
if @csv.count("\n") > 1
return { "#{attachment_name}.csv" => @csv.to_s,
"ERRORREPORT_#{attachment_name}.csv" => @errors_csv.to_s,
"ERRORREPORT_#{attachment_name}.txt" => format_errors }
end
{ "ERRORREPORT_#{attachment_name}.csv" => @errors_csv.to_s,
"ERRORREPORT_#{attachment_name}.txt" => format_errors }
end
# rubocop:disable Metrics/AbcSize
def send_email
attachment_name = "#{@form_info[:'982__a'].gsub(/\s/i, '_')}_#{Time.current.in_time_zone('Pacific Time (US & Canada)').to_date}"
body = @all_errors.empty? ? 'No errors found' : 'Line number in errors text file corresponds to line number in Errors spreadsheet'
RequestMailer.tind_spread_email(@email, "Tind batch load for #{@form_info[:'982__a']}", body, attachments(attachment_name)).deliver_now
end
# rubocop:disable Metrics/MethodLength
def create_rows(all_rows)
row_num = 0
error_row = 2
all_rows.each do |row|
errors = TindSpread::TindValidation.validate_row(row)
if errors.any?
@all_errors[error_row] = errors if errors.any?
error_row += 1
end
@csv << TindSpread::MakeBatch.add_row(row, @form_info) unless errors.any?
@errors_csv << TindSpread::MakeBatch.add_row(row, @form_info, filename_row: true) if errors.any?
row_num += 1
end
end
# rubocop:enable Metrics/MethodLength
def validate_header_row(headers)
header_errors = []
headers.each do |header|
header_errors << "Invalid header name: #{header.gsub(/^\d+:/, '')}" unless TindSpread::TindValidation.valid_header?(header)
end
header_errors
end
# rubocop:disable Metrics/MethodLength
def run
t = TindSpread::SpreadTool.new(@xlsx_path, @extension, @form_info[:directory])
all_rows = t.spread
@all_errors = {}
# Validate header row
header_errors = validate_header_row(all_rows.first.keys)
@all_errors[1] = header_errors if header_errors.any?
return send_email if header_errors.any?
@csv = TindSpread::MakeBatch.make_header(t.header(all_rows.first.keys), @form_info).encode('UTF-8')
@errors_csv = TindSpread::MakeBatch.make_header(t.header(all_rows.first.keys), @form_info, remove_filename: false).encode('UTF-8')
create_rows(all_rows)
@csv.to_s.gsub!("\xEF\xBB\xBF".force_encoding('UTF-8'), '')
@errors_csv.to_s.gsub!("\xEF\xBB\xBF".force_encoding('UTF-8'), '')
# File.write('output.csv', @csv)
# File.write('errors.csv', @errors_csv)
send_email
end
# rubocop:enable Metrics/MethodLength
# rubocop:enable Metrics/AbcSize
end
end