From 06968fc975f502fbfa58283d3f2eb4519e64c456 Mon Sep 17 00:00:00 2001 From: apoorv1316 Date: Mon, 20 May 2024 11:38:58 +0530 Subject: [PATCH] share report service & mailer --- .../v1/reports/time_entries_controller.rb | 11 +++++ app/mailers/share_report_mailer.rb | 18 ++++++++ app/policies/report_policy.rb | 4 ++ app/services/reports/share_service.rb | 41 +++++++++++++++++++ .../reports/time_entries/download_service.rb | 5 +++ .../share_report_mailer/share_report.html.erb | 13 ++++++ config/routes/internal_api.rb | 1 + .../previews/share_report_mailer_preview.rb | 5 +++ spec/mailers/share_report_mailer_spec.rb | 7 ++++ 9 files changed, 105 insertions(+) create mode 100644 app/mailers/share_report_mailer.rb create mode 100644 app/services/reports/share_service.rb create mode 100644 app/views/share_report_mailer/share_report.html.erb create mode 100644 spec/mailers/previews/share_report_mailer_preview.rb create mode 100644 spec/mailers/share_report_mailer_spec.rb diff --git a/app/controllers/internal_api/v1/reports/time_entries_controller.rb b/app/controllers/internal_api/v1/reports/time_entries_controller.rb index 47b4e5d929..14b24de3d7 100644 --- a/app/controllers/internal_api/v1/reports/time_entries_controller.rb +++ b/app/controllers/internal_api/v1/reports/time_entries_controller.rb @@ -15,4 +15,15 @@ def download send_data Reports::TimeEntries::DownloadService.new(params, current_company).process end + + def share + authorize :report + + recipients = params[:recipients] + subject = params[:subject] + message = params[:message] + + Reports::TimeEntries::DownloadService.new(params, current_company).share_report(recipients, subject, message) + render json: { message: "Report shared successfully" }, status: :ok + end end diff --git a/app/mailers/share_report_mailer.rb b/app/mailers/share_report_mailer.rb new file mode 100644 index 0000000000..bc8eb45990 --- /dev/null +++ b/app/mailers/share_report_mailer.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +class ShareReportMailer < ApplicationMailer + def share_report + # @message = message + + recipients = params[:recipients] + subject = params[:subject] + @message = params[:message] + pdf_data = params[:pdf_data] + filename = params[:filename] + + attachments[filename] = pdf_data + mail(to: recipients, subject:) + # attachments[report_file['filename']] = File.read(report_file['path']) + # mail(to: recipients, subject: subject, body: message) + end +end diff --git a/app/policies/report_policy.rb b/app/policies/report_policy.rb index 0da01a1f8b..0df1d6fe81 100644 --- a/app/policies/report_policy.rb +++ b/app/policies/report_policy.rb @@ -9,6 +9,10 @@ def download? user_owner_role? || user_admin_role? || user_book_keeper_role? end + def share? + user_owner_role? || user_admin_role? || user_book_keeper_role? + end + def new? user_owner_role? || user_admin_role? end diff --git a/app/services/reports/share_service.rb b/app/services/reports/share_service.rb new file mode 100644 index 0000000000..294cb77790 --- /dev/null +++ b/app/services/reports/share_service.rb @@ -0,0 +1,41 @@ +# frozen_string_literal: true + +class Reports::ShareService + attr_reader :report_type, :report_data, :current_company, :recipients, :subject, :message + + def initialize(report_type, report_data, current_company, recipients, subject, message) + @report_type = report_type + @report_data = report_data + @current_company = current_company + @recipients = recipients + @subject = subject + @message = message + end + + def process + report_file = generate_report_file + send_email(report_file) + end + + private + + def generate_report_file + case report_type + when :time_entries, :accounts_aging + Reports::GeneratePdf.new(report_type, report_data, current_company).process + else + raise ArgumentError, "Unsupported report type: #{report_type}" + end + end + + def send_email(report_file) + ShareReportMailer.with( + recipients:, + subject:, + message:, + pdf_data: report_file, + filename: "#{report_type}_report.pdf" + ).share_report.deliver_now + # ShareReportMailer.share_report(recipients, subject, message, report_file).deliver_now + end +end diff --git a/app/services/reports/time_entries/download_service.rb b/app/services/reports/time_entries/download_service.rb index bb70a22331..ca2b2bd8d1 100644 --- a/app/services/reports/time_entries/download_service.rb +++ b/app/services/reports/time_entries/download_service.rb @@ -8,6 +8,11 @@ def initialize(params, current_company) @reports = [] end + def share_report(recipients, subject, message) + fetch_complete_report + Reports::ShareService.new(:time_entries, reports, current_company, recipients, subject, message).process + end + private def fetch_complete_report diff --git a/app/views/share_report_mailer/share_report.html.erb b/app/views/share_report_mailer/share_report.html.erb new file mode 100644 index 0000000000..3b182e456f --- /dev/null +++ b/app/views/share_report_mailer/share_report.html.erb @@ -0,0 +1,13 @@ + + + + + + +

<%= @message %>

+ +

Attached is your requested report.

+ +

Thank you,

+ + diff --git a/config/routes/internal_api.rb b/config/routes/internal_api.rb index 169dfcf185..e92676c6ca 100644 --- a/config/routes/internal_api.rb +++ b/config/routes/internal_api.rb @@ -47,6 +47,7 @@ resources :time_entries, only: [:index] do collection do get :download + post :share end end resources :outstanding_overdue_invoices, only: [:index] diff --git a/spec/mailers/previews/share_report_mailer_preview.rb b/spec/mailers/previews/share_report_mailer_preview.rb new file mode 100644 index 0000000000..c82de61e8f --- /dev/null +++ b/spec/mailers/previews/share_report_mailer_preview.rb @@ -0,0 +1,5 @@ +# frozen_string_literal: true + +# Preview all emails at http://localhost:3000/rails/mailers/share_report_mailer +class ShareReportMailerPreview < ActionMailer::Preview +end diff --git a/spec/mailers/share_report_mailer_spec.rb b/spec/mailers/share_report_mailer_spec.rb new file mode 100644 index 0000000000..67a5320eb0 --- /dev/null +++ b/spec/mailers/share_report_mailer_spec.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +require "rails_helper" + +RSpec.describe ShareReportMailer, type: :mailer do + pending "add some examples to (or delete) #{__FILE__}" +end