Browse Source

Save a csv download record

pull/2785/head
Kat 7 months ago
parent
commit
712a4f0e31
  1. 1
      app/jobs/email_csv_job.rb
  2. 1
      app/jobs/scheme_email_csv_job.rb
  3. 6
      app/models/csv_download.rb
  4. 11
      db/migrate/20241118104046_add_csv_download_table.rb
  5. 13
      db/schema.rb
  6. 18
      spec/jobs/email_csv_job_spec.rb
  7. 27
      spec/jobs/scheme_email_csv_job_spec.rb

1
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)

1
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)

6
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

11
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

13
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

18
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

27
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

Loading…
Cancel
Save