|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +class School |
| 4 | + class ImportBatch |
| 5 | + class << self |
| 6 | + def call(csv_file:, current_user:) |
| 7 | + response = OperationResponse.new |
| 8 | + |
| 9 | + parsed_schools = parse_csv(csv_file) |
| 10 | + |
| 11 | + if parsed_schools[:error] |
| 12 | + response[:error] = parsed_schools[:error] |
| 13 | + return response |
| 14 | + end |
| 15 | + |
| 16 | + # Check for duplicate owner emails in the CSV |
| 17 | + duplicate_check = check_duplicate_owners(parsed_schools[:schools]) |
| 18 | + if duplicate_check[:error] |
| 19 | + response[:error] = duplicate_check[:error] |
| 20 | + return response |
| 21 | + end |
| 22 | + |
| 23 | + job = enqueue_import_job( |
| 24 | + schools_data: parsed_schools[:schools], |
| 25 | + user_id: current_user.id |
| 26 | + ) |
| 27 | + |
| 28 | + response[:job_id] = job.job_id |
| 29 | + response[:total_schools] = parsed_schools[:schools].length |
| 30 | + response |
| 31 | + rescue StandardError => e |
| 32 | + Sentry.capture_exception(e) |
| 33 | + response ||= OperationResponse.new |
| 34 | + response[:error] = SchoolImportError.format_error(:unknown_error, e.message) |
| 35 | + response |
| 36 | + end |
| 37 | + |
| 38 | + private |
| 39 | + |
| 40 | + def check_duplicate_owners(schools) |
| 41 | + owner_emails = schools.filter_map { |s| s[:owner_email]&.strip&.downcase } |
| 42 | + duplicates = owner_emails.select { |e| owner_emails.count(e) > 1 }.uniq |
| 43 | + |
| 44 | + if duplicates.any? |
| 45 | + error = SchoolImportError.format_error( |
| 46 | + :duplicate_owner_email, |
| 47 | + 'Duplicate owner emails found in CSV', |
| 48 | + { duplicate_emails: duplicates } |
| 49 | + ) |
| 50 | + return { error: error } |
| 51 | + end |
| 52 | + |
| 53 | + {} |
| 54 | + end |
| 55 | + |
| 56 | + def parse_csv(csv_file) |
| 57 | + require 'csv' |
| 58 | + |
| 59 | + csv_content = csv_file.read |
| 60 | + return { error: SchoolImportError.format_error(:csv_invalid_format, 'CSV file is empty') } if csv_content.blank? |
| 61 | + |
| 62 | + csv_data = CSV.parse(csv_content, headers: true, header_converters: :symbol) |
| 63 | + |
| 64 | + if csv_data.headers.nil? || !valid_headers?(csv_data.headers) |
| 65 | + return { |
| 66 | + error: SchoolImportError.format_error( |
| 67 | + :csv_invalid_format, |
| 68 | + 'Invalid CSV format. Required headers: name, website, address_line_1, municipality, country_code, owner_email' |
| 69 | + ) |
| 70 | + } |
| 71 | + end |
| 72 | + |
| 73 | + process_csv_rows(csv_data) |
| 74 | + rescue CSV::MalformedCSVError => e |
| 75 | + { error: SchoolImportError.format_error(:csv_malformed, "Invalid CSV file format: #{e.message}") } |
| 76 | + rescue StandardError => e |
| 77 | + Sentry.capture_exception(e) |
| 78 | + { error: SchoolImportError.format_error(:unknown_error, "Failed to parse CSV: #{e.message}") } |
| 79 | + end |
| 80 | + |
| 81 | + def process_csv_rows(csv_data) |
| 82 | + schools = [] |
| 83 | + errors = [] |
| 84 | + |
| 85 | + csv_data.each_with_index do |row, index| |
| 86 | + row_number = index + 2 # +2 because index starts at 0 and we skip header row |
| 87 | + school_data = row.to_h |
| 88 | + |
| 89 | + validation_errors = validate_school_data(school_data, row_number) |
| 90 | + if validation_errors |
| 91 | + errors << validation_errors |
| 92 | + next |
| 93 | + end |
| 94 | + |
| 95 | + schools << school_data |
| 96 | + end |
| 97 | + |
| 98 | + if errors.any? |
| 99 | + { error: SchoolImportError.format_row_errors(errors) } |
| 100 | + else |
| 101 | + { schools: schools } |
| 102 | + end |
| 103 | + end |
| 104 | + |
| 105 | + def valid_headers?(headers) |
| 106 | + required = %i[name website address_line_1 municipality country_code owner_email] |
| 107 | + required.all? { |h| headers.include?(h) } |
| 108 | + end |
| 109 | + |
| 110 | + def validate_school_data(data, row_number) |
| 111 | + errors = [] |
| 112 | + |
| 113 | + # Strip whitespace from all string fields |
| 114 | + data.each do |key, value| |
| 115 | + data[key] = value.strip if value.is_a?(String) |
| 116 | + end |
| 117 | + |
| 118 | + # Validate required fields |
| 119 | + required_fields = %i[name website address_line_1 municipality country_code owner_email] |
| 120 | + required_fields.each do |field| |
| 121 | + errors << { field: field.to_s, message: 'is required' } if data[field].blank? |
| 122 | + end |
| 123 | + |
| 124 | + # Validate field formats |
| 125 | + validate_country_code(data, errors) |
| 126 | + validate_website_format(data, errors) |
| 127 | + validate_email_format(data, errors) |
| 128 | + |
| 129 | + return nil if errors.empty? |
| 130 | + |
| 131 | + { row: row_number, errors: errors } |
| 132 | + end |
| 133 | + |
| 134 | + def validate_country_code(data, errors) |
| 135 | + return if data[:country_code].blank? |
| 136 | + return if ISO3166::Country.codes.include?(data[:country_code].upcase) |
| 137 | + |
| 138 | + errors << { field: 'country_code', message: "invalid code: #{data[:country_code]}" } |
| 139 | + end |
| 140 | + |
| 141 | + def validate_website_format(data, errors) |
| 142 | + return if data[:website].blank? |
| 143 | + return if data[:website].match?(School::VALID_URL_REGEX) |
| 144 | + |
| 145 | + errors << { field: 'website', message: 'invalid format' } |
| 146 | + end |
| 147 | + |
| 148 | + def validate_email_format(data, errors) |
| 149 | + return if data[:owner_email].blank? |
| 150 | + return if data[:owner_email].match?(URI::MailTo::EMAIL_REGEXP) |
| 151 | + |
| 152 | + errors << { field: 'owner_email', message: 'invalid email format' } |
| 153 | + end |
| 154 | + |
| 155 | + def enqueue_import_job(schools_data:, user_id:) |
| 156 | + SchoolImportJob.perform_later( |
| 157 | + schools_data: schools_data, |
| 158 | + user_id: user_id |
| 159 | + ) |
| 160 | + end |
| 161 | + end |
| 162 | + end |
| 163 | +end |
0 commit comments