From 7703a10e97ead337a2110a76486f89ae67edefbd Mon Sep 17 00:00:00 2001 From: Tomasz Subik Date: Fri, 22 Mar 2024 17:22:25 +0100 Subject: [PATCH] WIP: new email templates for operator documents validation process --- app/mailers/operator_mailer.rb | 14 ++++++++ app/models/operator_document.rb | 15 +++++++++ .../document_invalid.html.mjml | 32 +++++++++++++++++++ .../operator_mailer/document_invalid.text.erb | 17 ++++++++++ .../operator_mailer/document_valid.html.mjml | 12 +++++++ .../operator_mailer/document_valid.text.erb | 6 ++++ config/locales/mailers.en.yml | 10 ++++++ config/locales/mailers.fr.yml | 11 +++++++ .../previews/operator_mailer_preview.rb | 8 +++++ 9 files changed, 125 insertions(+) create mode 100644 app/views/mailers/operator_mailer/document_invalid.html.mjml create mode 100644 app/views/mailers/operator_mailer/document_invalid.text.erb create mode 100644 app/views/mailers/operator_mailer/document_valid.html.mjml create mode 100644 app/views/mailers/operator_mailer/document_valid.text.erb diff --git a/app/mailers/operator_mailer.rb b/app/mailers/operator_mailer.rb index 0020cb690..36071b3c3 100644 --- a/app/mailers/operator_mailer.rb +++ b/app/mailers/operator_mailer.rb @@ -50,4 +50,18 @@ def quarterly_newsletter(operator, user) mail to: user.email, subject: I18n.t("operator_mailer.quarterly_newsletter.subject", company: operator.name) end + + def document_valid(document, user) + @operator = document.operator + @document = document + @user = user + mail to: user.email, subject: I18n.t("operator_mailer.document_valid.subject") + end + + def document_invalid(document, user) + @operator = document.operator + @document = document + @user = user + mail to: user.email, subject: I18n.t("operator_mailer.document_invalid.subject") + end end diff --git a/app/models/operator_document.rb b/app/models/operator_document.rb index fe409dce3..b9245602a 100644 --- a/app/models/operator_document.rb +++ b/app/models/operator_document.rb @@ -61,6 +61,8 @@ class OperatorDocument < ApplicationRecord after_save :recalculate_scores, if: :saved_change_to_score_related_attributes? after_save :remove_notifications, if: :saved_change_to_expire_date? + after_commit :notify_about_changes, if: :saved_change_to_status? + scope :by_forest_types, ->(forest_type_id) { includes(:fmu).where(fmus: {forest_type: forest_type_id}) } scope :by_country, ->(country_id) { includes(:required_operator_document).where(required_operator_documents: {country_id: country_id}) } scope :by_required_operator_document_group, ->(required_operator_document_group_id) { includes(:required_operator_document).where(required_operator_documents: {required_operator_document_group_id: required_operator_document_group_id}) } @@ -217,4 +219,17 @@ def reason_or_file errors.add(:base, "File must be present or reason when document is non applicable") end end + + def notify_about_changes + notify_users(operator.all_users, "document_valid") if doc_valid? + notify_users(operator.all_users, "document_invalid") if doc_invalid? + end + + def notify_users(users, mail_template) + users.filter_actives.where.not(email: [nil, ""]).find_each do |user| + I18n.with_locale(user.locale.presence || I18n.default_locale) do + OperatorMailer.send(mail_template, self, user).deliver_later + end + end + end end diff --git a/app/views/mailers/operator_mailer/document_invalid.html.mjml b/app/views/mailers/operator_mailer/document_invalid.html.mjml new file mode 100644 index 000000000..46ecaf2b9 --- /dev/null +++ b/app/views/mailers/operator_mailer/document_invalid.html.mjml @@ -0,0 +1,32 @@ +

+ <%= t("mailers.greeting", name: @user.display_name, fallback: true) %> +

+ +

+ <%= t(".paragraph1") %> +

+ +

+ <%= t("mailers.company") %>: <%= @operator.name %>
+ <%= t("mailers.document") %>: <%= @document.name_with_fmu %>
+ <%= OperatorDocument.human_attribute_name(:start_date) %>: <%= @document.start_date %>
+ <%= OperatorDocument.human_attribute_name(:expire_date) %>: <%= @document.expire_date %>
+ Link: <%= link_to nil, @document&.document_file&.attachment&.url %> +

+ +

+ <%= t(".paragraph2") %> +

+ +

+ <%#= %> +

+ +

+ <%= t(".paragraph3", link: link_to(nil, "#{ENV["FRONTEND_URL"]}/operators/#{@operator.slug}/documentation", target: "_blank")).html_safe %> +

+ +

+ <%= t("mailers.salutation") %>
+ <%= t("mailers.signature") %> +

diff --git a/app/views/mailers/operator_mailer/document_invalid.text.erb b/app/views/mailers/operator_mailer/document_invalid.text.erb new file mode 100644 index 000000000..14c76ffe2 --- /dev/null +++ b/app/views/mailers/operator_mailer/document_invalid.text.erb @@ -0,0 +1,17 @@ +<%= t("mailers.greeting", name: @user.display_name, fallback: true) %> + +<%= t(".paragraph1") %> + +<%= t("mailers.company") %>: <%= @operator.name %> +<%= t("mailers.document") %>: <%= @document.name_with_fmu %> +<%= OperatorDocument.human_attribute_name(:start_date) %>: <%= @document.start_date %> +<%= OperatorDocument.human_attribute_name(:expire_date) %>: <%= @document.expire_date %> +Link: <%= @document&.document_file&.attachment&.url %> + +<%= t(".paragraph2") %> +<%# <%= @document.admin_comment %> + +<%= t(".paragraph3", link: "#{ENV["FRONTEND_URL"]}/operators/#{@operator.slug}/documentation") %> + +<%= t("mailers.salutation") %> +<%= t("mailers.signature") %> diff --git a/app/views/mailers/operator_mailer/document_valid.html.mjml b/app/views/mailers/operator_mailer/document_valid.html.mjml new file mode 100644 index 000000000..f6b7eb46d --- /dev/null +++ b/app/views/mailers/operator_mailer/document_valid.html.mjml @@ -0,0 +1,12 @@ +

+ <%= t("mailers.greeting", name: @user.display_name, fallback: true) %> +

+ +

+ <%= t(".paragraph1", document: @document.name_with_fmu, company: link_to(@operator.name, ENV["FRONTEND_URL"] + "/operators/"+ @operator.slug + "/documentation", target: "_blank")).html_safe %> +

+ +

+ <%= t("mailers.salutation") %>
+ <%= t("mailers.signature") %> +

diff --git a/app/views/mailers/operator_mailer/document_valid.text.erb b/app/views/mailers/operator_mailer/document_valid.text.erb new file mode 100644 index 000000000..b48a84aab --- /dev/null +++ b/app/views/mailers/operator_mailer/document_valid.text.erb @@ -0,0 +1,6 @@ +<%= t("mailers.greeting", name: @user.display_name, fallback: true) %> + +<%= t(".paragraph1", document: @document.name_with_fmu, company: @operator.name) %> + +<%= t("mailers.salutation") %> +<%= t("mailers.signature") %> diff --git a/config/locales/mailers.en.yml b/config/locales/mailers.en.yml index 716c9a664..14a78216d 100644 --- a/config/locales/mailers.en.yml +++ b/config/locales/mailers.en.yml @@ -6,6 +6,8 @@ en: contact_us: Feel free to contact us if you have any questions. observation_id: Observation ID report_name: Report name + company: Company + document: Document user_mailer: user_acceptance: subject: Welcome to the Open Timber Portal! @@ -70,6 +72,14 @@ en: other: "You have %{count} documents expired on the OTP" paragraph1: "%{company} has %{count} document(s) that are expired:" paragraph2: You can update these documents by logging on to your OTP profile at %{link}. + document_valid: + subject: "Open Timber Portal: new document published" + paragraph1: The document %{document} has been validated and is now published on %{company} profile on the Open Timber Portal. + document_invalid: + subject: "Open Timber Portal: uploaded document was not validated" + paragraph1: "The following document was reviewed and needs to be revised before it can be published:" + paragraph2: "The reviewer made the following comment:" + paragraph3: "You can make edits to this document by clicking on the following link and updating it on your profile: %{link}" system_mailer: user_created: subject: New account created diff --git a/config/locales/mailers.fr.yml b/config/locales/mailers.fr.yml index dcc8abd85..76dbb8bb2 100644 --- a/config/locales/mailers.fr.yml +++ b/config/locales/mailers.fr.yml @@ -6,6 +6,8 @@ fr: contact_us: N'hésitez pas à nous contacter si vous avez des questions. observation_id: Identifiant de l’observation report_name: Nom du rapport + company: Enterprise + document: Document user_mailer: user_acceptance: subject: Bienvenue sur la plateforme Open Timber Portal @@ -70,4 +72,13 @@ fr: other: "Vous avez %{count} documents expiré sur l'OTP" paragraph1: "%{company} a %{count} document(s) qui sont expiré:" paragraph2: "Vous pouvez actualiser ces documents directement sur votre profil, en vous connectant sur l'OTP: %{link}." + document_valid: + subject: "Open Timber Portal: nouveau document publié" + paragraph1: Le document %{document} a été validé et est désormais publié sur le profil de %{company} sur l’Open Timber Portal. + document_invalid: + subject: "Open Timber Portal: le document téléchargé n’a pas été validé" + paragraph1: "Le document ci-dessous a été passé en revue et a besoin d’être modifié avant de pouvoir être publié:" + paragraph2: "L'évaluateur a fait le commentaire suivant:" + paragraph3: "Vous pouvez modifier ce document en cliquant sur le lien suivant et en modifiant le document sur votre profil: %{link}" + diff --git a/spec/mailers/previews/operator_mailer_preview.rb b/spec/mailers/previews/operator_mailer_preview.rb index a921efc51..8084835b0 100644 --- a/spec/mailers/previews/operator_mailer_preview.rb +++ b/spec/mailers/previews/operator_mailer_preview.rb @@ -11,6 +11,14 @@ def expired_documents OperatorMailer.expired_documents operator, operator.all_users.filter_actives.first, documents_expired end + def document_valid + OperatorMailer.document_valid OperatorDocument.doc_valid.last, User.filter_actives.first + end + + def document_invalid + OperatorMailer.document_invalid OperatorDocument.doc_invalid.where.not(document_file: nil).last, User.filter_actives.first + end + private def documents_expired