Browse Source

Update seeds

pull/2807/head
Rachael Booth 8 months ago
parent
commit
dedbba9a8c
  1. 493
      db/seeds.rb
  2. 10
      spec/factories/organisation.rb
  3. 11
      spec/factories/sales_log.rb
  4. 3
      spec/factories/scheme.rb
  5. 6
      spec/factories/user.rb

493
db/seeds.rb

@ -6,429 +6,145 @@
# movies = Movie.create([{ name: 'Star Wars' }, { name: 'Lord of the Rings' }]) # movies = Movie.create([{ name: 'Star Wars' }, { name: 'Lord of the Rings' }])
# Character.create(name: 'Luke', movie: movies.first) # Character.create(name: 'Luke', movie: movies.first)
# rubocop:disable Rails/Output def find_or_create_user(organisation, email, role)
def create_data_protection_confirmation(user) case role
DataProtectionConfirmation.find_or_create_by!( when :data_provider
organisation: user.organisation, FactoryBot.create(:user, :if_unique, :data_provider, email: , organisation: , password: ENV["REVIEW_APP_USER_PASSWORD"])
confirmed: true, when :data_coordinator
data_protection_officer: user, FactoryBot.create(:user, :if_unique, :data_coordinator, :data_protection_officer, :with_dsa, email: , organisation: , password: ENV["REVIEW_APP_USER_PASSWORD"])
signed_at: Time.zone.local(2019, 1, 1), when :support
data_protection_officer_email: user.email, FactoryBot.create(:user, :if_unique, :support, email: , organisation: , password: ENV["REVIEW_APP_USER_PASSWORD"])
data_protection_officer_name: user.name, end
)
end end
unless Rails.env.test? unless Rails.env.test?
stock_owner1 = Organisation.find_or_create_by!( if LocalAuthority.count.zero?
name: "Stock Owner 1", la_path = "config/local_authorities_data/initial_local_authorities.csv"
address_line1: "2 Marsham Street", service = Imports::LocalAuthoritiesService.new(path: la_path)
address_line2: "London", service.call
postcode: "SW1P 4DF", end
holds_own_stock: true,
other_stock_owners: "None",
managing_agents_label: "None",
provider_type: "LA",
)
stock_owner2 = Organisation.find_or_create_by!(
name: "Stock Owner 2",
address_line1: "2 Marsham Street",
address_line2: "London",
postcode: "SW1P 4DF",
holds_own_stock: true,
other_stock_owners: "None",
managing_agents_label: "None",
provider_type: "LA",
)
managing_agent1 = Organisation.find_or_create_by!(
name: "Managing Agent 1 (PRP)",
address_line1: "2 Marsham Street",
address_line2: "London",
postcode: "SW1P 4DF",
holds_own_stock: true,
other_stock_owners: "None",
managing_agents_label: "None",
provider_type: "PRP",
)
managing_agent2 = Organisation.find_or_create_by!(
name: "Managing Agent 2",
address_line1: "2 Marsham Street",
address_line2: "London",
postcode: "SW1P 4DF",
holds_own_stock: true,
other_stock_owners: "None",
managing_agents_label: "None",
provider_type: "LA",
)
org = Organisation.find_or_create_by!( if LaRentRange.count.zero?
name: "MHCLG", Dir.glob("config/rent_range_data/*.csv").each do |path|
address_line1: "2 Marsham Street", start_year = File.basename(path, ".csv")
address_line2: "London", service = Imports::RentRangesService.new(start_year:, path:)
postcode: "SW1P 4DF", service.call
holds_own_stock: true,
other_stock_owners: "None",
managing_agents_label: "None",
provider_type: "LA",
) do
info = "Seeded MHCLG Organisation"
if Rails.env.development?
pp info
else
Rails.logger.info info
end end
end end
standalone_owns_stock = Organisation.find_or_create_by!( if LaSaleRange.count.zero?
name: "Standalone Owns Stock 1 Ltd", Dir.glob("config/sale_range_data/*.csv").each do |path|
address_line1: "2 Marsham Street", start_year = File.basename(path, ".csv")
address_line2: "London", service = Imports::SaleRangesService.new(start_year:, path:)
postcode: "SW1P 4DF", service.call
holds_own_stock: true, end
other_stock_owners: "None", end
managing_agents_label: "None",
provider_type: "LA", all_rent_periods = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
)
standalone_no_stock = Organisation.find_or_create_by!( mhclg = FactoryBot.create(
name: "Standalone No Stock 1 Ltd", :organisation,
:if_unique,
name: "MHCLG",
address_line1: "2 Marsham Street", address_line1: "2 Marsham Street",
address_line2: "London", address_line2: "London",
postcode: "SW1P 4DF", postcode: "SW1P 4DF",
holds_own_stock: false, holds_own_stock: true,
other_stock_owners: "None", other_stock_owners: "None",
managing_agents_label: "None", managing_agents_label: "None",
provider_type: "LA", provider_type: "LA",
) rent_periods: all_rent_periods,
User.find_or_create_by!(
name: "Provider Owns Stock",
email: "provider.owner1@example.com",
organisation: standalone_owns_stock,
role: "data_provider",
is_dpo: true,
) do |user|
user.password = ENV["REVIEW_APP_USER_PASSWORD"]
user.confirmed_at = Time.zone.now
create_data_protection_confirmation(user)
end
User.find_or_create_by!(
name: "Coordinator Owns Stock",
email: "coordinator.owner1@example.com",
organisation: standalone_owns_stock,
role: "data_coordinator",
is_dpo: true,
) do |user|
user.password = ENV["REVIEW_APP_USER_PASSWORD"]
user.confirmed_at = Time.zone.now
create_data_protection_confirmation(user)
end
User.find_or_create_by!(
name: "Provider No Stock",
email: "provider.nostock@example.com",
organisation: standalone_no_stock,
role: "data_provider",
is_dpo: true,
) do |user|
user.password = ENV["REVIEW_APP_USER_PASSWORD"]
user.confirmed_at = Time.zone.now
create_data_protection_confirmation(user)
end
User.find_or_create_by!(
name: "Coordinator No Stock",
email: "coordinator.nostock@example.com",
organisation: standalone_no_stock,
role: "data_coordinator",
) do |user|
user.password = ENV["REVIEW_APP_USER_PASSWORD"]
user.confirmed_at = Time.zone.now
end
User.find_or_create_by!(
name: "Stock owner 1",
email: "stock_owner1_dpo@example.com",
organisation: stock_owner1,
role: "data_coordinator",
is_dpo: true,
) do |user|
user.password = ENV["REVIEW_APP_USER_PASSWORD"]
user.confirmed_at = Time.zone.now
create_data_protection_confirmation(user)
end
User.find_or_create_by!(
name: "Stock owner 2",
email: "stock_owner2_dpo@example.com",
organisation: stock_owner2,
role: "data_coordinator",
is_dpo: true,
) do |user|
user.password = ENV["REVIEW_APP_USER_PASSWORD"]
user.confirmed_at = Time.zone.now
create_data_protection_confirmation(user)
end
User.find_or_create_by!(
name: "Managing agent 1",
email: "managing_agent1_dpo@example.com",
organisation: managing_agent1,
role: "data_coordinator",
is_dpo: true,
) do |user|
user.password = ENV["REVIEW_APP_USER_PASSWORD"]
user.confirmed_at = Time.zone.now
create_data_protection_confirmation(user)
end
User.find_or_create_by!(
name: "Managing agent 2",
email: "managing_agent2_dpo@example.com",
organisation: managing_agent2,
role: "data_coordinator",
is_dpo: true,
) do |user|
user.password = ENV["REVIEW_APP_USER_PASSWORD"]
user.confirmed_at = Time.zone.now
create_data_protection_confirmation(user)
end
OrganisationRelationship.find_or_create_by!(
parent_organisation: stock_owner1,
child_organisation: org,
)
OrganisationRelationship.find_or_create_by!(
parent_organisation: stock_owner2,
child_organisation: org,
)
OrganisationRelationship.find_or_create_by!(
parent_organisation: org,
child_organisation: managing_agent1,
)
OrganisationRelationship.find_or_create_by!(
parent_organisation: org,
child_organisation: managing_agent2,
) )
if Rails.env.development? || Rails.env.review? if Rails.env.development? || Rails.env.review?
User.find_or_create_by!( stock_owner1 = FactoryBot.create(:organisation, :if_unique, :la, :holds_own_stock, name: "Stock Owner 1", rent_periods: all_rent_periods)
name: "Provider", stock_owner2 = FactoryBot.create(:organisation, :if_unique, :la, :holds_own_stock, name: "Stock Owner 2", rent_periods: all_rent_periods.sample(5))
email: "provider@example.com", managing_agent1 = FactoryBot.create(:organisation, :if_unique, :prp, :holds_own_stock, name: "Managing Agent 1 (PRP)", rent_periods: all_rent_periods)
organisation: org, managing_agent2 = FactoryBot.create(:organisation, :if_unique, :la, :holds_own_stock, name: "Managing Agent 2", rent_periods: all_rent_periods.sample(5))
role: "data_provider", standalone_owns_stock = FactoryBot.create(:organisation, :if_unique, :la, :holds_own_stock, name: "Standalone Owns Stock 1 Ltd", rent_periods: all_rent_periods)
) do |user| standalone_no_stock = FactoryBot.create(:organisation, :if_unique, :la, :does_not_own_stock, name: "Standalone No Stock 1 Ltd", rent_periods: all_rent_periods)
user.password = ENV["REVIEW_APP_USER_PASSWORD"]
user.confirmed_at = Time.zone.now OrganisationRelationship.find_or_create_by!(
end parent_organisation: stock_owner1,
child_organisation: mhclg,
User.find_or_create_by!(
name: "Coordinator",
email: "coordinator@example.com",
organisation: org,
role: "data_coordinator",
is_dpo: true,
) do |user|
user.password = ENV["REVIEW_APP_USER_PASSWORD"]
user.confirmed_at = Time.zone.now
user.is_dpo = true
create_data_protection_confirmation(user)
end
support_user = User.find_or_create_by!(
name: "Support",
email: "support@example.com",
organisation: org,
role: "support",
is_dpo: true,
) do |user|
user.password = ENV["REVIEW_APP_USER_PASSWORD"]
user.confirmed_at = Time.zone.now
create_data_protection_confirmation(user)
end
pp "Seeded dummy users"
end
if (Rails.env.development? || Rails.env.review?) && SalesLog.count.zero?
SalesLog.find_or_create_by!(
assigned_to: support_user,
owning_organisation: org,
managing_organisation: org,
saledate: Time.zone.today,
purchid: "1",
ownershipsch: 1,
type: 2,
jointpur: 1,
jointmore: 1,
) )
OrganisationRelationship.find_or_create_by!(
SalesLog.find_or_create_by!( parent_organisation: stock_owner2,
assigned_to: support_user, child_organisation: mhclg,
owning_organisation: org,
managing_organisation: org,
saledate: Time.zone.today,
purchid: "1",
ownershipsch: 2,
type: 9,
jointpur: 1,
jointmore: 1,
) )
OrganisationRelationship.find_or_create_by!(
SalesLog.find_or_create_by!( parent_organisation: mhclg,
assigned_to: support_user, child_organisation: managing_agent1,
owning_organisation: org,
managing_organisation: org,
saledate: Time.zone.today,
purchid: "1",
ownershipsch: 3,
type: 10,
companybuy: 1,
) )
OrganisationRelationship.find_or_create_by!(
pp "Seeded a sales log of each type" parent_organisation: mhclg,
end child_organisation: managing_agent2,
if Rails.env.development? || Rails.env.review?
dummy_org = Organisation.find_or_create_by!(
name: "FooBar LTD",
address_line1: "Higher Kingston",
address_line2: "Yeovil",
postcode: "BA21 4AT",
holds_own_stock: true,
other_stock_owners: "None",
managing_agents_label: "None",
provider_type: "LA",
) )
pp "Seeded dummy FooBar LTD organisation" provider = find_or_create_user(mhclg, "provider@example.com", :data_provider)
coordinator = find_or_create_user(mhclg, "coordinator@example.com", :data_coordinator)
support = find_or_create_user(mhclg, "support@example.com", :support)
User.find_or_create_by!( stock_owner1 = find_or_create_user(stock_owner1, "stock_owner1_dpo@example.com", :data_coordinator)
name: "Dummy user", stock_owner2 = find_or_create_user(stock_owner2, "stock_owner2_dpo@example.com", :data_coordinator)
email: "dummy_org@example.com",
organisation: dummy_org,
role: "data_provider",
is_dpo: true,
) do |user|
user.password = ENV["REVIEW_APP_USER_PASSWORD"]
user.confirmed_at = Time.zone.now
create_data_protection_confirmation(user)
end
end
if (Rails.env.development? || Rails.env.review?) && Scheme.count.zero? managing_agent1 = find_or_create_user(managing_agent1, "managing_agent1_dpo@example.com", :data_coordinator)
scheme1 = Scheme.create!( managing_agent2 = find_or_create_user(managing_agent2, "managing_agent2_dpo@example.com", :data_coordinator)
service_name: "Beulahside Care",
sensitive: 0,
registered_under_care_act: 1,
support_type: 2,
scheme_type: 4,
intended_stay: "M",
primary_client_group: "O",
has_other_client_group: 1,
secondary_client_group: "H",
owning_organisation: org,
arrangement_type: "D",
confirmed: true,
created_at: Time.zone.now,
)
scheme2 = Scheme.create!( provider_owner1 = find_or_create_user(standalone_owns_stock, "provider.owner1@example.com", :data_provider)
service_name: "Abdullahview Point", coordinator_owner1 = find_or_create_user(standalone_owns_stock, "coordinator.owner1@example.com", :data_coordinator)
sensitive: 0,
registered_under_care_act: 1,
support_type: 2,
scheme_type: 5,
intended_stay: "S",
primary_client_group: "D",
secondary_client_group: "E",
has_other_client_group: 1,
owning_organisation: org,
arrangement_type: "D",
confirmed: true,
created_at: Time.zone.now,
)
Scheme.create!( find_or_create_user(standalone_no_stock, "provider.nostock@example.com", :data_provider)
service_name: "Caspermouth Center", find_or_create_user(standalone_no_stock, "coordinator.nostock@example.com", :data_coordinator)
sensitive: 1,
registered_under_care_act: 1,
support_type: 4,
scheme_type: 7,
intended_stay: "X",
primary_client_group: "G",
has_other_client_group: 1,
secondary_client_group: "R",
owning_organisation: dummy_org,
arrangement_type: "D",
confirmed: true,
created_at: Time.zone.now,
)
Location.create!( if Scheme.count.zero?
scheme: scheme1, scheme1 = FactoryBot.create(:scheme, service_name: "Beulahside Care", owning_organisation: mhclg)
location_code: "E09000033", scheme2 = FactoryBot.create(:scheme, service_name: "Abdullahview Point", owning_organisation: mhclg)
location_admin_district: "Westminster", scheme3 = FactoryBot.create(:scheme, :created_now, owning_organisation: mhclg)
postcode: "CU193AA", scheme4 = FactoryBot.create(:scheme, owning_organisation: stock_owner1)
name: "Rectory Road",
type_of_unit: 4,
units: 1,
mobility_type: "N",
)
Location.create!( [scheme1, scheme2, scheme3, scheme4].each do |scheme|
scheme: scheme1, FactoryBot.create(:location, scheme:)
location_code: "E09000033", end
location_admin_district: "Westminster", [scheme2, scheme3].each do |scheme|
postcode: "DM250DC", FactoryBot.create_list(3, :location, scheme:)
name: "Smithy Lane", end
type_of_unit: 1, end
units: 1,
mobility_type: "W",
)
Location.create!( users_with_logs = [support, coordinator, provider, provider_owner1, coordinator_owner1, managing_agent1, managing_agent2, stock_owner1, stock_owner2]
scheme: scheme2,
location_code: "E09000033",
location_admin_district: "Westminster",
postcode: "YX130WP",
name: "Smithy Lane",
type_of_unit: 2,
units: 1,
mobility_type: "W",
)
pp "Seeded dummy schemes"
end
if LocalAuthority.count.zero? if SalesLog.count.zero?
la_path = "config/local_authorities_data/initial_local_authorities.csv" users_with_logs.each do |user|
service = Imports::LocalAuthoritiesService.new(path: la_path) FactoryBot.create(:sales_log, :shared_ownership_setup_complete, assigned_to: user)
service.call FactoryBot.create(:sales_log, :discounted_ownership_setup_complete, assigned_to: user)
end FactoryBot.create(:sales_log, :outright_sale_setup_complete, assigned_to: user)
FactoryBot.create(:sales_log, :completed, assigned_to: user)
end
if (Rails.env.development? || Rails.env.review?) && LocalAuthorityLink.count.zero? FactoryBot.create(:sales_log, :completed, assigned_to: managing_agent1, owning_organisation: mhclg)
links_data_paths = ["config/local_authorities_data/local_authority_links_2023.csv", "config/local_authorities_data/local_authority_links_2022.csv"] FactoryBot.create(:sales_log, :completed, assigned_to: provider, owning_organisation: stock_owner1)
links_data_paths.each do |path|
service = Imports::LocalAuthorityLinksService.new(path:)
service.call
end end
pp "Seeded local authority links" if LettingsLog.count.zero?
end users_with_logs.each do |user|
FactoryBot.create(:lettings_log, :setup_completed, assigned_to: user)
if LaRentRange.count.zero? FactoryBot.create(:lettings_log, :completed, assigned_to: user)
Dir.glob("config/rent_range_data/*.csv").each do |path| if user.organisation.owned_schemes.any?
start_year = File.basename(path, ".csv") scheme = user.organisation.owned_schemes.first
service = Imports::RentRangesService.new(start_year:, path:) FactoryBot.create(:lettings_log, :setup_completed, :sh, scheme:, location: scheme.locations.first, assigned_to: user)
service.call end
end
FactoryBot.create(:lettings_log, :completed, assigned_to: managing_agent1, owning_organisation: mhclg)
FactoryBot.create(:lettings_log, :completed, assigned_to: provider, owning_organisation: stock_owner1)
end end
end
if LaSaleRange.count.zero? if LocalAuthorityLink.count.zero?
Dir.glob("config/sale_range_data/*.csv").each do |path| links_data_paths = ["config/local_authorities_data/local_authority_links_2023.csv", "config/local_authorities_data/local_authority_links_2022.csv"]
start_year = File.basename(path, ".csv") links_data_paths.each do |path|
service = Imports::SaleRangesService.new(start_year:, path:) service = Imports::LocalAuthorityLinksService.new(path:)
service.call service.call
end
end end
end end
end end
@ -438,4 +154,3 @@ if LocalAuthority.count.zero?
service = Imports::LocalAuthoritiesService.new(path:) service = Imports::LocalAuthoritiesService.new(path:)
service.call service.call
end end
# rubocop:enable Rails/Output

10
spec/factories/organisation.rb

@ -41,7 +41,13 @@ FactoryBot.define do
trait :prp do trait :prp do
provider_type { "PRP" } provider_type { "PRP" }
end end
trait :la do
provider_type { "LA" }
end
trait :holds_own_stock do
holds_own_stock { true }
end
trait :does_not_own_stock do trait :does_not_own_stock do
holds_own_stock { false } holds_own_stock { false }
end end
@ -53,6 +59,10 @@ FactoryBot.define do
data_protection_confirmation { nil } data_protection_confirmation { nil }
end end
trait :if_unique do
initialize_with { Organisation.find_or_create_by(name:) }
end
end end
factory :organisation_rent_period do factory :organisation_rent_period do

11
spec/factories/sales_log.rb

@ -32,6 +32,17 @@ FactoryBot.define do
jointpur { 2 } jointpur { 2 }
noint { 2 } noint { 2 }
privacynotice { 1 } privacynotice { 1 }
purchid { rand(999_999_999).to_s }
end
trait :discounted_ownership_setup_complete do
saledate_today
ownershipsch { 2 }
type { 9 }
jointpur { 1 }
jointmore { 1 }
noint { 2 }
privacynotice { 1 }
purchid { rand(999_999_999).to_s }
end end
trait :outright_sale_setup_complete do trait :outright_sale_setup_complete do
saledate_today saledate_today

3
spec/factories/scheme.rb

@ -41,5 +41,8 @@ FactoryBot.define do
support_type { 2 } support_type { 2 }
intended_stay { "M" } intended_stay { "M" }
end end
trait :created_now do
created_at { Time.zone.now }
end
end end
end end

6
spec/factories/user.rb

@ -1,7 +1,7 @@
FactoryBot.define do FactoryBot.define do
factory :user do factory :user do
sequence(:email) { "test#{SecureRandom.hex}@example.com" } sequence(:email) { "test#{SecureRandom.hex}@example.com" }
name { "Danny Rojas" } name { Faker::Name.name }
password { "pAssword1" } password { "pAssword1" }
organisation { association :organisation, with_dsa: is_dpo ? false : true } organisation { association :organisation, with_dsa: is_dpo ? false : true }
role { "data_provider" } role { "data_provider" }
@ -40,5 +40,9 @@ FactoryBot.define do
) )
end end
end end
trait :if_unique do
initialize_with { User.find_or_create_by(email:) }
end
end end
end end

Loading…
Cancel
Save