diff --git a/app/models/concerns/importable.rb b/app/models/concerns/importable.rb deleted file mode 100644 index e2976a1a2c9b7db5f8a6668628923a241e5333cd..0000000000000000000000000000000000000000 --- a/app/models/concerns/importable.rb +++ /dev/null @@ -1,36 +0,0 @@ -module Importable - extend ActiveSupport::Concern - - included do - belongs_to :user - - has_one_attached :file - - after_commit :parse_async - end - - def lines - csv.count - rescue - 'NA' - end - - def to_s - "#{user}, #{I18n.l created_at}" - end - - protected - - def parse_async - parse - end - handle_asynchronously :parse_async, queue: 'default' - - def parse - raise NotImplementedError - end - - def csv - @csv ||= CSV.parse file.blob.download, headers: true - end -end diff --git a/app/models/university/organization/import.rb b/app/models/university/organization/import.rb deleted file mode 100644 index 46f51178f73850836e626576f74352c27404124d..0000000000000000000000000000000000000000 --- a/app/models/university/organization/import.rb +++ /dev/null @@ -1,70 +0,0 @@ -# == Schema Information -# -# Table name: university_organization_imports -# -# id :uuid not null, primary key -# created_at :datetime not null -# updated_at :datetime not null -# university_id :uuid not null, indexed -# user_id :uuid not null, indexed -# -# Indexes -# -# index_university_organization_imports_on_university_id (university_id) -# index_university_organization_imports_on_user_id (user_id) -# -# Foreign Keys -# -# fk_rails_31152af0cd (university_id => universities.id) -# fk_rails_da057ff44d (user_id => users.id) -# -class University::Organization::Import < ApplicationRecord - include WithUniversity - - belongs_to :user - - has_one_attached :file - - after_save :parse - - def lines - csv.count - rescue - 'NA' - end - - def to_s - "#{user}, #{I18n.l created_at}" - end - - protected - - def parse - csv.each do |row| - university.organizations.where(university: university, name: row['name']).first_or_create do |o| - [ - :long_name, - :kind, - :siren, - :nic, - :description, - :address, - :zipcode, - :city, - :country, - :email, - :phone, - :url, - ].each do |property| - o[property] = row[property.to_s] - end - o.kind ||= :company - end - end - end - handle_asynchronously :parse, queue: 'default' - - def csv - @csv ||= CSV.parse file.blob.download, headers: true - end -end diff --git a/app/models/university/person/alumnus/import.rb b/app/models/university/person/alumnus/import.rb deleted file mode 100644 index 43e09d84c535aea87095ba0e7a799619369d8d39..0000000000000000000000000000000000000000 --- a/app/models/university/person/alumnus/import.rb +++ /dev/null @@ -1,179 +0,0 @@ -# == Schema Information -# -# Table name: university_person_alumnus_imports -# -# id :uuid not null, primary key -# created_at :datetime not null -# updated_at :datetime not null -# university_id :uuid not null, indexed -# user_id :uuid not null, indexed -# -# Indexes -# -# index_university_person_alumnus_imports_on_university_id (university_id) -# index_university_person_alumnus_imports_on_user_id (user_id) -# -# Foreign Keys -# -# fk_rails_3ff74ac195 (user_id => users.id) -# fk_rails_d14eb003f9 (university_id => universities.id) -# -class University::Person::Alumnus::Import < ApplicationRecord - include WithUniversity - include Importable - - def self.table_name - 'university_person_alumnus_imports' - end - - protected - - def parse - csv.each do |row| - person = import_person row - next unless person - import_cohort row, person - organization = import_organization row - next unless organization - import_experience row, person, organization - end - end - - def import_person(row) - first_name = clean_encoding row['first_name'] - last_name = clean_encoding row['last_name'] - email = clean_encoding(row['mail']).to_s.downcase - return if first_name.blank? && last_name.blank? && email.blank? - url = clean_encoding row['url'] - if email.present? - person = university.people - .where(email: email) - .first_or_create - person.first_name = first_name - person.last_name = last_name - elsif first_name.present? && last_name.present? - person = university.people - .where(first_name: first_name, last_name: last_name) - .first_or_create - end - return if person.nil? - # TODO all fields - # gender - # birth - # address - # zipcode - # city - # country - person.is_alumnus = true - person.url = url - person.slug = person.to_s.parameterize.dasherize - person.twitter ||= row['social_twitter'] - person.linkedin ||= row['social_linkedin'] - person.biography ||= clean_encoding row['biography'] - person.phone ||= row['mobile'] - person.phone ||= row['phone_personal'] - person.phone ||= row['phone_professional'] - byebug unless person.valid? - person.save - add_picture person, row['photo'] - person - end - - def import_cohort(row, person) - program = program_with_id row['program'] - return if program.nil? - academic_year = university.academic_years - .where(year: row['year']) - .first_or_create - cohort = university.education_cohorts - .where(program: program, academic_year: academic_year) - .first_or_create - person.add_to_cohort cohort - end - - def import_organization(row) - name = clean_encoding row['company_name'] - siren = clean_encoding row['company_siren'] - nic = clean_encoding row['company_nic'] - return if name.blank? - if !siren.blank? && !nic.blank? - organization = university.organizations - .find_by siren: siren, - nic: nic - elsif !siren.blank? - organization ||= university.organizations - .find_by siren: siren - end - organization ||= university.organizations - .find_by name: name - organization ||= university.organizations - .where( name: name, - siren: siren, - nic: nic) - .first_or_create - organization - end - - def import_experience(row, person, organization) - job = row['experience_job'] - from = row['experience_from'] - to = row['experience_to'] - experience = person.experiences - .where(university: university, - organization: organization, - from_year: from) - .first_or_create - experience.description = job - experience.to_year = to - experience.save - experience - end - - def add_picture(person, photo) - return if photo.nil? - return if person.picture.attached? - return unless photo.end_with?('.jpg') || photo.end_with?('.png') - begin - file = URI.open photo - filename = File.basename photo - person.picture.attach(io: file, filename: filename) - rescue - end - end - - def program_with_id(id) - if Rails.env.development? - # substitute local data for testing - substitutes = { - # Arnaud - 'c6b78fac-0a5f-4c44-ad22-4ee68ed382bb' => '23279cab-8bc1-4c75-bcd8-1fccaa03ad55', # DUT MMI - 'ae3e067a-63b4-4c3f-ba9c-468ade0e4182' => '863b8c9c-1ed1-4af7-b92c-7264dfb6b4da', # MASTER IJBA - 'f4d4a92f-8b8f-4778-a127-9293684666be' => '8dfaee2a-c876-4b1c-8e4e-8380d720c71f', # DU_BILINGUE - '6df53074-195c-4299-8b49-bbc9d7cad41a' => 'be3cb0b2-7f66-4c5f-b8d7-6a39a0480c46', # DU_JRI - '0d81d3a2-a12c-4326-a395-fd0df4a3ea4f' => '56a50383-3ef7-43f6-8e98-daf279e86802' # DUT_JOURNALISME - # Alex - # 'c6b78fac-0a5f-4c44-ad22-4ee68ed382bb' => '02e6f703-d15b-4841-ac95-3c47d88e21b5', # DUT MMI - # 'ae3e067a-63b4-4c3f-ba9c-468ade0e4182' => '8fdfafb7-11fd-456c-9f47-7fd76dddb373', # MASTER IJBA - # 'f4d4a92f-8b8f-4778-a127-9293684666be' => 'fab9b86c-8872-4df5-9a97-0e30b104a837', # DU_BILINGUE - # '6df53074-195c-4299-8b49-bbc9d7cad41a' => 'cb1a26b9-fe5c-4ad1-9715-71cec4642910', # DU_JRI - # '0d81d3a2-a12c-4326-a395-fd0df4a3ea4f' => '91c44fd2-f0a4-4189-a3f5-311322b7b472' # DUT_JOURNALISME - # Sebou - # 'c6b78fac-0a5f-4c44-ad22-4ee68ed382bb' => 'ea5d32be-b86a-4257-984a-4d84717dd1d6', # DUT MMI - # 'ae3e067a-63b4-4c3f-ba9c-468ade0e4182' => '701c3a4f-3585-4152-b866-da17f4e80e77', # MASTER IJBA - # 'f4d4a92f-8b8f-4778-a127-9293684666be' => '0c05b690-ebd1-4efa-862c-81ea0978fb0b', # DU_BILINGUE - # '6df53074-195c-4299-8b49-bbc9d7cad41a' => '4ded6dfa-2fab-4e77-b58d-0d97344a04d1', # DU_JRI - # '0d81d3a2-a12c-4326-a395-fd0df4a3ea4f' => '4edac5cd-6564-4e47-a18c-960d3e5de54e' # DUT_JOURNALISME - } - id = substitutes[id] if substitutes.has_key? id - end - university.education_programs.find_by(id: id) - end - - def clean_encoding(value) - return if value.nil? - if value.encoding != 'UTF-8' - value = value.force_encoding 'UTF-8' - end - value.strip - end -end diff --git a/app/models/university/with_people_and_organizations.rb b/app/models/university/with_people_and_organizations.rb index d543dcbc338c7725159976683c8fc4982b7bfbea..ac0d78e4c340b3f0f7e7b083e5a38e4aee2445ee 100644 --- a/app/models/university/with_people_and_organizations.rb +++ b/app/models/university/with_people_and_organizations.rb @@ -11,6 +11,6 @@ module University::WithPeopleAndOrganizations class_name: 'University::Organization', dependent: :destroy alias_attribute :organizations, :university_organizations - end + end diff --git a/app/services/importers/alumni.rb b/app/services/importers/alumni.rb new file mode 100644 index 0000000000000000000000000000000000000000..87811d3144dde8f68a714ddae61720242863e3c7 --- /dev/null +++ b/app/services/importers/alumni.rb @@ -0,0 +1,84 @@ +module Importers + class Alumni < Base + + protected + + def analyze_hash(hash, index) + hash_to_alumnus = HashToAlumnus.new(@university, hash) + add_error(hash_to_alumnus.error, index + 1) unless hash_to_alumnus.valid? + end + + end + + class HashToAlumnus + def initialize(university, hash) + @university = university + @hash = hash + @error = nil + # extract_variables + # save if valid? + end + + def valid? + return true + # if country_not_found? + # @error = "Country #{@country} not found" + # elsif !organization.valid? + # @error = "Unable to create the organization: #{organization.errors.full_messages}" + # end + # @error.nil? + end + + def error + @error + end + + def organization_name + @organization_name ||= @hash[0].to_s.strip + end + + protected + + def extract_variables + @long_name = @hash[1].to_s.strip + @kind = @hash[2].to_s.strip + @siren = @hash[3].to_s.strip + @nic = @hash[4].to_s.strip + @description = @hash[5].to_s.strip + @address = @hash[6].to_s.strip + @zipcode = @hash[7].to_s.strip + @city = @hash[8].to_s.strip + @country = @hash[9].to_s.strip + @email = @hash[10].to_s.strip + @phone = @hash[11].to_s.strip + @url = @hash[12].to_s.strip + end + + def country_not_found? + ISO3166::Country[@country].nil? + end + + def organization + unless @organization + @organization = University::Organization.where(university_id: @university.id, name: organization_name).first_or_initialize + @organization.long_name = @long_name + @organization.kind = @kind.to_sym + @organization.siren = @siren + @organization.nic = @nic + @organization.description = @description + @organization.address = @address + @organization.zipcode = @zipcode + @organization.city = @city + @organization.country = @country + @organization.email = @email + @organization.phone = @phone + @organization.url = @url + end + @organization + end + + def save + organization.save + end + end +end diff --git a/db/migrate/20220523172753_remove_unused_imports_tables.rb b/db/migrate/20220523172753_remove_unused_imports_tables.rb new file mode 100644 index 0000000000000000000000000000000000000000..2682a07ca11339962a63c6ccc1b1f5c9925dc439 --- /dev/null +++ b/db/migrate/20220523172753_remove_unused_imports_tables.rb @@ -0,0 +1,6 @@ +class RemoveUnusedImportsTables < ActiveRecord::Migration[6.1] + def change + drop_table :university_organization_imports + drop_table :university_person_alumnus_imports + end +end diff --git a/db/schema.rb b/db/schema.rb index f00d75039f7e82ba28958e26186179d26e6b306f..5d00fa8ee7a9a0cc54c84b41962d496d999c5313 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2022_05_23_102030) do +ActiveRecord::Schema.define(version: 2022_05_23_172753) do # These are extensions that must be enabled in order to support this database enable_extension "pgcrypto" @@ -651,15 +651,6 @@ ActiveRecord::Schema.define(version: 2022_05_23_102030) do t.jsonb "sso_mapping" end - create_table "university_organization_imports", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t| - t.uuid "university_id", null: false - t.uuid "user_id", null: false - t.datetime "created_at", precision: 6, null: false - t.datetime "updated_at", precision: 6, null: false - t.index ["university_id"], name: "index_university_organization_imports_on_university_id" - t.index ["user_id"], name: "index_university_organization_imports_on_user_id" - end - create_table "university_organizations", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t| t.uuid "university_id", null: false t.string "name" @@ -711,15 +702,6 @@ ActiveRecord::Schema.define(version: 2022_05_23_102030) do t.index ["user_id"], name: "index_university_people_on_user_id" end - create_table "university_person_alumnus_imports", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t| - t.uuid "university_id", null: false - t.uuid "user_id", null: false - t.datetime "created_at", precision: 6, null: false - t.datetime "updated_at", precision: 6, null: false - t.index ["university_id"], name: "index_university_person_alumnus_imports_on_university_id" - t.index ["user_id"], name: "index_university_person_alumnus_imports_on_user_id" - end - create_table "university_person_experiences", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t| t.uuid "university_id", null: false t.uuid "person_id", null: false @@ -872,13 +854,9 @@ ActiveRecord::Schema.define(version: 2022_05_23_102030) do add_foreign_key "research_theses", "universities" add_foreign_key "research_theses", "university_people", column: "author_id" add_foreign_key "research_theses", "university_people", column: "director_id" - add_foreign_key "university_organization_imports", "universities" - add_foreign_key "university_organization_imports", "users" add_foreign_key "university_organizations", "universities" add_foreign_key "university_people", "universities" add_foreign_key "university_people", "users" - add_foreign_key "university_person_alumnus_imports", "universities" - add_foreign_key "university_person_alumnus_imports", "users" add_foreign_key "university_person_experiences", "universities" add_foreign_key "university_person_experiences", "university_organizations", column: "organization_id" add_foreign_key "university_person_experiences", "university_people", column: "person_id"