Skip to content

Commit

Permalink
fix Combine service and add specs
Browse files Browse the repository at this point in the history
  • Loading branch information
nudded committed Oct 25, 2024
1 parent 3082ae4 commit 05bbb15
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 16 deletions.
34 changes: 18 additions & 16 deletions app/services/data_exports/combine_parts_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,32 @@ def initialize(data_export:)

def call
result.data_export = data_export
data_export.transaction do
data_export.completed!

Tempfile.create([data_export.resource_type, ".#{data_export.format}"]) do |tempfile|
tempfile.write(data_export.export_class.headers.join(';'))
Tempfile.create([data_export.resource_type, ".#{data_export.format}"]) do |tempfile|
tempfile.write(data_export.export_class.headers.join(','))
tempfile.write("\n")

# Note the order here, this is crucial to make sure the data is in the expected order
data_export.data_export_parts.order(:index).find_each { |part| tempfile.write(part.csv_lines) }
# Note the order here, this is crucial to make sure the data is in the expected order
data_export.data_export_parts.order(:index).find_each { |part| tempfile.write(part.csv_lines) }

tempfile.rewind
tempfile.rewind

data_export.file.attach(
io: tempfile,
filename: data_export.filename,
key: "data_exports/#{data_export.id}.#{format}",
content_type: "text/csv"
)

data_export.completed!
end
data_export.file.attach(
io: tempfile,
filename: data_export.filename,
key: "data_exports/#{data_export.id}.#{data_export.format}",
content_type: "text/csv"
)
end

data_export.completed!
DataExportMailer.with(data_export:).completed.deliver_later

result
end

private

attr_reader :data_export
end
end
55 changes: 55 additions & 0 deletions spec/services/data_exports/combine_parts_service_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe DataExports::CombinePartsService, type: :service do
subject(:result) { described_class.call(data_export:) }

let(:data_export) { create :data_export, :processing, resource_type: 'invoice_fees' }
let(:data_export_part) { create :data_export_part, data_export:, csv_lines: }
let(:csv_lines) do
<<~CSV
292ef60b-9e0c-42e7-9f50-44d5af4162ec,TWI-2B86-170-001,2024-06-06,cc16e6d5-b5e1-4e2c-9ad3-62b3ee4be302,charge,group,group,charge 1 description,group,Converted to EUR,"{:models=>""model_1""}",ff6c279c-9f6c-4962-987e-270936d52310,all_charges,2024-05-08T00:00:00+00:00,2024-06-06T12:48:59+00:00,USD,100.0,10.0,50,10000
CSV
end

before do
data_export_part
end

describe "#call" do
context "when there is only 1 part" do
it "adds the correct headers" do
expected_csv = <<~CSV
invoice_lago_id,invoice_number,invoice_issuing_date,fee_lago_id,fee_item_type,fee_item_code,fee_item_name,fee_item_description,fee_item_invoice_display_name,fee_item_filter_invoice_display_name,fee_item_grouped_by,subscription_external_id,subscription_plan_code,fee_from_date_utc,fee_to_date_utc,fee_amount_currency,fee_units,fee_precise_unit_amount,fee_taxes_amount_cents,fee_total_amount_cents
292ef60b-9e0c-42e7-9f50-44d5af4162ec,TWI-2B86-170-001,2024-06-06,cc16e6d5-b5e1-4e2c-9ad3-62b3ee4be302,charge,group,group,charge 1 description,group,Converted to EUR,"{:models=>""model_1""}",ff6c279c-9f6c-4962-987e-270936d52310,all_charges,2024-05-08T00:00:00+00:00,2024-06-06T12:48:59+00:00,USD,100.0,10.0,50,10000
CSV

expect(result).to be_success
expect(data_export.file.download).to eq(expected_csv)
end
end

context "when there are multiple parts" do
let(:data_export_part2) { create :data_export_part, data_export:, csv_lines: csv_lines2, index: 2 }
let(:csv_lines2) do
<<~CSV
392ef60b-9e0c-42e7-9f50-44d5af4162ec,TWI-2B86-170-001,2024-06-06,cc16e6d5-b5e1-4e2c-9ad3-62b3ee4be302,charge,group,group,charge 1 description,group,Converted to EUR,"{:models=>""model_1""}",ff6c279c-9f6c-4962-987e-270936d52310,all_charges,2024-05-08T00:00:00+00:00,2024-06-06T12:48:59+00:00,USD,100.0,10.0,50,10000
CSV
end

before { data_export_part2 }

it "combines the parts into 1 file on the data export" do
expected_csv = <<~CSV
invoice_lago_id,invoice_number,invoice_issuing_date,fee_lago_id,fee_item_type,fee_item_code,fee_item_name,fee_item_description,fee_item_invoice_display_name,fee_item_filter_invoice_display_name,fee_item_grouped_by,subscription_external_id,subscription_plan_code,fee_from_date_utc,fee_to_date_utc,fee_amount_currency,fee_units,fee_precise_unit_amount,fee_taxes_amount_cents,fee_total_amount_cents
292ef60b-9e0c-42e7-9f50-44d5af4162ec,TWI-2B86-170-001,2024-06-06,cc16e6d5-b5e1-4e2c-9ad3-62b3ee4be302,charge,group,group,charge 1 description,group,Converted to EUR,"{:models=>""model_1""}",ff6c279c-9f6c-4962-987e-270936d52310,all_charges,2024-05-08T00:00:00+00:00,2024-06-06T12:48:59+00:00,USD,100.0,10.0,50,10000
392ef60b-9e0c-42e7-9f50-44d5af4162ec,TWI-2B86-170-001,2024-06-06,cc16e6d5-b5e1-4e2c-9ad3-62b3ee4be302,charge,group,group,charge 1 description,group,Converted to EUR,"{:models=>""model_1""}",ff6c279c-9f6c-4962-987e-270936d52310,all_charges,2024-05-08T00:00:00+00:00,2024-06-06T12:48:59+00:00,USD,100.0,10.0,50,10000
CSV

expect(result).to be_success
expect(data_export.file.download).to eq(expected_csv)
end
end
end
end
10 changes: 10 additions & 0 deletions spec/services/data_exports/process_part_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,14 @@
expect { result }.to have_enqueued_job(DataExports::CombinePartsJob).with(data_export_part.data_export)
end
end

context 'when other parts have not been complete' do
let(:other_part) { create :data_export_part, data_export:, object_ids: [invoice.id], index: 2 }

before { other_part }

it "does not enqueue a job" do
expect { result }.not_to have_enqueued_job(DataExports::CombinePartsJob).with(data_export_part.data_export)
end
end
end

0 comments on commit 05bbb15

Please sign in to comment.