Skip to content

Commit

Permalink
Merge pull request #28 from CodiTramuntana/refactor/update_scopes_whe…
Browse files Browse the repository at this point in the history
…n_code_is_the_same

Update Scopes instead of creating new ones if code is the same
  • Loading branch information
laurajaime authored Jan 12, 2022
2 parents 03c10ca + dbaa3bd commit d6603be
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 16 deletions.
17 changes: 17 additions & 0 deletions lib/file_manager.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

module FileManager
def write_config_yaml!(filename, content)
File.write(filepath(filename), content.to_yaml)
puts "File generated 'config/civi_crm/#{filename}.yml'"
end

def filepath(filename)
Dir.mkdir("config/civi_crm") unless File.directory?("config/civi_crm")
Rails.root.join("config", "civi_crm", "#{filename}.yml").to_s
end

def load_config_yaml(filename)
YAML.load_file(filepath(filename))
end
end
23 changes: 7 additions & 16 deletions lib/tasks/civi_crm.rake
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# frozen_string_literal: true

require "file_manager"

namespace :civi_crm do
include FileManager

task init: ["import:all", "generate:all", "create:scopes"]

namespace :import do
Expand Down Expand Up @@ -114,26 +118,13 @@ namespace :civi_crm do
next if display_name[/\d/]

scope_name = display_name.strip
Decidim::Scope.find_or_create_by!(
scope = Decidim::Scope.find_or_initialize_by(
organization: organization,
name: { "ca" => scope_name, "en" => scope_name, "es" => scope_name },
code: contact_id
)
scope.name = { "ca" => scope_name, "en" => scope_name, "es" => scope_name }
scope.save!
end
end
end

def write_config_yaml!(filename, content)
File.write(filepath(filename), content.to_yaml)
puts "File generated 'config/civi_crm/#{filename}.yml'"
end

def filepath(filename)
Dir.mkdir("config/civi_crm") unless File.directory?("config/civi_crm")
Rails.root.join("config", "civi_crm", "#{filename}.yml").to_s
end

def load_config_yaml(filename)
YAML.load_file(filepath(filename))
end
end
85 changes: 85 additions & 0 deletions lib/tasks/erc_auth.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# frozen_string_literal: true

require "csv"
require "file_manager"

namespace :erc_auth do
include FileManager

namespace :csv_import do
task all: [:comarcals, :regionals, :locals, :local_comarcal_relationships, :local_regional_relationships]

desc "Generates a YAML file with the CSV with 'Comarcal' tag"
task comarcals: :environment do
csv_text = File.read("tmp/scopes_codes.csv")
csv = CSV.parse(csv_text, headers: true)

csv = csv.select { |row| row["type"] == "FC" }
csv = csv.map(&:to_h)

result = get_code_and_display_name(csv)
write_config_yaml!("comarcals", result)
end

desc "Generates a YAML file with the CSV with 'Regional' tag"
task regionals: :environment do
csv_text = File.read("tmp/scopes_codes.csv")
csv = CSV.parse(csv_text, headers: true)

csv = csv.select { |row| row["type"] == "FR" }
csv = csv.map(&:to_h)

result = get_code_and_display_name(csv)
write_config_yaml!("regionals", result)
end

desc "Generates a YAML file with the CSV with 'Locals' tag"
task locals: :environment do
csv_text = File.read("tmp/scopes_codes.csv")
csv = CSV.parse(csv_text, headers: true)

csv = csv.select { |row| row["type"] == "SL" }
csv = csv.map(&:to_h)

result = get_code_and_display_name(csv)
write_config_yaml!("locals", result)
end

desc "Generates a YAML file with the relationship between CSV 'Local' and 'Comarcal'"
task local_comarcal_relationships: :environment do
local_ids = load_config_yaml("locals").keys
comarcal_ids = load_config_yaml("comarcals").keys
result = get_code_relationships(local_ids, comarcal_ids, "comarcal")
write_config_yaml!("local_comarcal_relationships", result)
end

desc "Generates a YAML file with the relationship between CSV 'Local' and 'Regional'"
task local_regional_relationships: :environment do
local_ids = load_config_yaml("locals").keys
regional_ids = load_config_yaml("regionals").keys
result = get_code_relationships(local_ids, regional_ids, "regional")
write_config_yaml!("local_regional_relationships", result)
end

def get_code_and_display_name(body)
body.map! { |element| { element["code"] => element["name"] } }.reduce({}, :merge)
end

def get_code_relationships(local_ids, _filter_ids, scope_type)
local_ids.each_with_object({}) do |element, memo|
csv_text = File.read("tmp/scopes_hierarchy.csv")
csv = CSV.parse(csv_text, headers: true)

csv = csv.select { |row| row["SL"] == element }.first.to_h
next if csv.blank?

case scope_type
when "comarcal"
memo[csv["SL"]] = csv["FC"]
when "regional"
memo[csv["SL"]] = csv["FR"]
end
end
end
end
end

0 comments on commit d6603be

Please sign in to comment.