From 770e0bb018fdda85ffa4441c3023388d52a54116 Mon Sep 17 00:00:00 2001 From: Hadleigh Wallenberg Date: Wed, 11 Dec 2024 10:41:39 +0000 Subject: [PATCH 1/8] Added FAQ Feedback controller, model and specs --- .../green_lanes/faq_feedback_controller.rb | 41 +++++++++++++++++++ app/models/green_lanes/faq_feedback.rb | 12 ++++++ .../green_lanes/faq_feedback_serializer.rb | 18 ++++++++ config/routes.rb | 3 +- ...0900_add_green_lanes_faq_feedback_table.rb | 20 +++++++++ .../green_lanes/faq_feedback_factory.rb | 8 ++++ spec/models/green_lanes/faq_feedback_spec.rb | 34 +++++++++++++++ .../faq_feedback_controller_spec.rb | 37 +++++++++++++++++ .../faq_feedback_serializer_spec.rb | 28 +++++++++++++ 9 files changed, 200 insertions(+), 1 deletion(-) create mode 100644 app/controllers/api/admin/green_lanes/faq_feedback_controller.rb create mode 100644 app/models/green_lanes/faq_feedback.rb create mode 100644 app/serializers/api/admin/green_lanes/faq_feedback_serializer.rb create mode 100644 db/migrate/20241210100900_add_green_lanes_faq_feedback_table.rb create mode 100644 spec/factories/green_lanes/faq_feedback_factory.rb create mode 100644 spec/models/green_lanes/faq_feedback_spec.rb create mode 100644 spec/requests/api/admin/green_lanes/faq_feedback_controller_spec.rb create mode 100644 spec/serializers/api/admin/green_lanes/faq_feedback_serializer_spec.rb diff --git a/app/controllers/api/admin/green_lanes/faq_feedback_controller.rb b/app/controllers/api/admin/green_lanes/faq_feedback_controller.rb new file mode 100644 index 000000000..56f3d0b4a --- /dev/null +++ b/app/controllers/api/admin/green_lanes/faq_feedback_controller.rb @@ -0,0 +1,41 @@ +module Api + module Admin + module GreenLanes + class FaqFeedbackController < AdminController + before_action :authenticate_user! + + def create + faq_feedback = ::GreenLanes::FaqFeedback.new(faq_feedback_params) + + if faq_feedback.valid? && faq_feedback.save + render json: serialize(faq_feedback), + location: api_admin_green_lanes_faq_feedback_url(faq_feedback.id), + status: :created + else + render json: serialize_errors(faq_feedback), + status: :unprocessable_entity + end + end + + private + + def faq_feedback_params + params.require(:data).require(:attributes).permit( + :session_id, + :category_id, + :question_id, + :useful, + ) + end + + def serialize(*args) + Api::Admin::GreenLanes::FaqFeedbackSerializer.new(*args).serializable_hash + end + + def serialize_errors(exemption) + Api::Admin::ErrorSerializationService.new(exemption).call + end + end + end + end +end diff --git a/app/models/green_lanes/faq_feedback.rb b/app/models/green_lanes/faq_feedback.rb new file mode 100644 index 000000000..e62a487d7 --- /dev/null +++ b/app/models/green_lanes/faq_feedback.rb @@ -0,0 +1,12 @@ +module GreenLanes + class FaqFeedback < Sequel::Model(:green_lanes_faq_feedback) + plugin :timestamps, update_on_create: true + plugin :auto_validations, not_null: :presence + + # Ensure uniqueness of the composite key + def validate + super + validates_unique(%i[session_id category_id question_id]) + end + end +end diff --git a/app/serializers/api/admin/green_lanes/faq_feedback_serializer.rb b/app/serializers/api/admin/green_lanes/faq_feedback_serializer.rb new file mode 100644 index 000000000..2504be034 --- /dev/null +++ b/app/serializers/api/admin/green_lanes/faq_feedback_serializer.rb @@ -0,0 +1,18 @@ +module Api + module Admin + module GreenLanes + class FaqFeedbackSerializer + include JSONAPI::Serializer + + set_type :green_lanes_faq_feedback + + set_id :id + + attributes :session_id, + :category_id, + :question_id, + :useful + end + end + end +end diff --git a/config/routes.rb b/config/routes.rb index 0637954ea..6428a21d7 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -65,9 +65,10 @@ resources :themes, only: %i[index] resources :exempting_certificate_overrides, only: %i[index show create destroy] resources :exempting_additional_code_overrides, only: %i[index show create destroy] - resources :exemptions, only: %i[index show create update destroy] + resources :exemptions, only: %i[index create update destroy] resources :measures, only: %i[index show create update destroy] resources :update_notifications, only: %i[index show update] + resources :faq_feedback, only: %i[create show] end end end diff --git a/db/migrate/20241210100900_add_green_lanes_faq_feedback_table.rb b/db/migrate/20241210100900_add_green_lanes_faq_feedback_table.rb new file mode 100644 index 000000000..d18313fba --- /dev/null +++ b/db/migrate/20241210100900_add_green_lanes_faq_feedback_table.rb @@ -0,0 +1,20 @@ +# frozen_string_literal: true + +Sequel.migration do + change do + create_table :green_lanes_faq_feedback do + primary_key :id + String :session_id, null: false + Integer :category_id, null: false + Integer :question_id, null: false + Boolean :useful, null: false + DateTime :created_at + DateTime :updated_at + end + + alter_table :green_lanes_faq_feedback do + add_unique_constraint [:session_id, :category_id, :question_id], name: :unique_faq_feedback + end + end +end + diff --git a/spec/factories/green_lanes/faq_feedback_factory.rb b/spec/factories/green_lanes/faq_feedback_factory.rb new file mode 100644 index 000000000..545d232ec --- /dev/null +++ b/spec/factories/green_lanes/faq_feedback_factory.rb @@ -0,0 +1,8 @@ +FactoryBot.define do + factory :green_lanes_faq_feedback, class: 'GreenLanes::FaqFeedback' do + session_id { SecureRandom.uuid } + category_id { 1 } + question_id { 1 } + useful { true } + end +end diff --git a/spec/models/green_lanes/faq_feedback_spec.rb b/spec/models/green_lanes/faq_feedback_spec.rb new file mode 100644 index 000000000..4470461ae --- /dev/null +++ b/spec/models/green_lanes/faq_feedback_spec.rb @@ -0,0 +1,34 @@ +RSpec.describe GreenLanes::FaqFeedback do + describe 'attributes' do + it { is_expected.to respond_to :session_id } + it { is_expected.to respond_to :category_id } + it { is_expected.to respond_to :question_id } + it { is_expected.to respond_to :useful } + end + + describe 'validations' do + subject(:errors) { instance.tap(&:valid?).errors } + + let(:instance) { described_class.new } + + it { is_expected.to include session_id: ['is not present'] } + it { is_expected.to include category_id: ['is not present'] } + it { is_expected.to include question_id: ['is not present'] } + it { is_expected.to include useful: ['is not present'] } + + context 'with duplicate entry' do + let(:session_id) { SecureRandom.uuid } + let(:category_id) { 1 } + let(:question_id) { 1 } + + before do + create(:green_lanes_faq_feedback, session_id:, category_id:, question_id:) + end + + it 'fails validation' do + duplicate = build(:green_lanes_faq_feedback, session_id:, category_id:, question_id:) + expect(duplicate.valid?).to be false + end + end + end +end diff --git a/spec/requests/api/admin/green_lanes/faq_feedback_controller_spec.rb b/spec/requests/api/admin/green_lanes/faq_feedback_controller_spec.rb new file mode 100644 index 000000000..6df8968f2 --- /dev/null +++ b/spec/requests/api/admin/green_lanes/faq_feedback_controller_spec.rb @@ -0,0 +1,37 @@ +RSpec.describe Api::Admin::GreenLanes::FaqFeedbackController do + subject(:page_response) { make_request && response } + + let(:json_response) { JSON.parse(page_response.body) } + let(:faq_feedback) { create :green_lanes_faq_feedback } + + describe 'POST to #create' do + let(:make_request) do + authenticated_post api_admin_green_lanes_faq_feedback_index_path(format: :json), params: faq_feedback_data + end + let :faq_feedback_data do + { + data: { + type: :green_lanes_faq_feedback, + attributes: ex_attrs, + }, + } + end + + context 'with valid params' do + let(:ex_attrs) { build(:green_lanes_faq_feedback).to_hash } + + it { is_expected.to have_http_status :created } + it { is_expected.to have_attributes location: api_admin_green_lanes_faq_feedback_url(GreenLanes::FaqFeedback.last.id) } + end + + context 'with invalid params' do + let(:ex_attrs) { build(:green_lanes_faq_feedback, session_id: nil).to_hash } + + it { is_expected.to have_http_status :unprocessable_entity } + + it 'returns errors for faq_feedback' do + expect(json_response).to include('errors') + end + end + end +end diff --git a/spec/serializers/api/admin/green_lanes/faq_feedback_serializer_spec.rb b/spec/serializers/api/admin/green_lanes/faq_feedback_serializer_spec.rb new file mode 100644 index 000000000..5ace141aa --- /dev/null +++ b/spec/serializers/api/admin/green_lanes/faq_feedback_serializer_spec.rb @@ -0,0 +1,28 @@ +RSpec.describe Api::Admin::GreenLanes::FaqFeedbackSerializer do + subject(:serialized) do + described_class.new(faq_feedback).serializable_hash + end + + let(:faq_feedback) { create :green_lanes_faq_feedback } + + let :expected do + { + data: { + id: faq_feedback.id.to_s, + type: :green_lanes_faq_feedback, + attributes: { + session_id: faq_feedback.session_id, + category_id: faq_feedback.category_id, + question_id: faq_feedback.question_id, + useful: faq_feedback.useful, + }, + }, + } + end + + describe '#serializable_hash' do + it 'matches the expected hash' do + expect(serialized).to eq(expected) + end + end +end From 006285d81bc56099903bdac567aba5d4f3717a92 Mon Sep 17 00:00:00 2001 From: Hadleigh Wallenberg <127106895+HWallenberg@users.noreply.github.com> Date: Mon, 6 Jan 2025 15:47:30 +0000 Subject: [PATCH 2/8] Delete db/migrate/20241210100900_add_green_lanes_faq_feedback_table.rb Removed migration file as this was merged from another branch here https://github.com/trade-tariff/trade-tariff-backend/pull/2063 --- ...0900_add_green_lanes_faq_feedback_table.rb | 20 ------------------- 1 file changed, 20 deletions(-) delete mode 100644 db/migrate/20241210100900_add_green_lanes_faq_feedback_table.rb diff --git a/db/migrate/20241210100900_add_green_lanes_faq_feedback_table.rb b/db/migrate/20241210100900_add_green_lanes_faq_feedback_table.rb deleted file mode 100644 index d18313fba..000000000 --- a/db/migrate/20241210100900_add_green_lanes_faq_feedback_table.rb +++ /dev/null @@ -1,20 +0,0 @@ -# frozen_string_literal: true - -Sequel.migration do - change do - create_table :green_lanes_faq_feedback do - primary_key :id - String :session_id, null: false - Integer :category_id, null: false - Integer :question_id, null: false - Boolean :useful, null: false - DateTime :created_at - DateTime :updated_at - end - - alter_table :green_lanes_faq_feedback do - add_unique_constraint [:session_id, :category_id, :question_id], name: :unique_faq_feedback - end - end -end - From 5afa657068b6edf16c41a6eefaec111ce5a46ef3 Mon Sep 17 00:00:00 2001 From: Hadleigh Wallenberg Date: Mon, 6 Jan 2025 16:54:26 +0000 Subject: [PATCH 3/8] Removed bug with Exemptions methods in routes.rb --- config/routes.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/routes.rb b/config/routes.rb index 6428a21d7..eb53669a6 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -65,7 +65,7 @@ resources :themes, only: %i[index] resources :exempting_certificate_overrides, only: %i[index show create destroy] resources :exempting_additional_code_overrides, only: %i[index show create destroy] - resources :exemptions, only: %i[index create update destroy] + resources :exemptions, only: %i[index show create update destroy] resources :measures, only: %i[index show create update destroy] resources :update_notifications, only: %i[index show update] resources :faq_feedback, only: %i[create show] From 8b52d7757bd11529c5cf18f4fbaf7974aaad5fc5 Mon Sep 17 00:00:00 2001 From: Hadleigh Wallenberg Date: Mon, 6 Jan 2025 18:13:15 +0000 Subject: [PATCH 4/8] removed admin from faq_feedback route --- .../api/admin/green_lanes/faq_feedback_controller.rb | 2 +- config/routes.rb | 3 ++- .../api/admin/green_lanes/faq_feedback_controller_spec.rb | 4 ++-- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/app/controllers/api/admin/green_lanes/faq_feedback_controller.rb b/app/controllers/api/admin/green_lanes/faq_feedback_controller.rb index 56f3d0b4a..8e2fa9a88 100644 --- a/app/controllers/api/admin/green_lanes/faq_feedback_controller.rb +++ b/app/controllers/api/admin/green_lanes/faq_feedback_controller.rb @@ -9,7 +9,7 @@ def create if faq_feedback.valid? && faq_feedback.save render json: serialize(faq_feedback), - location: api_admin_green_lanes_faq_feedback_url(faq_feedback.id), + location: api_green_lanes_faq_feedback_url(faq_feedback.id), status: :created else render json: serialize_errors(faq_feedback), diff --git a/config/routes.rb b/config/routes.rb index eb53669a6..80e64c312 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -68,7 +68,6 @@ resources :exemptions, only: %i[index show create update destroy] resources :measures, only: %i[index show create update destroy] resources :update_notifications, only: %i[index show update] - resources :faq_feedback, only: %i[create show] end end end @@ -232,6 +231,8 @@ resources :category_assessments, only: %i[index] resources :themes, only: %i[index] + + resources :faq_feedback, only: %i[create show], controller: '/api/admin/green_lanes/faq_feedback' end end diff --git a/spec/requests/api/admin/green_lanes/faq_feedback_controller_spec.rb b/spec/requests/api/admin/green_lanes/faq_feedback_controller_spec.rb index 6df8968f2..fc782cb11 100644 --- a/spec/requests/api/admin/green_lanes/faq_feedback_controller_spec.rb +++ b/spec/requests/api/admin/green_lanes/faq_feedback_controller_spec.rb @@ -6,7 +6,7 @@ describe 'POST to #create' do let(:make_request) do - authenticated_post api_admin_green_lanes_faq_feedback_index_path(format: :json), params: faq_feedback_data + authenticated_post api_green_lanes_faq_feedback_index_path(format: :json), params: faq_feedback_data end let :faq_feedback_data do { @@ -21,7 +21,7 @@ let(:ex_attrs) { build(:green_lanes_faq_feedback).to_hash } it { is_expected.to have_http_status :created } - it { is_expected.to have_attributes location: api_admin_green_lanes_faq_feedback_url(GreenLanes::FaqFeedback.last.id) } + it { is_expected.to have_attributes location: api_green_lanes_faq_feedback_url(GreenLanes::FaqFeedback.last.id) } end context 'with invalid params' do From 103f0898814de103d51dfaecb63989f1dfad23bf Mon Sep 17 00:00:00 2001 From: Hadleigh Wallenberg Date: Tue, 7 Jan 2025 10:50:01 +0000 Subject: [PATCH 5/8] Added index and diagnostics to FAQ Feedback --- Dockerfile | 3 +++ .../api/admin/green_lanes/faq_feedback_controller.rb | 6 ++++++ config/routes.rb | 2 +- 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 78e7a315f..df20e4c1b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -31,6 +31,9 @@ RUN rm -rf node_modules log tmp && \ # Build runtime image FROM ruby:3.3.6-alpine3.20 as production +# Install PostgreSQL client tools +RUN apk add --no-cache postgresql-client + RUN apk add --update --no-cache postgresql-dev curl shared-mime-info tzdata && \ cp /usr/share/zoneinfo/Europe/London /etc/localtime && \ echo "Europe/London" > /etc/timezone diff --git a/app/controllers/api/admin/green_lanes/faq_feedback_controller.rb b/app/controllers/api/admin/green_lanes/faq_feedback_controller.rb index 8e2fa9a88..8adafa767 100644 --- a/app/controllers/api/admin/green_lanes/faq_feedback_controller.rb +++ b/app/controllers/api/admin/green_lanes/faq_feedback_controller.rb @@ -8,6 +8,7 @@ def create faq_feedback = ::GreenLanes::FaqFeedback.new(faq_feedback_params) if faq_feedback.valid? && faq_feedback.save + Rails.logger.info("FAQ feedback created: #{faq_feedback.id}") render json: serialize(faq_feedback), location: api_green_lanes_faq_feedback_url(faq_feedback.id), status: :created @@ -17,6 +18,11 @@ def create end end + def index + faq_feedback = ::GreenLanes::FaqFeedback.all + render json: serialize(faq_feedback) + end + private def faq_feedback_params diff --git a/config/routes.rb b/config/routes.rb index 80e64c312..cd3f637ca 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -232,7 +232,7 @@ resources :themes, only: %i[index] - resources :faq_feedback, only: %i[create show], controller: '/api/admin/green_lanes/faq_feedback' + resources :faq_feedback, only: %i[create index show], controller: '/api/admin/green_lanes/faq_feedback' end end From e073f29114758ffe9f2c9c2c55abc8c3e059cb0e Mon Sep 17 00:00:00 2001 From: Hadleigh Wallenberg Date: Tue, 7 Jan 2025 11:45:48 +0000 Subject: [PATCH 6/8] Added diagnostics to create faq feedback --- .../api/admin/green_lanes/faq_feedback_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/api/admin/green_lanes/faq_feedback_controller.rb b/app/controllers/api/admin/green_lanes/faq_feedback_controller.rb index 8adafa767..70031ee08 100644 --- a/app/controllers/api/admin/green_lanes/faq_feedback_controller.rb +++ b/app/controllers/api/admin/green_lanes/faq_feedback_controller.rb @@ -6,7 +6,7 @@ class FaqFeedbackController < AdminController def create faq_feedback = ::GreenLanes::FaqFeedback.new(faq_feedback_params) - + Rails.logger.info("FAQ feedback valid?: #{faq_feedback.valid?}") if faq_feedback.valid? && faq_feedback.save Rails.logger.info("FAQ feedback created: #{faq_feedback.id}") render json: serialize(faq_feedback), From 9e90d51969dcad839faa331859fe59dd194ab9c9 Mon Sep 17 00:00:00 2001 From: Hadleigh Wallenberg Date: Tue, 7 Jan 2025 12:52:24 +0000 Subject: [PATCH 7/8] removed user authentication --- .../api/admin/green_lanes/faq_feedback_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/api/admin/green_lanes/faq_feedback_controller.rb b/app/controllers/api/admin/green_lanes/faq_feedback_controller.rb index 70031ee08..4617c99a4 100644 --- a/app/controllers/api/admin/green_lanes/faq_feedback_controller.rb +++ b/app/controllers/api/admin/green_lanes/faq_feedback_controller.rb @@ -2,7 +2,7 @@ module Api module Admin module GreenLanes class FaqFeedbackController < AdminController - before_action :authenticate_user! + # before_action :authenticate_user! def create faq_feedback = ::GreenLanes::FaqFeedback.new(faq_feedback_params) From 4315ec5795f95eea24c4a05b350bf7a7e25b30c9 Mon Sep 17 00:00:00 2001 From: Hadleigh Wallenberg Date: Wed, 8 Jan 2025 17:14:49 +0000 Subject: [PATCH 8/8] Added check for table exists in migration file --- ...00900_add_green_lanes_faq_feedback_table.rb | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/db/migrate/20241210100900_add_green_lanes_faq_feedback_table.rb b/db/migrate/20241210100900_add_green_lanes_faq_feedback_table.rb index d97c2430e..70fc24f5f 100644 --- a/db/migrate/20241210100900_add_green_lanes_faq_feedback_table.rb +++ b/db/migrate/20241210100900_add_green_lanes_faq_feedback_table.rb @@ -2,14 +2,16 @@ Sequel.migration do change do - create_table :green_lanes_faq_feedback do - primary_key :id - String :session_id, null: false - Integer :category_id, null: false - Integer :question_id, null: false - Boolean :useful, null: false - DateTime :created_at - DateTime :updated_at + unless table_exists?(:green_lanes_faq_feedback) + create_table :green_lanes_faq_feedback do + primary_key :id + String :session_id, null: false + Integer :category_id, null: false + Integer :question_id, null: false + Boolean :useful, null: false + DateTime :created_at + DateTime :updated_at + end end alter_table :green_lanes_faq_feedback do