Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Share reports #1849

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions app/controllers/internal_api/v1/reports/time_entries_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
18 changes: 18 additions & 0 deletions app/mailers/share_report_mailer.rb
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions app/policies/report_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
41 changes: 41 additions & 0 deletions app/services/reports/share_service.rb
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions app/services/reports/time_entries/download_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions app/views/share_report_mailer/share_report.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html>
<head>
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
</head>
<body>
<p><%= @message %></p>

<p>Attached is your requested report.</p>

<p>Thank you,</p>
</body>
</html>
1 change: 1 addition & 0 deletions config/routes/internal_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
resources :time_entries, only: [:index] do
collection do
get :download
post :share
end
end
resources :outstanding_overdue_invoices, only: [:index]
Expand Down
5 changes: 5 additions & 0 deletions spec/mailers/previews/share_report_mailer_preview.rb
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions spec/mailers/share_report_mailer_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Loading