Browse Source
* some minor refactoring remove methods from child class that replicate methods on the parent class tidy up check for nil remove gubbins and inline method body given only used once * update import services for lettings and sales to import creation method write tests to cover this * create sales log field import service and associated spec file, with methods and tests for importing the creation method of logs that have already been imported * update lettings log field import service and related spec to allow importing creation method of logs * use the methods dynamically created by active record in all relevant places, removing obsolete methods in teh process. various tests tweaked to suppor this change. rake task from another ticket folded into this ticket to prevent merge conflicts * rename method for ruby conventions * update PR for altered spec upload id now decided to be a better indicator of bulk upload status, import service amended accordingly tests updated in line with this * update field import services in line with import services to use upload id rather than upload method as the source of truth for how a log was created * slight refactor to reduce nesting and dodge linter complaints * minor amendment to log creator spec in bulk upload to use enum dynamic methodsCLDC-2505-bulk-upload-pages
Arthur Campbell
1 year ago
committed by
GitHub
23 changed files with 248 additions and 69 deletions
@ -0,0 +1,32 @@
|
||||
module Imports |
||||
class SalesLogsFieldImportService < LogsImportService |
||||
def update_field(field, folder) |
||||
case field |
||||
when "creation_method" |
||||
import_from(folder, :update_creation_method) |
||||
else |
||||
raise "Updating #{field} is not supported by the field import service" |
||||
end |
||||
end |
||||
|
||||
private |
||||
|
||||
def update_creation_method(xml_doc) |
||||
old_id = meta_field_value(xml_doc, "document-id") |
||||
log = SalesLog.find_by(old_id:) |
||||
|
||||
return @logger.warn "sales log with old id #{old_id} not found" unless log |
||||
|
||||
upload_id = meta_field_value(xml_doc, "upload-id") |
||||
|
||||
if upload_id.nil? |
||||
@logger.info "sales log with old id #{old_id} entered manually, no need for update" |
||||
elsif log.creation_method_bulk_upload? |
||||
@logger.info "sales log #{log.id} creation method already set to bulk upload, no need for update" |
||||
else |
||||
log.creation_method_bulk_upload! |
||||
@logger.info "sales log #{log.id} creation method set to bulk upload" |
||||
end |
||||
end |
||||
end |
||||
end |
@ -0,0 +1,5 @@
|
||||
desc "set creation method to bulk upload if a log has a bulk upload id" |
||||
task set_creation_method: :environment do |
||||
LettingsLog.where.not(bulk_upload_id: nil).find_each(&:creation_method_bulk_upload!) |
||||
SalesLog.where.not(bulk_upload_id: nil).find_each(&:creation_method_bulk_upload!) |
||||
end |
@ -0,0 +1,76 @@
|
||||
require "rails_helper" |
||||
|
||||
RSpec.describe Imports::SalesLogsFieldImportService do |
||||
subject(:import_service) { described_class.new(storage_service, logger) } |
||||
|
||||
let(:storage_service) { instance_double(Storage::S3Service) } |
||||
let(:logger) { instance_double(ActiveSupport::Logger) } |
||||
|
||||
let(:fixture_directory) { "spec/fixtures/imports/sales_logs" } |
||||
let(:sales_log_filename) { "shared_ownership_sales_log" } |
||||
let(:sales_log_file) { File.open("#{fixture_directory}/#{sales_log_filename}.xml") } |
||||
let(:organisation) { create(:organisation, old_visible_id: "1") } |
||||
let(:old_user_id) { "c3061a2e6ea0b702e6f6210d5c52d2a92612d2aa" } |
||||
|
||||
let(:remote_folder) { "sales_logs" } |
||||
|
||||
before do |
||||
create(:user, old_user_id:, organisation:) |
||||
|
||||
allow(storage_service) |
||||
.to receive(:list_files) |
||||
.and_return(["#{sales_log_filename}.xml"]) |
||||
allow(storage_service) |
||||
.to receive(:get_file_io) |
||||
.with("#{sales_log_filename}.xml") |
||||
.and_return(sales_log_file) |
||||
end |
||||
|
||||
context "when updating creation method" do |
||||
let(:field) { "creation_method" } |
||||
let(:sales_log) { SalesLog.find_by(old_id: sales_log_filename) } |
||||
|
||||
before do |
||||
Imports::SalesLogsImportService.new(storage_service, logger).create_logs(fixture_directory) |
||||
sales_log_file.rewind |
||||
end |
||||
|
||||
context "and the log was manually entered" do |
||||
let(:sales_log_filename) { "shared_ownership_sales_log" } |
||||
|
||||
it "logs that bulk upload id does not need setting" do |
||||
expect(logger).to receive(:info).with("sales log with old id #{sales_log_filename} entered manually, no need for update") |
||||
expect { import_service.update_field(field, remote_folder) }.not_to(change { sales_log.reload.creation_method }) |
||||
end |
||||
end |
||||
|
||||
context "and the log was bulk uploaded and the creation method is already correct" do |
||||
let(:sales_log_filename) { "shared_ownership_sales_log2" } |
||||
|
||||
it "logs that bulk upload id does not need setting" do |
||||
expect(logger).to receive(:info).with(/sales log \d+ creation method already set to bulk upload, no need for update/) |
||||
expect { import_service.update_field(field, remote_folder) }.not_to(change { sales_log.reload.creation_method }) |
||||
end |
||||
end |
||||
|
||||
context "and the log was bulk uploaded and the creation method requires updating" do |
||||
let(:sales_log_filename) { "shared_ownership_sales_log2" } |
||||
|
||||
it "logs that bulk upload id does not need setting" do |
||||
sales_log.creation_method_single_log! |
||||
expect(logger).to receive(:info).with(/sales log \d+ creation method set to bulk upload/) |
||||
expect { import_service.update_field(field, remote_folder) }.to change { sales_log.reload.creation_method }.to "bulk upload" |
||||
end |
||||
end |
||||
|
||||
context "and the log was not previously imported" do |
||||
let(:sales_log_filename) { "shared_ownership_sales_log" } |
||||
|
||||
it "logs a warning that the log has not been found in the db" do |
||||
sales_log.destroy! |
||||
expect(logger).to receive(:warn).with("sales log with old id #{sales_log_filename} not found") |
||||
import_service.update_field(field, remote_folder) |
||||
end |
||||
end |
||||
end |
||||
end |
Loading…
Reference in new issue