From 54e1f32d03d1da6a47c537f79f103c632a5fbc1d Mon Sep 17 00:00:00 2001 From: pabois <pierreandre.boissinot@noesya.coop> Date: Mon, 23 May 2022 19:17:51 +0200 Subject: [PATCH] alumni imports + notification mailer --- .../university/alumni/imports_controller.rb | 17 ++++++++++----- app/mailers/notification_mailer.rb | 16 ++++++++++++++ app/models/import.rb | 10 ++++++--- .../university/alumni/imports/index.html.erb | 21 +++++-------------- .../university/alumni/imports/new.html.erb | 16 +++++++------- .../university/alumni/imports/show.html.erb | 7 ------- .../organizations/imports/new.html.erb | 8 ++++--- .../mailers/notifications/import.html.erb | 15 +++++++++++++ config/locales/en.yml | 13 +++++++++++- config/locales/fr.yml | 13 +++++++++++- config/locales/university/en.yml | 5 ++++- config/locales/university/fr.yml | 5 ++++- 12 files changed, 101 insertions(+), 45 deletions(-) create mode 100644 app/mailers/notification_mailer.rb delete mode 100644 app/views/admin/university/alumni/imports/show.html.erb create mode 100644 app/views/mailers/notifications/import.html.erb diff --git a/app/controllers/admin/university/alumni/imports_controller.rb b/app/controllers/admin/university/alumni/imports_controller.rb index 6de670ed8..1743ace14 100644 --- a/app/controllers/admin/university/alumni/imports_controller.rb +++ b/app/controllers/admin/university/alumni/imports_controller.rb @@ -1,14 +1,18 @@ class Admin::University::Alumni::ImportsController < Admin::University::ApplicationController - load_and_authorize_resource class: University::Person::Alumnus::Import, + load_and_authorize_resource class: Import, through: :current_university, - through_association: :person_alumnus_imports + through_association: :imports + + has_scope :for_status def index + @imports = apply_scopes(@imports.kind_alumni).ordered.page(params[:page]) breadcrumb end def show breadcrumb + render 'admin/imports/show' end def new @@ -16,11 +20,14 @@ class Admin::University::Alumni::ImportsController < Admin::University::Applicat end def create + @import.kind = :alumni @import.university = current_university @import.user = current_user if @import.save - redirect_to [:admin, @import], notice: "Import was successfully created." + redirect_to admin_university_alumni_import_path(@import), + notice: t('admin.successfully_created_html', model: @import.to_s) else + breadcrumb render :new, status: :unprocessable_entity end end @@ -31,7 +38,7 @@ class Admin::University::Alumni::ImportsController < Admin::University::Applicat super add_breadcrumb University::Person::Alumnus.model_name.human(count: 2), admin_university_alumni_path - add_breadcrumb University::Person::Alumnus::Import.model_name.human(count: 2), + add_breadcrumb Import.model_name.human(count: 2), admin_university_alumni_imports_path return unless @import @import.persisted? ? add_breadcrumb(@import, admin_university_alumni_import_path(@import)) @@ -39,7 +46,7 @@ class Admin::University::Alumni::ImportsController < Admin::University::Applicat end def import_params - params.require(:university_person_alumnus_import) + params.require(:import) .permit(:file) end end diff --git a/app/mailers/notification_mailer.rb b/app/mailers/notification_mailer.rb new file mode 100644 index 000000000..1b8521d1b --- /dev/null +++ b/app/mailers/notification_mailer.rb @@ -0,0 +1,16 @@ +class NotificationMailer < ApplicationMailer + helper :application # gives access to all helpers defined within `application_helper`. + default template_path: 'mailers/notifications' + + def import(import) + merge_with_university_infos(import.university, {}) + I18n.locale = import.user.language.iso_code + subject = import.finished_with_errors? ? t('mailers.notifications.import.subject_with_errors') : + t('mailers.notifications.import.subject_without_errors') + + @import = import + @url = send(import.url_pattern, import) + mail(from: import.university.mail_from[:full], to: import.user.email, subject: subject) + end + +end diff --git a/app/models/import.rb b/app/models/import.rb index 0d1181faa..d04d85645 100644 --- a/app/models/import.rb +++ b/app/models/import.rb @@ -56,6 +56,12 @@ class Import < ApplicationRecord super(value) end + def url_pattern + kind_alumni? ? + 'admin_university_alumni_import_url' : + 'admin_university_organizations_import_url' + end + private def file_validation @@ -74,13 +80,11 @@ class Import < ApplicationRecord end def send_mail_to_creator - # TODO - # ImportsMailer.companies(self).deliver_later + NotificationMailer.import(self).deliver_later end def status_changed_from_pending? saved_change_to_status? && status_before_last_save == 'pending' end - end diff --git a/app/views/admin/university/alumni/imports/index.html.erb b/app/views/admin/university/alumni/imports/index.html.erb index e48aa45f4..1003d9497 100644 --- a/app/views/admin/university/alumni/imports/index.html.erb +++ b/app/views/admin/university/alumni/imports/index.html.erb @@ -1,22 +1,11 @@ -<% content_for :title, University::Person::Alumnus::Import.model_name.human(count: 2) %> +<% content_for :title, Import.model_name.human(count: 2) %> -<table class="<%= table_classes %>"> - <thead> - <tr> - <th><%= University::Person::Alumnus::Import.human_attribute_name('name') %></th> - </tr> - </thead> - <tbody> - <% @imports.each do |import| %> - <tr> - <td><%= link_to import, admin_university_alumni_import_path(import) %></td> - </tr> - <% end %> - </tbody> -</table> +<%= render 'filters', current_path: admin_university_alumni_imports_path, filters: @filters if @filters.any? %> + +<%= render 'admin/imports/list', imports: @imports, path_pattern: 'admin_university_alumni_import_path' %> <% content_for :action_bar_right do %> - <%= link_to_if can?(:create, University::Person::Alumnus::Import), + <%= link_to_if can?(:create, University::Person::Alumnus), t('create'), new_admin_university_alumni_import_path, class: button_classes %> diff --git a/app/views/admin/university/alumni/imports/new.html.erb b/app/views/admin/university/alumni/imports/new.html.erb index 84e9abb84..6a5cafff2 100644 --- a/app/views/admin/university/alumni/imports/new.html.erb +++ b/app/views/admin/university/alumni/imports/new.html.erb @@ -1,21 +1,23 @@ -<% content_for :title, University::Organization::Import.model_name.human %> +<% content_for :title, Import.model_name.human %> <div class="row"> <div class="col-md-6"> <p> - Les données doivent être au format csv, comme l'exemple suivant.<br> - La première ligne doit être dédiée aux entêtes.<br> - Les noms des entêtes sont obligatoires et doivent être respectés strictement.<br> - Les champs marqués d'une astérisque sont obligatoires.<br> - Les caractères doivent être encodés en UTF-8.<br> - Les valeurs pour gender peuvent être m (masculin), f (féminin) et n (non binaire). + <%= t('imports.hint_html') %> + <br> + <%= t('university.alumni.import_hint_html') %> </p> <%= simple_form_for @import, url: admin_university_alumni_imports_path do |f| %> + <%= f.error_notification %> <%= f.error_notification message: f.object.errors[:base].to_sentence if f.object.errors[:base].present? %> + <%# as file can be empty the global object can be unset. To prevent crash in controller add an (unused) hidden field %> + <%= f.input :id, as: :hidden %> + <%= f.input :file %> + <% content_for :action_bar_right do %> <%= submit f %> <% end %> diff --git a/app/views/admin/university/alumni/imports/show.html.erb b/app/views/admin/university/alumni/imports/show.html.erb deleted file mode 100644 index d3fec028c..000000000 --- a/app/views/admin/university/alumni/imports/show.html.erb +++ /dev/null @@ -1,7 +0,0 @@ -<% content_for :title, @import %> - -<p><%= @import.lines %> lines</p> - -<%= link_to "#{@import.file.filename} (#{number_to_human_size @import.file.byte_size})", - url_for(@import.file), - class: button_classes %> diff --git a/app/views/admin/university/organizations/imports/new.html.erb b/app/views/admin/university/organizations/imports/new.html.erb index 2ff9088b4..805435dcd 100644 --- a/app/views/admin/university/organizations/imports/new.html.erb +++ b/app/views/admin/university/organizations/imports/new.html.erb @@ -5,15 +5,17 @@ <p> <%= t('imports.hint_html') %> <br> - <%= t('university.import_hint_html') %> + <%= t('university.organization.import_hint_html') %> </p> <%= simple_form_for @import, url: admin_university_organizations_imports_path do |f| %> - <%# as file can be empty the global object can be unset. To prevent crash in controller add an (unused) hidden field %> - <%= f.input :id, as: :hidden %> + <%= f.error_notification %> <%= f.error_notification message: f.object.errors[:base].to_sentence if f.object.errors[:base].present? %> + <%# as file can be empty the global object can be unset. To prevent crash in controller add an (unused) hidden field %> + <%= f.input :id, as: :hidden %> + <%= f.input :file %> <% content_for :action_bar_right do %> diff --git a/app/views/mailers/notifications/import.html.erb b/app/views/mailers/notifications/import.html.erb new file mode 100644 index 000000000..1fef88cdf --- /dev/null +++ b/app/views/mailers/notifications/import.html.erb @@ -0,0 +1,15 @@ +<p><%= t('mailers.notifications.import.text_line_1_html') %></p> +<p><%= t('mailers.notifications.import.text_line_2_html', url: @url) %></p> +<p><%= t('mailers.notifications.import.text_line_3_html', number: @import.number_of_lines) %></p> +<% if @import.finished_with_errors? %> + <p> + <%= t('mailers.notifications.import.text_errors_title') %> + <br/> + <ul> + <% @import.processing_errors.each do |obj| %> + <li><%= t('mailers.notifications.import.text_error_msg', line: obj['line'], error: obj['error']) %></li> + <% end %> + </ul> + </p> +<% end %> +<p><%= t('mailers.yours') %></p> diff --git a/config/locales/en.yml b/config/locales/en.yml index ae85766c0..89f02117d 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -150,7 +150,7 @@ en: import: error_msg: "Line %{line}: %{error}" errors: Errors - hint_html: "File must be an .xlsx excel file.<br>The first line must contain the headers.<br>The hearders are mandatory and must be strictly respected.<br>All characters must be encoded as UTF-8." + hint_html: "File must be an .xlsx excel file.<br>The first line must contain the headers.<br>The hearders are mandatory and must be strictly respected.<br>All characters must be encoded as UTF-8.<br>Fields marked with an asterisk are mandatory." initiated_by: "Initiated by:" number_of_lines: "Number of lines in the file:" status: "Status:" @@ -169,6 +169,17 @@ en: sign_in_with_credentials: Sign in with credentials sign_in_with_sso: Sign in through SSO subtitle: Sign in to your account to continue + mailers: + notifications: + import: + subject_with_errors: Your import has been processed with errors + subject_without_errors: Your import has been processed + text_line_1_html: Your import has been processed. + text_line_2_html: "You can check the result by clicking <a href=\"%{url}\">here</a>." + text_line_3_html: "Number of lines in the file: %{number}." + text_error_msg: "Line %{line}: %{error}" + text_errors_title: "Some errors have occured:" + yours: Yours. menu: admin: Admin profile: Profile diff --git a/config/locales/fr.yml b/config/locales/fr.yml index e034e354a..b4894dbc4 100644 --- a/config/locales/fr.yml +++ b/config/locales/fr.yml @@ -150,7 +150,7 @@ fr: imports: error_msg: "Ligne %{line} : %{error}" errors: Erreurs - hint_html: "Les données doivent être au format xlsx.<br>La première ligne doit être dédiée aux entêtes.<br>Les noms des entêtes sont obligatoires et doivent être respectés strictement.<br>Les caractères doivent être encodés en UTF-8." + hint_html: "Les données doivent être au format xlsx.<br>La première ligne doit être dédiée aux entêtes.<br>Les noms des entêtes sont obligatoires et doivent être respectés strictement.<br>Les caractères doivent être encodés en UTF-8.<br>Les champs marqués d'une astérisque sont obligatoires." initiated_by: "Initié par :" number_of_lines: "Nombre de lignes dans le fichier :" status: "Status :" @@ -169,6 +169,17 @@ fr: sign_in_with_credentials: Se connecter avec ses identifiants sign_in_with_sso: Se connecter en SSO subtitle: Vous devez être authentifié pour continuer + mailers: + notifications: + import: + subject_with_errors: Votre import a bien été traité mais comporte des erreurs + subject_without_errors: Votre import a bien été traité + text_line_1_html: Votre import a bien été traité. + text_line_2_html: "Vous pouvez voir le détail de l'import en cliquant <a href=\"%{url}\">ici</a>." + text_line_3_html: "Nombre de lignes traitées : %{number}." + text_error_msg: "Ligne %{line} : %{error}" + text_errors_title: "Des erreurs sont survenues :" + yours: Cordialement. menu: admin: Admin profile: Profil diff --git a/config/locales/university/en.yml b/config/locales/university/en.yml index be8382d7d..32a353dbc 100644 --- a/config/locales/university/en.yml +++ b/config/locales/university/en.yml @@ -137,12 +137,15 @@ en: non_profit: Association government: Government university: - import_hint_html: "Name field is mandatory.<br>Possible values for kind are: company, non_profit, government.<br>Siren, nic, zipcode and phone fields must have a text format, not numbers.<br>Country field must contain the ISO 3166 code of the country, so to upcase characters (<a href=\"https://en.wikipedia.org/wiki/List_of_ISO_3166_country_codes\" target=\_blank\">list</a>)" + alumni: + import_hint_html: "Possible values for gender are: m (male), f (female) and n (non binary).<br>Phone_professional, phone_personal, mobile and zipcode fields must have a text format, not numbers.<br>Country field must contain the ISO 3166 code of the country, so to upcase characters (<a href=\"https://en.wikipedia.org/wiki/List_of_ISO_3166_country_codes\" target=\_blank\">list</a>)<br>Social_twitter field should have no @." internal_key: Internal Key invoice_informations: Invoice informations manage_alumni: Manage alumni manage_cohorts: Manage cohorts manage_experiences: Manage experiences + organisation: + import_hint_html: "Possible values for kind are: company, non_profit, government.<br>Siren, nic, zipcode and phone fields must have a text format, not numbers.<br>Country field must contain the ISO 3166 code of the country, so to upcase characters (<a href=\"https://en.wikipedia.org/wiki/List_of_ISO_3166_country_codes\" target=\_blank\">list</a>)" person: administrator_roles: Administrator roles experience: diff --git a/config/locales/university/fr.yml b/config/locales/university/fr.yml index 38e9bf9f6..17bda962d 100644 --- a/config/locales/university/fr.yml +++ b/config/locales/university/fr.yml @@ -137,12 +137,15 @@ fr: non_profit: Association government: Structure gouvernementale university: - import_hint_html: "Le champ name est obligatoire.<br>Les valeurs possibles pour kind sont : company, non_profit, government.<br>Les champs siren, nic, zipcode et phone doivent être au format texte, pas nombre.<br>Le champ pays doit contenir le code ISO 3166 du pays, sur 2 caratères en majuscule (<a href=\"https://en.wikipedia.org/wiki/List_of_ISO_3166_country_codes\" target=\_blank\">liste</a>)" + alumni: + import_hint_html: "Les valeurs pour gender peuvent être m (masculin), f (féminin) et n (non binaire).<br>Les champs phone_professional, phone_personal, mobile et zipcode doivent être au format texte, pas nombre.<br>Le champ pays doit contenir le code ISO 3166 du pays, sur 2 caratères en majuscule (<a href=\"https://en.wikipedia.org/wiki/List_of_ISO_3166_country_codes\" target=\_blank\">liste</a>)<br>Le champ social_twitter ne doit pas contenir d'@." internal_key: Clé interne invoice_informations: Données de facturation manage_alumni: Gérer les alumni manage_cohorts: Gérer les promotions manage_experiences: Gérer les expériences professionnelles + organization: + import_hint_html: "Les valeurs possibles pour kind sont : company, non_profit, government.<br>Les champs siren, nic, zipcode et phone doivent être au format texte, pas nombre.<br>Le champ pays doit contenir le code ISO 3166 du pays, sur 2 caratères en majuscule (<a href=\"https://en.wikipedia.org/wiki/List_of_ISO_3166_country_codes\" target=\_blank\">liste</a>)" person: administrator_roles: Rôles administratifs experience: -- GitLab