Browse Source

various corrections from first pass at PO, tech review, linter, etc

pull/1568/head
Arthur Campbell 3 years ago
parent
commit
9673c49479
  1. 8
      app/models/log.rb
  2. 13
      app/services/csv/sales_log_csv_service.rb
  3. 4
      spec/factories/organisation.rb
  4. 3
      spec/factories/sales_log.rb
  5. 6
      spec/requests/sales_logs_controller_spec.rb
  6. 4
      spec/services/csv/sales_log_csv_service_spec.rb

8
app/models/log.rb

@ -137,13 +137,7 @@ class Log < ApplicationRecord
end end
def creation_method def creation_method
if bulk_upload_id.present? bulk_upload_id ? "bulk upload" : "single log"
"bulk upload"
elsif old_id.present?
"migrated"
else
"single log"
end
end end
private private

13
app/services/csv/sales_log_csv_service.rb

@ -18,6 +18,9 @@ module Csv
private private
ATTRIBUTES_OF_RELATED_OBJECTS = { ATTRIBUTES_OF_RELATED_OBJECTS = {
day: %i[saledate day],
month: %i[saledate month],
year: %i[saledate year],
is_dpo: %i[created_by is_dpo], is_dpo: %i[created_by is_dpo],
owning_organisation_name: %i[owning_organisation name], owning_organisation_name: %i[owning_organisation name],
}.freeze }.freeze
@ -34,6 +37,11 @@ module Csv
"owning_organisation_name" => "owning_organisation_id", "owning_organisation_name" => "owning_organisation_id",
}.freeze }.freeze
DATE_FIELDS = %w[
created_at
updated_at
]
def value(attribute, log) def value(attribute, log)
if ATTRIBUTES_OF_RELATED_OBJECTS.key? attribute.to_sym if ATTRIBUTES_OF_RELATED_OBJECTS.key? attribute.to_sym
call_chain = ATTRIBUTES_OF_RELATED_OBJECTS[attribute.to_sym] call_chain = ATTRIBUTES_OF_RELATED_OBJECTS[attribute.to_sym]
@ -44,6 +52,8 @@ module Csv
attribute = FIELDS_ALWAYS_EXPORTED_AS_LABELS[attribute] attribute = FIELDS_ALWAYS_EXPORTED_AS_LABELS[attribute]
field_value = log.send(attribute) field_value = log.send(attribute)
get_label(field_value, attribute, log) get_label(field_value, attribute, log)
elsif DATE_FIELDS.include? attribute
log.send(attribute)&.iso8601
else else
value = log.public_send(attribute) value = log.public_send(attribute)
case @export_type case @export_type
@ -68,6 +78,7 @@ module Csv
end end
ATTRIBUTE_MAPPINGS = { ATTRIBUTE_MAPPINGS = {
"saledate" => %w[day month year],
"exdate" => %w[exday exmonth exyear], "exdate" => %w[exday exmonth exyear],
"hodate" => %w[hoday homonth hoyear], "hodate" => %w[hoday homonth hoyear],
"postcode_full" => %w[pcode1 pcode2], "postcode_full" => %w[pcode1 pcode2],
@ -80,7 +91,7 @@ module Csv
def sales_log_attributes def sales_log_attributes
ordered_questions = FormHandler.instance.ordered_sales_questions_for_all_years ordered_questions = FormHandler.instance.ordered_sales_questions_for_all_years
ordered_questions.reject! { |q| q.id.match?(/((?<!la)_known)|(_check)/) } ordered_questions.reject! { |q| q.id.match?(/((?<!la)_known)|(_check)|(_asked)/) }
attributes = ordered_questions.flat_map do |question| attributes = ordered_questions.flat_map do |question|
if question.type == "checkbox" if question.type == "checkbox"
question.answer_options.keys question.answer_options.keys

4
spec/factories/organisation.rb

@ -14,10 +14,6 @@ FactoryBot.define do
old_visible_id { rand(9_999_999).to_s } old_visible_id { rand(9_999_999).to_s }
end end
trait :id_one do
id { 1 }
end
trait :prp do trait :prp do
provider_type { "PRP" } provider_type { "PRP" }
end end

3
spec/factories/sales_log.rb

@ -11,9 +11,6 @@ FactoryBot.define do
jointpur { 2 } jointpur { 2 }
saledate { Time.utc(2023, 2, 2, 10, 36, 49) } saledate { Time.utc(2023, 2, 2, 10, 36, 49) }
end end
trait :id_one do
id { 1 }
end
trait :shared_ownership do trait :shared_ownership do
ownershipsch { 1 } ownershipsch { 1 }
type { 30 } type { 30 }

6
spec/requests/sales_logs_controller_spec.rb

@ -346,7 +346,7 @@ RSpec.describe SalesLogsController, type: :request do
end end
it "displays the labelled CSV download link, with the search included in the query params" do it "displays the labelled CSV download link, with the search included in the query params" do
get "/sales-logs?search=#{log_to_search.id}", headers: headers, params: {} get "/sales-logs?search=#{log_to_search.id}", headers:, params: {}
download_link = page.find_link("Download (CSV)") download_link = page.find_link("Download (CSV)")
download_link_params = CGI.parse(URI.parse(download_link[:href]).query) download_link_params = CGI.parse(URI.parse(download_link[:href]).query)
expect(download_link_params).to include("search" => [log_to_search.id.to_s]) expect(download_link_params).to include("search" => [log_to_search.id.to_s])
@ -555,6 +555,8 @@ RSpec.describe SalesLogsController, type: :request do
end end
context "and export type is codes only" do context "and export type is codes only" do
let(:codes_only) { true }
it "has a hidden field with the export type" do it "has a hidden field with the export type" do
expect(page).to have_field("codes_only", type: "hidden", with: codes_only) expect(page).to have_field("codes_only", type: "hidden", with: codes_only)
end end
@ -573,7 +575,7 @@ RSpec.describe SalesLogsController, type: :request do
end end
end end
describe "POST #email-csv", focus: true do describe "POST #email-csv" do
let(:other_organisation) { FactoryBot.create(:organisation) } let(:other_organisation) { FactoryBot.create(:organisation) }
let(:user) { FactoryBot.create(:user, :support) } let(:user) { FactoryBot.create(:user, :support) }
let!(:sales_log) do let!(:sales_log) do

4
spec/services/csv/sales_log_csv_service_spec.rb

@ -2,8 +2,8 @@ require "rails_helper"
RSpec.describe Csv::SalesLogCsvService do RSpec.describe Csv::SalesLogCsvService do
let(:form_handler_mock) { instance_double(FormHandler) } let(:form_handler_mock) { instance_double(FormHandler) }
let(:organisation) { create(:organisation, :id_one) } let(:organisation) { create(:organisation) }
let!(:log) { create(:sales_log, :completed, :id_one, owning_organisation: organisation) } let!(:log) { create(:sales_log, :completed, owning_organisation: organisation) }
let(:service) { described_class.new(export_type: "labels") } let(:service) { described_class.new(export_type: "labels") }
let(:csv) { CSV.parse(service.prepare_csv(SalesLog.all)) } let(:csv) { CSV.parse(service.prepare_csv(SalesLog.all)) }

Loading…
Cancel
Save