From 712a4f0e31cb45c39f4378e84855ee09a34808e4 Mon Sep 17 00:00:00 2001 From: Kat <54268893+kosiakkatrina@users.noreply.github.com> Date: Mon, 18 Nov 2024 10:56:52 +0000 Subject: [PATCH] Save a csv download record --- app/jobs/email_csv_job.rb | 1 + app/jobs/scheme_email_csv_job.rb | 1 + app/models/csv_download.rb | 6 +++++ .../20241118104046_add_csv_download_table.rb | 11 ++++++++ db/schema.rb | 13 ++++++++- spec/jobs/email_csv_job_spec.rb | 18 +++++++++++++ spec/jobs/scheme_email_csv_job_spec.rb | 27 +++++++++++++++++++ 7 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 app/models/csv_download.rb create mode 100644 db/migrate/20241118104046_add_csv_download_table.rb diff --git a/app/jobs/email_csv_job.rb b/app/jobs/email_csv_job.rb index 58f2d50b8..55db39bb6 100644 --- a/app/jobs/email_csv_job.rb +++ b/app/jobs/email_csv_job.rb @@ -22,6 +22,7 @@ class EmailCsvJob < ApplicationJob storage_service = Storage::S3Service.new(Configuration::EnvConfigurationService.new, ENV["BULK_UPLOAD_BUCKET"]) storage_service.write_file(filename, BYTE_ORDER_MARK + csv_string) + CsvDownload.create!(user:, organisation: user.organisation, filename:, download_type: log_type) url = storage_service.get_presigned_url(filename, EXPIRATION_TIME) diff --git a/app/jobs/scheme_email_csv_job.rb b/app/jobs/scheme_email_csv_job.rb index 44d016a90..764c68882 100644 --- a/app/jobs/scheme_email_csv_job.rb +++ b/app/jobs/scheme_email_csv_job.rb @@ -25,6 +25,7 @@ class SchemeEmailCsvJob < ApplicationJob storage_service = Storage::S3Service.new(Configuration::EnvConfigurationService.new, ENV["BULK_UPLOAD_BUCKET"]) storage_service.write_file(filename, BYTE_ORDER_MARK + csv_string) + CsvDownload.create!(user:, organisation: user.organisation, filename:, download_type:) url = storage_service.get_presigned_url(filename, EXPIRATION_TIME) diff --git a/app/models/csv_download.rb b/app/models/csv_download.rb new file mode 100644 index 000000000..5ff378c30 --- /dev/null +++ b/app/models/csv_download.rb @@ -0,0 +1,6 @@ +class CsvDownload < ApplicationRecord + enum download_type: { lettings: "lettings", sales: "sales", schemes: "schemes", locations: "locations", combined: "combined" } + + belongs_to :user + belongs_to :organisation +end diff --git a/db/migrate/20241118104046_add_csv_download_table.rb b/db/migrate/20241118104046_add_csv_download_table.rb new file mode 100644 index 000000000..f0798ac4c --- /dev/null +++ b/db/migrate/20241118104046_add_csv_download_table.rb @@ -0,0 +1,11 @@ +class AddCsvDownloadTable < ActiveRecord::Migration[7.0] + def change + create_table :csv_downloads do |t| + t.column :download_type, :string + t.column :filename, :string + t.timestamps + t.references :user + t.references :organisation + end + end +end diff --git a/db/schema.rb b/db/schema.rb index ef635628c..f9c6a2c80 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.0].define(version: 2024_10_31_102744) do +ActiveRecord::Schema[7.0].define(version: 2024_11_18_104046) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -64,6 +64,17 @@ ActiveRecord::Schema[7.0].define(version: 2024_10_31_102744) do t.datetime "discarded_at" end + create_table "csv_downloads", force: :cascade do |t| + t.string "download_type" + t.string "filename" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.bigint "user_id" + t.bigint "organisation_id" + t.index ["organisation_id"], name: "index_csv_downloads_on_organisation_id" + t.index ["user_id"], name: "index_csv_downloads_on_user_id" + end + create_table "csv_variable_definitions", force: :cascade do |t| t.string "variable", null: false t.string "definition", null: false diff --git a/spec/jobs/email_csv_job_spec.rb b/spec/jobs/email_csv_job_spec.rb index 8e8a027ea..d60bf2f63 100644 --- a/spec/jobs/email_csv_job_spec.rb +++ b/spec/jobs/email_csv_job_spec.rb @@ -67,6 +67,15 @@ describe EmailCsvJob do expect(lettings_log_csv_service).to receive(:prepare_csv).with(lettings_logs) job.perform(user, nil, {}, nil, nil, codes_only_export) end + + it "creates a CsvDownload record" do + job.perform(user, nil, {}, nil, nil, codes_only_export, "lettings") + expect(CsvDownload.count).to eq(1) + expect(CsvDownload.first.user).to eq(user) + 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") + end end context "when exporting sales logs" do @@ -102,6 +111,15 @@ describe EmailCsvJob do expect(sales_log_csv_service).to receive(:prepare_csv).with(sales_logs) job.perform(user, nil, {}, nil, nil, codes_only_export, "sales") end + + it "creates a CsvDownload record" do + job.perform(user, nil, {}, nil, nil, codes_only_export, "sales") + expect(CsvDownload.count).to eq(1) + expect(CsvDownload.first.user).to eq(user) + 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") + end end it "sends an E-mail with the presigned URL and duration" do diff --git a/spec/jobs/scheme_email_csv_job_spec.rb b/spec/jobs/scheme_email_csv_job_spec.rb index 5ddaa91a6..f2eb33233 100644 --- a/spec/jobs/scheme_email_csv_job_spec.rb +++ b/spec/jobs/scheme_email_csv_job_spec.rb @@ -53,6 +53,15 @@ describe SchemeEmailCsvJob do job.perform(user, nil, {}, nil, nil, download_type) end end + + it "creates a CsvDownload record" do + job.perform(user, nil, {}, nil, nil, download_type) + expect(CsvDownload.count).to eq(1) + expect(CsvDownload.first.user).to eq(user) + expect(CsvDownload.first.organisation).to eq(user.organisation) + expect(CsvDownload.first.filename).to match(/schemes-.*\.csv/) + expect(CsvDownload.first.download_type).to eq("schemes") + end end context "when download type locations" do @@ -62,6 +71,15 @@ describe SchemeEmailCsvJob do expect(storage_service).to receive(:write_file).with(/locations-.*\.csv/, anything) job.perform(user, nil, {}, nil, nil, download_type) end + + it "creates a CsvDownload record" do + job.perform(user, nil, {}, nil, nil, download_type) + expect(CsvDownload.count).to eq(1) + expect(CsvDownload.first.user).to eq(user) + expect(CsvDownload.first.organisation).to eq(user.organisation) + expect(CsvDownload.first.filename).to match(/locations-.*\.csv/) + expect(CsvDownload.first.download_type).to eq("locations") + end end context "when download type combined" do @@ -71,6 +89,15 @@ describe SchemeEmailCsvJob do expect(storage_service).to receive(:write_file).with(/schemes-and-locations.*\.csv/, anything) job.perform(user, nil, {}, nil, nil, download_type) end + + it "creates a CsvDownload record" do + job.perform(user, nil, {}, nil, nil, download_type) + expect(CsvDownload.count).to eq(1) + expect(CsvDownload.first.user).to eq(user) + 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") + end end it "includes the organisation name in the filename when one is provided" do