From fadc112b08a537d72afeda1a29ab663485d2b573 Mon Sep 17 00:00:00 2001 From: pabois Date: Fri, 13 Oct 2023 14:29:35 +0200 Subject: [PATCH 1/3] 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 @@ def low_sms_credits(university, credits) 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 @@ def user_is_contact? 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 @@
-

<%= t('imports.initiated_by') %> <%= link_to_if can?(:read, @import.user), @import.user, [:admin, @import.user] %>

+

+ <%= 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 %> +

<% if @import.file.attached? %>

<%= link_to t('download_with_size', size: number_to_human_size(@import.file.byte_size)), url_for(@import.file), class: button_classes %>

<% 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) %> -
-<%= t('mailers.notifications.low_sms_credits.body_2_html', link: 'https://app.sendinblue.com/billing/addon/customize/sms') %> \ No newline at end of file +

<%= t('mailers.notifications.low_sms_credits.body_1_html', credits: @credits) %>

+

<%= t('mailers.notifications.low_sms_credits.body_2_html', link: 'https://app.sendinblue.com/billing/addon/customize/sms') %>

+

<%= t('mailers.yours') %>

\ 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 @@ +

<%= t('mailers.notifications.new_registration.body_1_html', username: @user.to_s) %>

+

<%= t('mailers.notifications.new_registration.body_2_html', link: admin_user_url(@user)) %>

+

<%= t('mailers.yours') %>

\ 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 @@

<%= t('mailers.notifications.website_invalid_access_token.text_line_1_html', website: @website) %>

-

<%= t('mailers.notifications.website_invalid_access_token.text_line_2_html', url: @url) %>

<%= t('mailers.yours') %>

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 example file @@ -276,6 +277,10 @@ en: body_1_html: "Warning, your SMS credits are low: %{credits} credits remaining!" body_2_html: "Click here to refull." subject: "Osuny - Low SMS Credits (%{credits})" + new_registration: + body_1_html: "A new user (%{username}) just registered." + body_2_html: "Click here 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 fichier d'exemple @@ -276,10 +277,14 @@ fr: body_1_html: "Attention, vos crédits SMS sont bas : %{credits} crédits restants !" body_2_html: "Cliquez ici 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 ici 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 ici. + text_line_2_html: "Pour résoudre ce problème, veuillez renseigner un nouveau jeton d'accès en cliquant ici." 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 @@ 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 @@ 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 @@ 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 @@ 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 @@ def low_sms_credits 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 From 1ea2ad4d289f781e0565020a07dc6327277ed2fc Mon Sep 17 00:00:00 2001 From: pabois Date: Fri, 13 Oct 2023 14:32:31 +0200 Subject: [PATCH 2/3] fix --- .../mailers/notifications/website_invalid_access_token.html.erb | 1 + 1 file changed, 1 insertion(+) 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 402fe3cdb..dc65ffc75 100644 --- a/app/views/mailers/notifications/website_invalid_access_token.html.erb +++ b/app/views/mailers/notifications/website_invalid_access_token.html.erb @@ -1,2 +1,3 @@

<%= t('mailers.notifications.website_invalid_access_token.text_line_1_html', website: @website) %>

+

<%= t('mailers.notifications.website_invalid_access_token.text_line_2_html', url: @url) %>

<%= t('mailers.yours') %>

From 8d6c683cb6d54c7ac310b09989653285ad33b521 Mon Sep 17 00:00:00 2001 From: pabois Date: Fri, 13 Oct 2023 17:19:48 +0200 Subject: [PATCH 3/3] updates --- app/views/mailers/notifications/low_sms_credits.html.erb | 2 +- config/locales/en.yml | 2 +- config/locales/fr.yml | 2 +- db/migrate/20231013090313_change_imports_user_nil.rb | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/app/views/mailers/notifications/low_sms_credits.html.erb b/app/views/mailers/notifications/low_sms_credits.html.erb index 4814cbc30..3bf50e278 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) %>

-

<%= t('mailers.notifications.low_sms_credits.body_2_html', link: 'https://app.sendinblue.com/billing/addon/customize/sms') %>

+

<%= t('mailers.notifications.low_sms_credits.body_2_html', link: 'https://app.brevo.com/billing/account/customize/message-credits') %>

<%= t('mailers.yours') %>

\ No newline at end of file diff --git a/config/locales/en.yml b/config/locales/en.yml index 231cd80d1..95e5cba29 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -284,7 +284,7 @@ en: 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." - text_line_2_html: "To solve this issue, please fill in a new access token by clicking here." + text_line_2_html: "To solve this issue, please fill in a new access token by clicking here." yours: Yours. menu: admin: Admin diff --git a/config/locales/fr.yml b/config/locales/fr.yml index e8d5a2e1f..a9947df5c 100644 --- a/config/locales/fr.yml +++ b/config/locales/fr.yml @@ -284,7 +284,7 @@ fr: 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 ici." + text_line_2_html: "Pour résoudre ce problème, veuillez renseigner un nouveau jeton d'accès en cliquant ici." 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 index 495d4d27b..00404e407 100644 --- a/db/migrate/20231013090313_change_imports_user_nil.rb +++ b/db/migrate/20231013090313_change_imports_user_nil.rb @@ -1,5 +1,5 @@ class ChangeImportsUserNil < ActiveRecord::Migration[7.0] def change - change_column :imports, :user_id, :uuid, null: true + change_column_null :imports, :user_id, true end end