Skip to content

Commit

Permalink
Merge pull request #513 from wri/feature/obs-non-concession-activity
Browse files Browse the repository at this point in the history
Feature/Observation with non concession activity
  • Loading branch information
tsubik authored Dec 10, 2024
2 parents ee265dc + 235f0e5 commit 2cb4257
Show file tree
Hide file tree
Showing 12 changed files with 118 additions and 4 deletions.
4 changes: 3 additions & 1 deletion app/admin/observation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def self.observations

permit_params :name, :lng, :pv, :lat, :lon, :subcategory_id, :severity_id, :country_id, :operator_id, :user_type,
:validation_status, :publication_date, :observation_report_id, :location_information, :evidence_type,
:evidence_on_report, :location_accuracy, :law_id, :fmu_id, :hidden,
:evidence_on_report, :location_accuracy, :law_id, :fmu_id, :hidden, :non_concession_activity,
:actions_taken, :is_physical_place, :force_translations_from,
relevant_operator_ids: [], government_ids: [],
observation_document_ids: [],
Expand Down Expand Up @@ -423,6 +423,7 @@ def self.observations
f.input :observation_type, input_html: {disabled: true}
if allow_override
if f.object.observation_type == "operator"
f.input :non_concession_activity if f.object.country.nil? || f.object.non_concession_activity_enabled?
f.input :fmu_id,
as: :nested_select,
level_1: {
Expand Down Expand Up @@ -472,6 +473,7 @@ def self.observations
else
f.input :country, input_html: {disabled: true}
f.input :operator, input_html: {disabled: true} if f.object.observation_type == "operator"
f.input :non_concession_activity, input_html: {disabled: true} if f.object.observation_type == "operator" && (f.object.country.nil? || f.object.non_concession_activity_enabled?)
f.input :fmu, input_html: {disabled: true} if f.object.observation_type == "operator"
f.input :subcategory, input_html: {disabled: true}
f.input :severity, as: :string, input_html: {
Expand Down
21 changes: 21 additions & 0 deletions app/assets/javascripts/observations.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,27 @@ $(document).ready(function() {

changeFieldVisibility();
$('#observation_evidence_type').on('change', changeFieldVisibility);

function changeNonConcessionActivity() {
const countrySelect = $('#observation_country_id');
const operatorSelect = $('#observation_operator_id');
const fmuSelect = $('#observation_fmu_id');

const nonConcessionActivity = $('#observation_non_concession_activity');

if (nonConcessionActivity.is(':checked')) {
fmuSelect.data('parent', 'country_id');
fmuSelect.data('parent-id', countrySelect.val());
} else {
fmuSelect.data('parent', 'operator_id');
fmuSelect.data('parent-id', operatorSelect.val());
}

// TODO: this is workaround for active admin addon nested select to reinitialize the select2
// make sure when updating the active admin addon to check if this is still working
document.dispatchEvent(new Event('has_many_add:after'));
}
$('#observation_non_concession_activity').on('change', changeNonConcessionActivity);
})


9 changes: 9 additions & 0 deletions app/models/observation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ class Observation < ApplicationRecord
validates :lng, numericality: {greater_than_or_equal_to: -180, less_than_or_equal_to: 180, allow_blank: true}
validates :evidence_on_report, presence: true, if: -> { evidence_type == "Evidence presented in the report" }
validate :status_changes, if: -> { user_type.present? }
validate :can_set_non_concession_activity, if: -> { non_concession_activity? }

validates :observers, presence: true
validates :observation_type, presence: true
Expand Down Expand Up @@ -301,6 +302,10 @@ def qc_metadata(qc_passed:)
}
end

def non_concession_activity_enabled?
country&.iso == "COD"
end

private

def nullify_evidence_on_report
Expand Down Expand Up @@ -382,6 +387,10 @@ def status_changes
"Invalid validation change for #{@user_type}. Can't move from '#{validation_status_was}' to '#{validation_status}'")
end

def can_set_non_concession_activity
errors.add(:non_concession_activity, :not_allowed_for_country) unless non_concession_activity_enabled?
end

def remove_documents
self.observation_documents = []
end
Expand Down
18 changes: 16 additions & 2 deletions app/resources/v1/observation_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class ObservationResource < BaseResource
attributes :observation_type, :publication_date, :pv, :is_active,
:details, :evidence_type, :evidence_on_report, :concern_opinion,
:litigation_status, :location_accuracy, :lat, :lng, :country_id,
:fmu_id, :location_information, :subcategory_id, :severity_id,
:fmu_id, :non_concession_activity, :location_information, :subcategory_id, :severity_id,
:created_at, :updated_at, :actions_taken, :validation_status, :validation_status_id,
:is_physical_place, :hidden, :user_type, :monitor_comment, :locale

Expand Down Expand Up @@ -73,10 +73,24 @@ class ObservationResource < BaseResource
records.where(id: records.joins(:observers).where(observers: {id: value}).pluck(:id))
}

filter :fmu_id, apply: ->(records, value, options) {
context = options[:context]
user = context[:current_user]

if context[:app] == "observations-tool" && user.present? && ["ngo", "ngo_manager", "admin"].include?(user.user_permission.user_role)
records.where(fmu_id: value)
else
# prevent searching up observations with non concession activity by fmu_id
records.where(fmu_id: value, non_concession_activity: false)
end
}

def fetchable_fields
return super if observations_tool_user?
non_fetchable_fields = [:monitor_comment, :created_at, :updated_at, :user, :modified_user]
non_fetchable_fields.concat([:fmu, :fmu_id]) if non_concession_activity

super - [:monitor_comment, :created_at, :updated_at, :user, :modified_user]
super - non_fetchable_fields
end

def self.sortable_fields(context)
Expand Down
1 change: 1 addition & 0 deletions app/views/admin/observations/_attributes_table.html.arb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ panel I18n.t("active_admin.observations_page.details") do
end
row :is_physical_place
row :location_information if observation.location_information.present?
row :non_concession_activity if observation.operator? && observation.non_concession_activity_enabled?
row :fmu if observation.fmu.present?
row :operator if observation.operator?
row :governments if observation.government?
Expand Down
2 changes: 2 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -833,6 +833,7 @@ en:
deleted_at: Deleted at #g
evidence_on_report: Evidence on report #g
evidence_type: Evidence type #g
non_concession_activity: Producer conducts activities in a concession that is attributed to another producer
fmu: :activerecord.models.fmu #g
governments: Governments #g
governments_observations: Governments observations #g
Expand Down Expand Up @@ -1478,6 +1479,7 @@ en:
errors:
messages:
password_complexity: "must contain at least one uppercase letter, one lowercase letter, and one digit"
not_allowed_for_country: "is not allowed for this country"
qc_in_progress: "QC must be in progress"
record_invalid: "Validation failed: %{errors}"
restrict_dependent_destroy:
Expand Down
2 changes: 2 additions & 0 deletions config/locales/fr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,7 @@ fr:
evidence_on_report: Preuve sur le rapport #g
evidence_type: Type de preuve #g
fmu: :activerecord.models.fmu #g
non_concession_activity: Le producteur exerce des activités dans une concession attribuée à un autre producteur
governments: Gouvernements #g
governments_observations: Observations des gouvernements #g
hidden: Invisible #g
Expand Down Expand Up @@ -1397,6 +1398,7 @@ fr:
errors:
messages:
password_complexity: "doit contenir au moins une lettre majuscule, une lettre minuscule et un chiffre"
not_allowed_for_country: "n'est pas autorisé pour ce pays"
qc_in_progress: "Le contrôle qualité doit être en cours"
record_invalid: 'La validation a échoué : %{errors}'
restrict_dependent_destroy:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddNonConcessionActivityToObservations < ActiveRecord::Migration[7.1]
def change
add_column :observations, :non_concession_activity, :boolean, default: false, null: false
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.1].define(version: 2024_09_18_130301) do
ActiveRecord::Schema[7.1].define(version: 2024_10_09_073948) do
create_schema "tiger"
create_schema "tiger_data"
create_schema "topology"
Expand Down Expand Up @@ -581,6 +581,7 @@
t.text "monitor_comment"
t.datetime "deleted_at", precision: nil
t.string "locale"
t.boolean "non_concession_activity", default: false, null: false
t.index ["country_id"], name: "index_observations_on_country_id"
t.index ["created_at"], name: "index_observations_on_created_at"
t.index ["deleted_at"], name: "index_observations_on_deleted_at"
Expand Down
1 change: 1 addition & 0 deletions spec/factories/observations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
is_active { true }
validation_status { "Published (no comments)" }
is_physical_place { true }
non_concession_activity { false }
lng { 12.2222 }
lat { 12.3333 }

Expand Down
41 changes: 41 additions & 0 deletions spec/integration/v1/observations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,47 @@ module V1
end
end

describe "non concession activity" do
let(:fmu) { create(:fmu) }
let(:report) { create(:observation_report, observers: [ngo_observer]) }
let!(:observation) { create(:observation, observation_report: report, country: create(:country, iso: "COD"), non_concession_activity: true, fmu: fmu) }

context "observation tool user" do
it "shows fmu for non concession activity observations" do
get "/observations", params: {app: "observations-tool", include: "fmu"}, headers: ngo_headers
expect(status).to eq(200)
expect(parsed_data.size).to eq(1)
expect(parsed_data.first[:id].to_i).to eq(observation.id)
expect(parsed_data.first[:attributes][:"fmu-id"]&.to_i).to eq(fmu.id)
expect(parsed_data.first[:relationships][:fmu][:data][:id].to_i).to eq(fmu.id)
end

it "finds observation using fmu filter" do
get "/observations", params: {app: "observations-tool", "filter[fmu_id]": fmu.id}, headers: ngo_headers
expect(status).to eq(200)
expect(parsed_data.size).to eq(1)
expect(parsed_data.first[:id].to_i).to eq(observation.id)
end
end

context "non observation tool user" do
it "does not show fmu for non concession activity observations" do
get "/observations", params: {app: "observations-tool", include: "fmu"}, headers: webuser_headers
expect(status).to eq(200)
expect(parsed_data.size).to eq(1)
expect(parsed_data.first[:id].to_i).to eq(observation.id)
expect(parsed_data.first[:attributes][:"fmu-id"]).to eq(nil)
expect(parsed_data.first[:relationships][:fmu]).to eq(nil)
end

it "cannot find observation using fmu filter" do
get "/observations", params: {app: "observations-tool", "filter[fmu_id]": fmu.id}, headers: webuser_headers
expect(status).to eq(200)
expect(parsed_data.size).to eq(0)
end
end
end

context "Create observations" do
describe "For admin user" do
it "Returns error object when the observation cannot be created by admin" do
Expand Down
15 changes: 15 additions & 0 deletions spec/models/observation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,21 @@
expect(subject.errors[:location_accuracy]).to include("is not included in the list")
end

describe "non concession activity" do
it "should be valid if checked for observation in COD" do
subject.country = build(:country, iso: "COD")
subject.non_concession_activity = true
expect(subject).to be_valid
end

it "should be invalid if checked for observation not in COD" do
subject.country = build(:country, iso: "POL")
subject.non_concession_activity = true
expect(subject).not_to be_valid
expect(subject.errors[:non_concession_activity]).to include("is not allowed for this country")
end
end

describe "#status_changes" do
let(:country) { create(:country) }
let(:status) { "Created" }
Expand Down

0 comments on commit 2cb4257

Please sign in to comment.