Last active
February 3, 2020 16:23
-
-
Save maryshirl/d33280093dc01074680d731b4843465e 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
csv_file = Rails.root.join("doc", "csp_data_20200204.csv").to_path | |
rows = [] | |
CSV.foreach(csv_file, headers: :first_row, encoding: "ISO8859-1") do |row| | |
rows << row.to_hash.symbolize_keys | |
end | |
Apartment::Tenant.switch!(Organisation.last.id) | |
csp_screening_items = [] | |
screening_item_not_found = [] | |
rows.each_with_index do |row, index| | |
puts index+1 | |
puts row | |
screening_item = ScreeningItem.find_by(ch_id: row[:service_item_id].to_i) | |
if screening_item.nil? | |
screening_item_not_found << row | |
next | |
end | |
default_value = ActiveModel::Type::Boolean.new.cast(row[:default]) || false | |
default = {"default"=>default_value} | |
id = SecureRandom.uuid | |
children_names = row[:children_names].present? ? JSON.parse(row[:children_names]) : [] | |
csp_screening_items << { | |
name: row[:service_item_name], | |
description: row[:service_item_description], | |
price: row[:defaul_price], | |
screening_item_id: screening_item.id, | |
duration: row[:duration].present? ? row[:duration].to_i : 0, | |
config: default, | |
results_duration: screening_item.results_duration, | |
affiliate_price: screening_item.affiliate_price, | |
children_ids: row[:children_ids].present? ? JSON.parse(row[:children_ids]) : [], | |
children_names: children_names, | |
company_id: row[:company_uuid], | |
site_id: row[:site_uuid], | |
position_id: row[:position_uuid], | |
company: row[:company], | |
site: row[:site], | |
position: row[:position], | |
id: id, | |
is_hidden: screening_item.is_hidden | |
} | |
end | |
children_not_formed = [] | |
csp_screening_items.each do |csp_screening_item| | |
csp_screening_item_dup = csp_screening_item.dup; nil | |
created_csp_screening_item = ScreeningItemDetail.create!(csp_screening_item_dup.slice!(:children_ids, :children_names, :company, :site, :position)) | |
if created_csp_screening_item.screening_item.collection? | |
children_uuids = ScreeningItem.where(ch_id: csp_screening_item[:children_ids]).pluck(:id) | |
if children_uuids.count == 0 || csp_screening_item[:children_ids].count != children_uuids.count | |
children_not_formed << csp_screening_item | |
next | |
end | |
puts csp_screening_item | |
children_uuids.each do |children_uuid| | |
ScreeningItemCollection.create(id: SecureRandom.uuid, set: created_csp_screening_item.screening_item.id, screening_item_detail_id: created_csp_screening_item.id, screening_item_id: children_uuid) | |
end | |
end | |
end | |
CsvGenerator.call("children_not_formed", children_not_formed.first.keys, children_not_formed.map{|x| x.values }, '[email protected]') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment