diff --git a/config/routes/internal_api.rb b/config/routes/internal_api.rb
index 3544e69123..169dfcf185 100644
--- a/config/routes/internal_api.rb
+++ b/config/routes/internal_api.rb
@@ -50,7 +50,11 @@
end
end
resources :outstanding_overdue_invoices, only: [:index]
- resources :accounts_aging, only: [:index]
+ resources :accounts_aging, only: [:index] do
+ collection do
+ get :download
+ end
+ end
end
resources :workspaces, only: [:index, :update]
diff --git a/spec/services/reports/accounts_aging/download_service_spec.rb b/spec/services/reports/accounts_aging/download_service_spec.rb
new file mode 100644
index 0000000000..09878446c3
--- /dev/null
+++ b/spec/services/reports/accounts_aging/download_service_spec.rb
@@ -0,0 +1,38 @@
+# frozen_string_literal: true
+
+require "rails_helper"
+
+RSpec.describe Reports::AccountsAging::DownloadService do
+ let(:current_company) { create(:company) }
+
+ describe "#process" do
+ let(:params) { { some_param: "value" } }
+ let(:reports_data) { { clients: [], total_amount_overdue_by_date_range: {} } }
+
+ subject { described_class.new(params, current_company) }
+
+ before do
+ allow(Reports::AccountsAging::FetchOverdueAmount).to receive(:new).and_return(
+ double(
+ "FetchOverdueAmount",
+ process: reports_data))
+ allow(Reports::GeneratePdf).to receive(:new).and_return(double("Reports::GeneratePdf", process: nil))
+ allow(Reports::GenerateCsv).to receive(:new).and_return(double("Reports::GenerateCsv", process: nil))
+ end
+
+ it "fetches complete report, generates PDF and CSV" do
+ allow(subject).to receive(:fetch_complete_report)
+ allow(subject).to receive(:generate_pdf)
+ allow(subject).to receive(:generate_csv)
+
+ subject.process
+ end
+
+ it "fetches complete report and generates CSV" do
+ allow(subject).to receive(:fetch_complete_report)
+ allow(subject).to receive(:generate_csv)
+
+ subject.process
+ end
+ end
+end
diff --git a/spec/services/reports/generate_csv_spec.rb b/spec/services/reports/generate_csv_spec.rb
new file mode 100644
index 0000000000..7feb8181e6
--- /dev/null
+++ b/spec/services/reports/generate_csv_spec.rb
@@ -0,0 +1,27 @@
+# frozen_string_literal: true
+
+require "rails_helper"
+
+RSpec.describe Reports::GenerateCsv do
+ describe "#process" do
+ let(:headers) { ["Name", "Age", "Email"] }
+ let(:data) do
+ [
+ ["John Doe", "30", "john@example.com"],
+ ["Jane Smith", "25", "jane@example.com"]
+ ]
+ end
+
+ subject { described_class.new(data, headers) }
+
+ it "generates CSV data with headers and data" do
+ csv_data = subject.process
+ parsed_csv = CSV.parse(csv_data)
+
+ expect(parsed_csv.first).to eq(headers)
+
+ expect(parsed_csv[1]).to eq(data.first)
+ expect(parsed_csv[2]).to eq(data.second)
+ end
+ end
+end
diff --git a/spec/services/reports/generate_pdf_spec.rb b/spec/services/reports/generate_pdf_spec.rb
new file mode 100644
index 0000000000..be927b3f81
--- /dev/null
+++ b/spec/services/reports/generate_pdf_spec.rb
@@ -0,0 +1,40 @@
+# frozen_string_literal: true
+
+require "rails_helper"
+
+RSpec.describe Reports::GeneratePdf do
+ let(:report_data) { double("report_data") }
+ let(:current_company) { double("current_company") }
+
+ describe "#process" do
+ context "when report type is time_entries" do
+ subject { described_class.new(:time_entries, report_data, current_company) }
+
+ it "generates PDF for time entries" do
+ allow(Pdf::HtmlGenerator).to receive(:new).with(
+ :time_entries,
+ locals: { report_data:, current_company: }).and_return(double("Pdf::HtmlGenerator", make: nil))
+ subject.process
+ end
+ end
+
+ context "when report type is accounts_aging" do
+ subject { described_class.new(:accounts_aging, report_data, current_company) }
+
+ it "generates PDF for accounts aging" do
+ allow(Pdf::HtmlGenerator).to receive(:new).with(
+ :accounts_aging,
+ locals: { report_data:, current_company: }).and_return(double("Pdf::HtmlGenerator", make: nil))
+ subject.process
+ end
+ end
+
+ context "when report type is unsupported" do
+ it "raises ArgumentError" do
+ expect {
+ described_class.new(:unsupported_report_type, report_data, current_company).process
+ }.to raise_error(ArgumentError, "Unsupported report type: unsupported_report_type")
+ end
+ end
+ end
+end
diff --git a/spec/services/reports/time_entries/download_service_spec.rb b/spec/services/reports/time_entries/download_service_spec.rb
index ed1e9a16f7..6a2f79a47d 100644
--- a/spec/services/reports/time_entries/download_service_spec.rb
+++ b/spec/services/reports/time_entries/download_service_spec.rb
@@ -6,6 +6,10 @@
let(:company) { create(:company) }
let(:client) { create(:client, :with_logo, company:) }
let(:project) { create(:project, client:) }
+ let(:csv_headers) do
+ "Project,Client,Note,Team Member,Date,Hours Logged"
+ end
+ let(:report_entries) { [double("TimeEntry")] }
before do
create_list(:user, 12)
@@ -31,5 +35,20 @@
all_users_with_name = User.all.order(:first_name).map { |u| u.full_name }
expect(data.pluck(:label)).to eq(all_users_with_name)
end
+
+ it "generates CSV report" do
+ data = subject.process
+ expect(data).to include(csv_headers)
+ end
+
+ it "generates a PDF report using Pdf::HtmlGenerator" do
+ subject { described_class.new(report_entries, current_company) }
+
+ html_generator = instance_double("Pdf::HtmlGenerator")
+ allow(Pdf::HtmlGenerator).to receive(:new).and_return(html_generator)
+
+ allow(html_generator).to receive(:make)
+ subject.process
+ end
end
end
diff --git a/spec/services/reports/time_entries/generate_csv_spec.rb b/spec/services/reports/time_entries/generate_csv_spec.rb
deleted file mode 100644
index abcaf43bf2..0000000000
--- a/spec/services/reports/time_entries/generate_csv_spec.rb
+++ /dev/null
@@ -1,33 +0,0 @@
-# frozen_string_literal: true
-
-require "rails_helper"
-
-RSpec.describe Reports::TimeEntries::GenerateCsv do
- let(:company) { create(:company) }
- let!(:entry) { create(:timesheet_entry) }
-
- describe "#process" do
- before do
- TimesheetEntry.reindex
- end
-
- subject { described_class.new(TimesheetEntry.search(load: false), company).process }
-
- let(:csv_headers) do
- "Project,Client,Note,Team Member,Date,Hours Logged"
- end
- let(:csv_data) do
- "#{entry.project_name}," \
- "#{entry.client_name}," \
- "#{entry.note}," \
- "#{entry.user_full_name}," \
- "#{entry.formatted_work_date}," \
- "#{entry.formatted_duration}"
- end
-
- it "returns CSV string" do
- expect(subject).to include(csv_headers)
- expect(subject).to include(csv_data)
- end
- end
-end
diff --git a/spec/services/reports/time_entries/generate_pdf_spec.rb b/spec/services/reports/time_entries/generate_pdf_spec.rb
deleted file mode 100644
index 09e1486b89..0000000000
--- a/spec/services/reports/time_entries/generate_pdf_spec.rb
+++ /dev/null
@@ -1,20 +0,0 @@
-# frozen_string_literal: true
-
-require "rails_helper"
-
-RSpec.describe Reports::TimeEntries::GeneratePdf do
- let(:report_entries) { [double("TimeEntry")] }
- let(:current_company) { double("Company") }
-
- subject { described_class.new(report_entries, current_company) }
-
- describe "#process" do
- it "generates a PDF report using Pdf::HtmlGenerator" do
- html_generator = instance_double("Pdf::HtmlGenerator")
- allow(Pdf::HtmlGenerator).to receive(:new).and_return(html_generator)
-
- allow(html_generator).to receive(:make)
- subject.process
- end
- end
-end
|