From 84971d0033997df73f026ab80d98b21cd798af58 Mon Sep 17 00:00:00 2001 From: samyou-softwire Date: Wed, 15 Apr 2026 15:16:10 +0100 Subject: [PATCH] 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"