Skip to content

Add new Customer model (including import script)#27

Merged
tekin merged 2 commits intodevelopfrom
290-add-customers-model
Aug 7, 2018
Merged

Add new Customer model (including import script)#27
tekin merged 2 commits intodevelopfrom
290-add-customers-model

Conversation

@tekin
Copy link
Copy Markdown
Contributor

@tekin tekin commented Aug 7, 2018

This adds the Customer model and a script to import all the customers we currently know about so that we can use the data to generate the finance report. More details in the commit messages.

@tekin tekin force-pushed the 290-add-customers-model branch 2 times, most recently from 0fc6128 to 755b96d Compare August 7, 2018 12:51
Copy link
Copy Markdown
Contributor

@leeky leeky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added some minor comments, but otherwise LGTM! :shipit:

t.timestamps
end

add_index :customers, :urn, unique: true
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Comment thread app/models/customer.rb Outdated
@@ -0,0 +1,6 @@
class Customer < ApplicationRecord
enum sector: %w[central_government wider_public_sector]
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it worth being explicit with the mappings here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated.

RSpec.describe Customer do
it { is_expected.to validate_presence_of(:name) }
it { is_expected.to validate_presence_of(:urn) }
it { is_expected.to validate_presence_of(:sector) }
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about a validate_uniqueness_of(:urn) test here for completeness?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

adding for completeness, although I'll be honest I'm not a huge fan of these declarative specs. not sure what value they add as they are simply mirror the validations on the model.

csv = CSV.table(Rails.root.join('db', 'data_migrate', '20180807095900_customers.csv'))

puts 'Importing customers...'
bar = ProgressBar.new(csv.count)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this actually render properly inside the docker containers slightly-broken terminal?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tested locally and it appears it does.

csv.each do |customer_row|
postcode = customer_row[:postcode] == 'XXXX' ? nil : customer_row[:postcode]

Customer.create!(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about making this able to be run more than once? (i.e. if the VPN dies in the middle of the script running)

Also, it's probably worth calling strip on all these input strings, and a lot of the other exports have included erroneous whitespace.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated.

This is the model that represents the part of government a supplier is
selling to. This model has become necessary so that we can generate the
report for finance that groups invoice totals and levys according to the
customers' sectors, i.e. "central government", or "wider public sector".

I'm keeping the attributes on this model to a minimum for now to avoid
having to add code we don't need just yet. Right now, all we really need
are the URN number and the sector. That said, I've additionally included
the name and postcode, mainly because we are expecting to need these two
fields very soon when we add the URN look-up tool to the supplier-facing
application.

I've also avoided adding too many validations or constraints on fields
such as the URN number. We can define and implement those when we get to
the backend administrative part of the system.
@tekin tekin force-pushed the 290-add-customers-model branch from 755b96d to f27a330 Compare August 7, 2018 14:38
Can be used to seed the database with all the customers we currently
know about:

  $ rails runner db/data_migrate/20180807095900_import_customers.rb

The CSV data was generated using the URNs file we are currently sharing
with suppliers. Some of the customers from that file have been excluded:

  - Customers with "Active" set to "False" as business cannot be
  reported against these
  - A handful of "bucket code" URNS that shouldn't be reported against
  by suppliers (e.g. Education/991100)

Note that some customers have been given a postcode of 'XXXX'. I'm
assuming this was to get around some limitation in MISO that meant a
postcode needs to be set. Rather than carry this over to the new system,
the script will ignore these postcodes.

Also note that I've updated the rubocop config to exclude the
Rails/Output rule for the data migrations, as there is no reason to
prevent outputting directly to STDOUT from scripts like this that are
expected to be run directly from the terminal.
@tekin tekin force-pushed the 290-add-customers-model branch from f27a330 to 9337b63 Compare August 7, 2018 14:42
@leeky
Copy link
Copy Markdown
Contributor

leeky commented Aug 7, 2018

👍

@tekin tekin merged commit 4336c22 into develop Aug 7, 2018
@tekin tekin deleted the 290-add-customers-model branch August 7, 2018 14:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants