Browse Source

CLDC-4331: Add model for download records

CLDC-4331-make-downloads-traceable
samyou-softwire 3 weeks ago
parent
commit
84971d0033
  1. 26
      app/models/download_record.rb
  2. 13
      db/migrate/20260415101455_create_download_record.rb
  3. 16
      db/schema.rb

26
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

13
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

16
db/schema.rb

@ -10,7 +10,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # 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 # These are extensions that must be enabled in order to support this database
enable_extension "plpgsql" 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" t.index ["organisation_id"], name: "index_data_protection_confirmations_on_organisation_id"
end 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| create_table "exports", force: :cascade do |t|
t.datetime "created_at", default: -> { "CURRENT_TIMESTAMP" } t.datetime "created_at", default: -> { "CURRENT_TIMESTAMP" }
t.datetime "started_at", null: false 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" t.index ["item_type", "item_id"], name: "index_versions_on_item_type_and_item_id"
end 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", "locations"
add_foreign_key "lettings_logs", "organisations", column: "owning_organisation_id", on_delete: :cascade add_foreign_key "lettings_logs", "organisations", column: "owning_organisation_id", on_delete: :cascade
add_foreign_key "lettings_logs", "schemes" add_foreign_key "lettings_logs", "schemes"

Loading…
Cancel
Save