diff --git a/app/mailers/data_export_mailer.rb b/app/mailers/data_export_mailer.rb index f6f7ddc7607c..c8cfbc640585 100644 --- a/app/mailers/data_export_mailer.rb +++ b/app/mailers/data_export_mailer.rb @@ -1,8 +1,6 @@ class DataExportMailer < ApplicationMailer def completed @data_export = params[:data_export] - @download_url = "https://google.com" - @export_filename = "filename" user = @data_export.user return if @data_export.file.blank? diff --git a/app/models/data_export.rb b/app/models/data_export.rb index f56121310536..b9f7ee030c42 100644 --- a/app/models/data_export.rb +++ b/app/models/data_export.rb @@ -21,4 +21,22 @@ def expired? expires_at < Time.zone.now end + + def filename + return if file.blank? + + "#{created_at.strftime('%Y%m%d%H%M%S')}_#{resource_type}.#{format}" + end + + def file_url + return if file.blank? + + blob_path = Rails.application.routes.url_helpers.rails_blob_path( + file, + host: 'void', + expires_in: 7.days + ) + + File.join(ENV['LAGO_API_URL'], blob_path) + end end diff --git a/app/views/data_export_mailer/completed.slim b/app/views/data_export_mailer/completed.slim index 403ee9ab961f..af314c25f070 100644 --- a/app/views/data_export_mailer/completed.slim +++ b/app/views/data_export_mailer/completed.slim @@ -27,16 +27,11 @@ table style='margin-bottom: 32px' width="100%" cellspacing="0" cellpadding="0" table cellspacing="0" cellpadding="0" tr td style="border-radius: 12px;" bgcolor="#006CFA" - a href="#{@download_url}" download="#{@export_filename}" style="padding: 10px 16px;font-size: 16px; color: #ffffff;text-decoration: none;font-weight:bold;display: inline-block;font-weight: 400;font-style: normal;line-height: 24px;" + a href="#{@data_export.file_url}" download="#{@data_export.filename}" style="padding: 10px 16px;font-size: 16px; color: #ffffff;text-decoration: none;font-weight:bold;display: inline-block;font-weight: 400;font-style: normal;line-height: 24px;" = I18n.t('email.data_export.main_cta_label') div style='margin-bottom: 32px;font-style: normal;font-weight: 400;font-size: 16px;line-height: 24px;color: #19212E;' = I18n.t('email.data_export.fallback_text') - a href="#{@forgot_url}" target="_blank" style="color: #006CFA;text-decoration: none;" - = @forgot_url div style='font-style: normal;font-weight: 400;font-size: 16px;line-height: 24px;' = I18n.t('email.data_export.thanks') div style='margin-bottom: 32px;font-style: normal;font-weight: 400;font-size: 16px;line-height: 24px;color: #19212E;' = I18n.t('email.data_export.lago_team') -div style='width: 100%;height: 1px;background-color: #D9DEE7;margin-bottom: 32px;' -div style="color: #66758F;font-style: normal;font-weight: 400;font-size: 14px;line-height: 20px;" - = I18n.t('email.data_export.email_info') diff --git a/spec/fixtures/export.csv b/spec/fixtures/export.csv new file mode 100644 index 000000000000..24b641339bac --- /dev/null +++ b/spec/fixtures/export.csv @@ -0,0 +1,3 @@ +invoice.lago_id,invoice.sequential_id,invoice.issuing_date,invoice.customer.customer_lago_id,invoice.customer.external_id,invoice.customer.country,invoice.customer.tax_identification_number,invoice.number,invoice.total_amount_cents,invoice.currency,invoice.invoice_type,invoice.payment_status,invoice.status,invoice.file_url,invoice.taxes_amount_cents,invoice.credit_notes_amount_cents,invoice.prepaid_credit_amount_cents,invoice.coupons_amount_cents,invoice.payment_due_date,invoice.payment_dispute_lost_at,invoice.payment_overdue +292ef60b-9e0c-42e7-9f50-44d5af4162ec,1,2024-06-06,80ebcc26-3703-4577-b13e-765591255df4,hooli_1,US,US12345,TWI-2B86-170-001,1000,USD,subscription,pending,finalized,https://file1.com,100,0,1000,0,2024-06-06,,true +7d430962-02cb-4183-b255-de3bb75af798,2,2024-06-07,80ebcc26-3703-4577-b13e-765591255df4,hooli_1,US,US12345,TWI-2B86-170-002,2000,USD,subscription,failed,draft,https://file2.com,200,100,0,0,2024-07-20,2024-06-06,false diff --git a/spec/mailers/data_export_mailer_spec.rb b/spec/mailers/data_export_mailer_spec.rb index 005b20d9e9c2..163775cb67cf 100644 --- a/spec/mailers/data_export_mailer_spec.rb +++ b/spec/mailers/data_export_mailer_spec.rb @@ -23,7 +23,7 @@ context 'when data export is not completed' do let(:data_export) { create(:data_export, :processing) } - it 'does something' do + it 'returns a mailer with nil values' do expect(mailer.to).to be_nil end end @@ -31,7 +31,7 @@ context 'when data export has no attached file' do let(:data_export) { create(:data_export, :completed, file: nil) } - it 'does something' do + it 'returns a mailer with nil values' do expect(mailer.to).to be_nil end end diff --git a/spec/models/data_export_spec.rb b/spec/models/data_export_spec.rb index 662af3c6434f..3f0e20b8564d 100644 --- a/spec/models/data_export_spec.rb +++ b/spec/models/data_export_spec.rb @@ -28,4 +28,40 @@ it { is_expected.to eq true } end end + + describe '.filename' do + subject(:filename) { data_export.filename } + + let(:data_export) { create :data_export, :completed } + + it 'returns the file name' do + freeze_time do + timestamp = Time.zone.now.strftime('%Y%m%d%H%M%S') + expect(filename).to eq("#{timestamp}_invoices.csv") + end + end + + context 'when data export does not have a file' do + let(:data_export) { create :data_export } + + it { is_expected.to be_nil } + end + end + + describe '.file_url' do + subject(:file_url) { data_export.file_url } + + let(:data_export) { create :data_export, :completed } + + it 'returns the file url' do + expect(file_url).to be_present + expect(file_url).to include(ENV['LAGO_API_URL']) + end + + context 'when data export does not have a file' do + let(:data_export) { create :data_export } + + it { is_expected.to be_nil } + end + end end