4 changed files with 254 additions and 224 deletions
@ -0,0 +1,121 @@ |
|||||||
|
class BulkUpdateFromCsv::UpdateLocationsFromCsvService |
||||||
|
def initialize(original_file_name:, updated_file_name:) |
||||||
|
@original_file_name = original_file_name |
||||||
|
@updated_file_name = updated_file_name |
||||||
|
end |
||||||
|
|
||||||
|
def call |
||||||
|
s3_service = Storage::S3Service.new(Configuration::EnvConfigurationService.new, ENV["CSV_DOWNLOAD_PAAS_INSTANCE"]) |
||||||
|
|
||||||
|
original_locations_csv = csv_from_path(@original_file_name, s3_service) |
||||||
|
updated_locations_csv = csv_from_path(@updated_file_name, s3_service) |
||||||
|
|
||||||
|
updated_locations_csv.each do |row| |
||||||
|
updated_attributes = attributes_from_row(row) |
||||||
|
|
||||||
|
original_row = original_locations_csv.find { |original_locations_row| original_locations_row[1] == updated_attributes["location_code"] } |
||||||
|
if original_row.blank? || original_row["location_code"].nil? |
||||||
|
Rails.logger.info("Location with id #{updated_attributes['location_code']} is not in the original location csv") |
||||||
|
next |
||||||
|
end |
||||||
|
|
||||||
|
original_attributes = attributes_from_row(original_row) |
||||||
|
|
||||||
|
location = Location.find_by(id: original_attributes["location_code"]) |
||||||
|
if location.blank? |
||||||
|
Rails.logger.info("Location with id #{original_attributes['location_code']} is not in the database") |
||||||
|
next |
||||||
|
end |
||||||
|
|
||||||
|
updated_attributes.each do |key, value| |
||||||
|
next unless value != original_attributes[key] && value.present? |
||||||
|
|
||||||
|
case key |
||||||
|
when "location_admin_district" |
||||||
|
update_location_admin_district(location, original_attributes, value) |
||||||
|
when "postcode" |
||||||
|
update_postcode(location, original_attributes, value) |
||||||
|
when "scheme_code" |
||||||
|
update_scheme(location, original_attributes, value) |
||||||
|
when "name", "units", "type_of_unit", "mobility_type" |
||||||
|
begin |
||||||
|
location[key] = value |
||||||
|
Rails.logger.info("Updating location #{original_attributes['location_code']} with #{key}: #{value}") |
||||||
|
rescue ArgumentError => e |
||||||
|
Rails.logger.info("Cannot update location #{original_attributes['location_code']} with #{key}: #{value}. #{e.message}") |
||||||
|
end |
||||||
|
when "location_code", "status", "active_dates" |
||||||
|
Rails.logger.info("Cannot update location #{original_attributes['location_code']} with #{key} as it it not a permitted field") |
||||||
|
end |
||||||
|
end |
||||||
|
|
||||||
|
unless location.changed? |
||||||
|
Rails.logger.info("No changes to location #{original_attributes['location_code']}.") |
||||||
|
next |
||||||
|
end |
||||||
|
|
||||||
|
save_location(location, original_attributes) |
||||||
|
end |
||||||
|
end |
||||||
|
|
||||||
|
private |
||||||
|
|
||||||
|
def csv_from_path(path, s3_service) |
||||||
|
original_file_io = s3_service.get_file_io(path) |
||||||
|
original_file_io.set_encoding_by_bom |
||||||
|
CSV.parse(original_file_io, headers: true) |
||||||
|
end |
||||||
|
|
||||||
|
def attributes_from_row(row) |
||||||
|
attributes = {} |
||||||
|
attributes["scheme_code"] = row[0] |
||||||
|
attributes["location_code"] = row[1] |
||||||
|
attributes["postcode"] = row[2] |
||||||
|
attributes["name"] = row[3] |
||||||
|
attributes["status"] = row[4] |
||||||
|
attributes["location_admin_district"] = row[5] |
||||||
|
attributes["units"] = row[6] |
||||||
|
attributes["type_of_unit"] = row[7] |
||||||
|
attributes["mobility_type"] = row[8] |
||||||
|
attributes["active_dates"] = row[9] |
||||||
|
attributes |
||||||
|
end |
||||||
|
|
||||||
|
def update_location_admin_district(location, original_attributes, value) |
||||||
|
location_code = Location.local_authorities_for_current_year.key(value) |
||||||
|
if location_code.present? |
||||||
|
location.location_code = location_code |
||||||
|
location.location_admin_district = value |
||||||
|
Rails.logger.info("Updating location #{original_attributes['location_code']} with location_code: #{location_code}") |
||||||
|
else |
||||||
|
Rails.logger.info("Cannot update location #{original_attributes['location_code']} with location_admin_district: #{value}. Location admin distrint #{value} is not a valid option") |
||||||
|
end |
||||||
|
end |
||||||
|
|
||||||
|
def update_postcode(location, original_attributes, value) |
||||||
|
if !value&.match(POSTCODE_REGEXP) |
||||||
|
Rails.logger.info("Cannot update location #{original_attributes['location_code']} with postcode: #{value}. #{I18n.t('validations.postcode')}") |
||||||
|
else |
||||||
|
location.postcode = PostcodeService.clean(value) |
||||||
|
Rails.logger.info("Updating location #{original_attributes['location_code']} with postcode: #{value}") |
||||||
|
end |
||||||
|
end |
||||||
|
|
||||||
|
def update_scheme(location, original_attributes, value) |
||||||
|
scheme = Scheme.find_by(id: value.delete("S")) |
||||||
|
if scheme.present? |
||||||
|
location["scheme_id"] = scheme.id |
||||||
|
Rails.logger.info("Updating location #{original_attributes['location_code']} with scheme: S#{scheme.id}") |
||||||
|
else |
||||||
|
Rails.logger.info("Cannot update location #{original_attributes['location_code']} with scheme_code: #{value}. Scheme with id #{value} is not in the database") |
||||||
|
end |
||||||
|
end |
||||||
|
|
||||||
|
def save_location(location, original_attributes) |
||||||
|
location.save! |
||||||
|
Rails.logger.info("Saved location #{original_attributes['location_code']}.") |
||||||
|
LettingsLog.where(location_id: location.id).update_all(values_updated_at: Time.zone.now) |
||||||
|
rescue ActiveRecord::RecordInvalid => e |
||||||
|
Rails.logger.error("Cannot update location #{original_attributes['location_code']}. #{e.message}") |
||||||
|
end |
||||||
|
end |
||||||
@ -0,0 +1,103 @@ |
|||||||
|
class BulkUpdateFromCsv::UpdateSchemesFromCsvService |
||||||
|
def initialize(original_file_name:, updated_file_name:) |
||||||
|
@original_file_name = original_file_name |
||||||
|
@updated_file_name = updated_file_name |
||||||
|
end |
||||||
|
|
||||||
|
def call |
||||||
|
s3_service = Storage::S3Service.new(Configuration::EnvConfigurationService.new, ENV["CSV_DOWNLOAD_PAAS_INSTANCE"]) |
||||||
|
|
||||||
|
original_schemes_csv = csv_from_path(@original_file_name, s3_service) |
||||||
|
updated_schemes_csv = csv_from_path(@updated_file_name, s3_service) |
||||||
|
|
||||||
|
updated_schemes_csv.each do |row| |
||||||
|
updated_attributes = attributes_from_row(row) |
||||||
|
|
||||||
|
original_row = original_schemes_csv.find { |original_schemes_row| original_schemes_row[0] == updated_attributes["scheme_code"] } |
||||||
|
if original_row.blank? || original_row["scheme_code"].nil? |
||||||
|
Rails.logger.info("Scheme with id #{updated_attributes['scheme_code']} is not in the original scheme csv") |
||||||
|
next |
||||||
|
end |
||||||
|
|
||||||
|
original_attributes = attributes_from_row(original_row) |
||||||
|
|
||||||
|
scheme = Scheme.find_by(id: original_attributes["scheme_code"].delete("S")) |
||||||
|
if scheme.blank? |
||||||
|
Rails.logger.info("Scheme with id #{original_attributes['scheme_code']} is not in the database") |
||||||
|
next |
||||||
|
end |
||||||
|
|
||||||
|
updated_attributes.each do |key, value| |
||||||
|
next unless value != original_attributes[key] && value.present? |
||||||
|
|
||||||
|
case key |
||||||
|
when "owning_organisation" |
||||||
|
update_owning_organisation(scheme, original_attributes, value) |
||||||
|
when "service_name", "sensitive", "scheme_type", "registered_under_care_act", "arrangement_type", "primary_client_group", "has_other_client_group", "secondary_client_group", "support_type", "intended_stay" |
||||||
|
begin |
||||||
|
scheme[key] = value |
||||||
|
Rails.logger.info("Updating scheme #{original_attributes['scheme_code']} with #{key}: #{value}") |
||||||
|
rescue ArgumentError => e |
||||||
|
Rails.logger.info("Cannot update scheme #{original_attributes['scheme_code']} with #{key}: #{value}. #{e.message}") |
||||||
|
end |
||||||
|
when "scheme_code", "status", "created_at", "active_dates" |
||||||
|
Rails.logger.info("Cannot update scheme #{original_attributes['scheme_code']} with #{key} as it it not a permitted field") |
||||||
|
end |
||||||
|
end |
||||||
|
|
||||||
|
unless scheme.changed? |
||||||
|
Rails.logger.info("No changes to scheme #{original_attributes['scheme_code']}.") |
||||||
|
next |
||||||
|
end |
||||||
|
|
||||||
|
save_scheme(scheme, original_attributes) |
||||||
|
end |
||||||
|
end |
||||||
|
|
||||||
|
private |
||||||
|
|
||||||
|
def csv_from_path(path, s3_service) |
||||||
|
original_file_io = s3_service.get_file_io(path) |
||||||
|
original_file_io.set_encoding_by_bom |
||||||
|
CSV.parse(original_file_io, headers: true) |
||||||
|
end |
||||||
|
|
||||||
|
def attributes_from_row(row) |
||||||
|
attributes = {} |
||||||
|
|
||||||
|
attributes["scheme_code"] = row[0] |
||||||
|
attributes["service_name"] = row[1] |
||||||
|
attributes["status"] = row[2] |
||||||
|
attributes["sensitive"] = row[3] |
||||||
|
attributes["scheme_type"] = row[4] |
||||||
|
attributes["registered_under_care_act"] = row[5] |
||||||
|
attributes["owning_organisation"] = row[6] |
||||||
|
attributes["arrangement_type"] = row[7] |
||||||
|
attributes["primary_client_group"] = row[8] |
||||||
|
attributes["has_other_client_group"] = row[9] |
||||||
|
attributes["secondary_client_group"] = row[10] |
||||||
|
attributes["support_type"] = row[11] |
||||||
|
attributes["intended_stay"] = row[12] |
||||||
|
attributes["created_at"] = row[13] |
||||||
|
attributes["active_dates"] = row[14] |
||||||
|
attributes |
||||||
|
end |
||||||
|
|
||||||
|
def update_owning_organisation(scheme, original_attributes, value) |
||||||
|
organisation = Organisation.find_by(name: value) |
||||||
|
if organisation.present? |
||||||
|
scheme["owning_organisation_id"] = organisation.id |
||||||
|
Rails.logger.info("Updating scheme #{original_attributes['scheme_code']} with owning_organisation: #{organisation.name}") |
||||||
|
else |
||||||
|
Rails.logger.info("Cannot update scheme #{original_attributes['scheme_code']} with owning_organisation: #{value}. Organisation with name #{value} is not in the database") |
||||||
|
end |
||||||
|
end |
||||||
|
|
||||||
|
def save_scheme(scheme, original_attributes) |
||||||
|
scheme.save! |
||||||
|
Rails.logger.info("Saved scheme #{original_attributes['scheme_code']}.") |
||||||
|
LettingsLog.where(scheme_id: scheme.id).update_all(values_updated_at: Time.zone.now) |
||||||
|
rescue ActiveRecord::RecordInvalid => e |
||||||
|
Rails.logger.error("Cannot update scheme #{original_attributes['scheme_code']}. #{e.message}") |
||||||
|
end |
||||||
|
end |
||||||
Loading…
Reference in new issue