send OrganizationNotifier.recent_posts in user's locale

This commit is contained in:
Marc Anguera Insa
2020-03-01 22:32:58 +01:00
parent ddfab699eb
commit 82de815a2b
3 changed files with 25 additions and 50 deletions

View File

@ -2,7 +2,7 @@
#
# Strategy: go throught all organizations and take latest active posts from last week
# posted by active members. Send an email to all active and online members
# with the email notifications enabled with those posts.
# with the email notifications enabled with those posts. Group emails by user's locale.
# Schedule defined in config/schedule.yml file.
@ -12,9 +12,19 @@ class OrganizationNotifierJob < ActiveJob::Base
def perform
Organization.all.find_each do |org|
posts = org.posts.active.of_active_members.from_last_week
if posts.present?
OrganizationNotifier.recent_posts(posts).deliver_now
users_by_locale(org).each do |locale, users|
OrganizationNotifier.recent_posts(posts, locale, users).deliver_now
end
end
end
end
private
def users_by_locale(organization)
organization.users.online_active.actives.
notifications.group_by { |u| u.locale || I18n.default_locale }
end
end

View File

@ -6,20 +6,15 @@ class OrganizationNotifier < ActionMailer::Base
#
# en.organization_notifier.recent_posts.subject
#
def recent_posts(posts)
def recent_posts(posts, locale, users)
# last 10 posts of offers and inquiries
@offers = posts.where(type: "Offer").take(10)
@inquiries = posts.where(type: "Inquiry").take(10)
@organization_name = posts.take.organization.name
mail(bcc: emails_newsletter(posts))
end
private
def emails_newsletter(posts)
posts.take.organization.users.online_active.actives.
notifications.pluck(:email)
I18n.with_locale(locale) do
mail(bcc: users.map(&:email))
end
end
end

View File

@ -1,52 +1,22 @@
require "spec_helper"
RSpec.describe OrganizationNotifier do
let (:test_organization) { Fabricate(:organization) }
let! (:offer) { Fabricate(:offer, organization: test_organization) }
let! (:inquiry) { Fabricate(:inquiry, organization: test_organization) }
let (:user) do
Fabricate(:user, sign_in_count: 2, email: "user@example.com")
end
let (:another_user) { Fabricate(:user, sign_in_count: 1) }
let (:yet_another_user) { Fabricate(:user, sign_in_count: 0) }
let! (:member) do
Fabricate(:member,
organization: test_organization,
user: user,
active: true)
end
let! (:another_member) do
Fabricate(:member,
organization: test_organization,
user: another_user,
active: false)
end
let! (:yet_another_member) do
Fabricate(:member,
organization: test_organization,
user: yet_another_user,
active: true)
end
before(:each) do
ActionMailer::Base.delivery_method = :test
ActionMailer::Base.perform_deliveries = true
ActionMailer::Base.deliveries = []
OrganizationNotifier.recent_posts(test_organization.posts).deliver_now
end
after(:each) do
ActionMailer::Base.deliveries.clear
end
let(:test_organization) { Fabricate(:organization) }
let!(:offer) { Fabricate(:offer, organization: test_organization) }
let!(:inquiry) { Fabricate(:inquiry, organization: test_organization) }
let(:user) { Fabricate(:user, email: "user@example.com", locale: :en) }
let(:member) { Fabricate(:member, organization: test_organization, user: user) }
describe "send an email" do
it "should send an email" do
expect(ActionMailer::Base.deliveries.count).to eq(1)
expect {
OrganizationNotifier.recent_posts(test_organization.posts, :en, [user]).deliver_now
}.to change { ActionMailer::Base.deliveries.count }.by(1)
end
end
describe "recent posts" do
let(:mail) { OrganizationNotifier.recent_posts(test_organization.posts) }
let(:mail) { OrganizationNotifier.recent_posts(test_organization.posts, :en, [user]) }
it "receive email only active and online users" do
expect(mail.bcc).to eql(["user@example.com"])