3 changed files with 60 additions and 2 deletions
@ -1,13 +1,33 @@
|
||||
class BulkUploadController < ApplicationController |
||||
XLS = "application/vnd.ms-excel".freeze |
||||
SPREADSHEET_CONTENT_TYPES = %w[ |
||||
application/vnd.ms-excel |
||||
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet |
||||
] |
||||
|
||||
FIRST_DATA_ROW = 7 |
||||
|
||||
def show |
||||
render "case_logs/bulk_upload" |
||||
end |
||||
|
||||
def process_bulk_upload |
||||
if params["case_log_bulk_upload"].content_type == XLS |
||||
if SPREADSHEET_CONTENT_TYPES.include?(params["case_log_bulk_upload"].content_type) |
||||
xlsx = Roo::Spreadsheet.open(params["case_log_bulk_upload"].tempfile, extension: :xlsx) |
||||
sheet = xlsx.sheet(0) |
||||
last_row = sheet.last_row |
||||
if last_row < FIRST_DATA_ROW |
||||
head :no_content |
||||
else |
||||
data_range = FIRST_DATA_ROW..last_row |
||||
data_range.map do |row_num| |
||||
row = sheet.row(row_num) |
||||
CaseLog.create!( |
||||
tenant_code: row[7], |
||||
startertenancy: row[8] |
||||
) |
||||
end |
||||
redirect_to(case_logs_path) |
||||
end |
||||
end |
||||
end |
||||
end |
||||
|
Binary file not shown.
@ -0,0 +1,38 @@
|
||||
require "rails_helper" |
||||
|
||||
RSpec.describe BulkUploadController, type: :request do |
||||
let(:url) { "/case_logs/bulk_upload" } |
||||
|
||||
before do |
||||
get url, params: {} |
||||
end |
||||
|
||||
describe "GET #show" do |
||||
it "returns a success response" do |
||||
expect(response).to be_successful |
||||
end |
||||
|
||||
it "returns a page with a file upload form" do |
||||
expect(response.body).to match(/<input id="case-log-bulk-upload-field" class="govuk-file-upload"/) |
||||
expect(response.body).to match(/<button type="submit" formnovalidate="formnovalidate" class="govuk-button"/) |
||||
end |
||||
end |
||||
|
||||
describe "POST #bulk upload" do |
||||
before do |
||||
@file = fixture_file_upload('2021_22_lettings_bulk_upload.xlsx', 'application/vnd.ms-excel') |
||||
end |
||||
|
||||
subject { post url, params: { case_log_bulk_upload: @file } } |
||||
|
||||
context "given a valid file based on the upload template" do |
||||
it "creates case logs for each row in the template" do |
||||
expect { subject }.to change(CaseLog, :count).by(2) |
||||
end |
||||
|
||||
it "redirects to the case log index page" do |
||||
expect(subject).to redirect_to(case_logs_path) |
||||
end |
||||
end |
||||
end |
||||
end |
Loading…
Reference in new issue