-
-
Notifications
You must be signed in to change notification settings - Fork 573
Expand file tree
/
Copy pathseeds.rb
More file actions
1254 lines (1102 loc) · 47.1 KB
/
seeds.rb
File metadata and controls
1254 lines (1102 loc) · 47.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# This file should contain all the record creation needed to seed the database with demo values.
# The data can then be loaded with `rails db:seed` (or along with the creation of the db with `rails db:setup`).
if Rails.env.production?
Rails.logger.info "Database seeding has been configured to work only in non production settings"
return
end
# ----------------------------------------------------------------------------
# Random Record Generators
# ----------------------------------------------------------------------------
load "lib/dispersed_past_dates_generator.rb"
def random_record_for_org(org, klass)
klass.where(organization: org).all.sample
end
# ----------------------------------------------------------------------------
# Script-Global Variables
# ----------------------------------------------------------------------------
# ----------------------------------------------------------------------------
# Base Items
# ----------------------------------------------------------------------------
require "seeds"
Seeds.seed_base_items
# ----------------------------------------------------------------------------
# NDBN Members
# ----------------------------------------------------------------------------
seed_file = Rails.root.join("spec", "fixtures", "ndbn-small-import.csv").open
SyncNDBNMembers.upload(seed_file)
# ----------------------------------------------------------------------------
# Organizations
# ----------------------------------------------------------------------------
pdx_org = Organization.find_or_create_by!(name: "Pawnee Diaper Bank") do |organization|
organization.street = "P.O. Box 22613"
organization.city = "Pawnee"
organization.state = "IN"
organization.zipcode = "12345"
organization.email = "info@pawneediaper.org"
end
Organization.seed_items(pdx_org)
sf_org = Organization.find_or_create_by!(name: "SF Diaper Bank") do |organization|
organization.street = "P.O. Box 12345"
organization.city = "San Francisco"
organization.state = "CA"
organization.zipcode = "90210"
organization.email = "info@sfdiaperbank.org"
end
Organization.seed_items(sf_org)
sc_org = Organization.find_or_create_by!(name: "Second City Essentials Bank") do |organization|
organization.street = Faker::Address.street_address
organization.city = Faker::Address.city
organization.state = Faker::Address.state_abbr
organization.zipcode = Faker::Address.zip_code
organization.email = "info@scdiaperbank.org"
end
Organization.seed_items(sc_org)
# The list of organizations that will have donations, purchases, requests, distributions,
# and the records those rely on generated.
complete_orgs = [pdx_org, sc_org]
# At least one of the items is marked as inactive
Organization.all.find_each do |org|
org.items.order(created_at: :desc).last.update(active: false)
end
def seed_random_item_with_name(organization, name)
base_items = BaseItem.all.map(&:to_h)
base_item = Array.wrap(base_items).sample
base_item[:name] = name
organization.seed_items(base_item)
end
# Add a couple unique items based on random base items named after the sc_bank
# so it will be clear if they are showing up where they aren't supposed to be
4.times do |index|
seed_random_item_with_name(sc_org, "Second City Item ##{index + 1}")
end
# Keep a list of these unique items so its easy to use them for later records
sc_org_unique_items = sc_org.items.where("name ilike ?", "%Second City Item #%")
# Assign a value to some organization items to verify totals are working
Organization.all.find_each do |org|
org.items.where(value_in_cents: 0).limit(10).each do |item|
item.update(value_in_cents: 100)
end
end
# ----------------------------------------------------------------------------
# Request Units
# ----------------------------------------------------------------------------
complete_orgs.each do |org|
%w[pack box flat].each do |name|
Unit.create!(organization: org, name: name)
end
org.items.each_with_index do |item, i|
if item.name == "Pads"
%w[box pack].each { |name| item.request_units.create!(name: name) }
elsif item.name == "Wipes (Baby)"
item.request_units.create!(name: "pack")
elsif item.name == "Kids Pull-Ups (5T-6T)"
%w[pack flat].each do |name|
item.request_units.create!(name: name)
end
end
end
end
# ----------------------------------------------------------------------------
# Item Categories
# ----------------------------------------------------------------------------
Organization.all.find_each do |org|
["One", "Two", "Three"].each do |letter|
FactoryBot.create(:item_category, organization: org, name: "Category #{letter}")
end
end
# ----------------------------------------------------------------------------
# Item <-> ItemCategory
# ----------------------------------------------------------------------------
Organization.all.find_each do |org|
# Added `nil` to randomly choose to not categorize items sometimes via sample
item_category_ids = org.item_categories.map(&:id) + [nil]
org.items.each do |item|
item.update(item_category_id: item_category_ids.sample)
end
end
# ----------------------------------------------------------------------------
# Partner Group & Item Categories
# ----------------------------------------------------------------------------
Organization.all.find_each do |org|
# Setup the Partner Group & their item categories
partner_group_one = FactoryBot.create(:partner_group, organization: org)
total_item_categories_to_add = Faker::Number.between(from: 1, to: 2)
org.item_categories.sample(total_item_categories_to_add).each do |item_category|
partner_group_one.item_categories << item_category
end
next unless org.name== pdx_org.name
partner_group_two=FactoryBot.create(:partner_group, organization: org)
org.item_categories.each do |item_category|
partner_group_two.item_categories << item_category
end
end
# ----------------------------------------------------------------------------
# Users
# ----------------------------------------------------------------------------
[
{email: "superadmin@example.com", organization_admin: false, super_admin: true},
{email: "org_admin1@example.com", organization_admin: true, organization: pdx_org},
{email: "org_admin2@example.com", organization_admin: true, organization: sf_org},
{email: "second_city_admin@example.com", organization_admin: true, organization: sc_org},
{email: "user_1@example.com", organization_admin: false, organization: pdx_org},
{email: "user_2@example.com", organization_admin: false, organization: sf_org},
{email: "second_city_user@example.com", organization_admin: false, organization: sc_org},
{email: "test@example.com", organization_admin: false, organization: pdx_org, super_admin: true},
{email: "test2@example.com", organization_admin: true, organization: pdx_org}
].each do |user_data|
user = User.create(
email: user_data[:email],
password: "password!",
password_confirmation: "password!"
)
if user_data[:organization]
user.add_role(:org_user, user_data[:organization])
end
if user_data[:organization_admin]
user.add_role(:org_admin, user_data[:organization])
end
if user_data[:super_admin]
user.add_role(:super_admin)
end
end
# ----------------------------------------------------------------------------
# Donation Sites
# ----------------------------------------------------------------------------
complete_orgs.each do |org|
[
{name: "#{org.city} Hardware", address: "1234 SE Some Ave., #{org.city}, #{org.state} 12345"},
{name: "#{org.city} Parks Department", address: "2345 NE Some St., #{org.city}, #{org.state} 12345"},
{name: "Waffle House", address: "3456 Some Bay., #{org.city}, #{org.state} 12345"},
{name: "Eagleton Country Club", address: "4567 Some Blvd., Eagleton, #{org.state} 12345"}
].each do |donation_option|
DonationSite.find_or_create_by!(address: donation_option[:address]) do |donation|
donation.name = donation_option[:name]
donation.organization = org
end
end
end
# ----------------------------------------------------------------------------
# Partners & Associated Data
# ----------------------------------------------------------------------------
note = [
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent ac enim orci. Donec id consequat est. Vivamus luctus vel erat quis tincidunt. Nunc quis varius justo. Integer quam augue, dictum vitae bibendum in, fermentum quis felis. Nam euismod ultrices velit a tristique. Vestibulum sed tincidunt erat. Vestibulum et ullamcorper sem. Sed ante leo, molestie vitae augue ac, aliquam ultrices enim."
]
[
{
name: "Pawnee Parent Service",
email: "verified@example.com",
status: :approved,
quota: 500,
notes: note.sample
},
{
name: "Pawnee Homeless Shelter",
email: "invited@pawneehomeless.com",
status: :invited,
notes: note.sample
},
{
name: "Pawnee Pregnancy Center",
email: "unverified@pawneepregnancy.com",
status: :invited,
notes: note.sample
},
{
name: "Pawnee Senior Citizens Center",
email: "recertification_required@example.com",
status: :recertification_required,
notes: note.sample
},
{
name: "Pawnee Middle School",
email: "waiting@example.com",
status: :awaiting_review,
notes: note.sample
},
{
name: "Second Street Community Outreach",
status: :approved,
email: "approved_2@example.com",
notes: note.sample
},
{
name: "Second City Senior Center",
email: "second_city_senior_center@example.com",
status: :approved,
quota: 500,
notes: note.sample,
organization: sc_org
}
].each do |partner_option|
p = Partner.find_or_create_by!(partner_option) do |partner|
partner.organization = if partner_option.key?(:organization)
partner_option[:organization]
else
pdx_org
end
partner.partner_group = if partner_option[:name] == "Second Street Community Outreach"
pdx_org.partner_groups.find_by(name: "Group 2")
else
partner.organization.partner_groups.first
end
end
# Base profile information all partners should have
# Includes fields in the agency_information, executive_director, and pick_up_person partial
# The counties and areas served by the partner are handled elsewere
profile = Partners::Profile.create!({
essentials_bank_id: p.organization_id,
partner_id: p.id,
address1: Faker::Address.street_address,
address2: Faker::Address.street_address,
city: Faker::Address.city,
executive_director_email: p.email,
executive_director_name: Faker::Name.name,
executive_director_phone: Faker::PhoneNumber.phone_number,
pick_up_email: Faker::Internet.email,
pick_up_name: Faker::Name.name,
pick_up_phone: Faker::PhoneNumber.phone_number,
primary_contact_email: Faker::Internet.email,
primary_contact_mobile: Faker::PhoneNumber.phone_number,
primary_contact_name: Faker::Name.name,
primary_contact_phone: Faker::PhoneNumber.phone_number,
state: Faker::Address.state_abbr,
website: Faker::Internet.domain_name,
zip_code: Faker::Address.zip,
zips_served: Faker::Address.zip,
})
# Optional information that only established partners (ready for approval, approved or require_recertification)
# would have
# Also only add information that corresponds to the partner_form_fields the org has chosen
if [ "awaiting_review", "approved", "recertification_required" ].include? p.status
agency_type = Partners::Profile::agency_types.values.sample
# The agency_information and partner_settings partials are always shown
profile.update(
agency_mission: Faker::Lorem.paragraph(sentence_count: 2),
agency_type: agency_type,
enable_child_based_requests: true,
enable_individual_requests: true,
enable_quantity_based_requests: true,
name: p.name,
other_agency_type: (agency_type == "OTHER") ? Faker::Lorem.word : nil,
program_address1: Faker::Address.street_address,
program_address2: Faker::Address.street_address,
program_city: Faker::Address.city,
program_state: Faker::Address.state_abbr,
program_zip_code: Faker::Address.zip,
)
if p.partials_to_show.include? "media_information"
profile.update(
facebook: Faker::Internet.url(host: 'facebook.com'),
instagram: Faker::Internet.url(host: 'instagram.com'),
no_social_media_presence: false,
twitter: Faker::Internet.url(host: 'twitter.com'),
)
end
if p.partials_to_show.include? "agency_stability"
founded_year = Faker::Date.between(from: 50.years.ago, to: Date.today).year
profile.update(
case_management: true,
currently_provide_diapers: true,
essentials_use: Faker::Lorem.paragraph(sentence_count: 2),
evidence_based: true,
form_990: true,
founded: founded_year,
program_age: Date.today.year - founded_year,
program_description: Faker::Lorem.paragraph(sentence_count: 2),
program_name: Faker::Company.name,
receives_essentials_from_other: Faker::Lorem.sentence,
)
end
if p.partials_to_show.include? "organizational_capacity"
profile.update(
client_capacity: Faker::Lorem.sentence,
describe_storage_space: Faker::Lorem.paragraph(sentence_count: 2),
storage_space: true,
)
end
if p.partials_to_show.include? "sources_of_funding"
profile.update(
essentials_budget: Faker::Lorem.sentence,
essentials_funding_source: Faker::Lorem.sentence,
sources_of_diapers: Faker::Lorem.sentence,
sources_of_funding: Faker::Lorem.sentence,
)
end
if p.partials_to_show.include? "population_served"
def calculate_array_of_percentages( num_entries )
percentages = []
remaining_percentage = 100
share_ceiling = 100 / num_entries
(num_entries - 1).times do
percentage = Faker::Number.within(range: 1..share_ceiling)
remaining_percentage -= percentage
percentages.append( percentage )
end
percentages.append( remaining_percentage )
return percentages
end
pop_percentages = calculate_array_of_percentages(8) # 8 population fields
poverty_percentages = calculate_array_of_percentages(4) # 4 poverty fields
profile.update(
above_1_2_times_fpl: poverty_percentages[0],
at_fpl_or_below: poverty_percentages[1],
greater_2_times_fpl: poverty_percentages[2],
income_requirement_desc: true,
income_verification: true,
population_american_indian: pop_percentages[0],
population_asian: pop_percentages[1],
population_black: pop_percentages[2],
population_hispanic: pop_percentages[3],
population_island: pop_percentages[4],
population_multi_racial: pop_percentages[5],
population_other: pop_percentages[6],
population_white: pop_percentages[7],
poverty_unknown: poverty_percentages[3],
zips_served: Faker::Address.zip_code,
)
end
if p.partials_to_show.include? "agency_distribution_information"
profile.update(
distribution_times: Faker::Lorem.sentence,
more_docs_required: Faker::Lorem.sentence,
new_client_times: Faker::Lorem.sentence,
)
end
end
user = ::User.create!(
name: Faker::Name.name,
password: "password!",
password_confirmation: "password!",
email: p.email,
invitation_sent_at: Time.utc(2021, 9, 8, 12, 43, 4),
last_sign_in_at: Time.utc(2021, 9, 9, 11, 34, 4)
)
user.add_role(:partner, p)
user_2 = ::User.create!(
name: Faker::Name.name,
password: "password!",
password_confirmation: "password!",
email: Faker::Internet.email,
invitation_sent_at: Time.utc(2021, 9, 16, 12, 43, 4),
last_sign_in_at: Time.utc(2021, 9, 17, 11, 34, 4)
)
user_2.add_role(:partner, p)
#
# Skip creating records that they would have created after
# they've accepted the invitation
#
next if p.status == "uninvited"
families = (1..Faker::Number.within(range: 4..13)).to_a.map do
Partners::Family.create!(
guardian_first_name: Faker::Name.first_name,
guardian_last_name: Faker::Name.last_name,
guardian_zip_code: Faker::Address.zip_code,
guardian_county: Faker::Address.community, # Faker doesn't have county, this has same flavor, and isn't country
guardian_phone: Faker::PhoneNumber.phone_number,
case_manager: Faker::Name.name,
home_adult_count: [1, 2, 3].sample,
home_child_count: [0, 1, 2, 3, 4, 5].sample,
home_young_child_count: [1, 2, 3, 4].sample,
sources_of_income: Partners::Family::INCOME_TYPES.sample(2),
guardian_employed: Faker::Boolean.boolean,
guardian_employment_type: Partners::Family::EMPLOYMENT_TYPES.sample,
guardian_monthly_pay: [1, 2, 3, 4].sample,
guardian_health_insurance: Partners::Family::INSURANCE_TYPES.sample,
comments: Faker::Lorem.paragraph,
military: false,
partner: p
)
end
requestable_items = PartnerFetchRequestableItemsService.new(partner_id: p.id).call.map(&:last)
families.each do |family|
Partners::AuthorizedFamilyMember.create!(
first_name: Faker::Name.first_name,
last_name: Faker::Name.last_name,
date_of_birth: Faker::Date.birthday(min_age: 18, max_age: 100),
gender: Faker::Gender.binary_type,
comments: Faker::Lorem.paragraph,
family: family
)
family.home_child_count.times do
Partners::Child.create!(
family: family,
first_name: Faker::Name.unique.first_name,
last_name: family.guardian_last_name,
date_of_birth: Faker::Date.birthday(min_age: 5, max_age: 18),
gender: Faker::Gender.binary_type,
child_lives_with: Partners::Child::CAN_LIVE_WITH.sample(2),
race: Partners::Child::RACES.sample,
agency_child_id: family.case_manager + family.guardian_last_name + family.guardian_first_name,
health_insurance: family.guardian_health_insurance,
comments: Faker::Lorem.paragraph,
active: Faker::Boolean.boolean,
archived: false,
requested_item_ids: requestable_items.sample(rand(4))
)
end
family.home_young_child_count.times do
Partners::Child.create!(
family: family,
first_name: Faker::Name.unique.first_name,
last_name: family.guardian_last_name,
date_of_birth: Faker::Date.birthday(min_age: 0, max_age: 5),
gender: Faker::Gender.binary_type,
child_lives_with: Partners::Child::CAN_LIVE_WITH.sample(2),
race: Partners::Child::RACES.sample,
agency_child_id: family.case_manager + family.guardian_last_name + family.guardian_first_name,
health_insurance: family.guardian_health_insurance,
comments: Faker::Lorem.paragraph,
active: Faker::Boolean.boolean,
archived: false,
requested_item_ids: requestable_items.sample(rand(4))
)
end
end
dates_generator = DispersedPastDatesGenerator.new
Faker::Number.within(range: 32..56).times do |index|
date = dates_generator.next
partner_request = ::Request.new(
partner_id: p.id,
organization_id: p.organization_id,
comments: Faker::Lorem.paragraph,
partner_user_id: p.primary_user.id,
created_at: date,
updated_at: date
)
pads = p.organization.items.find_by(name: "Pads")
new_item_request = Partners::ItemRequest.new(
item_id: pads.id,
quantity: Faker::Number.within(range: 10..30),
children: [],
name: pads.name,
partner_key: pads.partner_key,
created_at: date,
updated_at: date,
request_unit: "pack"
)
partner_request.item_requests << new_item_request
items = p.organization.items.sample(Faker::Number.within(range: 4..14)) - [pads]
partner_request.item_requests += items.map do |item|
Partners::ItemRequest.new(
item_id: item.id,
quantity: Faker::Number.within(range: 10..30),
children: [],
name: item.name,
partner_key: item.partner_key,
created_at: date,
updated_at: date
)
end
partner_request.request_items = partner_request.item_requests.map do |ir|
{
item_id: ir.item_id,
quantity: ir.quantity
}
end
# Guarantee that there is a request for the items unique to the Second City Bank
if (p.organization == sc_org) && (index < 4)
unique_item = sc_org_unique_items[index]
# Make sure we don't violate item request uniqueness if the unique_item was
# randomly selected already
if !partner_request.item_requests.any? { |item_request| item_request.item_id == unique_item.id }
partner_request.item_requests << Partners::ItemRequest.new(
item_id: unique_item.id,
quantity: Faker::Number.within(range: 10..30),
children: [],
name: unique_item.name,
partner_key: unique_item.partner_key,
created_at: date,
updated_at: date
)
end
end
partner_request.save!
end
end
# ----------------------------------------------------------------------------
# Storage Locations
# ----------------------------------------------------------------------------
inv_arbor = StorageLocation.find_or_create_by!(name: "Bulk Storage Location") do |inventory|
inventory.address = "Unknown"
inventory.organization = pdx_org
inventory.warehouse_type = StorageLocation::WAREHOUSE_TYPES[0]
inventory.square_footage = 10_000
end
inv_pdxdb = StorageLocation.find_or_create_by!(name: "Pawnee Main Bank (Office)") do |inventory|
inventory.address = "Unknown"
inventory.organization = pdx_org
inventory.warehouse_type = StorageLocation::WAREHOUSE_TYPES[1]
inventory.square_footage = 20_000
end
StorageLocation.find_or_create_by!(name: "Second City Bulk Storage") do |inventory|
inventory.address = "#{Faker::Address.street_address}, #{sc_org.city}, #{sc_org.state} #{sc_org.zipcode}"
inventory.organization = sc_org
inventory.warehouse_type = StorageLocation::WAREHOUSE_TYPES[0]
inventory.square_footage = 10_000
end
StorageLocation.find_or_create_by!(name: "Second City Main Bank (Office)") do |inventory|
inventory.address = "#{Faker::Address.street_address}, #{sc_org.city}, #{sc_org.state} #{sc_org.zipcode}"
inventory.organization = sc_org
inventory.warehouse_type = StorageLocation::WAREHOUSE_TYPES[1]
inventory.square_footage = 20_000
end
inactive_storage = StorageLocation.find_or_create_by!(name: "Inactive Storage Location") do |inventory|
inventory.address = "Unknown"
inventory.organization = pdx_org
inventory.warehouse_type = StorageLocation::WAREHOUSE_TYPES[2]
inventory.square_footage = 5_000
end
inactive_storage.discard
#
# Define all the InventoryItem for each of the StorageLocation
#
StorageLocation.active.each do |sl|
sl.organization.items.active.each do |item|
InventoryItem.create!(
storage_location: sl,
item: item,
quantity: Faker::Number.within(range: 500..2000)
)
end
end
Organization.all.find_each { |org| SnapshotEvent.publish(org) }
# Set minimum and recommended inventory levels for the complete organizations
# Only set inventory levels for the half of each org's items with the lowest stock
complete_orgs.each do |org|
half_items_count = (org.items.count / 2).to_i
low_items = org.items.left_joins(:inventory_items)
.select("items.*, SUM(inventory_items.quantity) AS total_quantity")
.group("items.id")
.order("total_quantity")
.limit(half_items_count).to_a
min_qty = low_items.first.total_quantity
max_qty = low_items.last.total_quantity
# Ensure at least one of the items unique to the Second City Bank has minimum
# and recommended quantities set
if (org == sc_org) && !(low_items & sc_org_unique_items).any?
low_items << sc_org_unique_items.last
end
low_items.each do |item|
min_value = rand((min_qty / 10).floor..(max_qty / 10).ceil) * 10
recommended_value = rand((min_value / 10).ceil..1000) * 10
item.update(on_hand_minimum_quantity: min_value, on_hand_recommended_quantity: recommended_value)
end
end
# Reload, since some of the items in sc_org_unique_items will have been altered
sc_org_unique_items.reload
complete_orgs.each do |org|
# ----------------------------------------------------------------------------
# Product Drives
# ----------------------------------------------------------------------------
[
{name: "First Product Drive",
start_date: 3.years.ago,
end_date: 3.years.ago,
organization: org},
{name: "Best Product Drive",
start_date: 3.weeks.ago,
end_date: 2.weeks.ago,
organization: org},
{name: "Second Best Product Drive",
start_date: 2.weeks.ago,
end_date: 1.week.ago,
organization: org}
].each { |product_drive| ProductDrive.find_or_create_by! product_drive }
# ----------------------------------------------------------------------------
# Product Drive Participants
# ----------------------------------------------------------------------------
[
{business_name: "A Good Place to Collect Diapers",
contact_name: "fred",
email: "good@place.is",
organization: org},
{business_name: "A Mediocre Place to Collect Diapers",
contact_name: "wilma",
email: "ok@place.is",
organization: org}
].each { |participant| ProductDriveParticipant.create! participant }
# ----------------------------------------------------------------------------
# Manufacturers
# ----------------------------------------------------------------------------
[
{name: "Manufacturer 1", organization: org},
{name: "Manufacturer 2", organization: org}
].each { |manu| Manufacturer.find_or_create_by! manu }
end
# ----------------------------------------------------------------------------
# Line Items
# ----------------------------------------------------------------------------
def seed_quantity(item_name, organization, storage_location, quantity)
return if quantity.zero?
item = Item.find_by(name: item_name, organization: organization)
adjustment = organization.adjustments.create!(
comment: "Starting inventory",
storage_location: storage_location,
user: User.with_role(:org_admin, organization).first
)
adjustment.line_items = [LineItem.new(quantity: quantity, item: item, itemizable: adjustment)]
AdjustmentCreateService.new(adjustment).call
end
JSON.parse(Rails.root.join("db", "base_items.json").read).each do |_category, entries|
entries.each do |entry|
seed_quantity(entry["name"], pdx_org, inv_arbor, entry["qty"]["arbor"])
seed_quantity(entry["name"], pdx_org, inv_pdxdb, entry["qty"]["pdxdb"])
end
end
# ----------------------------------------------------------------------------
# Barcode Items
# ----------------------------------------------------------------------------
[
{value: "10037867880046", name: "Kids (Size 5)", quantity: 108},
{value: "10037867880053", name: "Kids (Size 6)", quantity: 92},
{value: "10037867880039", name: "Kids (Size 4)", quantity: 124},
{value: "803516626364", name: "Kids (Size 1)", quantity: 40},
{value: "036000406535", name: "Kids (Size 1)", quantity: 44},
{value: "037000863427", name: "Kids (Size 1)", quantity: 35},
{value: "041260379000", name: "Kids (Size 3)", quantity: 160},
{value: "074887711700", name: "Wipes (Baby)", quantity: 8},
{value: "036000451306", name: "Kids Pull-Ups (4T-5T)", quantity: 56},
{value: "037000862246", name: "Kids (Size 4)", quantity: 92},
{value: "041260370236", name: "Kids (Size 4)", quantity: 68},
{value: "036000407679", name: "Kids (Size 4)", quantity: 24},
{value: "311917152226", name: "Kids (Size 4)", quantity: 82}
].each do |item|
BarcodeItem.find_or_create_by!(value: item[:value]) do |barcode|
barcode.item = pdx_org.items.find_by(name: item[:name])
barcode.quantity = item[:quantity]
barcode.organization = pdx_org
end
end
# ----------------------------------------------------------------------------
# Kits
# ----------------------------------------------------------------------------
complete_orgs.each do |org|
# Create comprehensive kits representing each NDBN category
# Diaper Care Kit - covering multiple diaper categories
diaper_kit_params = {
name: "Diaper Care Kit",
line_items_attributes: [
{item_id: org.items.find_by(name: "Kids (Size 1)").id, quantity: 50},
{item_id: org.items.find_by(name: "Kids (Size 2)").id, quantity: 50},
{item_id: org.items.find_by(name: "Kids (Size 3)").id, quantity: 50},
{item_id: org.items.find_by(name: "Wipes (Baby)").id, quantity: 10},
{item_id: org.items.find_by(name: "Diaper Rash Cream/Powder").id, quantity: 2}
].compact_blank
}
if diaper_kit_params[:line_items_attributes].any?
diaper_kit_service = KitCreateService.new(organization_id: org.id, kit_params: diaper_kit_params)
diaper_kit_service.call
end
# Menstrual Care Kit
menstrual_kit_params = {
name: "Menstrual Care Kit",
line_items_attributes: [
{item_id: org.items.find_by(name: "Pads").id, quantity: 20},
{item_id: org.items.find_by(name: "Tampons").id, quantity: 20},
{item_id: org.items.find_by(name: "Liners (Menstrual)").id, quantity: 15}
].compact_blank
}
if menstrual_kit_params[:line_items_attributes].any?
menstrual_kit_service = KitCreateService.new(organization_id: org.id, kit_params: menstrual_kit_params)
menstrual_kit_service.call
end
# Adult Incontinence Kit
adult_kit_params = {
name: "Adult Incontinence Kit",
line_items_attributes: [
{item_id: org.items.find_by(name: "Adult Briefs (Large/X-Large)").id, quantity: 30},
{item_id: org.items.find_by(name: "Adult Incontinence Pads").id, quantity: 25},
{item_id: org.items.find_by(name: "Wipes (Adult)").id, quantity: 5},
{item_id: org.items.find_by(name: "Underpads (Pack)").id, quantity: 5}
].compact_blank
}
if adult_kit_params[:line_items_attributes].any?
adult_kit_service = KitCreateService.new(organization_id: org.id, kit_params: adult_kit_params)
adult_kit_service.call
end
# Baby Care Essentials Kit - covering miscellaneous category
baby_care_kit_params = {
name: "Baby Care Essentials Kit",
line_items_attributes: [
{item_id: org.items.find_by(name: "Bibs (Adult & Child)").id, quantity: 5},
{item_id: org.items.find_by(name: "Wipes (Baby)").id, quantity: 8},
{item_id: org.items.find_by(name: "Diaper Rash Cream/Powder").id, quantity: 1},
{item_id: org.items.find_by(name: "Cloth Diapers (Prefolds & Fitted)").id, quantity: 10}
].compact_blank
}
if baby_care_kit_params[:line_items_attributes].any?
baby_care_service = KitCreateService.new(organization_id: org.id, kit_params: baby_care_kit_params)
baby_care_service.call
end
# Training Kit - covering training pants category
training_kit_params = {
name: "Potty Training Kit",
line_items_attributes: [
{item_id: org.items.find_by(name: "Cloth Potty Training Pants/Underwear").id, quantity: 8},
{item_id: org.items.find_by(name: "Kids Pull-Ups (2T-3T)").id, quantity: 20},
{item_id: org.items.find_by(name: "Kids Pull-Ups (3T-4T)").id, quantity: 20},
{item_id: org.items.find_by(name: "Wipes (Baby)").id, quantity: 5}
].compact_blank
}
if training_kit_params[:line_items_attributes].any?
training_kit_service = KitCreateService.new(organization_id: org.id, kit_params: training_kit_params)
training_kit_service.call
end
end
# Create kit inventory for storage locations
complete_orgs.each do |org|
org.storage_locations.active.each do |storage_location|
org.kits.active.each do |kit|
next unless kit.item # Ensure kit has an associated item
# Create inventory for each kit
InventoryItem.create!(
storage_location: storage_location,
item: kit.item,
quantity: Faker::Number.within(range: 10..50)
)
end
end
end
dates_generator = DispersedPastDatesGenerator.new
complete_orgs.each do |org|
# ----------------------------------------------------------------------------
# Donations
# ----------------------------------------------------------------------------
# Make some donations of all sorts
20.times.each do |index|
source = Donation::SOURCES.values.sample
# Depending on which source it uses, additional data may need to be provided.
donation = Donation.new(
source: source,
storage_location: org.storage_locations.active.sample,
organization: org,
issued_at: dates_generator.next
)
case source
when Donation::SOURCES[:product_drive]
donation.product_drive = org.product_drives.find_by(name: "Best Product Drive")
donation.product_drive_participant = random_record_for_org(org, ProductDriveParticipant)
when Donation::SOURCES[:donation_site]
donation.donation_site = random_record_for_org(org, DonationSite)
when Donation::SOURCES[:manufacturer]
donation.manufacturer = random_record_for_org(org, Manufacturer)
end
rand(1..5).times.each do
donation.line_items.push(LineItem.new(quantity: rand(250..500), item: random_record_for_org(org, Item)))
end
# Guarantee that there are at least a few donations for the items unique to the Second City Bank
if (org == sc_org) && (index < 4)
donation.line_items.push(LineItem.new(quantity: rand(250..500), item: sc_org_unique_items[index]))
end
DonationCreateService.call(donation)
end
# ----------------------------------------------------------------------------
# Distributions
# ----------------------------------------------------------------------------
inventory = InventoryAggregate.inventory_for(org.id)
# Make some distributions, but don't use up all the inventory
20.times.each do |index|
issued_at = dates_generator.next
storage_location = org.storage_locations.active.sample
stored_inventory_items_sample = inventory.storage_locations[storage_location.id].items.values.sample(20)
delivery_method = Distribution.delivery_methods.keys.sample
shipping_cost = (delivery_method == "shipped") ? rand(20.0..100.0).round(2).to_s : nil
distribution = Distribution.new(
storage_location: storage_location,
partner: random_record_for_org(org, Partner),
organization: org,
issued_at: issued_at,
created_at: 3.days.ago(issued_at),
delivery_method: delivery_method,
shipping_cost: shipping_cost,
comment: "Urgent"
)
stored_inventory_items_sample.each do |stored_inventory_item|
distribution_qty = rand(stored_inventory_item.quantity / 2)
if distribution_qty >= 1
distribution.line_items.push(LineItem.new(quantity: distribution_qty,
item_id: stored_inventory_item.item_id))
end
end
# Guarantee that there are at least a few distributions for the items unique to the Second City Bank
if (org == sc_org) && (index < 4)
unique_item_id = sc_org_unique_items[index].id
distribution_qty = rand(storage_location.item_total(unique_item_id) / 2)
distribution.line_items.push(
LineItem.new(
quantity: distribution_qty,
item_id: unique_item_id
)
)
end
DistributionCreateService.new(distribution).call
end
# Create some distributions that use kits instead of individual items
kit_items = org.items.joins(:kit).where(kits: {active: true})
if kit_items.any?
5.times do |index|
issued_at = dates_generator.next
storage_location = org.storage_locations.active.sample
kit_item = kit_items.sample
# Check if there's inventory for this kit
kit_inventory_qty = storage_location.item_total(kit_item.id)
next if kit_inventory_qty.zero?
delivery_method = Distribution.delivery_methods.keys.sample
shipping_cost = (delivery_method == "shipped") ? rand(20.0..100.0).round(2).to_s : nil
kit_distribution = Distribution.new(
storage_location: storage_location,
partner: random_record_for_org(org, Partner),
organization: org,
issued_at: issued_at,
created_at: 3.days.ago(issued_at),
delivery_method: delivery_method,
shipping_cost: shipping_cost,
comment: "Kit distribution"
)
distribution_qty = [rand(1..3), kit_inventory_qty / 2].min
if distribution_qty >= 1
kit_distribution.line_items.push(
LineItem.new(
quantity: distribution_qty,
item_id: kit_item.id
)
)
DistributionCreateService.new(kit_distribution).call
end
end
end
end
# ----------------------------------------------------------------------------
# Broadcast Announcements
# ----------------------------------------------------------------------------
BroadcastAnnouncement.create(
user: User.find_by(email: "superadmin@example.com"),
message: "This is the staging /demo server. There may be new features here! Stay tuned!",
link: "https://example.com",
expiry: Time.zone.today + 7.days,