Display more specific invalid_voucher message to customer

It was implemented with error code  409 in https://github.com/openfoodfoundation/vine/pull/112
This commit is contained in:
David Cook
2025-09-09 16:08:58 +10:00
parent 419f4490d6
commit 368da19993
5 changed files with 35 additions and 7 deletions

View File

@@ -90,11 +90,13 @@ class VoucherAdjustmentsController < BaseController
voucher_code: voucher_params[:voucher_code], enterprise: @order.distributor
)
voucher = vine_voucher_validator.validate
errors = vine_voucher_validator.errors
return nil if vine_voucher_validator.errors[:not_found_voucher].present?
return nil if errors[:not_found_voucher].present?
if vine_voucher_validator.errors.present?
@order.errors.add(:voucher_code, I18n.t('checkout.errors.add_voucher_error'))
if errors.present?
message = errors[:invalid_voucher] || I18n.t('checkout.errors.add_voucher_error')
@order.errors.add(:voucher_code, message)
return nil
end

View File

@@ -47,7 +47,7 @@ module Vine
end
def handle_errors(response)
if response[:status] == 400
if [400, 409].include?(response[:status])
message = response[:body] && JSON.parse(response[:body]).dig("meta", "message")
key = VINE_ERRORS.fetch(message, :invalid_voucher)
errors[:invalid_voucher] = I18n.t("vine_voucher_validator_service.errors.#{key}")

View File

@@ -161,7 +161,19 @@ RSpec.describe VoucherAdjustmentsController do
post("/voucher_adjustments", params:)
expect(response).to be_unprocessable
expect(flash[:error]).to match "There was an error while adding the voucher"
expect(flash[:error]).to match "The voucher is not valid"
end
end
context "when voucher has expired" do
it "returns 422 and an error message" do
mock_vine_voucher_validator(voucher: nil,
errors: { invalid_voucher: "The voucher has expired" })
post("/voucher_adjustments", params:)
expect(response).to be_unprocessable
expect(flash[:error]).to match "The voucher has expired"
end
end
@@ -219,6 +231,20 @@ RSpec.describe VoucherAdjustmentsController do
)
end
end
context "when voucher has expired" do
it "returns 422 and an error message" do
vine_voucher = build(:vine_voucher, code: vine_voucher_code,
enterprise: distributor)
mock_vine_voucher_validator(voucher: vine_voucher,
errors: { invalid_voucher: "The voucher has expired" })
post("/voucher_adjustments", params:)
expect(response).to be_unprocessable
expect(flash[:error]).to match "The voucher has expired"
end
end
end
end
end

View File

@@ -448,7 +448,7 @@ RSpec.describe Vine::VoucherValidatorService, feature: :connected_apps do
}
it "adds a specific error message" do
mock_api_exception(type: Faraday::BadRequestError, status: 400, body: data)
mock_api_exception(type: Faraday::BadRequestError, status: 409, body: data)
validate_voucher_service.validate

View File

@@ -198,7 +198,7 @@ RSpec.describe "As a consumer, I want to checkout my order" do
fill_in "Enter voucher code", with: "KM1891"
click_button("Apply")
expect(page).to have_content("There was an error while adding the voucher")
expect(page).to have_content("The voucher is not valid")
expect(Vouchers::Vine.find_by(code: "KM1891", enterprise: distributor)).to be_nil
end
end