Skip to content

Commit

Permalink
Add DataExport file_url and filename methods
Browse files Browse the repository at this point in the history
file_url creates a link with expiration time

filename is based in the creation date, resource type and format of the
export
  • Loading branch information
ancorcruz committed Jul 1, 2024
1 parent c18d82f commit 03f1d93
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 10 deletions.
2 changes: 0 additions & 2 deletions app/mailers/data_export_mailer.rb
Original file line number Diff line number Diff line change
@@ -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?
Expand Down
18 changes: 18 additions & 0 deletions app/models/data_export.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 1 addition & 6 deletions app/views/data_export_mailer/completed.slim
Original file line number Diff line number Diff line change
Expand Up @@ -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')
3 changes: 3 additions & 0 deletions spec/fixtures/export.csv
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions spec/mailers/data_export_mailer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@
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

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
Expand Down
36 changes: 36 additions & 0 deletions spec/models/data_export_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 03f1d93

Please sign in to comment.