Skip to content

Commit

Permalink
WIP: new email templates for operator documents validation process
Browse files Browse the repository at this point in the history
  • Loading branch information
tsubik committed Mar 25, 2024
1 parent ad08a2f commit 7703a10
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 0 deletions.
14 changes: 14 additions & 0 deletions app/mailers/operator_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
15 changes: 15 additions & 0 deletions app/models/operator_document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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}) }
Expand Down Expand Up @@ -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
32 changes: 32 additions & 0 deletions app/views/mailers/operator_mailer/document_invalid.html.mjml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<p>
<%= t("mailers.greeting", name: @user.display_name, fallback: true) %>
</p>

<p>
<%= t(".paragraph1") %>
</p>

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

<p>
<%= t(".paragraph2") %>
</p>

<p style="background-color: #f7f6ed; padding: 20px">
<em><%#= %></em>
</p>

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

<p>
<%= t("mailers.salutation") %> <br/>
<%= t("mailers.signature") %>
</p>
17 changes: 17 additions & 0 deletions app/views/mailers/operator_mailer/document_invalid.text.erb
Original file line number Diff line number Diff line change
@@ -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") %>
12 changes: 12 additions & 0 deletions app/views/mailers/operator_mailer/document_valid.html.mjml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<p>
<%= t("mailers.greeting", name: @user.display_name, fallback: true) %>
</p>

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

<p>
<%= t("mailers.salutation") %> <br/>
<%= t("mailers.signature") %>
</p>
6 changes: 6 additions & 0 deletions app/views/mailers/operator_mailer/document_valid.text.erb
Original file line number Diff line number Diff line change
@@ -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") %>
10 changes: 10 additions & 0 deletions config/locales/mailers.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions config/locales/mailers.fr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}"


8 changes: 8 additions & 0 deletions spec/mailers/previews/operator_mailer_preview.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 7703a10

Please sign in to comment.