-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/operator documents new notifications #485
Changes from all commits
d9fb139
af4f70e
ed20038
750bdc5
e12da83
3995812
3b114e7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
class OperatorDocumentMailer < ApplicationMailer | ||
def expiring_documents(operator, user, documents) | ||
@documents = sort_documents(documents) | ||
@user = user | ||
@operator = operator | ||
|
||
mail to: user.email, subject: I18n.t("operator_document_mailer.expiring_documents.subject") | ||
end | ||
|
||
def expired_documents(operator, user, documents) | ||
@documents = sort_documents(documents) | ||
@user = user | ||
@operator = operator | ||
|
||
mail to: user.email, subject: I18n.t("operator_document_mailer.expired_documents.subject", count: @documents.count) | ||
end | ||
|
||
def document_valid(document, user) | ||
@operator = document.operator | ||
@document = document | ||
@user = user | ||
mail to: user.email, subject: I18n.t("operator_document_mailer.document_valid.subject") | ||
end | ||
|
||
def document_accepted_as_not_required(document, user) | ||
@operator = document.operator | ||
@document = document | ||
@user = user | ||
mail to: user.email, subject: I18n.t("operator_document_mailer.document_accepted_as_not_required.subject") | ||
end | ||
|
||
def document_invalid(document, user) | ||
@operator = document.operator | ||
@document = document | ||
@user = user | ||
mail to: user.email, subject: I18n.t("operator_document_mailer.document_invalid.subject") | ||
end | ||
|
||
private | ||
|
||
def sort_documents(documents) | ||
documents.sort_by { |d| [d.fmu&.name || "_", d.required_operator_document.name] } | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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,18 @@ def reason_or_file | |
errors.add(:base, "File must be present or reason when document is non applicable") | ||
end | ||
end | ||
|
||
def notify_about_changes | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. mmm.. from the task in Pivotal it looks like the goal is to notify when the documents are ready to be reviewed. But here the task tells the operators that their documents were reviewed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you looked at wrong PT task. This one is "Set up email notifications to alert private sector users when their documents have been published or if they have been rejected", there is another one for notifying admins that document is ready for review (I will add a new PR today for it). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yup. I probably checked the wrong task. |
||
notify_users(operator.all_users, "document_valid") if doc_valid? | ||
notify_users(operator.all_users, "document_accepted_as_not_required") if doc_not_required? | ||
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 | ||
OperatorDocumentMailer.send(mail_template, self, user).deliver_later | ||
end | ||
end | ||
end | ||
end |
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> |
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") %> |
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><%= @document.admin_comment %></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> |
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") %> |
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> |
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") %> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
require "rails_helper" | ||
|
||
RSpec.describe OperatorDocumentMailer, type: :mailer do | ||
include ActionView::Helpers::UrlHelper | ||
|
||
let(:operator) { create(:operator) } | ||
let(:user) { build(:operator_user, operator: operator) } | ||
|
||
describe "expiring_documents" do | ||
let(:country) { create(:country) } | ||
let(:operator) { create(:operator, country_id: country.id) } | ||
let(:rod1) { create(:required_operator_document_country, country_id: country.id) } | ||
let(:rod2) { create(:required_operator_document_country, country_id: country.id) } | ||
let(:document1) { | ||
create(:operator_document_country, required_operator_document_id: rod1.id, operator_id: operator, expire_date: Date.tomorrow) | ||
} | ||
let(:document2) { | ||
create(:operator_document_country, required_operator_document_id: rod2.id, operator_id: operator, expire_date: Date.tomorrow) | ||
} | ||
let(:documents) { [document1, document2] } | ||
let(:mail) { OperatorDocumentMailer.expiring_documents(operator, user, documents) } | ||
|
||
it "renders the headers" do | ||
expect(mail.subject).to eq("Expiring document(s) on the Open Timber Portal") | ||
expect(mail.to).to eq([user.email]) | ||
end | ||
|
||
it "renders the body" do | ||
expect(mail.body.encoded).to match(/The following document\(s\) from #{operator.name} is\/are expiring in 1 day/) | ||
end | ||
end | ||
|
||
describe "document_valid" do | ||
let(:document) { create(:operator_document_country, operator: operator, force_status: "doc_valid") } | ||
let(:mail) { OperatorDocumentMailer.document_valid(document, user) } | ||
|
||
it "renders the headers" do | ||
expect(mail.subject).to eq("Open Timber Portal: new document published") | ||
expect(mail.to).to eq([user.email]) | ||
end | ||
|
||
it "renders the body" do | ||
expect(mail.body.encoded).to match(/The document #{document.name_with_fmu} has been validated and is now published/) | ||
end | ||
end | ||
|
||
describe "document_accepted_as_not_required" do | ||
let(:document) { create(:operator_document_country, operator: operator, force_status: "doc_not_required") } | ||
let(:mail) { OperatorDocumentMailer.document_accepted_as_not_required(document, user) } | ||
|
||
it "renders the headers" do | ||
expect(mail.subject).to eq("Open Timber Portal: updated document") | ||
expect(mail.to).to eq([user.email]) | ||
end | ||
|
||
it "renders the body" do | ||
expect(mail.body.encoded).to match(/The document #{document.name_with_fmu} has been approved as not required/) | ||
end | ||
end | ||
|
||
describe "document_invalid" do | ||
let(:document) { create(:operator_document_country, operator: operator, force_status: "doc_invalid") } | ||
let(:mail) { OperatorDocumentMailer.document_invalid(document, user) } | ||
|
||
it "renders the headers" do | ||
expect(mail.subject).to eq("Open Timber Portal: uploaded document was not validated") | ||
expect(mail.to).to eq([user.email]) | ||
end | ||
|
||
it "renders the body" do | ||
expect(mail.body.encoded).to match(/The following document was reviewed and needs to be revised/) | ||
end | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mmm.. I didn't get this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I had some validations errors here as in mailer's previews I'm not using persisted data, just
User.new
entity. I want to just get the reset token here without saving in db.