diff --git a/lib/email_digest.rb b/lib/email_digest.rb index 0b4d796db..209014dcc 100644 --- a/lib/email_digest.rb +++ b/lib/email_digest.rb @@ -7,7 +7,7 @@ class EmailDigest end def notifications - user.notifications.not_emailed.where(notifiable_type: "ProposalNotification").to_a + user.notifications.not_emailed.where(notifiable_type: "ProposalNotification") end def pending_notifications? @@ -16,8 +16,12 @@ class EmailDigest def deliver if pending_notifications? - Mailer.proposal_notification_digest(user, notifications).deliver_later + Mailer.proposal_notification_digest(user, notifications.to_a).deliver_later end end + def mark_as_emailed + notifications.update_all(emailed_at: Time.now) + end + end \ No newline at end of file diff --git a/lib/tasks/emails.rake b/lib/tasks/emails.rake index 7c76b20c8..ffadebf05 100644 --- a/lib/tasks/emails.rake +++ b/lib/tasks/emails.rake @@ -5,6 +5,7 @@ namespace :emails do User.email_digest.find_each do |user| email_digest = EmailDigest.new(user) email_digest.deliver + email_digest.mark_as_emailed end end diff --git a/spec/lib/email_digests_spec.rb b/spec/lib/email_digests_spec.rb index 244d1270a..d03ea91fd 100644 --- a/spec/lib/email_digests_spec.rb +++ b/spec/lib/email_digests_spec.rb @@ -94,4 +94,27 @@ describe EmailDigest do end + describe "mark_as_emailed" do + + it "marks notifications as emailed" do + user = create(:user) + + proposal_notification = create(:proposal_notification) + notification1 = create(:notification, notifiable: proposal_notification, user: user) + notification2 = create(:notification, notifiable: proposal_notification, user: user) + + expect(notification1.emailed_at).to_not be + expect(notification2.emailed_at).to_not be + + email_digest = EmailDigest.new(user) + email_digest.mark_as_emailed + + notification1.reload + notification2.reload + expect(notification1.emailed_at).to be + expect(notification2.emailed_at).to be + end + + end + end \ No newline at end of file