From fadc112b08a537d72afeda1a29ab663485d2b573 Mon Sep 17 00:00:00 2001
From: pabois <pierreandre.boissinot@noesya.coop>
Date: Fri, 13 Oct 2023 14:29:35 +0200
Subject: [PATCH] add new mailer and fix old ones

---
 app/mailers/notification_mailer.rb                     | 10 ++++++++++
 app/models/import.rb                                   |  5 ++---
 app/models/user.rb                                     |  1 +
 app/models/user/with_registration_context.rb           |  6 ++++++
 app/views/admin/imports/show.html.erb                  |  9 ++++++++-
 .../mailers/notifications/low_sms_credits.html.erb     |  6 +++---
 .../mailers/notifications/new_registration.html.erb    |  3 +++
 .../website_invalid_access_token.html.erb              |  1 -
 config/locales/en.yml                                  |  5 +++++
 config/locales/fr.yml                                  |  7 ++++++-
 db/migrate/20231013090313_change_imports_user_nil.rb   |  5 +++++
 db/schema.rb                                           |  9 +++++----
 test/mailers/previews/notification_mailer_preview.rb   |  5 +++++
 13 files changed, 59 insertions(+), 13 deletions(-)
 create mode 100644 app/views/mailers/notifications/new_registration.html.erb
 create mode 100644 db/migrate/20231013090313_change_imports_user_nil.rb

diff --git a/app/mailers/notification_mailer.rb b/app/mailers/notification_mailer.rb
index c4c9aa3b1..432586240 100644
--- a/app/mailers/notification_mailer.rb
+++ b/app/mailers/notification_mailer.rb
@@ -34,8 +34,18 @@ class NotificationMailer < ApplicationMailer
     merge_with_university_infos(university, {})
     @credits = credits.to_i
     mails = university.users.server_admin.pluck(:email)
+    I18n.locale = university.default_language.iso_code
     subject = t('mailers.notifications.low_sms_credits.subject', credits: @credits)
     mail(from: university.mail_from[:full], to: mails, subject: subject)
   end
+  
+  def new_registration(university, user)
+    merge_with_university_infos(university, {})
+    @user = user
+    mails = university.users.where.not(id: @user.id).where(role: [:server_admin, :admin]).pluck(:email)
+    I18n.locale = university.default_language.iso_code
+    subject = t('mailers.notifications.new_registration.subject', mail: @user.email)
+    mail(from: university.mail_from[:full], to: mails, subject: subject)
+  end
 
 end
diff --git a/app/models/import.rb b/app/models/import.rb
index d3ebfb313..6e4c56d27 100644
--- a/app/models/import.rb
+++ b/app/models/import.rb
@@ -10,7 +10,7 @@
 #  created_at        :datetime         not null
 #  updated_at        :datetime         not null
 #  university_id     :uuid             not null, indexed
-#  user_id           :uuid             not null, indexed
+#  user_id           :uuid             indexed
 #
 # Indexes
 #
@@ -24,11 +24,10 @@
 #
 class Import < ApplicationRecord
   belongs_to :university
-  belongs_to :user
+  belongs_to :user, optional: true
 
   has_one_attached_deletable :file
 
-
   enum kind: { organizations: 0, alumni_cohorts: 1, people_experiences: 2 }, _prefix: :kind
   enum status: { pending: 0, finished: 1, finished_with_errors: 2 }
 
diff --git a/app/models/user.rb b/app/models/user.rb
index 3da9b8ced..6e92659a9 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -72,6 +72,7 @@ class User < ApplicationRecord
 
   belongs_to :language
   has_many :university_people, class_name: 'University::Person', dependent: :nullify
+  has_many :imports, class_name: 'Import', dependent: :nullify
 
   scope :ordered, -> { order(:last_name, :first_name) }
   scope :for_language, -> (language_id) { where(language_id: language_id) }
diff --git a/app/models/user/with_registration_context.rb b/app/models/user/with_registration_context.rb
index 2b994b4dd..a06d5dda1 100644
--- a/app/models/user/with_registration_context.rb
+++ b/app/models/user/with_registration_context.rb
@@ -6,6 +6,8 @@ module User::WithRegistrationContext
 
     validate :extranet_access, on: :create, if: -> { registration_context.is_a?(Communication::Extranet) }
 
+    after_create :send_notification_to_admins, unless: -> { registration_context.is_a?(Communication::Extranet) }
+
     private
 
     def extranet_access
@@ -30,5 +32,9 @@ module User::WithRegistrationContext
       registration_context.has_feature?(:contacts) && registration_context.connected_people.where(email: email).any?
     end
 
+    def send_notification_to_admins
+      NotificationMailer.new_registration(university, self).deliver_later
+    end
+
   end
 end
diff --git a/app/views/admin/imports/show.html.erb b/app/views/admin/imports/show.html.erb
index 468671382..813803e88 100644
--- a/app/views/admin/imports/show.html.erb
+++ b/app/views/admin/imports/show.html.erb
@@ -2,7 +2,14 @@
 
 <div class="row">
   <div class="col-md-6">
-    <p><%= t('imports.initiated_by') %> <%= link_to_if can?(:read, @import.user), @import.user, [:admin, @import.user] %></p>
+    <p>
+      <%= t('imports.initiated_by') %> 
+      <% if @import.user %>
+        <%= link_to_if can?(:read, @import.user), @import.user, [:admin, @import.user] %>
+      <% else %>
+        <%= t('imports.deleted_user') %> 
+      <% end %>
+    </p>
     <% if @import.file.attached? %>
       <p><%= link_to t('download_with_size', size: number_to_human_size(@import.file.byte_size)), url_for(@import.file), class: button_classes  %></p>
     <% end %>
diff --git a/app/views/mailers/notifications/low_sms_credits.html.erb b/app/views/mailers/notifications/low_sms_credits.html.erb
index 1407e0440..4814cbc30 100644
--- a/app/views/mailers/notifications/low_sms_credits.html.erb
+++ b/app/views/mailers/notifications/low_sms_credits.html.erb
@@ -1,3 +1,3 @@
-<%= t('mailers.notifications.low_sms_credits.body_1_html', credits: @credits) %>
-<br>
-<%= t('mailers.notifications.low_sms_credits.body_2_html', link: 'https://app.sendinblue.com/billing/addon/customize/sms') %>
\ No newline at end of file
+<p><%= t('mailers.notifications.low_sms_credits.body_1_html', credits: @credits) %></p>
+<p><%= t('mailers.notifications.low_sms_credits.body_2_html', link: 'https://app.sendinblue.com/billing/addon/customize/sms') %></p>
+<p><%= t('mailers.yours') %></p>
\ No newline at end of file
diff --git a/app/views/mailers/notifications/new_registration.html.erb b/app/views/mailers/notifications/new_registration.html.erb
new file mode 100644
index 000000000..14e3fdb0e
--- /dev/null
+++ b/app/views/mailers/notifications/new_registration.html.erb
@@ -0,0 +1,3 @@
+<p><%= t('mailers.notifications.new_registration.body_1_html', username: @user.to_s) %></p>
+<p><%= t('mailers.notifications.new_registration.body_2_html', link: admin_user_url(@user)) %></p>
+<p><%= t('mailers.yours') %></p>
\ No newline at end of file
diff --git a/app/views/mailers/notifications/website_invalid_access_token.html.erb b/app/views/mailers/notifications/website_invalid_access_token.html.erb
index dc65ffc75..402fe3cdb 100644
--- a/app/views/mailers/notifications/website_invalid_access_token.html.erb
+++ b/app/views/mailers/notifications/website_invalid_access_token.html.erb
@@ -1,3 +1,2 @@
 <p><%= t('mailers.notifications.website_invalid_access_token.text_line_1_html', website: @website) %></p>
-<p><%= t('mailers.notifications.website_invalid_access_token.text_line_2_html', url: @url) %></p>
 <p><%= t('mailers.yours') %></p>
diff --git a/config/locales/en.yml b/config/locales/en.yml
index b7df966e0..231cd80d1 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -236,6 +236,7 @@ en:
   hello: "Hello %{name}!"
   home: Home
   imports:
+    deleted_user: Deleted user
     error_msg: "Line %{line}: %{error}"
     errors: Errors
     example_file_html: Download an <a href="%{link}" target="_blank">example file</a>
@@ -276,6 +277,10 @@ en:
         body_1_html: "Warning, your SMS credits are low: %{credits} credits remaining!"
         body_2_html: "Click <a href=\"%{link}\" target=\"_blank\" style=\"color: #c72b43;\">here</a> to refull."
         subject: "Osuny - Low SMS Credits (%{credits})"
+      new_registration:
+        body_1_html: "A new user (%{username}) just registered."
+        body_2_html: "Click <a href=\"%{link}\" target=\"_blank\" style=\"color: #c72b43;\">here</a> to see the account."
+        subject: "A new user just registered: %{mail}"
       website_invalid_access_token:
         subject: "Expired access token for \"%{website}\""
         text_line_1_html: "The access token used for the website \"%{website}\" has expired and does not allow the website to be updated anymore."
diff --git a/config/locales/fr.yml b/config/locales/fr.yml
index bd8323c6f..e8d5a2e1f 100644
--- a/config/locales/fr.yml
+++ b/config/locales/fr.yml
@@ -236,6 +236,7 @@ fr:
   hello: "Bonjour %{name} !"
   home: Accueil
   imports:
+    deleted_user: Utilisateur supprimé
     error_msg: "Ligne %{line} : %{error}"
     errors: Erreurs
     example_file_html: Télécharger un <a href="%{link}" target="_blank">fichier d'exemple</a>
@@ -276,10 +277,14 @@ fr:
         body_1_html: "Attention, vos crédits SMS sont bas : %{credits} crédits restants !"
         body_2_html: "Cliquez <a href=\"%{link}\" target=\"_blank\" style=\"color: #c72b43;\">ici</a> pour recharger le compte."
         subject: "Osuny - Credits SMS bas (%{credits})"
+      new_registration:
+        body_1_html: "Un nouvel utilisateur (%{username}) vient de s'enregistrer."
+        body_2_html: "Cliquez <a href=\"%{link}\" target=\"_blank\" style=\"color: #c72b43;\">ici</a> pour voir son compte."
+        subject: "Un nouvel utilisateur vient de s'inscrire : %{mail}"
       website_invalid_access_token:
         subject: Jeton d'accès expiré pour « %{website} »
         text_line_1_html: Le jeton d'accès utilisé pour le site « %{website} » a expiré et ne permet plus la mise à jour du site.
-        text_line_2_html: Pour résoudre ce problème, veuillez renseigner un nouveau jeton d'accès en cliquant <a href=\"%{url}\">ici</a>.
+        text_line_2_html: "Pour résoudre ce problème, veuillez renseigner un nouveau jeton d'accès en cliquant <a href=\"%{url}\">ici</a>."
     yours: Cordialement.
   menu:
     admin: Admin
diff --git a/db/migrate/20231013090313_change_imports_user_nil.rb b/db/migrate/20231013090313_change_imports_user_nil.rb
new file mode 100644
index 000000000..495d4d27b
--- /dev/null
+++ b/db/migrate/20231013090313_change_imports_user_nil.rb
@@ -0,0 +1,5 @@
+class ChangeImportsUserNil < ActiveRecord::Migration[7.0]
+  def change
+    change_column :imports, :user_id, :uuid, null: true
+  end
+end
diff --git a/db/schema.rb b/db/schema.rb
index eb300673c..405b2a002 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[7.0].define(version: 2023_10_06_145950) do
+ActiveRecord::Schema[7.0].define(version: 2023_10_13_090313) do
   # These are extensions that must be enabled in order to support this database
   enable_extension "pgcrypto"
   enable_extension "plpgsql"
@@ -106,8 +106,8 @@ ActiveRecord::Schema[7.0].define(version: 2023_10_06_145950) do
     t.datetime "updated_at", null: false
     t.string "title"
     t.boolean "published", default: true
-    t.uuid "heading_id"
     t.uuid "communication_website_id"
+    t.uuid "heading_id"
     t.string "migration_identifier"
     t.index ["about_type", "about_id"], name: "index_communication_website_blocks_on_about"
     t.index ["communication_website_id"], name: "index_communication_blocks_on_communication_website_id"
@@ -229,6 +229,7 @@ ActiveRecord::Schema[7.0].define(version: 2023_10_06_145950) do
     t.text "home_sentence"
     t.text "sass"
     t.text "css"
+    t.boolean "allow_experiences_modification", default: true
     t.index ["about_type", "about_id"], name: "index_communication_extranets_on_about"
     t.index ["university_id"], name: "index_communication_extranets_on_university_id"
   end
@@ -389,7 +390,7 @@ ActiveRecord::Schema[7.0].define(version: 2023_10_06_145950) do
     t.index ["university_id"], name: "index_communication_website_pages_on_university_id"
   end
 
-  create_table "communication_website_permalinks", id: :uuid, default: -> { "public.gen_random_uuid()" }, force: :cascade do |t|
+  create_table "communication_website_permalinks", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
     t.uuid "university_id", null: false
     t.uuid "website_id", null: false
     t.string "about_type", null: false
@@ -650,7 +651,7 @@ ActiveRecord::Schema[7.0].define(version: 2023_10_06_145950) do
     t.integer "kind"
     t.integer "status", default: 0
     t.uuid "university_id", null: false
-    t.uuid "user_id", null: false
+    t.uuid "user_id"
     t.datetime "created_at", null: false
     t.datetime "updated_at", null: false
     t.index ["university_id"], name: "index_imports_on_university_id"
diff --git a/test/mailers/previews/notification_mailer_preview.rb b/test/mailers/previews/notification_mailer_preview.rb
index d5557f836..eb971f394 100644
--- a/test/mailers/previews/notification_mailer_preview.rb
+++ b/test/mailers/previews/notification_mailer_preview.rb
@@ -23,5 +23,10 @@ class NotificationMailerPreview < BaseMailerPreview
     NotificationMailer.low_sms_credits(university, credits)
   end
 
+  # Preview this email at http://localhost:3000/rails/mailers/notification_mailer/new_registration
+  def new_registration
+    NotificationMailer.new_registration(university, user)
+  end
+
 
 end
-- 
GitLab