Skip to content

Commit

Permalink
OrganizationNotifier: send email to confirmed users only
Browse files Browse the repository at this point in the history
  • Loading branch information
markets committed May 1, 2024
1 parent 5c19f88 commit 5a897da
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 7 deletions.
8 changes: 4 additions & 4 deletions app/jobs/organization_notifier_job.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# A weekly digest email.
#
# 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
# posted by active members. Send an email to all confirmed, active and online members
# with the email notifications enabled with those posts. Group emails by user's locale.

# Schedule defined in config/schedule.yml file.
Expand All @@ -24,11 +24,11 @@ def perform
private

def users_by_locale(organization)
with_notifications = organization.users.online_active.actives.notifications
org_locales = with_notifications.pluck(:locale).uniq
org_users = organization.users.online_active.actives.confirmed.notifications
org_locales = org_users.pluck(:locale).uniq

org_locales.each_with_object({}) do |locale, hash|
hash[locale] = with_notifications.where(locale: locale)
hash[locale] = org_users.where(locale: locale)
end
end
end
1 change: 0 additions & 1 deletion app/mailers/organization_notifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ class OrganizationNotifier < ActionMailer::Base
default from: "\"TimeOverflow\" <[email protected]>"

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)

Expand Down
1 change: 1 addition & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class User < ApplicationRecord
scope :actives, -> { joins(:members).where(members: { active: true }) }
scope :online_active, -> { where("sign_in_count > 0") }
scope :notifications, -> { where(notifications: true) }
scope :confirmed, -> { where.not(confirmed_at: nil) }

validates :username, presence: true
validates :email, presence: true, uniqueness: true
Expand Down
4 changes: 2 additions & 2 deletions spec/mailers/organization_notifier_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
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(:user) { Fabricate(:user, email: "user@timeoverflow.org", locale: :en) }
let(:member) { Fabricate(:member, organization: test_organization, user: user) }

describe "send an email" do
Expand All @@ -17,7 +17,7 @@
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"])
expect(mail.bcc).to eql(["user@timeoverflow.org"])
end
it "to should be null" do
expect(mail.to).to be_nil
Expand Down

0 comments on commit 5a897da

Please sign in to comment.