From f1a501f76764c15f8a039dc94cef45759c4d2afd Mon Sep 17 00:00:00 2001 From: Tomasz Subik Date: Fri, 22 Dec 2023 18:03:13 +0100 Subject: [PATCH 1/5] create observation document - observation join table --- app/admin/observation_document.rb | 13 +++---- app/models/observation.rb | 2 +- app/models/observation_document.rb | 2 +- ...reate_observation_document_observations.rb | 34 +++++++++++++++++++ db/schema.rb | 17 +++++++--- 5 files changed, 54 insertions(+), 14 deletions(-) create mode 100644 db/migrate/20231222154005_create_observation_document_observations.rb diff --git a/app/admin/observation_document.rb b/app/admin/observation_document.rb index 7b1182dab..5fe5e43d4 100644 --- a/app/admin/observation_document.rb +++ b/app/admin/observation_document.rb @@ -24,9 +24,6 @@ csv do column :id - column :observation do |od| - od.observation&.id - end column :name column :user do |od| od.user&.name @@ -38,7 +35,7 @@ index do column :id - column :observation, sortable: "observation_id" + column :observations column :name column :attachment do |o| link_to o&.name, o.attachment&.url if o.attachment&.url @@ -62,7 +59,7 @@ end end - filter :observation, as: :select, collection: -> { Observation.joins(:observation_documents).distinct.order(:id).pluck(:id) } + filter :observations, as: :select, collection: -> { Observation.joins(:observation_documents).distinct.order(:id).pluck(:id) } filter :name, as: :select filter :attachment, as: :select filter :user @@ -73,7 +70,7 @@ form do |f| f.semantic_errors(*f.object.errors.attribute_names) f.inputs do - f.input :observation, collection: Observation.all.map { |o| [o.id, o.id] }, input_html: {disabled: true} + f.input :observations, input_html: {disabled: true} f.input :user, input_html: {disabled: true} f.input :name f.input :attachment, as: :file, hint: f.object&.attachment&.file&.filename @@ -85,7 +82,7 @@ show do attributes_table do row :id - row :observation + row :observations row :attachment do |o| link_to o&.name, o.attachment&.url if o.attachment&.url end @@ -99,7 +96,7 @@ controller do def scoped_collection - end_of_association_chain.includes(:user) + end_of_association_chain.includes(:user, :observations) end end end diff --git a/app/models/observation.rb b/app/models/observation.rb index dfe90f19c..4bc9e7e12 100644 --- a/app/models/observation.rb +++ b/app/models/observation.rb @@ -109,7 +109,7 @@ class Observation < ApplicationRecord has_many :observation_operators, dependent: :destroy has_many :relevant_operators, through: :observation_operators, source: :operator - has_many :observation_documents, dependent: :destroy + has_and_belongs_to_many :observation_documents, inverse_of: :observations accepts_nested_attributes_for :observation_documents, allow_destroy: true accepts_nested_attributes_for :observation_report, allow_destroy: true diff --git a/app/models/observation_document.rb b/app/models/observation_document.rb index fdfaa5972..99bf16572 100644 --- a/app/models/observation_document.rb +++ b/app/models/observation_document.rb @@ -23,7 +23,7 @@ class ObservationDocument < ApplicationRecord # TODO: only nils in the database, maybe that should be removed? belongs_to :user, inverse_of: :observation_documents, touch: true, optional: true - belongs_to :observation, inverse_of: :observation_documents, touch: true + has_and_belongs_to_many :observations, inverse_of: :observation_documents skip_callback :commit, :after, :remove_attachment! after_destroy :move_attachment_to_private_directory diff --git a/db/migrate/20231222154005_create_observation_document_observations.rb b/db/migrate/20231222154005_create_observation_document_observations.rb new file mode 100644 index 000000000..295fc3d9a --- /dev/null +++ b/db/migrate/20231222154005_create_observation_document_observations.rb @@ -0,0 +1,34 @@ +class CreateObservationDocumentObservations < ActiveRecord::Migration[7.0] + def up + create_table :observation_documents_observations do |t| + t.belongs_to :observation_document, foreign_key: {on_delete: :cascade}, index: {name: "observation_documents_observations_doc_index"}, null: false + t.belongs_to :observation, foreign_key: {on_delete: :cascade}, index: {name: "observation_documents_observations_obs_index"}, null: false + t.index [:observation_document_id, :observation_id], unique: true, name: "observation_documents_observations_double_index" + t.timestamps + end + + query = <<-SQL + INSERT INTO observation_documents_observations (observation_document_id, observation_id, created_at, updated_at) + SELECT + od.id, o.id, NOW(), NOW() + FROM + observation_documents od JOIN observations o ON od.observation_id = o.id + SQL + execute(query) + + remove_reference :observation_documents, :observation + end + + def down + add_reference :observation_documents, :observation, foreign_key: true, index: true + + query = <<-SQL + UPDATE observation_documents SET observation_id = odo.observation_id + FROM (SELECT DISTINCT ON (observation_document_id) observation_document_id, observation_id FROM observation_documents_observations) odo + WHERE observation_documents.id = odo.observation_document_id + SQL + execute(query) + + drop_table :observation_documents_observations + end +end diff --git a/db/schema.rb b/db/schema.rb index a95e4f220..8dc98a28e 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.0].define(version: 2023_11_30_113839) do +ActiveRecord::Schema[7.0].define(version: 2023_12_22_154005) do # These are extensions that must be enabled in order to support this database enable_extension "address_standardizer" enable_extension "address_standardizer_data_us" @@ -391,13 +391,21 @@ t.datetime "updated_at", precision: nil, null: false t.integer "user_id" t.datetime "deleted_at", precision: nil - t.integer "observation_id" t.index ["deleted_at"], name: "index_observation_documents_on_deleted_at" t.index ["name"], name: "index_observation_documents_on_name" - t.index ["observation_id"], name: "index_observation_documents_on_observation_id" t.index ["user_id"], name: "index_observation_documents_on_user_id" end + create_table "observation_documents_observations", force: :cascade do |t| + t.bigint "observation_document_id", null: false + t.bigint "observation_id", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["observation_document_id", "observation_id"], name: "observation_documents_observations_double_index", unique: true + t.index ["observation_document_id"], name: "observation_documents_observations_doc_index" + t.index ["observation_id"], name: "observation_documents_observations_obs_index" + end + create_table "observation_histories", id: :serial, force: :cascade do |t| t.integer "validation_status" t.integer "observation_type" @@ -1116,8 +1124,9 @@ add_foreign_key "notifications", "notification_groups", on_delete: :nullify add_foreign_key "notifications", "operator_documents", on_delete: :cascade add_foreign_key "notifications", "users", on_delete: :cascade - add_foreign_key "observation_documents", "observations" add_foreign_key "observation_documents", "users" + add_foreign_key "observation_documents_observations", "observation_documents", on_delete: :cascade + add_foreign_key "observation_documents_observations", "observations", on_delete: :cascade add_foreign_key "observation_histories", "categories", on_delete: :cascade add_foreign_key "observation_histories", "countries", on_delete: :cascade add_foreign_key "observation_histories", "fmus", on_delete: :cascade From bc813a7e4224f1ed822eaa174fa324847cbe1b7d Mon Sep 17 00:00:00 2001 From: Tomasz Subik Date: Wed, 3 Jan 2024 15:57:20 +0100 Subject: [PATCH 2/5] move evidence type to observation document and name it as document_type --- app/admin/observation_document.rb | 4 ++ app/importers/observations_importer.rb | 2 +- app/models/observation.rb | 30 +++++++-------- app/models/observation_document.rb | 7 +++- .../observation_document_uploader.rb | 2 +- .../resource/_attributes_table.html.arb | 2 + config/environments/development.rb | 2 + ...d_document_type_to_observation_document.rb | 38 +++++++++++++++++++ db/schema.rb | 3 +- spec/factories/observations.rb | 2 +- 10 files changed, 70 insertions(+), 22 deletions(-) create mode 100644 db/migrate/20231222171303_add_document_type_to_observation_document.rb diff --git a/app/admin/observation_document.rb b/app/admin/observation_document.rb index 5fe5e43d4..e1c2f4745 100644 --- a/app/admin/observation_document.rb +++ b/app/admin/observation_document.rb @@ -25,6 +25,7 @@ csv do column :id column :name + column :document_type column :user do |od| od.user&.name end @@ -37,6 +38,7 @@ column :id column :observations column :name + column :document_type column :attachment do |o| link_to o&.name, o.attachment&.url if o.attachment&.url end @@ -73,6 +75,7 @@ f.input :observations, input_html: {disabled: true} f.input :user, input_html: {disabled: true} f.input :name + f.input :document_type f.input :attachment, as: :file, hint: f.object&.attachment&.file&.filename f.actions @@ -83,6 +86,7 @@ attributes_table do row :id row :observations + row :document_type row :attachment do |o| link_to o&.name, o.attachment&.url if o.attachment&.url end diff --git a/app/importers/observations_importer.rb b/app/importers/observations_importer.rb index cb37daf31..6d6addda2 100644 --- a/app/importers/observations_importer.rb +++ b/app/importers/observations_importer.rb @@ -4,7 +4,7 @@ class ObservationsImporter < FileDataImport::BaseImporter PERMITTED_ATTRIBUTES = %i[ observation_type publication_date pv is_active location_accuracy - lat lng fmu_id evidence_type location_information actions_taken + lat lng fmu_id location_information actions_taken evidence_on_report validation_status is_physical_place user_id observer_ids ].freeze diff --git a/app/models/observation.rb b/app/models/observation.rb index 4bc9e7e12..bcbc152df 100644 --- a/app/models/observation.rb +++ b/app/models/observation.rb @@ -26,7 +26,6 @@ # law_id :integer # location_information :string # is_physical_place :boolean default(TRUE), not null -# evidence_type :integer # location_accuracy :integer # evidence_on_report :string # hidden :boolean default(FALSE), not null @@ -50,18 +49,18 @@ class Observation < ApplicationRecord translates :details, :concern_opinion, :litigation_status, touch: true, versioning: :paper_trail, paranoia: true active_admin_translates :details, :concern_opinion, :litigation_status + enum evidence_type: { + "No Evidence" => 0, "Uploaded documents" => 1, "Evidence presented in the report" => 2 + } enum observation_type: {"operator" => 0, "government" => 1} enum validation_status: {"Created" => 0, "Ready for QC" => 1, "QC in progress" => 2, "Approved" => 3, "Rejected" => 4, "Needs revision" => 5, "Ready for publication" => 6, "Published (no comments)" => 7, "Published (not modified)" => 8, "Published (modified)" => 9} - enum evidence_type: {"Government Documents" => 0, "Company Documents" => 1, "Photos" => 2, - "Testimony from local communities" => 3, "Other" => 4, "Evidence presented in the report" => 5, - "Maps" => 6} enum location_accuracy: {"Estimated location" => 0, "GPS coordinates extracted from photo" => 1, "Accurate GPS coordinates" => 2} - validate_enum_attributes :observation_type, :evidence_type, :location_accuracy + validate_enum_attributes :evidence_type, :observation_type, :location_accuracy STATUS_TRANSITIONS = { monitor: { @@ -125,7 +124,7 @@ class Observation < ApplicationRecord validate :active_government end - validate :evidence_presented_in_the_report + validates :evidence_on_report, presence: true, if: -> { evidence_type == "Evidence presented in the report" } validate :status_changes, if: -> { user_type.present? } validates :observers, presence: true @@ -134,6 +133,8 @@ class Observation < ApplicationRecord validates :admin_comment, presence: true, if: -> { validation_status == "Needs revision" } + before_validation :nullify_evidence_on_report, if: -> { evidence_type != "Evidence presented in the report" } + before_save :set_active_status before_save :check_is_physical_place before_save :set_centroid @@ -150,7 +151,7 @@ class Observation < ApplicationRecord after_save :update_reports_observers, if: :saved_change_to_observation_report_id? after_save :create_history, if: :saved_changes? - after_save :remove_documents, if: -> { evidence_type == "Evidence presented in the report" } + after_save :remove_documents, if: -> { evidence_type != "Uploaded documents" } after_save :update_fmu_geojson after_commit :notify_about_creation, on: :create @@ -225,6 +226,10 @@ def published? private + def nullify_evidence_on_report + self.evidence_on_report = nil + end + def check_is_physical_place return if is_physical_place @@ -302,17 +307,8 @@ def status_changes "Invalid validation change for #{@user_type}. Can't move from '#{validation_status_was}' to '#{validation_status}'") end - def evidence_presented_in_the_report - if evidence_type == "Evidence presented in the report" && evidence_on_report.blank? - errors.add(:evidence_on_report, "You must add information on where to find the evidence on the report") - end - if evidence_type != "Evidence presented in the report" && evidence_on_report.present? - errors.add(:evidence_on_report, "This field can only be present when the evidence is presented on the report") - end - end - def remove_documents - ObservationDocument.where(observation_id: id).destroy_all + self.observation_documents = [] end # If the observation is for an fmu, it updates its geojson with the new count diff --git a/app/models/observation_document.rb b/app/models/observation_document.rb index 99bf16572..5a7c844f9 100644 --- a/app/models/observation_document.rb +++ b/app/models/observation_document.rb @@ -16,10 +16,15 @@ class ObservationDocument < ApplicationRecord has_paper_trail + acts_as_paranoid mount_base64_uploader :attachment, ObservationDocumentUploader include MoveableAttachment - acts_as_paranoid + enum document_type: { + "Government Documents" => 0, "Company Documents" => 1, "Photos" => 2, + "Testimony from local communities" => 3, "Other" => 4, "Maps" => 5 + } + validate_enum_attributes :document_type # TODO: only nils in the database, maybe that should be removed? belongs_to :user, inverse_of: :observation_documents, touch: true, optional: true diff --git a/app/uploaders/observation_document_uploader.rb b/app/uploaders/observation_document_uploader.rb index bd37cb46b..ede028cbe 100644 --- a/app/uploaders/observation_document_uploader.rb +++ b/app/uploaders/observation_document_uploader.rb @@ -14,7 +14,7 @@ def filename else [ model.id, - (model.observation.evidence_type || "other").parameterize + (model.document_type || "other").parameterize ].join("-") end diff --git a/app/views/active_admin/resource/_attributes_table.html.arb b/app/views/active_admin/resource/_attributes_table.html.arb index 784e776ae..7a2ee9e40 100644 --- a/app/views/active_admin/resource/_attributes_table.html.arb +++ b/app/views/active_admin/resource/_attributes_table.html.arb @@ -27,6 +27,8 @@ panel I18n.t("active_admin.observations_page.details") do render partial: "map", locals: {center: [r.lng, r.lat], geojson: r.fmu&.geojson, bbox: r.fmu&.bbox} end end + row :evidence_type + row :evidence_on_report row :actions_taken row :concern_opinion row :observation_report diff --git a/config/environments/development.rb b/config/environments/development.rb index aef45a663..771d7896a 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -65,4 +65,6 @@ # Uncomment if you wish to allow Action Cable access from any origin. # config.action_cable.disable_request_forgery_protection = true + + # config.active_record.migration_error = false end diff --git a/db/migrate/20231222171303_add_document_type_to_observation_document.rb b/db/migrate/20231222171303_add_document_type_to_observation_document.rb new file mode 100644 index 000000000..aff814b13 --- /dev/null +++ b/db/migrate/20231222171303_add_document_type_to_observation_document.rb @@ -0,0 +1,38 @@ +class AddDocumentTypeToObservationDocument < ActiveRecord::Migration[7.0] + def up + add_column :observation_documents, :document_type, :integer, default: 0, null: false + # 5 is evidence on report in observations table + # changing Maps from 6 to 5 + query = <<~SQL + UPDATE observation_documents SET document_type = odo.evidence_type, updated_at = NOW() + FROM ( + SELECT DISTINCT ON (observation_document_id) observation_document_id, evidence_type + FROM observation_documents_observations + INNER JOIN observations ON observations.id = observation_documents_observations.observation_id + WHERE evidence_type <> 5 AND evidence_type IS NOT NULL + ) odo + WHERE observation_documents.id = odo.observation_document_id + SQL + execute(query) + execute("UPDATE observation_documents SET document_type = 5 WHERE document_type = 6") + execute("UPDATE observations SET evidence_type = 2 WHERE evidence_type = 5") # update for evidence on report + execute("UPDATE observations SET evidence_type = 1 WHERE EXISTS (SELECT * FROM observation_documents_observations where observation_id = observations.id)") # update for docuements + execute("UPDATE observations SET evidence_type = 0 WHERE evidence_type is null") # for the rest + end + + def down + query = <<~SQL + UPDATE observations SET evidence_type = odo.document_type, updated_at = NOW() + FROM ( + SELECT DISTINCT ON (observation_id) observation_id, document_type + FROM observation_documents_observations + INNER JOIN observation_documents ON observation_documents.id = observation_documents_observations.observation_document_id + ) odo + WHERE observations.id = odo.observation_id + SQL + execute(query) + execute("UPDATE observations SET evidence_type = 6 WHERE evidence_type = 5") + execute("UPDATE observations SET evidence_type = 5 WHERE evidence_type is null and coalesce(evidence_on_report, '') <> ''") + remove_column :observation_documents, :document_type + end +end diff --git a/db/schema.rb b/db/schema.rb index 8dc98a28e..159427a53 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.0].define(version: 2023_12_22_154005) do +ActiveRecord::Schema[7.0].define(version: 2023_12_22_171303) do # These are extensions that must be enabled in order to support this database enable_extension "address_standardizer" enable_extension "address_standardizer_data_us" @@ -391,6 +391,7 @@ t.datetime "updated_at", precision: nil, null: false t.integer "user_id" t.datetime "deleted_at", precision: nil + t.integer "document_type", default: 0, null: false t.index ["deleted_at"], name: "index_observation_documents_on_deleted_at" t.index ["name"], name: "index_observation_documents_on_name" t.index ["user_id"], name: "index_observation_documents_on_user_id" diff --git a/spec/factories/observations.rb b/spec/factories/observations.rb index 399d0cca8..ecd36443d 100644 --- a/spec/factories/observations.rb +++ b/spec/factories/observations.rb @@ -47,7 +47,7 @@ observers { build_list(:observer, 1) } observation_type { "operator" } is_active { true } - evidence_type { "Photos" } + evidence_type { "No evidence" } publication_date { DateTime.now.to_date } location_accuracy { "Estimated location" } lng { 12.2222 } From 6e2ee5181f25607398425dd57c783b73402775e8 Mon Sep 17 00:00:00 2001 From: Tomasz Subik Date: Fri, 5 Jan 2024 18:23:21 +0100 Subject: [PATCH 3/5] fix migration --- ...d_document_type_to_observation_document.rb | 45 ++++++++++++++++++- 1 file changed, 43 insertions(+), 2 deletions(-) diff --git a/db/migrate/20231222171303_add_document_type_to_observation_document.rb b/db/migrate/20231222171303_add_document_type_to_observation_document.rb index aff814b13..4a4cdc2fe 100644 --- a/db/migrate/20231222171303_add_document_type_to_observation_document.rb +++ b/db/migrate/20231222171303_add_document_type_to_observation_document.rb @@ -15,9 +15,50 @@ def up SQL execute(query) execute("UPDATE observation_documents SET document_type = 5 WHERE document_type = 6") + execute("UPDATE observations SET evidence_type = 0 WHERE evidence_type <> 5") # set no evidence for all except evidence on report execute("UPDATE observations SET evidence_type = 2 WHERE evidence_type = 5") # update for evidence on report - execute("UPDATE observations SET evidence_type = 1 WHERE EXISTS (SELECT * FROM observation_documents_observations where observation_id = observations.id)") # update for docuements - execute("UPDATE observations SET evidence_type = 0 WHERE evidence_type is null") # for the rest + execute( + <<~SQL + UPDATE observations SET evidence_type = 1 WHERE EXISTS ( + SELECT * FROM observation_documents_observations odo + INNER JOIN observation_documents od ON od.id = odo.observation_document_id + WHERE observation_id = observations.id AND od.deleted_at IS NULL + ) + SQL + ) # update for linked with non deleted documents + + # Testing data after migration + # I was testing migration correctness with those queries so I will leave them here + result = execute( + "SELECT id FROM observations WHERE evidence_type <> 2 AND coalesce(evidence_on_report, '') <> ''" + ) + raise "Mismatched type when evidence_on_report present" if result.count.positive? + result = execute( + <<~SQL + SELECT id FROM observations WHERE evidence_type <> 1 AND EXISTS ( + SELECT * FROM + observation_documents_observations odo + INNER JOIN observation_documents od on od.id = odo.observation_document_id + WHERE odo.observation_id = observations.id AND od.deleted_at IS NULL + ) + SQL + ) + raise "Documents exists for observations with no linked documents" if result.count.positive? + result = execute( + <<~SQL + SELECT id FROM observations WHERE evidence_type = 1 AND NOT EXISTS ( + SELECT * FROM + observation_documents_observations odo + INNER JOIN observation_documents od on od.id = odo.observation_document_id + WHERE odo.observation_id = observations.id AND od.deleted_at IS NULL + ) + SQL + ) + raise "No documents when evidence type is 1: #{result.values.flatten}" if result.count.positive? + result = execute( + "SELECT id FROM observations WHERE evidence_type = 0 AND validation_status <> 0" + ) + Rails.logger.debug "No evidence for non created (non drafted) observations #{result.values.flatten}" if result.count.positive? end def down From ca3e55d81ec4ebd690d40a0fd2ab109f0bb6fbb0 Mon Sep 17 00:00:00 2001 From: Tomasz Subik Date: Fri, 5 Jan 2024 18:55:59 +0100 Subject: [PATCH 4/5] add observation_report reference to observation documents --- app/admin/observation_document.rb | 6 ++++++ app/models/observation_document.rb | 1 + app/models/observation_report.rb | 1 + ...servation_report_to_observation_document.rb | 18 ++++++++++++++++++ db/schema.rb | 5 ++++- 5 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20240105172847_add_observation_report_to_observation_document.rb diff --git a/app/admin/observation_document.rb b/app/admin/observation_document.rb index e1c2f4745..68791d456 100644 --- a/app/admin/observation_document.rb +++ b/app/admin/observation_document.rb @@ -36,6 +36,7 @@ index do column :id + column :observation_report column :observations column :name column :document_type @@ -62,6 +63,9 @@ end filter :observations, as: :select, collection: -> { Observation.joins(:observation_documents).distinct.order(:id).pluck(:id) } + filter :observation_report, + label: -> { I18n.t("activerecord.models.observation_report") }, as: :select, + collection: -> { ObservationReport.where(id: ObservationDocument.select(:observation_report_id)).order(:title) } filter :name, as: :select filter :attachment, as: :select filter :user @@ -72,6 +76,7 @@ form do |f| f.semantic_errors(*f.object.errors.attribute_names) f.inputs do + f.input :observation_report, input_html: {disabled: true} f.input :observations, input_html: {disabled: true} f.input :user, input_html: {disabled: true} f.input :name @@ -85,6 +90,7 @@ show do attributes_table do row :id + row :observation_report row :observations row :document_type row :attachment do |o| diff --git a/app/models/observation_document.rb b/app/models/observation_document.rb index 5a7c844f9..c6129b640 100644 --- a/app/models/observation_document.rb +++ b/app/models/observation_document.rb @@ -28,6 +28,7 @@ class ObservationDocument < ApplicationRecord # TODO: only nils in the database, maybe that should be removed? belongs_to :user, inverse_of: :observation_documents, touch: true, optional: true + belongs_to :observation_report, inverse_of: :observation_documents, touch: true, optional: true has_and_belongs_to_many :observations, inverse_of: :observation_documents skip_callback :commit, :after, :remove_attachment! diff --git a/app/models/observation_report.rb b/app/models/observation_report.rb index 91e5fc766..a36bd0995 100644 --- a/app/models/observation_report.rb +++ b/app/models/observation_report.rb @@ -27,6 +27,7 @@ class ObservationReport < ApplicationRecord has_many :observation_report_observers, dependent: :destroy has_many :observers, through: :observation_report_observers has_many :observations, dependent: :destroy + has_many :observation_documents, inverse_of: :observation_report, dependent: :destroy skip_callback :commit, :after, :remove_attachment! after_destroy :move_attachment_to_private_directory diff --git a/db/migrate/20240105172847_add_observation_report_to_observation_document.rb b/db/migrate/20240105172847_add_observation_report_to_observation_document.rb new file mode 100644 index 000000000..582080aea --- /dev/null +++ b/db/migrate/20240105172847_add_observation_report_to_observation_document.rb @@ -0,0 +1,18 @@ +class AddObservationReportToObservationDocument < ActiveRecord::Migration[7.0] + def change + add_reference :observation_documents, :observation_report, foreign_key: true, index: true + + reversible do |dir| + dir.up do + execute( + <<~SQL + UPDATE observation_documents SET observation_report_id = o.observation_report_id + FROM observations o + INNER JOIN observation_documents_observations odo ON odo.observation_id = o.id + WHERE odo.observation_document_id = observation_documents.id + SQL + ) + end + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 159427a53..f89d32596 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.0].define(version: 2023_12_22_171303) do +ActiveRecord::Schema[7.0].define(version: 2024_01_05_172847) do # These are extensions that must be enabled in order to support this database enable_extension "address_standardizer" enable_extension "address_standardizer_data_us" @@ -392,8 +392,10 @@ t.integer "user_id" t.datetime "deleted_at", precision: nil t.integer "document_type", default: 0, null: false + t.bigint "observation_report_id" t.index ["deleted_at"], name: "index_observation_documents_on_deleted_at" t.index ["name"], name: "index_observation_documents_on_name" + t.index ["observation_report_id"], name: "index_observation_documents_on_observation_report_id" t.index ["user_id"], name: "index_observation_documents_on_user_id" end @@ -1125,6 +1127,7 @@ add_foreign_key "notifications", "notification_groups", on_delete: :nullify add_foreign_key "notifications", "operator_documents", on_delete: :cascade add_foreign_key "notifications", "users", on_delete: :cascade + add_foreign_key "observation_documents", "observation_reports" add_foreign_key "observation_documents", "users" add_foreign_key "observation_documents_observations", "observation_documents", on_delete: :cascade add_foreign_key "observation_documents_observations", "observations", on_delete: :cascade From 649609d8c2b4614619f23bc76f75a17ccac264b0 Mon Sep 17 00:00:00 2001 From: Tomasz Subik Date: Tue, 9 Jan 2024 13:25:08 +0100 Subject: [PATCH 5/5] API changes for multiple evidences --- app/models/observation.rb | 4 +--- app/models/observation_history.rb | 4 +--- app/resources/v1/observation_document_resource.rb | 11 ++++++++--- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/app/models/observation.rb b/app/models/observation.rb index bcbc152df..5ead77b65 100644 --- a/app/models/observation.rb +++ b/app/models/observation.rb @@ -49,9 +49,7 @@ class Observation < ApplicationRecord translates :details, :concern_opinion, :litigation_status, touch: true, versioning: :paper_trail, paranoia: true active_admin_translates :details, :concern_opinion, :litigation_status - enum evidence_type: { - "No Evidence" => 0, "Uploaded documents" => 1, "Evidence presented in the report" => 2 - } + enum evidence_type: {"No evidence" => 0, "Uploaded documents" => 1, "Evidence presented in the report" => 2} enum observation_type: {"operator" => 0, "government" => 1} enum validation_status: {"Created" => 0, "Ready for QC" => 1, "QC in progress" => 2, "Approved" => 3, "Rejected" => 4, "Needs revision" => 5, "Ready for publication" => 6, diff --git a/app/models/observation_history.rb b/app/models/observation_history.rb index 82e2e5714..3acd1f400 100644 --- a/app/models/observation_history.rb +++ b/app/models/observation_history.rb @@ -33,9 +33,7 @@ class ObservationHistory < ApplicationRecord "Rejected" => 4, "Needs revision" => 5, "Ready for publication" => 6, "Published (no comments)" => 7, "Published (not modified)" => 8, "Published (modified)" => 9} - enum evidence_type: {"Government Documents" => 0, "Company Documents" => 1, "Photos" => 2, - "Testimony from local communities" => 3, "Other" => 4, "Evidence presented in the report" => 5, - "Maps" => 6} + enum evidence_type: {"No evidence" => 0, "Uploaded documents" => 1, "Evidence presented in the report" => 2} enum location_accuracy: {"Estimated location" => 0, "GPS coordinates extracted from photo" => 1, "Accurate GPS coordinates" => 2} enum fmu_forest_type: ForestType::TYPES_WITH_CODE diff --git a/app/resources/v1/observation_document_resource.rb b/app/resources/v1/observation_document_resource.rb index 67bbd5269..f4255e97c 100644 --- a/app/resources/v1/observation_document_resource.rb +++ b/app/resources/v1/observation_document_resource.rb @@ -4,10 +4,15 @@ module V1 class ObservationDocumentResource < BaseResource caching - attributes :name, :attachment, :created_at, :updated_at + attributes :name, :attachment, :document_type, :created_at, :updated_at - has_one :observation + has_many :observations + has_one :observation_report - filters :observation_id, :name + filters :observation_report_id, :name + + filter :observation_id, apply: ->(records, value, _options) { + records.where(id: ObservationDocument.joins(:observations).where(observations: value)) + } end end