From f5292daf6478aa9242631eff158bd415a6bcfa4f Mon Sep 17 00:00:00 2001
From: pabois <pierreandre.boissinot@noesya.coop>
Date: Thu, 28 Oct 2021 14:29:02 +0200
Subject: [PATCH] imported authors

---
 .../communication/website/imported/author.rb  | 74 +++++++++++++++++++
 .../communication/website/imported/website.rb | 11 +++
 app/services/wordpress.rb                     |  4 +
 ...e_communication_website_imported_author.rb | 28 +++++++
 db/schema.rb                                  | 21 +++++-
 5 files changed, 137 insertions(+), 1 deletion(-)
 create mode 100644 app/models/communication/website/imported/author.rb
 create mode 100644 db/migrate/20211028120556_create_communication_website_imported_author.rb

diff --git a/app/models/communication/website/imported/author.rb b/app/models/communication/website/imported/author.rb
new file mode 100644
index 000000000..8040783bd
--- /dev/null
+++ b/app/models/communication/website/imported/author.rb
@@ -0,0 +1,74 @@
+# == Schema Information
+#
+# Table name: communication_website_imported_authors
+#
+#  id            :uuid             not null, primary key
+#  data          :jsonb
+#  description   :text
+#  identifier    :string
+#  name          :string
+#  slug          :string
+#  created_at    :datetime         not null
+#  updated_at    :datetime         not null
+#  author_id     :uuid             not null
+#  university_id :uuid             not null
+#  website_id    :uuid             not null
+#
+# Indexes
+#
+#  idx_communication_website_imported_auth_on_author      (author_id)
+#  idx_communication_website_imported_auth_on_university  (university_id)
+#  idx_communication_website_imported_auth_on_website     (website_id)
+#
+# Foreign Keys
+#
+#  fk_rails_...  (author_id => communication_website_authors.id)
+#  fk_rails_...  (university_id => universities.id)
+#  fk_rails_...  (website_id => communication_website_imported_websites.id)
+#
+class Communication::Website::Imported::Author < ApplicationRecord
+
+  belongs_to :university
+  belongs_to :website,
+             class_name: 'Communication::Website::Imported::Website'
+  belongs_to :author,
+             class_name: 'Communication::Website::Author',
+             optional: true
+
+  before_validation :sync
+
+  default_scope { order(:name) }
+
+  def data=(value)
+    super value
+    self.slug = value['slug']
+    self.name = value['name']
+    self.description = value['description']
+  end
+
+  def to_s
+    "#{name}"
+  end
+
+  protected
+
+  def sync
+    if author.nil?
+      self.author = Communication::Website::Author.new university: university,
+                                                   website: website.website # Real website, not imported website
+      self.author.last_name = "Doe" # No title yet
+      self.author.first_name = "John" # No title yet
+      self.author.save
+    end
+    puts "Update author #{author.id}"
+    sanitized_name = Wordpress.clean_string(self.name.to_s).split(' ')
+    sanitized_biography = Wordpress.clean_string description.to_s
+    unless sanitized_name.blank?
+      author.first_name = sanitized_name.first
+      author.last_name = sanitized_name[1..10].join(' ')
+    end
+    author.slug = slug
+    author.biography = sanitized_biography unless sanitized_biography.blank?
+    author.save
+  end
+end
diff --git a/app/models/communication/website/imported/website.rb b/app/models/communication/website/imported/website.rb
index 2a168e236..a740032f9 100644
--- a/app/models/communication/website/imported/website.rb
+++ b/app/models/communication/website/imported/website.rb
@@ -23,6 +23,8 @@ class Communication::Website::Imported::Website < ApplicationRecord
   belongs_to :university
   belongs_to :website,
              class_name: 'Communication::Website'
+  has_many   :authors,
+             class_name: 'Communication::Website::Imported::Author'
   has_many   :categories,
              class_name: 'Communication::Website::Imported::Category'
   has_many   :media,
@@ -33,6 +35,7 @@ class Communication::Website::Imported::Website < ApplicationRecord
              class_name: 'Communication::Website::Imported::Post'
 
   def run!
+    sync_authors
     sync_categories
     sync_media
     sync_pages
@@ -46,6 +49,14 @@ class Communication::Website::Imported::Website < ApplicationRecord
     @wordpress ||= Wordpress.new website.domain_url
   end
 
+  def sync_authors
+    wordpress.authors.each do |data|
+      author = authors.where(university: university, identifier: data['id']).first_or_initialize
+      author.data = data
+      author.save
+    end
+  end
+
   def sync_categories
     wordpress.categories.each do |data|
       category = categories.where(university: university, identifier: data['id']).first_or_initialize
diff --git a/app/services/wordpress.rb b/app/services/wordpress.rb
index ad6add34d..627871fb9 100644
--- a/app/services/wordpress.rb
+++ b/app/services/wordpress.rb
@@ -51,6 +51,10 @@ class Wordpress
     @domain = domain
   end
 
+  def authors
+    load "#{domain}/wp-json/wp/v2/users"
+  end
+
   def categories
     load "#{domain}/wp-json/wp/v2/categories"
   end
diff --git a/db/migrate/20211028120556_create_communication_website_imported_author.rb b/db/migrate/20211028120556_create_communication_website_imported_author.rb
new file mode 100644
index 000000000..12195c70b
--- /dev/null
+++ b/db/migrate/20211028120556_create_communication_website_imported_author.rb
@@ -0,0 +1,28 @@
+class CreateCommunicationWebsiteImportedAuthor < ActiveRecord::Migration[6.1]
+  def change
+    create_table :communication_website_imported_authors, id: :uuid do |t|
+      t.references :university,
+                   null: false,
+                   foreign_key: true,
+                   type: :uuid,
+                   index: { name: 'idx_communication_website_imported_auth_on_university' }
+      t.references :website,
+                   null: false,
+                   foreign_key: { to_table: :communication_website_imported_websites },
+                   type: :uuid,
+                   index: { name: 'idx_communication_website_imported_auth_on_website' }
+      t.references :author,
+                   null: false,
+                   foreign_key: { to_table: :communication_website_authors },
+                   type: :uuid,
+                   index: { name: 'idx_communication_website_imported_auth_on_author' }
+
+      t.string :name
+      t.text :description
+      t.string :slug
+      t.string :identifier
+      t.jsonb :data
+      t.timestamps
+    end
+  end
+end
diff --git a/db/schema.rb b/db/schema.rb
index cce82cf82..3a95f13d6 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: 2021_10_28_100510) do
+ActiveRecord::Schema.define(version: 2021_10_28_120556) do
 
   # These are extensions that must be enabled in order to support this database
   enable_extension "pgcrypto"
@@ -114,6 +114,22 @@ ActiveRecord::Schema.define(version: 2021_10_28_100510) do
     t.index ["communication_website_post_id", "communication_website_category_id"], name: "post_category"
   end
 
+  create_table "communication_website_imported_authors", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
+    t.uuid "university_id", null: false
+    t.uuid "website_id", null: false
+    t.uuid "author_id", null: false
+    t.string "name"
+    t.text "description"
+    t.string "slug"
+    t.string "identifier"
+    t.jsonb "data"
+    t.datetime "created_at", precision: 6, null: false
+    t.datetime "updated_at", precision: 6, null: false
+    t.index ["author_id"], name: "idx_communication_website_imported_auth_on_author"
+    t.index ["university_id"], name: "idx_communication_website_imported_auth_on_university"
+    t.index ["website_id"], name: "idx_communication_website_imported_auth_on_website"
+  end
+
   create_table "communication_website_imported_categories", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
     t.uuid "university_id", null: false
     t.uuid "website_id", null: false
@@ -448,6 +464,9 @@ ActiveRecord::Schema.define(version: 2021_10_28_100510) do
   add_foreign_key "communication_website_categories", "communication_website_categories", column: "parent_id"
   add_foreign_key "communication_website_categories", "communication_websites"
   add_foreign_key "communication_website_categories", "universities"
+  add_foreign_key "communication_website_imported_authors", "communication_website_authors", column: "author_id"
+  add_foreign_key "communication_website_imported_authors", "communication_website_imported_websites", column: "website_id"
+  add_foreign_key "communication_website_imported_authors", "universities"
   add_foreign_key "communication_website_imported_categories", "communication_website_categories", column: "category_id"
   add_foreign_key "communication_website_imported_categories", "communication_website_imported_websites", column: "website_id"
   add_foreign_key "communication_website_imported_categories", "universities"
-- 
GitLab