diff --git a/app/controllers/lettings_logs_controller.rb b/app/controllers/lettings_logs_controller.rb index cd2a6ca5a..6f8efe28e 100644 --- a/app/controllers/lettings_logs_controller.rb +++ b/app/controllers/lettings_logs_controller.rb @@ -96,6 +96,7 @@ class LettingsLogsController < LogsController end def email_csv + DownloadRecord.build_from_user(download_type: :lettings_log, download_filters: session_filters.to_s, user: current_user).save! all_orgs = params["organisation_select"] == "all" EmailCsvJob.perform_later(current_user, search_term, session_filters, all_orgs, nil, codes_only_export?, "lettings", session_filters["years"].first.to_i) redirect_to csv_confirmation_lettings_logs_path diff --git a/app/controllers/organisations_controller.rb b/app/controllers/organisations_controller.rb index d4e4f34fc..a99c78dcf 100644 --- a/app/controllers/organisations_controller.rb +++ b/app/controllers/organisations_controller.rb @@ -40,6 +40,7 @@ class OrganisationsController < ApplicationController end def email_schemes_csv + DownloadRecord.build_from_user(download_type: :scheme_location, download_filters: session_filters.merge({ download_type: params[:download_type] }).to_s, user: current_user).save! SchemeEmailCsvJob.perform_later(current_user, search_term, session_filters, false, @organisation, params[:download_type]) redirect_to schemes_csv_confirmation_organisation_path end @@ -72,6 +73,7 @@ class OrganisationsController < ApplicationController end end format.csv do + DownloadRecord.build_from_user(download_type: :user, download_filters: session_filters.to_s, user: current_user).save! send_data byte_order_mark + unpaginated_filtered_users.to_csv, filename: "users-#{@organisation.name}-#{Time.zone.now}.csv" end end @@ -205,6 +207,7 @@ class OrganisationsController < ApplicationController end def email_lettings_csv + DownloadRecord.build_from_user(download_type: :lettings_log, download_filters: session_filters.to_s, user: current_user).save! EmailCsvJob.perform_later(current_user, search_term, session_filters, false, @organisation, codes_only_export?, "lettings", session_filters["years"].first.to_i) redirect_to lettings_logs_csv_confirmation_organisation_path end @@ -243,6 +246,7 @@ class OrganisationsController < ApplicationController end def email_sales_csv + DownloadRecord.build_from_user(download_type: :sales_log, download_filters: session_filters.to_s, user: current_user).save! EmailCsvJob.perform_later(current_user, search_term, session_filters, false, @organisation, codes_only_export?, "sales", session_filters["years"].first.to_i) redirect_to sales_logs_csv_confirmation_organisation_path end diff --git a/app/controllers/sales_logs_controller.rb b/app/controllers/sales_logs_controller.rb index 3c389ccf1..4e53e4f13 100644 --- a/app/controllers/sales_logs_controller.rb +++ b/app/controllers/sales_logs_controller.rb @@ -70,6 +70,7 @@ class SalesLogsController < LogsController end def email_csv + DownloadRecord.build_from_user(download_type: :sales_log, download_filters: session_filters.to_s, user: current_user).save! all_orgs = params["organisation_select"] == "all" EmailCsvJob.perform_later(current_user, search_term, session_filters, all_orgs, nil, codes_only_export?, "sales", session_filters["years"].first.to_i) redirect_to csv_confirmation_sales_logs_path diff --git a/app/controllers/schemes_controller.rb b/app/controllers/schemes_controller.rb index 4b018ee34..b5e796f72 100644 --- a/app/controllers/schemes_controller.rb +++ b/app/controllers/schemes_controller.rb @@ -236,6 +236,7 @@ class SchemesController < ApplicationController end def email_csv + DownloadRecord.build_from_user(download_type: :scheme_location, download_filters: session_filters.merge({ download_type: params[:download_type] }).to_s, user: current_user).save! all_orgs = params["organisation_select"] == "all" SchemeEmailCsvJob.perform_later(current_user, search_term, session_filters, all_orgs, nil, params[:download_type]) redirect_to csv_confirmation_schemes_path diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 57036cabe..d8982dca9 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -24,6 +24,7 @@ class UsersController < ApplicationController format.html format.csv do if current_user.support? + DownloadRecord.build_from_user(download_type: :user, download_filters: session_filters.to_s, user: current_user).save! send_data byte_order_mark + filtered_users.to_csv, filename: "users-#{Time.zone.now}.csv" else head :unauthorized diff --git a/app/models/download_record.rb b/app/models/download_record.rb new file mode 100644 index 000000000..09c8ee851 --- /dev/null +++ b/app/models/download_record.rb @@ -0,0 +1,25 @@ +# Used to allow for easier auditing of what users downloaded what info +# Caches some info about the user at the time of download +class DownloadRecord < ApplicationRecord + belongs_to :user + belongs_to :user_organisation, class_name: "Organisation" + + DOWNLOAD_TYPE = { + user: 0, + lettings_log: 1, + sales_log: 2, + scheme_location: 3, + }.freeze + + enum download_type: DOWNLOAD_TYPE + enum user_role: User::ROLES + + def self.build_from_user(user:, **attrs) + new( + user:, + user_organisation: user.organisation, + user_role: user.role, + **attrs, + ) + end +end diff --git a/db/migrate/20260415101455_create_download_record.rb b/db/migrate/20260415101455_create_download_record.rb new file mode 100644 index 000000000..4fbaffa06 --- /dev/null +++ b/db/migrate/20260415101455_create_download_record.rb @@ -0,0 +1,13 @@ +class CreateDownloadRecord < ActiveRecord::Migration[7.2] + def change + create_table :download_records do |t| + t.integer :download_type, null: false + t.string :download_filters, null: false + t.references :user, null: false, foreign_key: true + t.references :user_organisation, null: false, foreign_key: { to_table: :organisations } + t.integer :user_role, null: false + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 37e5c764e..b4938827a 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.2].define(version: 2026_03_05_095832) do +ActiveRecord::Schema[7.2].define(version: 2026_04_15_101455) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -106,6 +106,18 @@ ActiveRecord::Schema[7.2].define(version: 2026_03_05_095832) do t.index ["organisation_id"], name: "index_data_protection_confirmations_on_organisation_id" end + create_table "download_records", force: :cascade do |t| + t.integer "download_type", null: false + t.string "download_filters", null: false + t.bigint "user_id", null: false + t.bigint "user_organisation_id", null: false + t.integer "user_role", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["user_id"], name: "index_download_records_on_user_id" + t.index ["user_organisation_id"], name: "index_download_records_on_user_organisation_id" + end + create_table "exports", force: :cascade do |t| t.datetime "created_at", default: -> { "CURRENT_TIMESTAMP" } t.datetime "started_at", null: false @@ -943,6 +955,8 @@ ActiveRecord::Schema[7.2].define(version: 2026_03_05_095832) do t.index ["item_type", "item_id"], name: "index_versions_on_item_type_and_item_id" end + add_foreign_key "download_records", "organisations", column: "user_organisation_id" + add_foreign_key "download_records", "users" add_foreign_key "lettings_logs", "locations" add_foreign_key "lettings_logs", "organisations", column: "owning_organisation_id", on_delete: :cascade add_foreign_key "lettings_logs", "schemes"