Skip to content

Commit

Permalink
Add allow list for management report
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-s-ccs committed May 2, 2023
1 parent 58aa17c commit 0a4d604
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 97 deletions.
Original file line number Diff line number Diff line change
@@ -1,108 +1,116 @@
module FacilitiesManagement::RM6232
class Admin::ProcurementCsvExport
COLUMN_LABELS = [
'Reference number',
'Contract name',
'Date created',
'Buyer organisation',
'Buyer organisation address',
'Buyer sector',
'Buyer contact name',
'Buyer contact job title',
'Buyer contact email address',
'Buyer contact telephone number',
'Buyer opted in to be contacted',
'Services',
'Regions',
'Annual contract cost',
'Lot',
'Shortlisted Suppliers'
].freeze

TIME_FORMAT = '%e %B %Y, %l:%M%P'.freeze
TIME_ZONE = 'London'.freeze
LIST_ITEM_SEPARATOR = ";\n".freeze

def self.call(start_date, end_date)
CSV.generate do |csv|
csv << COLUMN_LABELS
find_procurements(start_date, end_date).each { |procurement| csv << create_procurement_row(procurement) }
class << self
COLUMN_LABELS = [
'Reference number',
'Contract name',
'Date created',
'Buyer organisation',
'Buyer organisation address',
'Buyer sector',
'Buyer contact name',
'Buyer contact job title',
'Buyer contact email address',
'Buyer contact telephone number',
'Buyer opted in to be contacted',
'Services',
'Regions',
'Annual contract cost',
'Lot',
'Shortlisted Suppliers'
].freeze

TIME_FORMAT = '%e %B %Y, %l:%M%P'.freeze
TIME_ZONE = 'London'.freeze
LIST_ITEM_SEPARATOR = ";\n".freeze

def call(start_date, end_date)
CSV.generate do |csv|
csv << COLUMN_LABELS
find_procurements(start_date, end_date).each { |procurement| csv << create_procurement_row(procurement) }
end
end
end

def self.find_procurements(start_date, end_date)
Procurement.where(created_at: (start_date..(end_date + 1))).order(created_at: :desc)
end
private

# rubocop:disable Metrics/AbcSize
def self.create_procurement_row(procurement)
[
procurement.contract_number,
procurement.contract_name,
localised_datetime(procurement.created_at),
procurement.user.buyer_detail.organisation_name,
procurement.user.buyer_detail.full_organisation_address,
procurement.user.buyer_detail.sector_name,
procurement.user.buyer_detail.contact_opt_in ? procurement.user.buyer_detail.full_name : '',
procurement.user.buyer_detail.job_title,
procurement.user.buyer_detail.contact_opt_in ? procurement.user.email : '',
procurement.user.buyer_detail.contact_opt_in ? string_as_formula(procurement.user.buyer_detail.telephone_number) : '',
procurement.user.buyer_detail.contact_opt_in ? 'Yes' : 'No',
expand_services(procurement.service_codes),
expand_regions(procurement.region_codes),
delimited_contract_value(procurement.annual_contract_value),
procurement.lot_number,
shortlisted_suppliers(procurement)
]
end
# rubocop:enable Metrics/AbcSize
def find_procurements(start_date, end_date)
Procurement.where(created_at: (start_date..(end_date + 1))).where.not(user_id: test_user_ids).order(created_at: :desc)
end

def self.localised_datetime(datetime)
datetime.in_time_zone(TIME_ZONE).strftime(TIME_FORMAT)
end
# rubocop:disable Metrics/AbcSize
def create_procurement_row(procurement)
[
procurement.contract_number,
procurement.contract_name,
localised_datetime(procurement.created_at),
procurement.user.buyer_detail.organisation_name,
procurement.user.buyer_detail.full_organisation_address,
procurement.user.buyer_detail.sector_name,
procurement.user.buyer_detail.contact_opt_in ? procurement.user.buyer_detail.full_name : '',
procurement.user.buyer_detail.job_title,
procurement.user.buyer_detail.contact_opt_in ? procurement.user.email : '',
procurement.user.buyer_detail.contact_opt_in ? string_as_formula(procurement.user.buyer_detail.telephone_number) : '',
procurement.user.buyer_detail.contact_opt_in ? 'Yes' : 'No',
expand_services(procurement.service_codes),
expand_regions(procurement.region_codes),
delimited_contract_value(procurement.annual_contract_value),
procurement.lot_number,
shortlisted_suppliers(procurement)
]
end
# rubocop:enable Metrics/AbcSize

def self.string_as_formula(string)
return if string.blank?
def localised_datetime(datetime)
datetime.in_time_zone(TIME_ZONE).strftime(TIME_FORMAT)
end

"=\"#{string}\""
end
def string_as_formula(string)
return if string.blank?

def self.expand_services(service_codes)
return if service_codes.blank?
"=\"#{string}\""
end

service_codes.compact.map do |code|
"#{code} #{service_names[code] || 'service description not found'};\n"
end.join
end
def expand_services(service_codes)
return if service_codes.blank?

def self.service_names
@service_names ||= FacilitiesManagement::RM6232::Service.pluck(:code, :name).to_h
end
service_codes.compact.map do |code|
"#{code} #{service_names[code] || 'service description not found'};\n"
end.join
end

def self.expand_regions(region_codes)
return if region_codes.blank?
def service_names
@service_names ||= FacilitiesManagement::RM6232::Service.pluck(:code, :name).to_h
end

region_codes.compact.map do |code|
"#{code} #{region_names[code] || 'region description not found'};\n"
end.join
end
def expand_regions(region_codes)
return if region_codes.blank?

def self.region_names
@region_names ||= FacilitiesManagement::Region.all.to_h { |region| [region.code, region.name] }
end
region_codes.compact.map do |code|
"#{code} #{region_names[code] || 'region description not found'};\n"
end.join
end

def self.delimited_contract_value(value)
return if value.blank?
def region_names
@region_names ||= FacilitiesManagement::Region.all.to_h { |region| [region.code, region.name] }
end

helpers.number_with_precision(value, precision: 2, delimiter: ',')
end
def delimited_contract_value(value)
return if value.blank?

def self.shortlisted_suppliers(procurement)
procurement.supplier_names.join(LIST_ITEM_SEPARATOR)
end
helpers.number_with_precision(value, precision: 2, delimiter: ',')
end

def shortlisted_suppliers(procurement)
procurement.supplier_names.join(LIST_ITEM_SEPARATOR)
end

def self.helpers
@helpers ||= ActionController::Base.helpers
def helpers
@helpers ||= ActionController::Base.helpers
end

def test_user_ids
User.where(email: ENV.fetch('TEST_USER_EMAILS', '').split(',')).pluck(:id)
end
end
end
end
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Feature: Service and selection and annual contract cost result in correct sub lot
Feature: Service selection and annual contract cost result in correct sub lot

Background: Navigate to the services page
Given I sign in and navigate to my account for 'RM6232'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,14 @@ Feature: Results validations
And I click on 'Continue'
Then I am on the 'Results' page

Scenario Outline: Contract name is blank
Given I click on '<save_button>'
Scenario: Contract name is blank
Given I click on 'Save and continue'
Then I should see the following error messages:
| Enter your contract name |

Examples:
| save_button |
| Save and continue |
# | Save and return to procurements dashboard |

Scenario: Contract name is taken
Given I have a procurement with the name 'Taken contract name'
And I enter 'Taken contract name' into the contract name field
And I click on 'Save and continue'
Then I should see the following error messages:
| This contract name is already in use |

# Some steps are commented out due to us not showing the state on the dahsboard anymore
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
end

describe 'generate_csv' do
let(:test_users) { '' }
let(:user_1) { create(:user, :with_detail) }
let(:user_2) { create(:user) }

Expand All @@ -61,6 +62,7 @@
let(:generated_csv) { CSV.parse(management_report.management_report_csv.download, headers: true) }

before do
ENV['TEST_USER_EMAILS'] = test_users
buyer_detail
procurement_1
procurement_2
Expand Down Expand Up @@ -128,5 +130,23 @@
end
# rubocop:enable RSpec/MultipleExpectations
end

context 'when the buyer email is in the TEST_USER_EMAILS list' do
let(:test_users) { user_2.email }
let(:start_date) { 5.days.ago }
let(:end_date) { Time.zone.now }

let(:generated_csv) { CSV.parse(management_report.management_report_csv.download) }

it 'has the correct headers' do
expect(generated_csv.first).to eq ['Reference number', 'Contract name', 'Date created', 'Buyer organisation', 'Buyer organisation address', 'Buyer sector', 'Buyer contact name', 'Buyer contact job title', 'Buyer contact email address', 'Buyer contact telephone number', 'Buyer opted in to be contacted', 'Services', 'Regions', 'Annual contract cost', 'Lot', 'Shortlisted Suppliers']
end

it 'has the correct data' do
expect(generated_csv.length).to eq 3
expect(generated_csv[1][0..1] + generated_csv[1][3..]).to eq ['RM6232-000003-2022', 'Procurement 3', 'MyString', 'MyString, MyString, MyString, MyString SW1W 9SZ', 'Defence and Security', 'MyString', 'MyString', user_1.email, '="07500404040"', 'Yes', "E.1 Mechanical and Electrical Engineering Maintenance;\nG.1 Hard Landscaping Services;\nJ.1 Mail Services;\n", "UKI4 Inner London - East;\nUKI5 Outer London - East and North East;\n", '50,000,000.00', '1c', "Berge-Koepp;\nBernier, Luettgen and Bednar;\nBins, Yost and Donnelly;\nBlick, O'Kon and Larkin;\nBreitenberg-Mante;\nCummerata, Lubowitz and Ebert;\nGoyette Group;\nHarris LLC;\nHeidenreich Inc;\nLind, Stehr and Dickinson;\nLowe, Abernathy and Toy;\nMiller, Walker and Leffler;\nMuller Inc;\nRohan-Windler;\nSatterfield LLC;\nSchmeler Inc;\nSchmeler-Leffler;\nSchultz-Wilkinson;\nTerry-Greenholt;\nYost LLC;\nZboncak and Sons"]
expect(generated_csv[2][0..1] + generated_csv[2][3..]).to eq ['RM6232-000001-2022', 'Procurement 1', 'MyString', 'MyString, MyString, MyString, MyString SW1W 9SZ', 'Defence and Security', 'MyString', 'MyString', user_1.email, '="07500404040"', 'Yes', "E.1 Mechanical and Electrical Engineering Maintenance;\nE.2 Ventilation and air conditioning systems maintenance;\n", "UKI4 Inner London - East;\nUKI5 Outer London - East and North East;\n", '12,345.00', '2a', "Abshire, Schumm and Farrell;\nBrakus, Lueilwitz and Blanda;\nConn, Hayes and Lakin;\nDach Inc;\nFeest Group;\nHarber LLC;\nHudson, Spinka and Schuppe;\nJenkins, Price and White;\nKirlin-Glover;\nMetz Inc;\nMoore Inc;\nO'Reilly, Emmerich and Reichert;\nRoob-Kessler;\nSchulist-Wuckert;\nSkiles LLC;\nTorphy Inc;\nTremblay, Jacobi and Kozey;\nTurner-Pouros"]
end
end
end
end

0 comments on commit 0a4d604

Please sign in to comment.