diff --git a/app/controllers/api/v1/quote_check_feedbacks_controller.rb b/app/controllers/api/v1/quote_check_feedbacks_controller.rb index 06b26d1..63c7b9d 100644 --- a/app/controllers/api/v1/quote_check_feedbacks_controller.rb +++ b/app/controllers/api/v1/quote_check_feedbacks_controller.rb @@ -6,6 +6,7 @@ module V1 class QuoteCheckFeedbacksController < BaseController before_action :authorize_request before_action :quote_check + before_action :validation_error_details, if: -> { params[:validation_error_detail_id].present? } def create @quote_check_feedback = quote_check.feedbacks.create!(quote_check_feedback_params) @@ -22,7 +23,18 @@ def quote_check end def quote_check_feedback_params - params.permit(:validation_error_details_id, :is_helpful, :comment) + raw_params = params.permit(:validation_error_details_id, :is_helpful, :comment) + return raw_params unless defined?(@validation_error_details) + + raw_params.merge(validation_error_details_id: @validation_error_details.fetch("id")) + end + + def validation_error_details + # validation_error_detail_id is in singular in path params + @validation_error_details ||= quote_check.validation_error_details.detect do |details| + details.fetch("id") == params[:validation_error_detail_id] + end || raise(ActiveRecord::RecordNotFound, + "Couldn't find ValidationErrorDetails with 'id'=#{params[:validation_error_detail_id]}") end end end diff --git a/config/routes/api.rb b/config/routes/api.rb index c3a7ce0..02750dd 100644 --- a/config/routes/api.rb +++ b/config/routes/api.rb @@ -6,6 +6,12 @@ resources :profiles, only: %i[index] resources :quote_checks, only: %i[create show] do resources :feedbacks, only: %i[create], controller: "quote_check_feedbacks" + resources :quote_check_validation_error_details, + path: "error_details", + as: :validation_error_details, + only: %i[] do + resources :feedbacks, only: %i[create], controller: "quote_check_feedbacks" + end end end end diff --git a/spec/requests/api/v1/quote_check_validation_error_details_feedbacks_doc_spec.rb b/spec/requests/api/v1/quote_check_validation_error_details_feedbacks_doc_spec.rb new file mode 100644 index 0000000..84c6935 --- /dev/null +++ b/spec/requests/api/v1/quote_check_validation_error_details_feedbacks_doc_spec.rb @@ -0,0 +1,62 @@ +# frozen_string_literal: true + +require "swagger_helper" + +describe "Devis API" do + path "/quote_checks/{quote_check_id}/error_details/{error_details_id}/feedbacks" do + # TODO: i18n? + post "Déposer un retour" do + tags "Devis" + security [basic_auth: []] + consumes "application/json" + produces "application/json" + + parameter name: :quote_check_id, in: :path, type: :string + parameter name: :error_details_id, in: :path, type: :string + parameter name: :quote_check_feedback, in: :body, schema: { + type: :object, + properties: { + is_helpful: { type: :boolean, nullable: false }, + comment: { + type: :string, + nullable: true, + maxLength: QuoteCheckFeedback.validators_on(:comment).detect do |validator| + validator.is_a?(ActiveModel::Validations::LengthValidator) + end&.options&.[](:maximum) + } + }, + required: %w[is_helpful] + } + + let(:quote_check) { create(:quote_check, :invalid) } + let(:quote_check_id) { quote_check.id } + let(:error_details_id) { quote_check.error_details.first.fetch("id") } + let(:quote_check_feedback) do + build(:quote_check_feedback, quote_check: QuoteCheck.find(quote_check_id)).attributes + end + + response "201", "Retour téléversé" do + schema "$ref" => "#/components/schemas/quote_check_feedback" + + # See https://github.com/rswag/rswag/issues/316 + let(:Authorization) { basic_auth_header.fetch("Authorization") } # rubocop:disable RSpec/VariableName + + run_test! + end + + response "422", "missing params" do + schema "$ref" => "#/components/schemas/api_error" + + let(:quote_check_feedback) do + build(:quote_check_feedback).attributes + .except("validation_error_details_id") + .merge("is_helpful" => nil) + end + + let(:Authorization) { basic_auth_header.fetch("Authorization") } # rubocop:disable RSpec/VariableName + + run_test! + end + end + end +end diff --git a/spec/requests/api/v1/quote_check_validation_error_details_feedbacks_spec.rb b/spec/requests/api/v1/quote_check_validation_error_details_feedbacks_spec.rb new file mode 100644 index 0000000..728268e --- /dev/null +++ b/spec/requests/api/v1/quote_check_validation_error_details_feedbacks_spec.rb @@ -0,0 +1,72 @@ +# frozen_string_literal: true + +# spec/controllers/posts_controller_spec.rb +require "rails_helper" + +RSpec.describe "/api/v1/quote_checks/:quote_check_id/error_details/:validation_error_detail_id/feedbacks" do + let(:quote_check) { create(:quote_check, :invalid) } + let(:validation_error_details_id) { quote_check.validation_error_details.first.fetch("id") } + let(:quote_check_id) { quote_check.id } + + let(:json) { response.parsed_body } + + describe "POST /api/v1/quote_checks/:quote_check_id/error_details/:validation_error_detail_id/feedbacks" do + let(:quote_check_feedback_params) do + { + is_helpful: false, + comment: "FAUX" + } + end + + # rubocop:disable RSpec/ExampleLength + it "returns a successful response" do + post api_v1_quote_check_validation_error_detail_feedbacks_url( + quote_check_id: quote_check_id, + validation_error_detail_id: validation_error_details_id + ), params: quote_check_feedback_params, + headers: basic_auth_header + expect(response).to be_successful + end + + it "returns a created response" do + post api_v1_quote_check_validation_error_detail_feedbacks_url( + quote_check_id: quote_check_id, + validation_error_detail_id: validation_error_details_id + ), params: quote_check_feedback_params, + headers: basic_auth_header + expect(response).to have_http_status(:created) + end + + it "returns the QuoteCheckFeedback" do + post api_v1_quote_check_validation_error_detail_feedbacks_url( + quote_check_id: quote_check_id, + validation_error_detail_id: validation_error_details_id + ), params: quote_check_feedback_params, + headers: basic_auth_header + expect(json.fetch("quote_check_id")).to eq(quote_check_id) + end + + it "creates a QuoteCheckFeedback" do + expect do + post api_v1_quote_check_validation_error_detail_feedbacks_url( + quote_check_id: quote_check_id, + validation_error_detail_id: validation_error_details_id + ), params: quote_check_feedback_params, + headers: basic_auth_header + end.to change(QuoteCheckFeedback, :count).by(1) + end + + context "with wrong error details id" do + it "returns a not found response" do + post api_v1_quote_check_validation_error_detail_feedbacks_url( + quote_check_id: quote_check_id, + validation_error_detail_id: "wrong" + ), + params: quote_check_feedback_params, + headers: basic_auth_header + expect(response).to have_http_status(:not_found) + end + end + # rubocop:enable RSpec/ExampleLength + end +end diff --git a/swagger/v1/mon-devis-sans-oublis_api_v1_swagger.yaml b/swagger/v1/mon-devis-sans-oublis_api_v1_swagger.yaml index 5ab1a63..a8bbb2d 100644 --- a/swagger/v1/mon-devis-sans-oublis_api_v1_swagger.yaml +++ b/swagger/v1/mon-devis-sans-oublis_api_v1_swagger.yaml @@ -68,6 +68,52 @@ paths: required: - validation_error_details_id - is_helpful + "/quote_checks/{quote_check_id}/error_details/{error_details_id}/feedbacks": + post: + summary: Déposer un retour + tags: + - Devis + security: + - basic_auth: [] + parameters: + - name: quote_check_id + in: path + required: true + schema: + type: string + - name: error_details_id + in: path + required: true + schema: + type: string + responses: + '201': + description: Retour téléversé + content: + application/json: + schema: + "$ref": "#/components/schemas/quote_check_feedback" + '422': + description: missing params + content: + application/json: + schema: + "$ref": "#/components/schemas/api_error" + requestBody: + content: + application/json: + schema: + type: object + properties: + is_helpful: + type: boolean + nullable: false + comment: + type: string + nullable: true + maxLength: 1000 + required: + - is_helpful "/quote_checks": post: summary: Téléverser un devis