diff --git a/app/controllers/api/v2/green_lanes/category_assessments_controller.rb b/app/controllers/api/v2/green_lanes/category_assessments_controller.rb index e2bfc2a25..4a3d36643 100644 --- a/app/controllers/api/v2/green_lanes/category_assessments_controller.rb +++ b/app/controllers/api/v2/green_lanes/category_assessments_controller.rb @@ -28,6 +28,7 @@ def index .new(presented_assessments, include: %w[geographical_area excluded_geographical_areas + exemptions theme regulation measure_type]) diff --git a/app/presenters/api/v2/green_lanes/category_assessment_presenter.rb b/app/presenters/api/v2/green_lanes/category_assessment_presenter.rb index 20e5d6df7..f0cdf3e31 100644 --- a/app/presenters/api/v2/green_lanes/category_assessment_presenter.rb +++ b/app/presenters/api/v2/green_lanes/category_assessment_presenter.rb @@ -66,7 +66,7 @@ def excluded_geographical_area_ids end def exemptions - certificates + additional_codes + @exemptions ||= (certificates + additional_codes + pseudo_exemptions) end def certificates @@ -77,6 +77,10 @@ def additional_codes Array.wrap(additional_code) end + def pseudo_exemptions + ExemptionPresenter.wrap(@category_assessment.exemptions) + end + def regulation RegulationPresenter.new(super) end diff --git a/app/presenters/api/v2/green_lanes/exemption_presenter.rb b/app/presenters/api/v2/green_lanes/exemption_presenter.rb new file mode 100644 index 000000000..431b8c780 --- /dev/null +++ b/app/presenters/api/v2/green_lanes/exemption_presenter.rb @@ -0,0 +1,15 @@ +module Api + module V2 + module GreenLanes + class ExemptionPresenter < WrapDelegator + def id + code + end + + def formatted_description + description + end + end + end + end +end diff --git a/app/serializers/api/v2/green_lanes/category_assessment_serializer.rb b/app/serializers/api/v2/green_lanes/category_assessment_serializer.rb index 827f19314..d52668d29 100644 --- a/app/serializers/api/v2/green_lanes/category_assessment_serializer.rb +++ b/app/serializers/api/v2/green_lanes/category_assessment_serializer.rb @@ -5,7 +5,6 @@ class CategoryAssessmentSerializer include JSONAPI::Serializer set_type :category_assessment - set_id :id has_many :exemptions, serializer: lambda { |record, _params| @@ -14,6 +13,8 @@ class CategoryAssessmentSerializer GreenLanes::CertificateSerializer when AdditionalCode AdditionalCodeSerializer + when ExemptionPresenter + GreenLanes::ExemptionSerializer else raise 'Unknown type' end diff --git a/app/serializers/api/v2/green_lanes/exemption_serializer.rb b/app/serializers/api/v2/green_lanes/exemption_serializer.rb new file mode 100644 index 000000000..106a9996b --- /dev/null +++ b/app/serializers/api/v2/green_lanes/exemption_serializer.rb @@ -0,0 +1,15 @@ +module Api + module V2 + module GreenLanes + class ExemptionSerializer + include JSONAPI::Serializer + + set_id :code + + attributes :code, + :description, + :formatted_description + end + end + end +end diff --git a/spec/presenters/api/v2/green_lanes/category_assessment_presenter_spec.rb b/spec/presenters/api/v2/green_lanes/category_assessment_presenter_spec.rb index 94566fa3a..da24e0034 100644 --- a/spec/presenters/api/v2/green_lanes/category_assessment_presenter_spec.rb +++ b/spec/presenters/api/v2/green_lanes/category_assessment_presenter_spec.rb @@ -106,7 +106,13 @@ it { is_expected.to match_array certificates << additional_code } end - context 'with neither' do + context 'with pseudo exemption' do + before { assessment.add_exemption create(:green_lanes_exemption) } + + it { is_expected.to include instance_of Api::V2::GreenLanes::ExemptionPresenter } + end + + context 'with no exemptions' do it { is_expected.to be_empty } end end diff --git a/spec/presenters/api/v2/green_lanes/exemption_presenter_spec.rb b/spec/presenters/api/v2/green_lanes/exemption_presenter_spec.rb new file mode 100644 index 000000000..059e3b141 --- /dev/null +++ b/spec/presenters/api/v2/green_lanes/exemption_presenter_spec.rb @@ -0,0 +1,10 @@ +RSpec.describe Api::V2::GreenLanes::ExemptionPresenter do + subject { described_class.new(exemption) } + + let(:exemption) { create :green_lanes_exemption } + + it { is_expected.to have_attributes id: exemption.code } + it { is_expected.to have_attributes code: exemption.code } + it { is_expected.to have_attributes description: exemption.description } + it { is_expected.to have_attributes formatted_description: exemption.description } +end diff --git a/spec/serializers/api/v2/green_lanes/category_assessment_serializer_spec.rb b/spec/serializers/api/v2/green_lanes/category_assessment_serializer_spec.rb index f21e3a9cb..bcd790122 100644 --- a/spec/serializers/api/v2/green_lanes/category_assessment_serializer_spec.rb +++ b/spec/serializers/api/v2/green_lanes/category_assessment_serializer_spec.rb @@ -7,9 +7,12 @@ ).serializable_hash.as_json end + before { category_assessment.add_exemption exemption } + let(:category_assessment) { create :category_assessment, measure: } let(:certificate) { create :certificate, :exemption, :with_certificate_type, :with_description } let(:measure) { create :measure, :with_additional_code, :with_base_regulation, certificate: } + let(:exemption) { create :green_lanes_exemption } let :presented do Api::V2::GreenLanes::CategoryAssessmentPresenter.wrap(category_assessment).first @@ -28,6 +31,7 @@ data: [ { id: certificate.id, type: 'certificate' }, { id: measure.additional_code.id.to_s, type: 'additional_code' }, + { id: exemption.code, type: 'exemption' }, ], }, geographical_area: { @@ -76,6 +80,15 @@ formatted_description: be_a(String), }, }, + { + id: exemption.code, + type: 'exemption', + attributes: { + code: exemption.code, + description: be_a(String), + formatted_description: be_a(String), + }, + }, { id: measure.geographical_area_id, type: 'geographical_area', diff --git a/spec/serializers/api/v2/green_lanes/exemption_serializer_spec.rb b/spec/serializers/api/v2/green_lanes/exemption_serializer_spec.rb new file mode 100644 index 000000000..c07635936 --- /dev/null +++ b/spec/serializers/api/v2/green_lanes/exemption_serializer_spec.rb @@ -0,0 +1,22 @@ +RSpec.describe Api::V2::GreenLanes::ExemptionSerializer do + subject { described_class.new(presented).serializable_hash.as_json } + + let(:exemption) { create :green_lanes_exemption } + let(:presented) { Api::V2::GreenLanes::ExemptionPresenter.new exemption } + + let(:expected_pattern) do + { + data: { + id: exemption.code, + type: 'exemption', + attributes: { + code: exemption.code, + description: exemption.description, + formatted_description: exemption.description, + }, + }, + } + end + + it { is_expected.to include_json(expected_pattern) } +end diff --git a/spec/services/green_lanes/permutation_calculator_service_spec.rb b/spec/services/green_lanes/permutation_calculator_service_spec.rb index d0e99d048..b5ecab694 100644 --- a/spec/services/green_lanes/permutation_calculator_service_spec.rb +++ b/spec/services/green_lanes/permutation_calculator_service_spec.rb @@ -17,6 +17,26 @@ it { is_expected.to eq(measures.map { |m| [m] }) } end + context 'with related measures' do + let(:measures) { [measure, measure2] } + + let :measure do + create :measure, :with_additional_code, :with_measure_type, :with_base_regulation + end + + let :measure2 do + create :measure, measure_type_id: measure.measure_type_id, + generating_regulation: measure.generating_regulation, + additional_code_sid: measure.additional_code_sid, + additional_code_id: measure.additional_code_id, + additional_code_type_id: measure.additional_code_type_id, + geographical_area_id: measure.geographical_area_id + end + + it { is_expected.to have_attributes length: 1 } + it { expect(permutations[0]).to eq_pk measures } + end + context 'with mixture of related and unrelated' do let :measures do measures = create_list(:measure, 2, :with_measure_type, :with_base_regulation)