Created
October 27, 2014 12:27
-
-
Save TrevorS/777496b1abe1d3286136 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'csv' | |
namespace :customers do | |
desc 'Load customers and their addresses' | |
task :load, [:path] => :environment do |t, args| | |
headers = [:telephone, :name, :lot_no, :street_direction, :street_name, :street_subtitle, | |
:city, :service_type, :lcp_name, :nap_name, :nap_count, :nap_size] | |
modem_models = %w{ZNID-GPON-2424 ZNID-GPON-2425 ZNID-GPON-2426}.map { |m| { name: m } } | |
models = ModemModel.create!(modem_models) | |
account_names = %w{Business Residential}.map { |n| { name: n } } | |
CustomerAccountType.create!(account_names) | |
# Read in the CSV and skip the header line, then parse | |
CSV.read(args[:path], headers: headers).drop(1).each do |line| | |
telephone, full_name, lot_no, street_direction, street_name, street_subtitle, | |
city, service_type, _, nap_name, _, _ = | |
line.values_at(*headers).map { |val| val.strip unless val.nil? } | |
# Create the address | |
address = Address.create!(street_name: format_street(street_name, street_subtitle), | |
secondary_address: lot_no, | |
street_direction: street_direction, | |
locality: city) | |
modem = setup_nap_and_modem(nap_name, address, models.sample) | |
# Link the address with a NAP and modem if appropriate | |
# Create the customer | |
name = full_name.titleize.split(/\s+/) | |
account_number = Customer.maximum(:id).to_i.next | |
account_type = find_account_type(service_type) | |
customer = Customer.create!(account_number: account_number, | |
surname: name.last, | |
first_name: name.first, | |
address: address, | |
modem: modem, | |
customer_account_type: account_type) if modem | |
customer.customer_contacts.create!(name: customer.full_name, | |
phone_number: telephone) if customer | |
end | |
end | |
end | |
def find_account_type(service_type) | |
if service_type == 'R1' | |
CustomerAccountType.find_by(name: 'Residential') | |
else | |
CustomerAccountType.find_by(name: 'Business') | |
end | |
end | |
def format_street(street_name, street_subtitle) | |
name = street_name.titleize unless street_name.nil? | |
[name, street_subtitle].join(' ') | |
end | |
def setup_nap_and_modem(nap_name, address, model) | |
if !nap_name.nil? | |
nap = NetworkAccessPoint.find_by(name: nap_name) | |
nap.addresses << address unless nap.nil? | |
port = nap.assignable_ports.first | |
port.create_modem!(serial_number: Faker::Number.number(10), | |
modem_model: model) if port | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment