From 46050a983d580316062e22885f54290ccb735a7d Mon Sep 17 00:00:00 2001
From: Arnaud Levy <contact@arnaudlevy.com>
Date: Wed, 13 Apr 2022 22:56:41 +0200
Subject: [PATCH] experiences

---
 app/models/university/organization.rb         |  3 ++
 app/models/university/person.rb               |  2 +
 .../university/person/alumnus/import.rb       | 48 +++++++++++++++++--
 app/models/university/person/experience.rb    | 33 +++++++++++++
 .../person/alumnus/imports/new.html.erb       | 24 ++++++++++
 .../extranet/organizations/show.html.erb      | 11 +++++
 app/views/extranet/persons/show.html.erb      | 10 ++++
 ...56_create_university_person_experiences.rb | 14 ++++++
 db/schema.rb                                  | 19 +++++++-
 .../university/person/experiences.yml         | 43 +++++++++++++++++
 .../university/person/experience_test.rb      | 33 +++++++++++++
 11 files changed, 235 insertions(+), 5 deletions(-)
 create mode 100644 app/models/university/person/experience.rb
 create mode 100644 db/migrate/20220413203256_create_university_person_experiences.rb
 create mode 100644 test/fixtures/university/person/experiences.yml
 create mode 100644 test/models/university/person/experience_test.rb

diff --git a/app/models/university/organization.rb b/app/models/university/organization.rb
index f4de3cad8..7dd420162 100644
--- a/app/models/university/organization.rb
+++ b/app/models/university/organization.rb
@@ -37,6 +37,9 @@ class University::Organization < ApplicationRecord
   include WithUniversity
   include WithSlug
 
+  has_many :experiences,
+           class_name: 'University::Person::Experience'
+
   has_summernote :text
 
   has_one_attached_deletable :logo
diff --git a/app/models/university/person.rb b/app/models/university/person.rb
index d04e0786a..247d6682e 100644
--- a/app/models/university/person.rb
+++ b/app/models/university/person.rb
@@ -87,6 +87,8 @@ class University::Person < ApplicationRecord
                           through: :education_programs,
                           source: :websites
 
+  has_many                :experiences
+
   accepts_nested_attributes_for :involvements
 
   validates_presence_of   :first_name, :last_name
diff --git a/app/models/university/person/alumnus/import.rb b/app/models/university/person/alumnus/import.rb
index 7bfc413b4..9f76a1cbb 100644
--- a/app/models/university/person/alumnus/import.rb
+++ b/app/models/university/person/alumnus/import.rb
@@ -29,10 +29,15 @@ class University::Person::Alumnus::Import < ApplicationRecord
   protected
 
   def parse
+    # substitute local data for testing
+    substitutes = {
+      'c6b78fac-0a5f-4c44-ad22-4ee68ed382bb' => '23279cab-8bc1-4c75-bcd8-1fccaa03ad55'
+    }
     csv.each do |row|
-      # row['program'] = '23279cab-8bc1-4c75-bcd8-1fccaa03ad55' #TMP local fix
+      program_id = row['program']
+      program_id = substitutes[program_id] if substitutes.has_key? program_id
       program = university.education_programs
-                          .find_by(id: row['program'])
+                          .find_by(id: program_id)
       next if program.nil?
       academic_year = university.academic_years
                                 .where(year: row['year'])
@@ -44,6 +49,7 @@ class University::Person::Alumnus::Import < ApplicationRecord
       first_name = clean_encoding row['first_name']
       last_name = clean_encoding row['last_name']
       email = clean_encoding(row['mail']).to_s.downcase
+      next if first_name.blank? && last_name.blank? && email.blank?
       url = clean_encoding row['url']
       if email.present?
         person = university.people
@@ -59,8 +65,6 @@ class University::Person::Alumnus::Import < ApplicationRecord
       # TODO all fields
       # gender
       # birth
-      # phone_professional
-      # phone_personal
       # address
       # zipcode
       # city
@@ -72,10 +76,46 @@ class University::Person::Alumnus::Import < ApplicationRecord
       person.linkedin ||= row['social_linkedin']
       person.biography ||= row['biography']
       person.phone ||= row['mobile']
+      person.phone ||= row['phone_personal']
+      person.phone ||= row['phone_professional']
       byebug unless person.valid?
       person.save
       cohort.people << person unless person.in?(cohort.people)
       add_picture person, row['photo']
+
+      company_name = clean_encoding row['company_name']
+      company_siren = clean_encoding row['company_siren']
+      company_nic = clean_encoding row['company_nic']
+      if company_name.present?
+        if !row['company_siren'].blank? && !row['company_nic'].blank?
+          organization = university.organizations
+                                   .find_by siren: company_siren,
+                                            nic: company_nic
+        elsif !row['company_siren'].blank?
+          organization ||= university.organizations
+                                   .find_by siren: company_siren
+        end
+        if !company_name.blank?
+          organization ||= university.organizations
+                                     .find_by name: company_name
+        end
+        organization ||= university.organizations
+                                   .where( name: company_name,
+                                           siren: company_siren,
+                                           nic: company_nic)
+                                   .first_or_create
+        experience_job = row['experience_job']
+        experience_from = row['experience_from']
+        experience_to = row['experience_to']
+        experience = person.experiences
+                           .where(university: university,
+                                  organization: organization,
+                                  description: experience_job)
+                           .first_or_create
+        experience.from_year = experience_from
+        experience.to_year = experience_to
+        experience.save
+      end
     end
   end
 
diff --git a/app/models/university/person/experience.rb b/app/models/university/person/experience.rb
new file mode 100644
index 000000000..d4b2dbb11
--- /dev/null
+++ b/app/models/university/person/experience.rb
@@ -0,0 +1,33 @@
+# == Schema Information
+#
+# Table name: university_person_experiences
+#
+#  id              :uuid             not null, primary key
+#  description     :text
+#  from_year       :integer
+#  to_year         :integer
+#  created_at      :datetime         not null
+#  updated_at      :datetime         not null
+#  organization_id :uuid             not null, indexed
+#  person_id       :uuid             not null, indexed
+#  university_id   :uuid             not null, indexed
+#
+# Indexes
+#
+#  index_university_person_experiences_on_organization_id  (organization_id)
+#  index_university_person_experiences_on_person_id        (person_id)
+#  index_university_person_experiences_on_university_id    (university_id)
+#
+# Foreign Keys
+#
+#  fk_rails_18125d90df  (person_id => university_people.id)
+#  fk_rails_38aaa18a3b  (organization_id => university_organizations.id)
+#  fk_rails_923d0b71fd  (university_id => universities.id)
+#
+class University::Person::Experience < ApplicationRecord
+  include WithUniversity
+  belongs_to :person
+  belongs_to :organization
+
+  scope :ordered, -> { order(from_year: :desc)}
+end
diff --git a/app/views/admin/university/person/alumnus/imports/new.html.erb b/app/views/admin/university/person/alumnus/imports/new.html.erb
index 054e17203..95f80f952 100644
--- a/app/views/admin/university/person/alumnus/imports/new.html.erb
+++ b/app/views/admin/university/person/alumnus/imports/new.html.erb
@@ -97,6 +97,30 @@
           <th>social_linkedin</th>
           <td>https://www.linkedin.com/in/stephanedupond</td>
         </tr>
+        <tr>
+          <th>company_name</th>
+          <td>Le Monde</td>
+        </tr>
+        <tr>
+          <th>company_siren</th>
+          <td>433891850</td>
+        </tr>
+        <tr>
+          <th>company_nic</th>
+          <td>00052</td>
+        </tr>
+        <tr>
+          <th>experience_job</th>
+          <td>Journaliste</td>
+        </tr>
+        <tr>
+          <th>experience_from</th>
+          <td>2018</td>
+        </tr>
+        <tr>
+          <th>experience_to</th>
+          <td>2021</td>
+        </tr>
       </tbody>
     </table>
   </div>
diff --git a/app/views/extranet/organizations/show.html.erb b/app/views/extranet/organizations/show.html.erb
index 0794ba8fd..b2137522e 100644
--- a/app/views/extranet/organizations/show.html.erb
+++ b/app/views/extranet/organizations/show.html.erb
@@ -3,3 +3,14 @@
 <header class="mb-5">
   <h1><%= @organization %></h1>
 </header>
+
+<ul>
+  <% @organization.experiences.ordered.each do |experience| %>
+    <li>
+      <%= link_to experience.person, experience.person %>
+      <%= experience.description %>
+      <%= experience.from_year %>
+      <%= experience.to_year %>
+    </li>
+  <% end %>
+</ul>
diff --git a/app/views/extranet/persons/show.html.erb b/app/views/extranet/persons/show.html.erb
index 9cedca1d9..93deb12bd 100644
--- a/app/views/extranet/persons/show.html.erb
+++ b/app/views/extranet/persons/show.html.erb
@@ -18,6 +18,16 @@
 <div class="row">
   <div class="col-md-6">
     <p><%= @person.biography %></p>
+    <ul>
+      <% @person.experiences.ordered.each do |experience| %>
+        <li>
+          <%= experience.description %>
+          <%= experience.from_year %>
+          <%= experience.to_year %>
+          <%= link_to experience.organization, experience.organization %>
+        </li>
+      <% end %>
+    </ul>
   </div>
   <div class="offset-md-3 col-md-3">
     <dl>
diff --git a/db/migrate/20220413203256_create_university_person_experiences.rb b/db/migrate/20220413203256_create_university_person_experiences.rb
new file mode 100644
index 000000000..45b366a82
--- /dev/null
+++ b/db/migrate/20220413203256_create_university_person_experiences.rb
@@ -0,0 +1,14 @@
+class CreateUniversityPersonExperiences < ActiveRecord::Migration[6.1]
+  def change
+    create_table :university_person_experiences, id: :uuid do |t|
+      t.references :university, null: false, foreign_key: true, type: :uuid
+      t.references :person, null: false, foreign_key: {to_table: :university_people}, type: :uuid
+      t.references :organization, null: false, foreign_key: {to_table: :university_organizations}, type: :uuid
+      t.text :description
+      t.integer :from_year
+      t.integer :to_year
+
+      t.timestamps
+    end
+  end
+end
diff --git a/db/schema.rb b/db/schema.rb
index 319a9edd7..66fe4a4ce 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_04_13_071151) do
+ActiveRecord::Schema.define(version: 2022_04_13_203256) do
 
   # These are extensions that must be enabled in order to support this database
   enable_extension "pgcrypto"
@@ -687,6 +687,20 @@ ActiveRecord::Schema.define(version: 2022_04_13_071151) do
     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
+    t.uuid "organization_id", null: false
+    t.text "description"
+    t.integer "from_year"
+    t.integer "to_year"
+    t.datetime "created_at", precision: 6, null: false
+    t.datetime "updated_at", precision: 6, null: false
+    t.index ["organization_id"], name: "index_university_person_experiences_on_organization_id"
+    t.index ["person_id"], name: "index_university_person_experiences_on_person_id"
+    t.index ["university_id"], name: "index_university_person_experiences_on_university_id"
+  end
+
   create_table "university_person_involvements", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
     t.uuid "university_id", null: false
     t.uuid "person_id", null: false
@@ -831,6 +845,9 @@ ActiveRecord::Schema.define(version: 2022_04_13_071151) do
   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"
   add_foreign_key "university_person_involvements", "universities"
   add_foreign_key "university_person_involvements", "university_people", column: "person_id"
   add_foreign_key "university_roles", "universities"
diff --git a/test/fixtures/university/person/experiences.yml b/test/fixtures/university/person/experiences.yml
new file mode 100644
index 000000000..8dfb3071e
--- /dev/null
+++ b/test/fixtures/university/person/experiences.yml
@@ -0,0 +1,43 @@
+# == Schema Information
+#
+# Table name: university_person_experiences
+#
+#  id              :uuid             not null, primary key
+#  description     :text
+#  from_year       :integer
+#  to_year         :integer
+#  created_at      :datetime         not null
+#  updated_at      :datetime         not null
+#  organization_id :uuid             not null, indexed
+#  person_id       :uuid             not null, indexed
+#  university_id   :uuid             not null, indexed
+#
+# Indexes
+#
+#  index_university_person_experiences_on_organization_id  (organization_id)
+#  index_university_person_experiences_on_person_id        (person_id)
+#  index_university_person_experiences_on_university_id    (university_id)
+#
+# Foreign Keys
+#
+#  fk_rails_18125d90df  (person_id => university_people.id)
+#  fk_rails_38aaa18a3b  (organization_id => university_organizations.id)
+#  fk_rails_923d0b71fd  (university_id => universities.id)
+#
+# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
+
+one:
+  university: one
+  person: one
+  organization: one
+  description: MyText
+  from_year: 1
+  to_year: 
+
+two:
+  university: two
+  person: two
+  organization: two
+  description: MyText
+  from_year: 1
+  to_year: 
diff --git a/test/models/university/person/experience_test.rb b/test/models/university/person/experience_test.rb
new file mode 100644
index 000000000..9d0053000
--- /dev/null
+++ b/test/models/university/person/experience_test.rb
@@ -0,0 +1,33 @@
+# == Schema Information
+#
+# Table name: university_person_experiences
+#
+#  id              :uuid             not null, primary key
+#  description     :text
+#  from_year       :integer
+#  to_year         :integer
+#  created_at      :datetime         not null
+#  updated_at      :datetime         not null
+#  organization_id :uuid             not null, indexed
+#  person_id       :uuid             not null, indexed
+#  university_id   :uuid             not null, indexed
+#
+# Indexes
+#
+#  index_university_person_experiences_on_organization_id  (organization_id)
+#  index_university_person_experiences_on_person_id        (person_id)
+#  index_university_person_experiences_on_university_id    (university_id)
+#
+# Foreign Keys
+#
+#  fk_rails_18125d90df  (person_id => university_people.id)
+#  fk_rails_38aaa18a3b  (organization_id => university_organizations.id)
+#  fk_rails_923d0b71fd  (university_id => universities.id)
+#
+require "test_helper"
+
+class University::Person::ExperienceTest < ActiveSupport::TestCase
+  # test "the truth" do
+  #   assert true
+  # end
+end
-- 
GitLab