Browse Source

Set expiration time on the record

pull/2785/head
Kat 7 months ago
parent
commit
1e21fa0578
  1. 2
      app/jobs/email_csv_job.rb
  2. 2
      app/jobs/scheme_email_csv_job.rb
  3. 2
      app/models/csv_download.rb
  4. 1
      db/migrate/20241118104046_add_csv_download_table.rb
  5. 1
      db/schema.rb
  6. 1
      spec/factories/csv_download.rb
  7. 2
      spec/jobs/email_csv_job_spec.rb
  8. 3
      spec/jobs/scheme_email_csv_job_spec.rb

2
app/jobs/email_csv_job.rb

@ -28,7 +28,7 @@ class EmailCsvJob < ApplicationJob
end end
storage_service.write_file(filename, BYTE_ORDER_MARK + csv_string) storage_service.write_file(filename, BYTE_ORDER_MARK + csv_string)
csv_download = CsvDownload.create!(user:, organisation: user.organisation, filename:, download_type: log_type) csv_download = CsvDownload.create!(user:, organisation: user.organisation, filename:, download_type: log_type, expiration_time: EXPIRATION_TIME)
url = download_csv_download_path(csv_download.id) url = download_csv_download_path(csv_download.id)

2
app/jobs/scheme_email_csv_job.rb

@ -31,7 +31,7 @@ class SchemeEmailCsvJob < ApplicationJob
end end
storage_service.write_file(filename, BYTE_ORDER_MARK + csv_string) storage_service.write_file(filename, BYTE_ORDER_MARK + csv_string)
csv_download = CsvDownload.create!(user:, organisation: user.organisation, filename:, download_type:) csv_download = CsvDownload.create!(user:, organisation: user.organisation, filename:, download_type:, expiration_time: EXPIRATION_TIME)
url = download_csv_download_path(csv_download.id) url = download_csv_download_path(csv_download.id)

2
app/models/csv_download.rb

@ -5,6 +5,6 @@ class CsvDownload < ApplicationRecord
belongs_to :organisation belongs_to :organisation
def expired? def expired?
created_at < 24.hours.ago created_at < expiration_time.seconds.ago
end end
end end

1
db/migrate/20241118104046_add_csv_download_table.rb

@ -3,6 +3,7 @@ class AddCsvDownloadTable < ActiveRecord::Migration[7.0]
create_table :csv_downloads do |t| create_table :csv_downloads do |t|
t.column :download_type, :string t.column :download_type, :string
t.column :filename, :string t.column :filename, :string
t.column :expiration_time, :integer
t.timestamps t.timestamps
t.references :user t.references :user
t.references :organisation t.references :organisation

1
db/schema.rb

@ -67,6 +67,7 @@ ActiveRecord::Schema[7.0].define(version: 2024_11_18_104046) do
create_table "csv_downloads", force: :cascade do |t| create_table "csv_downloads", force: :cascade do |t|
t.string "download_type" t.string "download_type"
t.string "filename" t.string "filename"
t.integer "expiration_time"
t.datetime "created_at", null: false t.datetime "created_at", null: false
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
t.bigint "user_id" t.bigint "user_id"

1
spec/factories/csv_download.rb

@ -4,5 +4,6 @@ FactoryBot.define do
user { create(:user) } user { create(:user) }
organisation { user.organisation } organisation { user.organisation }
filename { "lettings.csv" } filename { "lettings.csv" }
expiration_time { 24.hours.to_i }
end end
end end

2
spec/jobs/email_csv_job_spec.rb

@ -72,6 +72,7 @@ describe EmailCsvJob do
expect(CsvDownload.first.organisation).to eq(user.organisation) expect(CsvDownload.first.organisation).to eq(user.organisation)
expect(CsvDownload.first.filename).to match(/lettings-logs-.*\.csv/) expect(CsvDownload.first.filename).to match(/lettings-logs-.*\.csv/)
expect(CsvDownload.first.download_type).to eq("lettings") expect(CsvDownload.first.download_type).to eq("lettings")
expect(CsvDownload.first.expiration_time).to eq(86400)
end end
end end
@ -116,6 +117,7 @@ describe EmailCsvJob do
expect(CsvDownload.first.organisation).to eq(user.organisation) expect(CsvDownload.first.organisation).to eq(user.organisation)
expect(CsvDownload.first.filename).to match(/sales-logs-.*\.csv/) expect(CsvDownload.first.filename).to match(/sales-logs-.*\.csv/)
expect(CsvDownload.first.download_type).to eq("sales") expect(CsvDownload.first.download_type).to eq("sales")
expect(CsvDownload.first.expiration_time).to eq(86400)
end end
end end

3
spec/jobs/scheme_email_csv_job_spec.rb

@ -59,6 +59,7 @@ describe SchemeEmailCsvJob do
expect(CsvDownload.first.organisation).to eq(user.organisation) expect(CsvDownload.first.organisation).to eq(user.organisation)
expect(CsvDownload.first.filename).to match(/schemes-.*\.csv/) expect(CsvDownload.first.filename).to match(/schemes-.*\.csv/)
expect(CsvDownload.first.download_type).to eq("schemes") expect(CsvDownload.first.download_type).to eq("schemes")
expect(CsvDownload.first.expiration_time).to eq(86400)
end end
end end
@ -77,6 +78,7 @@ describe SchemeEmailCsvJob do
expect(CsvDownload.first.organisation).to eq(user.organisation) expect(CsvDownload.first.organisation).to eq(user.organisation)
expect(CsvDownload.first.filename).to match(/locations-.*\.csv/) expect(CsvDownload.first.filename).to match(/locations-.*\.csv/)
expect(CsvDownload.first.download_type).to eq("locations") expect(CsvDownload.first.download_type).to eq("locations")
expect(CsvDownload.first.expiration_time).to eq(86400)
end end
end end
@ -95,6 +97,7 @@ describe SchemeEmailCsvJob do
expect(CsvDownload.first.organisation).to eq(user.organisation) expect(CsvDownload.first.organisation).to eq(user.organisation)
expect(CsvDownload.first.filename).to match(/schemes-and-locations-.*\.csv/) expect(CsvDownload.first.filename).to match(/schemes-and-locations-.*\.csv/)
expect(CsvDownload.first.download_type).to eq("combined") expect(CsvDownload.first.download_type).to eq("combined")
expect(CsvDownload.first.expiration_time).to eq(86400)
end end
end end

Loading…
Cancel
Save