diff --git a/app/mailers/application_mailer.rb b/app/mailers/application_mailer.rb index 286b2239..e3e736ed 100644 --- a/app/mailers/application_mailer.rb +++ b/app/mailers/application_mailer.rb @@ -1,4 +1,7 @@ +# frozen_string_literal: true + +# Base mailer class class ApplicationMailer < ActionMailer::Base - default from: 'from@example.com' + default from: 'SocialNetwork ' layout 'mailer' end diff --git a/app/mailers/user_report_mailer.rb b/app/mailers/user_report_mailer.rb new file mode 100644 index 00000000..7099cbe4 --- /dev/null +++ b/app/mailers/user_report_mailer.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +require Rails.root.join('app', 'services', 'weekly_report_service.rb') + +# User report mailer class +class UserReportMailer < ApplicationMailer + def weekly_report_mailer + @user = User.find(params[:user_id]) + @weekly_report = WeeklyReportService.new(@user).weekly_report + mail(to: @user.email, subject: 'Weekly report!') + end +end diff --git a/app/services/weekly_report_service.rb b/app/services/weekly_report_service.rb new file mode 100644 index 00000000..c028553d --- /dev/null +++ b/app/services/weekly_report_service.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: True + +# Service responsible for compiling a weekly user report +class WeeklyReportService + WEEK_AGO = 1.week.ago.utc + + def initialize(user) + @user = user + end + + def weekly_report + { subscriptions: weekly_subscriptions, subscribers: weekly_subscribers } + end + + private + + def weekly_subscriptions + @user.subscriptions.where('user_subscriptions.created_at >= ?', WEEK_AGO).order('created_at DESC') + end + + def weekly_subscribers + @user.subscribers.where('user_subscriptions.created_at >= ?', WEEK_AGO).order('created_at DESC') + end +end diff --git a/app/views/user_report_mailer/_users.html.erb b/app/views/user_report_mailer/_users.html.erb new file mode 100644 index 00000000..4a360577 --- /dev/null +++ b/app/views/user_report_mailer/_users.html.erb @@ -0,0 +1,4 @@ +<% users.each_with_index do |user, counter| %> + <%= "#{counter + 1} #{user.profile.surname} #{user.profile.name}" %> +
+<% end %> diff --git a/app/views/user_report_mailer/weekly_report_mailer.html.erb b/app/views/user_report_mailer/weekly_report_mailer.html.erb new file mode 100644 index 00000000..11868172 --- /dev/null +++ b/app/views/user_report_mailer/weekly_report_mailer.html.erb @@ -0,0 +1,20 @@ + + + + <%= csrf_meta_tags %> + + + +

Hi, <%= @user.profile.name %>

+

Your weekly report is here! 🙈 Check it out! 🙄

+

New subscriptions:

+ <%= render partial: "users", locals: { users: @weekly_report[:subscriptions]} %> +

New subscribers:

+ <%= render partial: "users", locals: { users: @weekly_report[:subscribers]} %> + + diff --git a/app/views/user_report_mailer/weekly_report_mailer.text.erb b/app/views/user_report_mailer/weekly_report_mailer.text.erb new file mode 100644 index 00000000..2917b9bb --- /dev/null +++ b/app/views/user_report_mailer/weekly_report_mailer.text.erb @@ -0,0 +1,15 @@ +Hi, <%= @user.profile.name %> +Your weekly report is here! 🙈 Check it out! 🙄 +=============================================== + +New subscriptions: +<% @weekly_report[:subscriptions].each_with_index do |user, counter| %> + <%= "#{counter + 1} #{user.profile.surname} #{user.profile.name}" %> +<% end %> + +New subscribers: +<% @weekly_report[:subscribers].each_with_index do |user, counter| %> + <%= "#{counter + 1} #{user.profile.surname} #{user.profile.name}" %> +<% end %> + +=============================================== diff --git a/spec/mailers/previews/user_report_mailer_preview.rb b/spec/mailers/previews/user_report_mailer_preview.rb new file mode 100644 index 00000000..e037e21d --- /dev/null +++ b/spec/mailers/previews/user_report_mailer_preview.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +# Preview all emails at http://localhost:3000/rails/mailers/user_report_mailer +class UserReportMailerPreview < ActionMailer::Preview + def weekly_report_mailer + UserReportMailer.with(user_id: 340).weekly_report_mailer + end +end