From 1e21fa057812be28304e6c2c1caa394122655ab3 Mon Sep 17 00:00:00 2001 From: Kat <54268893+kosiakkatrina@users.noreply.github.com> Date: Tue, 19 Nov 2024 08:47:32 +0000 Subject: [PATCH] Set expiration time on the record --- app/jobs/email_csv_job.rb | 2 +- app/jobs/scheme_email_csv_job.rb | 2 +- app/models/csv_download.rb | 2 +- db/migrate/20241118104046_add_csv_download_table.rb | 1 + db/schema.rb | 1 + spec/factories/csv_download.rb | 1 + spec/jobs/email_csv_job_spec.rb | 2 ++ spec/jobs/scheme_email_csv_job_spec.rb | 3 +++ 8 files changed, 11 insertions(+), 3 deletions(-) diff --git a/app/jobs/email_csv_job.rb b/app/jobs/email_csv_job.rb index b16ff9f6a..ae9711d3e 100644 --- a/app/jobs/email_csv_job.rb +++ b/app/jobs/email_csv_job.rb @@ -28,7 +28,7 @@ class EmailCsvJob < ApplicationJob end 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) diff --git a/app/jobs/scheme_email_csv_job.rb b/app/jobs/scheme_email_csv_job.rb index ab856eee3..ddef947e0 100644 --- a/app/jobs/scheme_email_csv_job.rb +++ b/app/jobs/scheme_email_csv_job.rb @@ -31,7 +31,7 @@ class SchemeEmailCsvJob < ApplicationJob end 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) diff --git a/app/models/csv_download.rb b/app/models/csv_download.rb index 50e8bdafa..4064c62f3 100644 --- a/app/models/csv_download.rb +++ b/app/models/csv_download.rb @@ -5,6 +5,6 @@ class CsvDownload < ApplicationRecord belongs_to :organisation def expired? - created_at < 24.hours.ago + created_at < expiration_time.seconds.ago end end diff --git a/db/migrate/20241118104046_add_csv_download_table.rb b/db/migrate/20241118104046_add_csv_download_table.rb index f0798ac4c..9b4f73f0b 100644 --- a/db/migrate/20241118104046_add_csv_download_table.rb +++ b/db/migrate/20241118104046_add_csv_download_table.rb @@ -3,6 +3,7 @@ class AddCsvDownloadTable < ActiveRecord::Migration[7.0] create_table :csv_downloads do |t| t.column :download_type, :string t.column :filename, :string + t.column :expiration_time, :integer t.timestamps t.references :user t.references :organisation diff --git a/db/schema.rb b/db/schema.rb index f9c6a2c80..5b6ddacdb 100644 --- a/db/schema.rb +++ b/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| t.string "download_type" t.string "filename" + t.integer "expiration_time" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.bigint "user_id" diff --git a/spec/factories/csv_download.rb b/spec/factories/csv_download.rb index 4ee95e368..415f69de6 100644 --- a/spec/factories/csv_download.rb +++ b/spec/factories/csv_download.rb @@ -4,5 +4,6 @@ FactoryBot.define do user { create(:user) } organisation { user.organisation } filename { "lettings.csv" } + expiration_time { 24.hours.to_i } end end diff --git a/spec/jobs/email_csv_job_spec.rb b/spec/jobs/email_csv_job_spec.rb index 7808e69ff..80b1c8d1b 100644 --- a/spec/jobs/email_csv_job_spec.rb +++ b/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.filename).to match(/lettings-logs-.*\.csv/) expect(CsvDownload.first.download_type).to eq("lettings") + expect(CsvDownload.first.expiration_time).to eq(86400) end end @@ -116,6 +117,7 @@ describe EmailCsvJob do expect(CsvDownload.first.organisation).to eq(user.organisation) expect(CsvDownload.first.filename).to match(/sales-logs-.*\.csv/) expect(CsvDownload.first.download_type).to eq("sales") + expect(CsvDownload.first.expiration_time).to eq(86400) end end diff --git a/spec/jobs/scheme_email_csv_job_spec.rb b/spec/jobs/scheme_email_csv_job_spec.rb index f3e96014c..16fc7b365 100644 --- a/spec/jobs/scheme_email_csv_job_spec.rb +++ b/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.filename).to match(/schemes-.*\.csv/) expect(CsvDownload.first.download_type).to eq("schemes") + expect(CsvDownload.first.expiration_time).to eq(86400) end end @@ -77,6 +78,7 @@ describe SchemeEmailCsvJob do expect(CsvDownload.first.organisation).to eq(user.organisation) expect(CsvDownload.first.filename).to match(/locations-.*\.csv/) expect(CsvDownload.first.download_type).to eq("locations") + expect(CsvDownload.first.expiration_time).to eq(86400) end end @@ -95,6 +97,7 @@ describe SchemeEmailCsvJob do expect(CsvDownload.first.organisation).to eq(user.organisation) expect(CsvDownload.first.filename).to match(/schemes-and-locations-.*\.csv/) expect(CsvDownload.first.download_type).to eq("combined") + expect(CsvDownload.first.expiration_time).to eq(86400) end end