From 84971d0033997df73f026ab80d98b21cd798af58 Mon Sep 17 00:00:00 2001 From: samyou-softwire Date: Wed, 15 Apr 2026 15:16:10 +0100 Subject: [PATCH 1/7] CLDC-4331: Add model for download records --- app/models/download_record.rb | 26 +++++++++++++++++++ .../20260415101455_create_download_record.rb | 13 ++++++++++ db/schema.rb | 16 +++++++++++- 3 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 app/models/download_record.rb create mode 100644 db/migrate/20260415101455_create_download_record.rb diff --git a/app/models/download_record.rb b/app/models/download_record.rb new file mode 100644 index 000000000..d6a91efb2 --- /dev/null +++ b/app/models/download_record.rb @@ -0,0 +1,26 @@ +# Not used functionally, but used to allow 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, + organisation: 3, + scheme: 4, + }.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" From 754a13d92c96e7e68eb4d51af93cba51f109028a Mon Sep 17 00:00:00 2001 From: samyou-softwire Date: Wed, 15 Apr 2026 15:29:19 +0100 Subject: [PATCH 2/7] CLDC-4331: Add a row to database on all download paths --- app/controllers/lettings_logs_controller.rb | 1 + app/controllers/organisations_controller.rb | 4 ++++ app/controllers/sales_logs_controller.rb | 1 + app/controllers/schemes_controller.rb | 1 + app/controllers/users_controller.rb | 1 + 5 files changed, 8 insertions(+) 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..6803e7825 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, download_filters: session_filters.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..4e6b75182 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, download_filters: session_filters.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 From 199b1a97134d3bee4aa81eab062b6791f23f8886 Mon Sep 17 00:00:00 2001 From: samyou-softwire Date: Thu, 16 Apr 2026 14:28:42 +0100 Subject: [PATCH 3/7] fixup! CLDC-4331: Add model for download records improve comment --- app/models/download_record.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/download_record.rb b/app/models/download_record.rb index d6a91efb2..13022fe9b 100644 --- a/app/models/download_record.rb +++ b/app/models/download_record.rb @@ -1,4 +1,4 @@ -# Not used functionally, but used to allow auditing of what users downloaded what info +# 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 From f5d5375a42f792308fe5d838c8f568425999850b Mon Sep 17 00:00:00 2001 From: samyou-softwire Date: Thu, 16 Apr 2026 14:42:22 +0100 Subject: [PATCH 4/7] fixup! CLDC-4331: Add model for download records remove organisation type --- app/models/download_record.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/app/models/download_record.rb b/app/models/download_record.rb index 13022fe9b..96a735d47 100644 --- a/app/models/download_record.rb +++ b/app/models/download_record.rb @@ -8,7 +8,6 @@ class DownloadRecord < ApplicationRecord user: 0, lettings_log: 1, sales_log: 2, - organisation: 3, scheme: 4, }.freeze From e843e2587f8c551484397c4e8e96f5da41efa5f3 Mon Sep 17 00:00:00 2001 From: samyou-softwire Date: Thu, 16 Apr 2026 15:12:17 +0100 Subject: [PATCH 5/7] CLDC-4331: Record kind of schemes download --- app/controllers/organisations_controller.rb | 2 +- app/controllers/schemes_controller.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/controllers/organisations_controller.rb b/app/controllers/organisations_controller.rb index 6803e7825..40f367cf1 100644 --- a/app/controllers/organisations_controller.rb +++ b/app/controllers/organisations_controller.rb @@ -40,7 +40,7 @@ class OrganisationsController < ApplicationController end def email_schemes_csv - DownloadRecord.build_from_user(download_type: :scheme, download_filters: session_filters.to_s, user: current_user).save! + DownloadRecord.build_from_user(download_type: :scheme, 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 diff --git a/app/controllers/schemes_controller.rb b/app/controllers/schemes_controller.rb index 4e6b75182..ebab2ff28 100644 --- a/app/controllers/schemes_controller.rb +++ b/app/controllers/schemes_controller.rb @@ -236,7 +236,7 @@ class SchemesController < ApplicationController end def email_csv - DownloadRecord.build_from_user(download_type: :scheme, download_filters: session_filters.to_s, user: current_user).save! + DownloadRecord.build_from_user(download_type: :scheme, 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 From 10091839026782bfce1c0b22e348d044602bd959 Mon Sep 17 00:00:00 2001 From: samyou-softwire Date: Fri, 17 Apr 2026 11:19:21 +0100 Subject: [PATCH 6/7] fixup! CLDC-4331: Add model for download records fix scheme id mention location in name --- app/models/download_record.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/download_record.rb b/app/models/download_record.rb index 96a735d47..09c8ee851 100644 --- a/app/models/download_record.rb +++ b/app/models/download_record.rb @@ -8,7 +8,7 @@ class DownloadRecord < ApplicationRecord user: 0, lettings_log: 1, sales_log: 2, - scheme: 4, + scheme_location: 3, }.freeze enum download_type: DOWNLOAD_TYPE From 3afe5eb68d3fa327fed88baa7cbf0a6d6e736a59 Mon Sep 17 00:00:00 2001 From: samyou-softwire Date: Mon, 20 Apr 2026 09:45:17 +0100 Subject: [PATCH 7/7] fixup! fixup! CLDC-4331: Add model for download records --- app/controllers/organisations_controller.rb | 2 +- app/controllers/schemes_controller.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/controllers/organisations_controller.rb b/app/controllers/organisations_controller.rb index 40f367cf1..a99c78dcf 100644 --- a/app/controllers/organisations_controller.rb +++ b/app/controllers/organisations_controller.rb @@ -40,7 +40,7 @@ class OrganisationsController < ApplicationController end def email_schemes_csv - DownloadRecord.build_from_user(download_type: :scheme, download_filters: session_filters.merge({ download_type: params[:download_type] }).to_s, user: current_user).save! + 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 diff --git a/app/controllers/schemes_controller.rb b/app/controllers/schemes_controller.rb index ebab2ff28..b5e796f72 100644 --- a/app/controllers/schemes_controller.rb +++ b/app/controllers/schemes_controller.rb @@ -236,7 +236,7 @@ class SchemesController < ApplicationController end def email_csv - DownloadRecord.build_from_user(download_type: :scheme, download_filters: session_filters.merge({ download_type: params[:download_type] }).to_s, user: current_user).save! + 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